我想使用CUDA 5.0鏈接來編寫可重用的CUDA對象。我已經設置了這個簡單的測試,但是我的內核無聲地失敗(運行沒有錯誤或異常並輸出垃圾)。如何從包含設備成員變量的類中調用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集合)。運行在Win7-64位用的GeForce 580
在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的建議試驗了DECLs(__device__ __host__
),但沒有任何效果。
任何人都可以建議如何使他的工作? (代碼在不在課堂內時起作用)
GTX580不支持sm_35,所以我猜你正在GTX680(開普勒)上運行這個。 –
謝謝。我會檢查文檔,看看我能做什麼(我的卡支持最高)。我已經得到了580.明天會公佈結果。 – axon
@OguzMeteer,你不能猜測。另外,GTX 680是Compute 3.0,而不是3.5。 – sgarizvi