2010-11-23 13 views
0

很簡單的問題,但我不能讓它工作...如何在我的主要功能上使用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的東西。

我應該把這個結構放在哪裏?我如何鏈接這個和我的主要?

回答

0

至少有三種方法可以解決問題。

首先是使用nvcc來編譯main.cpp,如果可以的話。

C++的方法是使用指針實現模式(pImpl)。基本上,您將Rand48類分爲兩部分:公共方法保留在Rand48中,唯一的非方法成員是指向Impl的指針的前向聲明。這樣,您可以將Rand48類放入.h然後,您可以將CUDA特定的代碼放入Rand48::Impl中,該代碼與Rand48類本體本身斷開連接。

C方式是轉發聲明Rand48類本身(而不是Rand48::Impl),並且還聲明瞭需要Rand48*的非成員函數。

+0

我發現一個問題,使我的代碼工作。 `HTTP:// stackoverflow.com /問題/ 2090974 /如何對獨立型CUDA代碼 - 到 - 多files`。另外,我還包括聲明所有Rand48使用的標題,然後VS接受它。像魅力一樣工作。謝謝。 – hfingler 2010-11-24 04:09:05

相關問題