HeterogeneousCore
CUDAUtilities
src
allocate_device.cc
Go to the documentation of this file.
1
#include <limits>
2
3
#include "
FWCore/Utilities/interface/Likely.h
"
4
#include "
HeterogeneousCore/CUDAUtilities/interface/ScopedSetDevice.h
"
5
#include "
HeterogeneousCore/CUDAUtilities/interface/allocate_device.h
"
6
#include "
HeterogeneousCore/CUDAUtilities/interface/cudaCheck.h
"
7
8
#include "
getCachingDeviceAllocator.h
"
9
10
namespace
{
11
const
size_t
maxAllocationSize =
12
notcub::CachingDeviceAllocator::IntPow
(
cms::cuda::allocator::binGrowth
,
cms::cuda::allocator::maxBin
);
13
}
14
15
namespace
cms::cuda
{
16
void
*
allocate_device
(
int
dev,
size_t
nbytes, cudaStream_t
stream
) {
17
void
*ptr =
nullptr
;
18
if
constexpr (
allocator::useCaching
) {
19
if
(
UNLIKELY
(nbytes > maxAllocationSize)) {
20
throw
std::runtime_error(
"Tried to allocate "
+ std::to_string(nbytes) +
21
" bytes, but the allocator maximum is "
+ std::to_string(maxAllocationSize));
22
}
23
cudaCheck
(
allocator::getCachingDeviceAllocator
().DeviceAllocate(dev, &ptr, nbytes,
stream
));
24
}
else
{
25
ScopedSetDevice
setDeviceForThisScope(dev);
26
cudaCheck
(cudaMalloc(&ptr, nbytes));
27
}
28
return
ptr;
29
}
30
31
void
free_device
(
int
device,
void
*ptr) {
32
if
constexpr (
allocator::useCaching
) {
33
cudaCheck
(
allocator::getCachingDeviceAllocator
().DeviceFree(device, ptr));
34
}
else
{
35
ScopedSetDevice
setDeviceForThisScope(device);
36
cudaCheck
(cudaFree(ptr));
37
}
38
}
39
40
}
// namespace cms::cuda
Likely.h
cms::cuda
Definition:
Product.h:14
allocate_device.h
cms::cuda::allocator::useCaching
constexpr bool useCaching
Definition:
getCachingDeviceAllocator.h:14
cms::cuda::stream
cudaStream_t stream
Definition:
HistoContainer.h:57
getCachingDeviceAllocator.h
UNLIKELY
#define UNLIKELY(x)
Definition:
Likely.h:21
cms::cuda::ScopedSetDevice
Definition:
ScopedSetDevice.h:10
notcub::CachingDeviceAllocator::IntPow
static unsigned int IntPow(unsigned int base, unsigned int exp)
Definition:
CachingDeviceAllocator.h:211
cms::cuda::allocator::maxBin
constexpr unsigned int maxBin
Definition:
getCachingDeviceAllocator.h:20
cms::cuda::free_device
void free_device(int device, void *ptr)
Definition:
allocate_device.cc:31
cudaCheck.h
cudaCheck
#define cudaCheck(ARG,...)
Definition:
cudaCheck.h:62
cms::cuda::allocate_device
void * allocate_device(int dev, size_t nbytes, cudaStream_t stream)
Definition:
allocate_device.cc:16
cms::cuda::allocator::binGrowth
constexpr unsigned int binGrowth
Definition:
getCachingDeviceAllocator.h:16
ScopedSetDevice.h
cms::cuda::allocator::getCachingDeviceAllocator
notcub::CachingDeviceAllocator & getCachingDeviceAllocator()
Definition:
getCachingDeviceAllocator.h:45
Generated for CMSSW Reference Manual by
1.8.16