【发布时间】:2013-03-23 13:44:38
【问题描述】:
我想使用 CUDA 5.0 链接来编写可重用的 CUDA 对象。我已经设置了这个简单的测试,但是我的内核静默失败(运行时没有错误或异常并输出垃圾)。
我的简单测试(如下)将整数数组分配给 CUDA 设备内存。 CUDA 内核应该使用顺序条目 (0,1,2,....,9) 填充数组。设备数组被复制到 CPU 内存并输出到控制台。
目前,此代码输出“0,0,0,0,0,0,0,0,0”,而不是所需的“0,1,2,3,4,5,6,7,8 ,9”。它是使用 VS2010 和 CUDA 5.0 编译的(设置了 compute_35 和 sm_35)。使用 GeForce 580 在 Win7-64 位上运行。
在 Test.h 中:
class Test
{
public:
Test();
~Test();
void Run();
private:
int* cuArray;
};
在 Test.cu 中:
#include <stdio.h>
#include <assert.h>
#include <cuda_runtime.h>
#include "Test.h"
#define ARRAY_LEN 10
__global__ void kernel(int *p)
{
int elemID = blockIdx.x * blockDim.x + threadIdx.x;
p[elemID] = elemID;
}
Test::Test()
{
cudaMalloc(&cuArray, ARRAY_LEN * sizeof(int));
}
Test::~Test()
{
cudaFree(cuArray);
}
void Test::Run()
{
kernel<<<1,ARRAY_LEN>>>(cuArray);
// Copy the array contents to CPU-accessible memory
int cpuArray[ARRAY_LEN];
cudaMemcpy(static_cast<void*>(cpuArray), static_cast<void*>(cuArray), ARRAY_LEN * sizeof(int), cudaMemcpyDeviceToHost);
// Write the array contents to console
for (int i = 0; i < ARRAY_LEN; ++i)
printf("%d,", cpuArray[i]);
printf("\n");
}
在 main.cpp 中:
#include <iostream>
#include "Test.h"
int main()
{
Test t;
t.Run();
}
我已经按照@harrism 的建议尝试了 DECL (__device__ __host__),但没有效果。
任何人都可以建议如何制作他的作品吗? (代码不在类中时有效。)
【问题讨论】:
-
GTX580 不支持 sm_35,所以我猜你是在 GTX680 (kepler) 上运行它。
-
谢谢。我会检查文档,看看我能做什么(我的卡支持的最高 sm)。我的分数是 580。明天会发布结果。
-
@OguzMeteer,你不能只是猜测。此外,GTX 680 是 Compute 3.0,而不是 3.5。
-
你说你的代码运行没有错误或异常,但你没有做任何 cuda 错误检查所以你不会知道。
-
@robert 我愿意,因为异常是从 Visual Studio 控制台报告的......为了简单起见,我从发布的代码版本中取出了 CHECKED_CALL() 宏。还是谢谢。