'sprintf-like function for CUDA device-side code?
I could not find anything in internet. Due to the fact that it is possible to use printf
in a __device__
function I am wondering if there is a sprintf
like function due to the fact that printf
is "using" the result from sprintf
to be displayed in stdout
.
Solution 1:[1]
No there isn't anything built into CUDA for this.
Within CUDA the implementation of device printf
is a special case and does not use the same mechanisms as the C library printf.
Solution 2:[2]
sprintf()
, snprintf()
and additional printf()
-family functions are now available on the development branch of the CUDA Kernel Author's Toolkit, a.k.a. cuda-kat. Signatures:
namespace kat {
__device__ int sprintf(char* s, const char* format, ...);
__device__ int snprintf(char* s, size_t n, const char* format, ...);
}
... and they do exactly what you would expect. In particular, they support the C standard features which CUDA printf()
does not, and then some (e.g. specifying a string argument's field width using an extra argument; format specifiers for size_t
, and ptrdiff_t
, and printing in base-2).
Caveat: I am the author of cuda-kat, so I'm biased...
Always prefer snprintf()
which takes the buffer size oversprintf()
which might overflow.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Robert Crovella |
Solution 2 |