Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get CUDA's printf to print to an arbitrary stream?

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?

like image 223
einpoklum Avatar asked Jan 28 '26 19:01

einpoklum


1 Answers

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).

like image 194
Sebastian Avatar answered Jan 31 '26 10:01

Sebastian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!