c++ Cuda代码不运行内核

标签 c++ cuda

我最近安装了 Ubuntu 14.04.1 LTS。我正在编译我编写的一个小型矩阵加法程序,matrixAddition.cu。我这样编译代码:nvcc matrixAddition.cu -o matAdd。 生成的矩阵应显示 7 7 7 ...,因为我添加了一个 5 的数组和一个 2 的数组。但是,它为我打印 5 5 5 ...。内核是否由于某种原因无法工作?我错过了一些代码吗?感谢阅读。

矩阵加法

    #include <iostream>
    #include <stdio.h>

  __global__ void matAdd(int * d_arra, int * d_arrb, int * length)
  {
    int id = threadIdx.x;
    if(id<*length)
    {
            d_arra[id]=d_arra[id]+d_arrb[id];
    }
 }


int main () {

    //cpu varriables
    int arra[100];
    int arrb[100];
    int leng = 100;

    //gpu varriables
    int * d_arra;
    int * d_arrb;
    int * length;

    //-std=c++11
    for(int itr=0;itr<100;itr++){arra[itr]=5;arrb[itr]=2;}

    cudaMalloc( (void**)&d_arra, 100*sizeof(int)); 
    cudaMalloc( (void**)&d_arrb, 100*sizeof(int)); 
    cudaMemcpy( d_arra, arra, 100*sizeof(int), cudaMemcpyHostToDevice ); 
    cudaMemcpy( d_arrb, arrb, 100*sizeof(int), cudaMemcpyHostToDevice ); 
    cudaMemcpy( length, &leng, sizeof(int), cudaMemcpyHostToDevice ); 


    //max thread per block 1024
    matAdd<<<1, 1024>>>(d_arra, d_arrb, length);
    cudaMemcpy( arra, d_arra, 100*sizeof(int), cudaMemcpyDeviceToHost ); 
    cudaFree( d_arra );
    cudaFree( d_arrb );
    cudaFree( length );

    std::cout << " our data \n";

    for(int itr=0;itr<100;itr++){std::cout << arra[itr]; if(itr%10==0&&itr!=0){std::cout <<"\n";}}
    std::cout<<std::endl;


 return 0;
}

最佳答案

该错误在 C 语言中很常见。变量“length”未初始化。在将数据复制到地址之前,您忘记为点变量分配内存空间。

如果使用类型“int *”,你必须为它做 cudaMalloc 作为 d_arra/d_arrb。但最好使用类型“int”而不是“int *”作为 CUDA Samples 中的示例 vectorAdd,因为只有一个 int 被传递到您的 CUDA 设备。

另一个小问题在你的 cout 部分。由于数组索引是从0开始的,所以当"(itr+1)%10==0 && itr!=0"时需要输出一个换行符。

//cpu varriables
int arra[100];
int arrb[100];
int leng = 100;

//gpu varriables
int * d_arra;
int * d_arrb;
int * length;

//-std=c++11
for (int itr=0; itr<100; itr++)
{
    arra[itr]=5;
    arrb[itr]=2;
}

cudaMalloc( (void**)&d_arra, 100*sizeof(int)); 
cudaMalloc( (void**)&d_arrb, 100*sizeof(int)); 
cudaMalloc( (void**)&length, 1*sizeof(int)); // Add this line
cudaMemcpy( d_arra, arra, 100*sizeof(int), cudaMemcpyHostToDevice ); 
cudaMemcpy( d_arrb, arrb, 100*sizeof(int), cudaMemcpyHostToDevice ); 
cudaMemcpy( length, &leng, sizeof(int), cudaMemcpyHostToDevice ); 


//max thread per block 1024
matAdd<<<1, 1024>>>(d_arra, d_arrb, length);
cudaMemcpy( arra, d_arra, 100*sizeof(int), cudaMemcpyDeviceToHost ); 
cudaFree( d_arra );
cudaFree( d_arrb );
cudaFree( length );

std::cout << " our data \n";

for (int itr=0; itr<100; itr++)
{
    std::cout << arra[itr]; 
    if((itr+1)%10==0 && itr!=0)
    {
        std::cout <<"\n";
    }
}
std::cout<<std::endl;

关于c++ Cuda代码不运行内核,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27872963/

相关文章:

cmake - 缺少的变量是 : CMAKE_CUDA_COMPILE_WHOLE_COMPILATION

c++ - 如何在全局实例化变量上处理 cudaFree

c++ - "fun"和 "&fun"之间的类型差异?

c++ - C++ 中的三维整数数组

C++错误C2143语法错误: missing ';' before function name

windows - 如何在 Windows 上部署 CUDA 程序?

memory-management - CUDA 映射内存是否占用 GPU RAM?

c++ - 取消引用和后缀++优先级

c++ - DLL 不会在生成后事件中复制

c++ - 如何有效地将内核 malloc 数据返回给 cpu