Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Advice for extending libfmt for GPU support? #3939

Closed
jaredhoberock opened this issue Apr 18, 2024 · 2 comments
Closed

Advice for extending libfmt for GPU support? #3939

jaredhoberock opened this issue Apr 18, 2024 · 2 comments
Labels

Comments

@jaredhoberock
Copy link

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
Copy link
Contributor

vitaut commented Apr 19, 2024

You can define FMT_ASSERT so that assert_fail is not called.

@jaredhoberock
Copy link
Author

Thanks for the help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants