2017-05-19 40 views
-2

我在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

這是顯示的結果: enter image description here

+1

製作SIZE 512並重試。然後再看看你發佈的所有內容,以及你自己爲什麼可以工作 – talonmies

+2

請將文本信息顯示爲文本,而不是圖形。他們是文字,而不是繪畫作品。 – Gerhardh

回答

3

你的GPU只能推出高達512個線程就像它在程序輸出中說的那樣。 (Max threads per block)但是,您正在一個塊中啓動1024個線程。由於您使用無效的啓動配置啓動了內核,因此您的內核根本無法啓動。您應該更改塊中的線程數。

#define SIZE 512 

對於計算能力> = 2.0,每個塊的線程數限制爲1024,但您的GPU是計算能力1.0。

+0

這樣一個小錯誤,我沒有發現,現在它適用於我。 –