File indexing completed on 2025-12-15 10:14:15
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #include <fmt/format.h>
0013
0014 #include <atomic>
0015 #include <memory_resource>
0016 #include <string>
0017
0018 namespace Gaudi {
0019 namespace CUDA {
0020 inline std::string SI( double number, std::string_view unit ) {
0021 static const std::string prefix[] = { "q", "r", "y", "z", "a", "f", "p", "n", "µ", "m", "",
0022 "k", "M", "G", "T", "P", "E", "Z", "Y", "R", "Q" };
0023 int idx = std::floor( ( std::log10( number ) + 30 ) / 3 );
0024 if ( idx < 0 ) { idx = 0; }
0025 if ( idx > 20 ) { idx = 20; }
0026
0027 double reduced = std::round( number / std::pow( 10, 3 * idx - 36 ) ) / 1e6;
0028 if ( unit.size() <= 1 ) { return fmt::format( "{} {}{}", reduced, prefix[idx], unit ); }
0029 return fmt::format( "{} {}·{}", reduced, prefix[idx], unit );
0030 }
0031 std::string err_fmt( cudaError_t err, std::string file, int line );
0032 cudaError_t cuda_stream_await( cudaStream_t stream );
0033 std::pmr::memory_resource* get_pinned_memory_resource();
0034 namespace Detail {
0035 class PinnedMemoryResource : public std::pmr::memory_resource {
0036 void* do_allocate( std::size_t bytes, std::size_t alignment ) override;
0037 void do_deallocate( void* p, std::size_t bytes, std::size_t alignment ) override;
0038 bool do_is_equal( const std::pmr::memory_resource& other ) const noexcept override;
0039 std::atomic_size_t num_allocs = 0;
0040 std::atomic_size_t num_deallocs = 0;
0041
0042 public:
0043 ~PinnedMemoryResource() {
0044 #ifndef NDEBUG
0045 fmt::print( "Allocated {} times and deallocated {} times\n", num_allocs.load(), num_deallocs.load() );
0046 #endif
0047 }
0048 };
0049 }
0050
0051 }
0052 }