fmtlib / fmt

A modern formatting library
https://fmt.dev
Other
19.9k stars 2.43k forks source link

Advice for extending libfmt for GPU support? #3939

Closed jaredhoberock closed 2 months ago

jaredhoberock commented 2 months ago

Hello fmt maintainers, thanks for such a neat library.

I would like to use fmt in GPU code and have been exploring what would be necessary to make that possible. I've had some success with the Circle compiler. Here is my fork with a demo program: https://github.com/jaredhoberock/fmt/blob/circle/hello_gpu.cpp

I found it necessary to make one small edit to fmt::detail::assert_fail to make this work. GPU code cannot call std::fprintf or std::terminate, so another option was necessary:

FMT_FUNC void assert_fail(const char* file, int line, const char* message) {
#if defined(__circle_lang__) and defined(__CUDACC__)
  NV_IF_ELSE_TARGET(NV_IS_DEVICE, (
    // GPU code has no access to either std::fprintf or std::terminate, so call __assert_fail
    __assert_fail(message, file, line, /*function=*/nullptr);
  ), (
    // Use unchecked std::fprintf to avoid triggering another assertion when
    // writing to stderr fails
    std::fprintf(stderr, "%s:%d: assertion failed: %s", file, line, message);
    // Chosen instead of std::abort to satisfy Clang in CUDA mode during device
    // code pass.
    std::terminate();
  ))
#else
  // Use unchecked std::fprintf to avoid triggering another assertion when
  // writing to stderr fails
  ...
#endif
}

I know that fmt is flexible, so I wonder if there already exists an option to configure fmt to achieve this sort of thing without editing the source.

Failing that, would the maintainers be interested in brainstorming an enhancement that could deal this issue directly?

vitaut commented 2 months ago

You can define FMT_ASSERT so that assert_fail is not called.

jaredhoberock commented 2 months ago

Thanks for the help!