基本的に、メイン関数に配置する必要がある cuPrintf によって提供される 3 つの呼び出しのラッパーを作成しました。私の kernel.cu ファイルでは、いくつかの extern "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
これが他の誰かに役立つことを願っています。