我在CUDA 6.0 C/C++中編寫示例程序。程序可以識別設備,但在運行時間內似乎有錯誤:結果數組的元素都等於0,沒有任何理由。 (我的GPU:的Geforce EN9400GT ASUS) 這是我的代碼錯誤的結果在CUDA 6.0中
#include <stdio.h>
#include <malloc.h>
#include <cuda_runtime.h>
#define SIZE 1024
__global__ void VectorAdd(int* a, int* b, int* c, int n)
{
int i = threadIdx.x;
if (i < n) {
c[i] = a[i] + b[i];
}
}
void printResult(int* ar) {
for (int i = 0; i < 10; i++) {
printf("[%d] = %d\n", i, ar[i]);
}
}
int main() {
int *a, *b, *c;
int *d_a, *d_b, *d_c;
int device, count;
cudaDeviceProp* prop = (cudaDeviceProp*)malloc(sizeof(cudaDeviceProp));
int GPUavail = cudaGetDeviceCount(&count);
if (GPUavail != cudaSuccess) {
printf("There is no GPU device available\n");
exit(EXIT_FAILURE);
}
cudaGetDeviceProperties(prop, device);
printf("Device name: %s\n", prop->name);
printf("Global memory: %zd\n", prop->totalGlobalMem);
printf("Shared memory: %zd\n", prop->sharedMemPerBlock);
printf("Max threads per block: %d\n", prop->maxThreadsPerBlock);
printf("Device ID: %d\n", prop->pciDeviceID);
printf("TCC Driver: %d\n", prop->tccDriver);
a = (int*)malloc(SIZE * sizeof(int));
b = (int*)malloc(SIZE * sizeof(int));
c = (int*)malloc(SIZE * sizeof(int));
cudaMalloc(&d_a, SIZE*sizeof(int));
cudaMalloc(&d_b, SIZE*sizeof(int));
cudaMalloc(&d_c, SIZE*sizeof(int));
for (int i = 0; i < SIZE; i++) {
a[i] = i;
b[i] = i;
c[i] = 0;
}
cudaMemcpy(d_a, a, SIZE*sizeof(int), cudaMemcpyHostToDevice);
cudaMemcpy(d_b, b, SIZE*sizeof(int), cudaMemcpyHostToDevice);
cudaMemcpy(d_c, c, SIZE*sizeof(int), cudaMemcpyHostToDevice);
VectorAdd << < 1, SIZE >> >(d_a, d_b, d_c, SIZE);
cudaMemcpy(c, d_c, SIZE*sizeof(int), cudaMemcpyDeviceToHost);
printResult(c);
free(a);
free(b);
free(c);
cudaFree(d_a);
cudaFree(d_b);
cudaFree(d_c);
}
來源:https://developer.nvidia.com/how-to-cuda-c-cpp
製作SIZE 512並重試。然後再看看你發佈的所有內容,以及你自己爲什麼可以工作 – talonmies
請將文本信息顯示爲文本,而不是圖形。他們是文字,而不是繪畫作品。 – Gerhardh