【问题标题】:CUDA - Simple adder program always gives zeroCUDA - 简单的加法器程序总是给出零
【发布时间】:2014-11-23 00:29:45
【问题描述】:

我遇到了一个简单的 CUDA 程序问题,它只添加了两个数字。我在 Windows 7 上使用 Geforce GT320M GPU 的笔记本电脑上运行它。我用Visual Studio 2013 编译了这个程序(我不知道这是否意味着什么)。问题是我总是得到0 结果。我试图检查给定的参数(只返回数组中给方法的所有参数),它们似乎都是0。我在另一台计算机(在大学)上运行这个程序,它运行得很好,并返回正确的结果。所以我认为应该有一些设置问题,但我不确定。

#include <cuda.h>
#include <stdio.h>

#include "cuda_runtime.h"
#include "device_launch_parameters.h"

__global__ void add(int a, int b, int* c)
{
    *c = a + b;

    return;
}

int main(int argc, char** argv)
{
    int c;
    int* dev_c;

    cudaMalloc((void**)&dev_c, sizeof(int));

    add << <1, 1 >> >(1, 2, dev_c);

    cudaMemcpy(&c, dev_c, sizeof(int), cudaMemcpyDeviceToHost);

    printf("a + b = %d\n", c);

    cudaFree(dev_c);

    return 0;
}

我还运行我在某处找到的这段代码 sn-p。

cudaSetDevice(0);
cudaDeviceSynchronize();
cudaThreadSynchronize();

这没有返回任何东西。

【问题讨论】:

标签: c++ cuda visual-studio-2013


【解决方案1】:

如果您使用典型的 CUDA 模板来创建一个使用 CUDA 的新 Visual Studio 项目,那么您必须注意正确设置要编译的计算能力,并在需要时更改默认值。这可以通过设置来完成,例如,

compute_12,sm_12

CUDA C/C++ Configuration Properties。在您的情况下,默认计算能力是2.0,而您的卡是以前的架构。这是您计算错误的根源。

附:截至 2014 年 9 月,CUDA 6.5 是唯一支持 Visual Studio 2013 的 CUDA 版本,请参阅 Is Cuda 6 supported with Visual Studio 2013?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-13
    • 1970-01-01
    • 2021-03-18
    • 1970-01-01
    • 2011-04-17
    • 2012-05-18
    相关资源
    最近更新 更多