基本的CUDA程序有问题。代码还是编译器?

时间:2015-06-21 02:56:20

标签: c++ cuda

我正在参加在线并行编程课程。家庭作业在他们网站上的虚拟机中完成。我的第一个任务(下面)按原样运行。将数字从0调整到ARRAY_SIZE。当我尝试在我的机器上运行它时。我得到一些奇怪的价值观。我发现代码没有任何问题。有什么建议? (我的机器上的输出发布在下面)。

是的,我知道我的内核被称为立方体,尽管事实上我只是对数字进行平方。我从来没有改过它。

#include <stdio.h>

__global__ void cube( float* d_in, float* d_out ){
int idx = threadIdx.x;
float f = d_in[idx];
d_out[idx] = f*f;
}

int main(){
    const int ARRAY_SIZE = 8;
    const int ARRAY_BYTES = ARRAY_SIZE * sizeof(float);

    // Host memory
    float h_in[ARRAY_SIZE];
    float h_out[ARRAY_SIZE];
    for( int i = 0; i < ARRAY_SIZE; i++ )
        h_in[i] = (float)i;

    // Device memory pointers
    float* d_in;
    float* d_out;

    // Allocate device memory
    cudaMalloc( (void**) &d_in, ARRAY_BYTES );
    cudaMalloc( (void**) &d_out, ARRAY_BYTES );

    // Transfer input to device
    cudaMemcpy( d_in, h_in, ARRAY_BYTES, cudaMemcpyHostToDevice );

    // Launch the kernel
    cube<<<1,ARRAY_SIZE>>>(d_out,d_in);

    // Transfer device to host
    cudaMemcpy( h_out, d_out, ARRAY_BYTES, cudaMemcpyDeviceToHost );

    for(int i = 0; i < ARRAY_SIZE; i++)
    printf("%f\n",h_out[i]);



    // Free memory
    cudaFree(d_in);
    cudaFree(d_out);

    return 0;
    }

输出低于

dan@mojo:~/Dropbox/code/gpu_programming$ nvcc -o first first.cu 
dan@mojo:~/Dropbox/code/gpu_programming$ ./first
-0.000000
-nan
-0.000000
-nan
-0.000000
nan
-nan
-nan

1 个答案:

答案 0 :(得分:2)

启动内核时切换参数的顺序,即

cube<<<1,ARRAY_SIZE>>>(d_in, d_out);
相关问题