2017-05-19 14 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

そして、これが表示された結果である:あなたのGPUのみ512件のスレッド件まで起動することができ enter image description here

+1

SIZE 512にしてもう一度やり直してください。あなたが投稿したすべてのものを見直してください。また、それがなぜ機能するのですか? – talonmies

+2

テキストメッセージをグラフィックスではなくテキストとして表示してください。それらはテキストであり、描かれたアートワークではありません。 – Gerhardh

答えて

3

ブロックごとに、あなたのプログラムの出力に書かれています。 (Max threads per block)しかし、ブロック内で1024スレッドを起動しています。無効な起動設定でカーネルを起動したため、カーネルはまったく起動しませんでした。ブロック内のスレッド数を変更する必要があります。

#define SIZE 512 

コンピューティング機能> = 2.0の場合、ブロックあたりのスレッド制限は1024ですが、GPUは計算機能1.0です。

+0

私はそれを見つけられなかったような小さなエラーは、今私のために働く。 –

関連する問題