Everything goes to a raw string
It is possible to use Strf on CUDA devices,
but are naturally some limitations:
you can’t write to FILE*
nor std::string
,
nor anything that is not usable in device code.
Basically, you can only write raw strings.
So, the kernel — the function marked with __global__
qualifier — needs to receive a char*
( or a pointer to another character type )
pointing to where the content is to be written.
#include <strf.hpp>
__global__ void sample(char* dest, std::size_t dest_size)
{
strf::cstr_destination writer{dest, dest_size};
auto print = strf::to(writer);
print("Hello World!\n");
print("blah blah blah", 1234, '\n');
writer.finish();
}
Static link and CMake
To build Strf as static library for CUDA, you only need to compile the file
strf/strf.cu
. And your code
shall define the macro STRF_CUDA_SEPARATE_COMPILATION
.
Alternatively, you can use Strf CMake project. In this case, you need
to turn on the STRF_CUDA_SUPPORT
option:
cmake -G <generator-name> -DSTRF_CUDA_SEPARATE_COMPILATION=ON … <path-to-source>
As explained in another document, it is possible to
integrate Strf using add_subdirectory
, or
exporting and importing. Both solution gives you the
Strf::StrfCuda
CMake target, which represents the Srtf static
library for CUDA.
Unsupported features on CUDA devices
The folowing features are not supported on device code:
-
to_basic_string
,to_string
,to_u8string
,to_u16string
,to_u32string
,to_wstring
-
to(std::basic_streambuf</*…*/>
-
to(FILE*)
-
wto(FILE*)
-
uppercase
,lowercase
,mixedcase
( instead, you need to typelettercase::upper
,lettercase::lower
andlettercase::mixed
).