-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add public interface for constructing and freeing caching allocators
- Loading branch information
Showing
6 changed files
with
81 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
HeterogeneousCore/CUDAUtilities/interface/cachingAllocators.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#ifndef HeterogeneousCore_CUDAUtilities_interface_cachingAllocators_h | ||
#define HeterogeneousCore_CUDAUtilities_interface_cachingAllocators_h | ||
|
||
namespace cms::cuda::allocator { | ||
// Use caching or not | ||
constexpr bool useCaching = true; | ||
|
||
// these intended to be called only from CUDAService | ||
void cachingAllocatorsConstruct(); | ||
void cachingAllocatorsFreeCached(); | ||
} // namespace cms::cuda::allocator | ||
|
||
#endif |
42 changes: 42 additions & 0 deletions
42
HeterogeneousCore/CUDAUtilities/src/cachingAllocatorCommon.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#ifndef HeterogeneousCore_CUDACore_src_cachingAllocatorCommon | ||
#define HeterogeneousCore_CUDACore_src_cachingAllocatorCommon | ||
|
||
#include "HeterogeneousCore/CUDAUtilities/interface/cudaCheck.h" | ||
#include "HeterogeneousCore/CUDAUtilities/interface/deviceCount.h" | ||
|
||
#include <algorithm> | ||
#include <limits> | ||
|
||
namespace cms::cuda::allocator { | ||
// Growth factor (bin_growth in cub::CachingDeviceAllocator | ||
constexpr unsigned int binGrowth = 2; | ||
// Smallest bin, corresponds to binGrowth^minBin bytes (min_bin in cub::CacingDeviceAllocator | ||
constexpr unsigned int minBin = 8; | ||
// Largest bin, corresponds to binGrowth^maxBin bytes (max_bin in cub::CachingDeviceAllocator). Note that unlike in cub, allocations larger than binGrowth^maxBin are set to fail. | ||
constexpr unsigned int maxBin = 30; | ||
// Total storage for the allocator. 0 means no limit. | ||
constexpr size_t maxCachedBytes = 0; | ||
// Fraction of total device memory taken for the allocator. In case there are multiple devices with different amounts of memory, the smallest of them is taken. If maxCachedBytes is non-zero, the smallest of them is taken. | ||
constexpr double maxCachedFraction = 0.8; | ||
constexpr bool debug = false; | ||
|
||
inline size_t minCachedBytes() { | ||
size_t ret = std::numeric_limits<size_t>::max(); | ||
int currentDevice; | ||
cudaCheck(cudaGetDevice(¤tDevice)); | ||
const int numberOfDevices = deviceCount(); | ||
for (int i = 0; i < numberOfDevices; ++i) { | ||
size_t freeMemory, totalMemory; | ||
cudaCheck(cudaSetDevice(i)); | ||
cudaCheck(cudaMemGetInfo(&freeMemory, &totalMemory)); | ||
ret = std::min(ret, static_cast<size_t>(maxCachedFraction * freeMemory)); | ||
} | ||
cudaCheck(cudaSetDevice(currentDevice)); | ||
if (maxCachedBytes > 0) { | ||
ret = std::min(ret, maxCachedBytes); | ||
} | ||
return ret; | ||
} | ||
} // namespace cms::cuda::allocator | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#include "HeterogeneousCore/CUDAUtilities/interface/cachingAllocators.h" | ||
|
||
#include "getCachingDeviceAllocator.h" | ||
#include "getCachingHostAllocator.h" | ||
|
||
namespace cms::cuda::allocator { | ||
void cachingAllocatorsConstruct() { | ||
cms::cuda::allocator::getCachingDeviceAllocator(); | ||
cms::cuda::allocator::getCachingHostAllocator(); | ||
} | ||
|
||
void cachingAllocatorsFreeCached() { | ||
cms::cuda::allocator::getCachingDeviceAllocator().FreeAllCached(); | ||
cms::cuda::allocator::getCachingHostAllocator().FreeAllCached(); | ||
} | ||
} // namespace cms::cuda::allocator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters