2016-11-30 185 views
0

執行內核功能後,打印輸出始終爲0。 經過一些測試,cudaMemcpy仍然是正確的。但內核似乎不工作,無法從d_inputs獲取正確的數據。 有人可以幫忙解釋嗎?謝謝!CUDA輸出始終爲0

#include <cuda_runtime.h> 
#include <cuda.h> 
#include <stdio.h> 
#include <sys/time.h> 
#include <math.h> 

#define N 32 

__global__ void Kernel_double(int niters, int* d_inputs,double* d_outputs) 
{ 
    int tid = blockIdx.x * blockDim.x + threadIdx.x; 

    if (tid<N) { 
    double val =(double) d_inputs[tid]; 
    /*for (int iter=0; iter < niters; iter++){ 
    val = (sqrt(pow(val,2.0)) + 5.0) - 101.0; 
    val = (val/3.0) + 102.0; 
    val = (val + 1.07) - 103.0; 
    val = (val/1.037) + 104.0; 
    val = (val + 3.00) - 105.0; 
    val = (val/0.22) + 106.0; 
    }*/ 
    val = val + 1.0; 
    //printf("This is %f\n",val); 
    d_outputs[tid] = val; 
} 
} 

int main(int argc, char **argv) 
{ 

    int niters = 10; 
    printf("Iterate %d times with GPU 0 or CPU 1: %d\n", niters, cpu); 

    int inputs[N]; 
    for (int i = 0; i<N; i++){ 
    inputs[i] = i+1; 
    } 

    int d_inputs[N]; 
    double d_outputs[N]; 
    double outputs[N]; 

    cudaMalloc((void**)&d_inputs, N*sizeof(int)); 
    cudaMalloc((void**)&d_outputs, N*sizeof(double)); 
    printf("test %d \n", inputs[3]); 
    cudaMemcpy(d_inputs, inputs, N*sizeof(int), cudaMemcpyHostToDevice); 
    printf("test %d \n", d_inputs[1]); 
    Kernel_double<<<16,2>>>(niters, d_inputs,d_outputs); 
    //cudaDeviceSynchronize(); 
    cudaMemcpy(outputs, d_outputs, N*sizeof(double), cudaMemcpyDeviceToHost); 
    for(int j =0;j<10; j++){ 
     printf("Outputs[%d] is: %f and %f\n",j, d_outputs[j], outputs[j]); 
     } 
    cudaFree(d_inputs); 
    cudaFree(d_outputs); 

    return EXIT_SUCCESS; 
} 
+1

你絕對沒有錯誤在你的代碼檢查,所以它隨時可能被失敗點,你不會知道。在每次可能會失敗的呼叫之後添加錯誤檢查,並查看是否有助於縮小問題範圍。 –

+0

CHECK(cudaMalloc((void **)&d_inputs,N * sizeof(int))); \t CHECK(cudaGetLastError()); (cudaMalloc((void **)&d_outputs,N * sizeof(double))); \t CHECK(cudaGetLastError()); \t printf(「test%d \ n」,inputs [3]); (cudaMemcpy(d_inputs,inputs,N * sizeof(int),cudaMemcpyHostToDevice)); \t CHECK(cudaGetLastError()); \t printf(「test%d \ n」,d_inputs [1]); \t Kernel_double <<<16,2> >>(niters,d_inputs,d_outputs); \t CHECK(cudaGetLastError()); – user45690

+0

添加後,我得到錯誤,在cudaMemcpy行中,說無效的參數 – user45690

回答

4
  1. 您有一個CUDA代碼的麻煩任何時候,你應該使用proper cuda error checkingcuda-memcheck運行代碼,以前要求別人的幫助。即使你不明白錯誤輸出,它也會對其他試圖幫助你的人有用。如果您在此處使用了適當的cuda錯誤檢查,則會由於下面的第3項而被通知您的cudaMemcpy操作報告了無效參數。
  2. 你的代碼不會編譯。 cpu沒有在任何地方定義。
  3. 我們不分配的,或創建設備指針這樣的:

    int d_inputs[N]; 
    double d_outputs[N]; 
    

    這些都是編譯器被允許當作好像它是一個常量指針創建堆棧變量(陣列)。相反,你應該做這樣的:

    int *d_inputs; 
    double *d_outputs; 
    

    ,編譯器理解,這些修改指針(你將cudaMalloc以後修改)。

  4. 一旦你修復項目3的問題,這會不會是合法的:

    printf("test %d \n", d_inputs[1]); 
    

    因爲這需要解引用在主機代碼的設備指針(d_inputs),這是在CUDA非法的,至少你在這裏完成了。稍後在代碼中printf聲明中也有類似問題(使用d_outputs)。

下面的代碼具有上述項目涉及到一定程度,並且似乎爲我正常運行:

$ cat t44.cu 
#include <cuda_runtime.h> 
#include <cuda.h> 
#include <stdio.h> 
#include <sys/time.h> 
#include <math.h> 

#define N 32 

__global__ void Kernel_double(int niters, int* d_inputs,double* d_outputs) 
{ 
    int tid = blockIdx.x * blockDim.x + threadIdx.x; 

    if (tid<N) { 
    double val =(double) d_inputs[tid]; 
    /*for (int iter=0; iter < niters; iter++){ 
    val = (sqrt(pow(val,2.0)) + 5.0) - 101.0; 
    val = (val/3.0) + 102.0; 
    val = (val + 1.07) - 103.0; 
    val = (val/1.037) + 104.0; 
    val = (val + 3.00) - 105.0; 
    val = (val/0.22) + 106.0; 
    }*/ 
    val = val + 1.0; 
    //printf("This is %f\n",val); 
    d_outputs[tid] = val; 
} 
} 

int main(int argc, char **argv) 
{ 

    int niters = 10; 
    int cpu = 0; 
    printf("Iterate %d times with GPU 0 or CPU 1: %d\n", niters, cpu); 

    int inputs[N]; 
    for (int i = 0; i<N; i++){ 
    inputs[i] = i+1; 
    } 

    int *d_inputs; 
    double *d_outputs; 
    double outputs[N]; 

    cudaMalloc((void**)&d_inputs, N*sizeof(int)); 
    cudaMalloc((void**)&d_outputs, N*sizeof(double)); 
    printf("test %d \n", inputs[3]); 
    cudaMemcpy(d_inputs, inputs, N*sizeof(int), cudaMemcpyHostToDevice); 
// printf("test %d \n", d_inputs[1]); 
    Kernel_double<<<16,2>>>(niters, d_inputs,d_outputs); 
    //cudaDeviceSynchronize(); 
    cudaMemcpy(outputs, d_outputs, N*sizeof(double), cudaMemcpyDeviceToHost); 
    for(int j =0;j<10; j++){ 
     printf("Outputs[%d] is: %f\n",j, outputs[j]); 
     } 
    cudaFree(d_inputs); 
    cudaFree(d_outputs); 

    return EXIT_SUCCESS; 
} 
$ nvcc -lineinfo -arch=sm_61 -o t44 t44.cu 
$ cuda-memcheck ./t44 
========= CUDA-MEMCHECK 
Iterate 10 times with GPU 0 or CPU 1: 0 
test 4 
Outputs[0] is: 2.000000 
Outputs[1] is: 3.000000 
Outputs[2] is: 4.000000 
Outputs[3] is: 5.000000 
Outputs[4] is: 6.000000 
Outputs[5] is: 7.000000 
Outputs[6] is: 8.000000 
Outputs[7] is: 9.000000 
Outputs[8] is: 10.000000 
Outputs[9] is: 11.000000 
========= ERROR SUMMARY: 0 errors 
$ 
+0

非常感謝!我會嘗試逐點糾正我的代碼,並習慣錯誤檢查工具。非常感謝! – user45690

+0

如果這有幫助,請考慮接受這個答案。 – Taro