3

CUDA's printf() in kernels prints to the standard output stream of my process. Now, I want to, at the least, redirect this printout to an arbitrary output stream , from here on. I do mean an arbitrary stream, that is not just a file descriptor (as is requested here) - I want to be able to use stringstreams, logging infrastructure etc.

If that's possible, what I would really like to be able to do something like tell a single kernel send its printf() output to some output stream. Is this possible?

einpoklum
  • 118,144
  • 57
  • 340
  • 684

1 Answers1

1

Under Windows CUDA uses the standard handle, which is active, when the context is first initialized.

You can

  1. open/create a file or a named pipe,
  2. use SetStdHandle(STD_OUTPUT_HANDLE, handle) and call cudaFree(0) or some similar function in the beginning of your program to initialize the context. For the driver API the position of cuInit(0) is deciding. Afterwards you can
  3. reset the SetStdHandle to the previous value, if your remaining program needs it. CUDA keeps printing to your set stream handle.

Just be careful that the CUDA command is really the first one for the process.

I believe that it works similarly under Linux (probably by using the two-argument dup2 to redirect stdout).

Sebastian
  • 1,834
  • 2
  • 10
  • 22
  • I'm not a Windows person, but that seems useful. However - the `cudaFree(0)` is very hacky and not official, IMHO. Can you state that in terms of the CUDA driver API ? Is it the `cuInit()` call? A `cuPrimaryCtxRetain()` call? – einpoklum Dec 03 '21 at 13:56
  • It is the cuInit(). I put it into the answer. You can try it out under Linux by creating a small program, which prints out something from a kernel (I had to be careful that the minimalistic printing kernel had a side-effect and was not optimized away). Then use int oldout = dup2(1); (void)dup2(2, 1); before cuInit(0) and (void)dup2(oldout, 1); after cuInit(0) vs. all before or after cuInit(0). The dup2 commands should stream the Cuda output to stderr instead. If you redirect stderr of your program 2> errorlog.txt you will hopefully see a difference. The mentioned commands where not tested yet. – Sebastian Dec 04 '21 at 05:17