2016-03-06 166 views
1

我正在关注并行计算的udacity系列,该系列在nvidia的网站上可以找到,用于学习cuda,第一个程序是计算非常平坦的结果。CUDA程序产生意想不到的结果

该代码被简单地假设来计算一个数的立方体,但是该程序产生的输出是:

#include <stdio.h> 

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

int main(int argc, char ** argv) { 
    const int ARRAY_SIZE = 64; 
    const int ARRAY_BYTES = ARRAY_SIZE * sizeof(float); 
    // generate the input array on the host 
    float h_in[ARRAY_SIZE]; 
    for (int i = 0; i < ARRAY_SIZE; i++) { 
     h_in[i] = float(i); 
    } 
    float h_out[ARRAY_SIZE]; 

    // declare GPU memory pointers 
    float * d_in; 
    float * d_out; 

    // allocate GPU memory 
    cudaMalloc((void**) &d_in, ARRAY_BYTES); 
    cudaMalloc((void**) &d_out, ARRAY_BYTES); 

    // transfer the array to the GPU 
    cudaMemcpy(d_in, h_in, ARRAY_BYTES, cudaMemcpyHostToDevice); 

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

    // copy back the result array to the CPU 
    cudaMemcpy(h_out, d_out, ARRAY_BYTES, cudaMemcpyDeviceToHost); 

    // print out the resulting array 
    for (int i =0; i < ARRAY_SIZE; i++) { 
     printf("%f", h_out[i]); 
     printf(((i % 4) != 3) ? "\t" : "\n"); 
    } 

    cudaFree(d_in); 
    cudaFree(d_out); 

    return 0; 
} 

    0.000000 0.000000 0.000000 0.000000 
-13140721427756115471762456576.000000 0.000000 0.000006 0.000000 
0.000000 0.000000 0.000000 0.000000 
0.000000 0.000000 0.000003 0.000000 
0.000000 0.000000 -13140721427756115471762456576.000000 0.000000 
0.000000 0.000000 0.000006 0.000000 
0.000000 0.000000 0.000000 0.000000 
0.000000 0.000000 0.000000 0.000000 
0.000000 0.000000 0.000003 0.000000 
0.000000 0.000000 -13140721427756115471762456576.000000 0.000000 
0.000000 0.000000 0.000000 0.000000 
0.000006 0.000000 0.000000 0.000000 
0.000000 0.000000 0.000000 0.000000 
0.000006 0.000000 0.000000 0.000000 
0.000000 0.000000 0.000000 0.000000 
-13141061438142882086217842688.000000 0.000000 0.000000 0.000000 



uname -a 

产生

Linux ubuntu14 3.19.0-43-generiC#49~14.04.1-Ubuntu SMP Thu Dec 31 15:44:49 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux 

而且nvcc --version 产生

nvcc: NVIDIA (R) Cuda compiler driver 
Copyright (c) 2005-2013 NVIDIA Corporation 
Built on Wed_Jul_17_18:36:13_PDT_2013 
Cuda compilation tools, release 5.5, V5.5.0 

我从apt-get安装了这个,我正在运行一个gtx 980 ti

我在nvidia cuda下载网站上注意到的一件事情是,核心3.19没有明确提到在ubuntu 14.04上支持,这可能只是一个内核问题吗?如果没有正确的方向点赞赏。

+0

@OliverCharlesworth:你的权利,但是只有1块是运行,所以在这种情况下没有真正的相关性。 – talonmies

+0

我不确定,因为我是新来的cuda,但我不认为这会导致问题?我认为这与我的cuda安装有关。 – joshu

+0

@talonmies - 足够公平(自从我使用Cuda以来,这已经是**很长的时间了) –

回答

4

问题中的代码是完全正确的,但是OP已经为他/她的GPU模型(带有GTX 980Ti的CUDA 5.5)安装了不受支持的CUDA版本。升级到受支持的版本可消除该问题。

[注:这个答案已经从评论组装并添加为社区维基条目,以获得问题关闭解答列表为CUDA标签]

相关问题