Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-04-26 09:01:13

0001 /***********************************************************************************\
0002 * (c) Copyright 2023-2024 CERN for the benefit of the LHCb and ATLAS collaborations *
0003 *                                                                                   *
0004 * This software is distributed under the terms of the Apache version 2 licence,     *
0005 * copied verbatim in the file "LICENSE".                                            *
0006 *                                                                                   *
0007 * In applying this licence, CERN does not waive the privileges and immunities       *
0008 * granted to it by virtue of its status as an Intergovernmental Organization        *
0009 * or submit itself to any jurisdiction.                                             *
0010 \***********************************************************************************/
0011 
0012 #ifndef __CUDACC__
0013 #  include <cuda_runtime.h>
0014 #endif
0015 #include <fmt/format.h>
0016 
0017 #include <atomic>
0018 #include <memory_resource>
0019 #include <string>
0020 
0021 #define CUDA_CHECK( stmt )                                                                                             \
0022   {                                                                                                                    \
0023     cudaError_t temp_error = ( stmt );                                                                                 \
0024     if ( ( temp_error ) != cudaSuccess ) {                                                                             \
0025       std::string errmsg = Gaudi::CUDA::err_fmt( temp_error, __FILE__, __LINE__ );                                     \
0026       error() << errmsg << endmsg;                                                                                     \
0027       return StatusCode::FAILURE;                                                                                      \
0028     }                                                                                                                  \
0029   }
0030 
0031 namespace Gaudi {
0032   namespace CUDA {
0033     inline std::string SI( double number, std::string_view unit ) {
0034       static const std::string prefix[] = { "q", "r", "y", "z", "a", "f", "p", "n", "µ", "m", "",
0035                                             "k", "M", "G", "T", "P", "E", "Z", "Y", "R", "Q" };
0036       int                      idx      = std::floor( ( std::log10( number ) + 30 ) / 3 );
0037       if ( idx < 0 ) { idx = 0; }
0038       if ( idx > 20 ) { idx = 20; }
0039       // splitting 30 into 36 - 6 gives us 6 digits after the decimal point
0040       double reduced = std::round( number / std::pow( 10, 3 * idx - 36 ) ) / 1e6;
0041       if ( unit.size() <= 1 ) { return fmt::format( "{} {}{}", reduced, prefix[idx], unit ); }
0042       return fmt::format( "{} {}·{}", reduced, prefix[idx], unit );
0043     }
0044     std::string                err_fmt( cudaError_t err, std::string file, int line );
0045     cudaError_t                cuda_stream_await( cudaStream_t stream );
0046     std::pmr::memory_resource* get_pinned_memory_resource();
0047     namespace Detail {
0048       class PinnedMemoryResource : public std::pmr::memory_resource {
0049         void*              do_allocate( std::size_t bytes, std::size_t alignment ) override;
0050         void               do_deallocate( void* p, std::size_t bytes, std::size_t alignment ) override;
0051         bool               do_is_equal( const std::pmr::memory_resource& other ) const noexcept override;
0052         std::atomic_size_t num_allocs   = 0;
0053         std::atomic_size_t num_deallocs = 0;
0054 
0055       public:
0056         ~PinnedMemoryResource() {
0057           fmt::print( "Allocated {} times and deallocated {} times\n", num_allocs.load(), num_deallocs.load() );
0058         }
0059       };
0060     } // namespace Detail
0061 
0062   } // namespace CUDA
0063 } // namespace Gaudi