我基本上为 cuPrintf 提供的 3 个调用创建了一个包装器,这些调用需要放在主函数中。在我的 kernel.cu 文件中,我定义了一些外部“C”函数。然后在 main.cpp 中,我声明它们将它们纳入范围。
在 kernel.cu 中:
// Include section
#include "cuPrintf.cu"
//define all __device__ and __global__ functions for the kernel here
extern "C"
{
void LaunchKernel(<type> *input) { kernel<<< grid, dim >>>(input); }
void InitCuPrintf() { cudaPrintfInit(); }
void DisplayCuPrintf() { cudaPrintfDisplay(stdout, 1); }
void EndCuPrintf() { cudaPrintfEnd(); }
}
在 main.cpp 中:
// you do NOT need to include any cuPrintf files here. Just in the kernel.cu file
#include <SDL.h> // this is the library requiring me to do this stuff ...
#include "SDL_helper.h" // all of the SDL functions I defined are separated out
#include <cuda_runtime.h>
// in global space
extern "C" {
void LaunchKernel(struct circle *circles);
void InitCuPrintf();
void DisplayCuPrintf();
void EndCuPrintf();
}
int main(nt argc, char **argv)
{
// put these where you would normally place the cuPrintf functions they correspond to
InitCuPrintf();
// I left his in here because if you're needing to do this for cuPrintf, you prolly need
// need a wrapper to lauch your kernel from outside the .cu file as well.
LaunchKernel( input );
DisplayCuPrintf();
// SDL functions from SDL.h and SDL_helper.h would be in here somewhere
EndCuPrintf()
}
而已!我在我的项目目录中制作了 cuPrintf.cu 和 cuPrintf.cuh 的副本,因此我不必在编译中链接到某个随机目录。我的 nvcc / g++ 命令如下。我在 MAC 上编码,所以它们是 Mac OS X 特定的......
nvcc ./kernel.cu -I./ -I/usr/local/cuda/include/ -c -m64
g++ ./main.cpp -I./ -I/usr/include/ -I/usr/local/cuda/include/ -L/usr/local/cuda/lib/ -lcudart -LSDLmain -lSDL -framework Cocoa ./SDL_helper.o ./kernel.o
注意:我将所有 SDL 函数分离到一个单独的 SDL_helper.c 文件中,我在运行 nvcc之前编译了该文件:
g++ ./SDL_helper.c -I./ -c
我希望这对其他人有帮助。