很簡單的問題,但我不能讓它工作...如何在我的主要功能上使用CUDA函數的結構(它們在不同的文件上)?如何鏈接它們?使用VS2008
我有這樣的結構:
struct Rand48
{
// strided iteration constants (48-bit, distributed on 2x 24-bit)
uint2 A, C;
// CUDA array -- random numbers for all threads
uint2 *state;
// random number for a single thread (used by CUDA device functions only)
uint2 state0;
// magic constants for rand48
static const unsigned long long a = 0x5DEECE66DLL, c = 0xB;
void init(int nThreads, int seed) {
uint2* seeds = new uint2[ nThreads ];
cudaMalloc((void**) &state, sizeof(uint2)*nThreads);
// calculate strided iteration constants
unsigned long long A, C;
A = 1LL; C = 0LL;
for (unsigned int i = 0; i < (unsigned int)nThreads; ++i) {
C += A*c;
A *= a;
}
this->A.x = A & 0xFFFFFFLL;
this->A.y = (A >> 24) & 0xFFFFFFLL;
this->C.x = C & 0xFFFFFFLL;
this->C.y = (C >> 24) & 0xFFFFFFLL;
// prepare first nThreads random numbers from seed
unsigned long long x = (((unsigned long long)seed) << 16) | 0x330E;
for (unsigned int i = 0; i < (unsigned int)nThreads; ++i) {
x = a*x + c;
seeds[i].x = x & 0xFFFFFFLL;
seeds[i].y = (x >> 24) & 0xFFFFFFLL;
}
cudaMemcpy(state, seeds, sizeof(uint2)*nThreads, cudaMemcpyHostToDevice);
delete[] seeds;
}
void destroy() {
cudaFree((void*) state);
}
};
它有一些CUDA功能,如cudamalloc,和一些正常的主機C代碼。
我該如何做這項工作?像:
如果我把這個代碼放在.cu
文件中,VS會用nvcc編譯它。但是接下來我不會在我的main.cpp文件中聲明結構(包括.cu
可能也不會)。 如果我把它放在.h
文件中,VS會抱怨我沒有聲明int2
和所有其他CUDA的東西。
我應該把這個結構放在哪裏?我如何鏈接這個和我的主要?
我發現一個問題,使我的代碼工作。 `HTTP:// stackoverflow.com /問題/ 2090974 /如何對獨立型CUDA代碼 - 到 - 多files`。另外,我還包括聲明所有Rand48使用的標題,然後VS接受它。像魅力一樣工作。謝謝。 – hfingler 2010-11-24 04:09:05