简体   繁体   中英

How to do you use cuPrintf with cuda projects that have the main() in a .cpp file?

So I'm trying to speed up some collision detection code that uses SDL to draw some colliding balls to a surface / window in Mac OS X. I can do the collision and draw stuff in a sequential manner just fine already. I would like to be able to debug the cuda version with cuPrintf, but I can't get it to work since my main() is not in a .cu file. So I can't initialize cuPrintf, nor can I print the buffer. If I create a couple extern "C" functions and build them into the .cpp file, I get nothing. If I try to put the wrapper functions in the .cu file with the rest of my cuda code I get an "Error: use of external function ... is not supported". I've used it on smaller projects with everything just inside one big .cu file, and it works great. But I can't do that this time because I have to separate the SDL and cuda code, and the SDL has to go in the main() as well.

Any one else ever have this problem ?

I basically created a wrapper for the 3 calls provided by cuPrintf that need to be put in the main function. In my kernel.cu file I defined some extern "C" functions. Then in the main.cpp I declared them to bring them in scope.

In 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(); }
}

In 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()
}

That's it! I made a copy of cuPrintf.cu and cuPrintf.cuh in my project directory so I didn't have to link to some random directory in the compile. My nvcc / g++ commands are below. I code on a MAC, so they are Mac OS X specific...

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

NOTE: I separated all of my SDL functions into a separate SDL_helper.c file which I compiled before running nvcc with:

g++ ./SDL_helper.c -I./ -c

I hope this helps someone else.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM