Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:15:45

0001 #include <limits>
0002 
0003 #include "FWCore/Utilities/interface/Likely.h"
0004 #include "HeterogeneousCore/CUDAUtilities/interface/allocate_host.h"
0005 #include "HeterogeneousCore/CUDAUtilities/interface/cudaCheck.h"
0006 
0007 #include "getCachingHostAllocator.h"
0008 
0009 namespace {
0010   const size_t maxAllocationSize =
0011       notcub::CachingDeviceAllocator::IntPow(cms::cuda::allocator::binGrowth, cms::cuda::allocator::maxBin);
0012 }
0013 
0014 namespace cms::cuda {
0015   void *allocate_host(size_t nbytes, cudaStream_t stream) {
0016     void *ptr = nullptr;
0017     if constexpr (allocator::useCaching) {
0018       if (UNLIKELY(nbytes > maxAllocationSize)) {
0019         throw std::runtime_error("Tried to allocate " + std::to_string(nbytes) +
0020                                  " bytes, but the allocator maximum is " + std::to_string(maxAllocationSize));
0021       }
0022       cudaCheck(allocator::getCachingHostAllocator().HostAllocate(&ptr, nbytes, stream));
0023     } else {
0024       cudaCheck(cudaMallocHost(&ptr, nbytes));
0025     }
0026     return ptr;
0027   }
0028 
0029   void free_host(void *ptr) {
0030     if constexpr (allocator::useCaching) {
0031       cudaCheck(allocator::getCachingHostAllocator().HostFree(ptr));
0032     } else {
0033       cudaCheck(cudaFreeHost(ptr));
0034     }
0035   }
0036 
0037 }  // namespace cms::cuda