forked from cms-sw/cmssw
-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use unified memory for conditions #157
Open
makortel
wants to merge
12
commits into
cms-patatrack:CMSSW_10_4_X_Patatrack
Choose a base branch
from
makortel:eventsetupUnifiedMemory
base: CMSSW_10_4_X_Patatrack
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
7067416
Full workflow from raw data to pixel tracks and vertices on GPUs (#216)
VinInn 3f30343
Move cuda_bad_alloc to its own header
makortel fd0f069
Add CUDAManagedAllocator
makortel eb8daf4
Add CUDAManagedVector
makortel afd89f2
Add CUDAESManaged
makortel aba7012
Migrate PixelCPEFast to unified memory
makortel bc928df
Migrate SiPixelFedCablingMapGPUWrapper to unified memory
makortel 9a1fd14
Migrate SiPixelGainCalibrationForHLTGPU to unified memory
makortel 6b2937a
Remove CUDAESProduct as obsolete
makortel 5a160b2
Reduce calls to cudaMemPrefetchAsync
makortel 6d62b3d
Boolean flag per device
makortel cbeb333
Back to GPU struct of pointers
makortel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#ifndef HeterogeneousCore_CUDACore_interface_CUDAESManaged_h | ||
#define HeterogeneousCore_CUDACore_interface_CUDAESManaged_h | ||
|
||
#include <atomic> | ||
#include <vector> | ||
|
||
#include <cuda_runtime.h> | ||
#include <cuda/api_wrappers.h> | ||
|
||
#include "HeterogeneousCore/CUDAUtilities/interface/cudaCheck.h" | ||
|
||
/** | ||
* Class to help with memory allocations for ESProducts. Each CUDA | ||
* ESProduct wrapper should | ||
* - include an instance of this class as their member | ||
* - use allocate() to allocate the memory buffers | ||
* - call advise() after filling the buffers in CPU | ||
* - call prefetch() before returning the CUDA ESProduct | ||
* | ||
* It owns the allocated memory and frees it in its destructor. | ||
*/ | ||
class CUDAESManaged { | ||
public: | ||
CUDAESManaged(); | ||
~CUDAESManaged(); | ||
|
||
template <typename T> | ||
T *allocate(size_t elements, size_t elementSize=sizeof(T)) { | ||
T *ptr = nullptr; | ||
auto size = elementSize*elements; | ||
cudaCheck(cudaMallocManaged(&ptr, size)); | ||
buffers_.emplace_back(ptr, size); | ||
return ptr; | ||
} | ||
|
||
template <typename T> | ||
void allocate(T **ptr, size_t elements, size_t elementSize=sizeof(T)) { | ||
*ptr = allocate<T>(elements, elementSize); | ||
} | ||
|
||
// Record a buffer allocated elsewhere to be used in advise/prefetch | ||
/* | ||
void record(void *ptr, size_t size) { | ||
buffers_.emplace_back(ptr, size); | ||
} | ||
*/ | ||
|
||
void advise() const; | ||
|
||
void prefetchAsync(cuda::stream_t<>& stream) const; | ||
|
||
private: | ||
std::vector<std::pair<void *, size_t> > buffers_; | ||
mutable std::vector<std::atomic<bool>> prefetched_; | ||
}; | ||
|
||
#endif |
This file was deleted.
Oops, something went wrong.
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,35 @@ | ||
#include "HeterogeneousCore/CUDACore/interface/CUDAESManaged.h" | ||
#include "HeterogeneousCore/CUDAServices/interface/numberOfCUDADevices.h" | ||
|
||
CUDAESManaged::CUDAESManaged(): prefetched_(numberOfCUDADevices()) { | ||
for(auto& pref: prefetched_) { | ||
pref.store(false); | ||
} | ||
} | ||
|
||
CUDAESManaged::~CUDAESManaged() { | ||
for(auto& ptrSize: buffers_) { | ||
cudaFree(ptrSize.first); | ||
} | ||
} | ||
|
||
void CUDAESManaged::advise() const { | ||
for(const auto& ptrSize: buffers_) { | ||
cudaCheck(cudaMemAdvise(ptrSize.first, ptrSize.second, cudaMemAdviseSetReadMostly, 0)); // device is ignored for this advise | ||
} | ||
} | ||
|
||
void CUDAESManaged::prefetchAsync(cuda::stream_t<>& stream) const { | ||
// The boolean atomic is an optimization attempt, it doesn't really | ||
// matter if more than one thread/edm stream issues the prefetches | ||
// as long as most of the prefetches are avoided. | ||
auto& pref = prefetched_[stream.device_id()]; | ||
if(pref.load()) | ||
return; | ||
|
||
for(const auto& ptrSize: buffers_) { | ||
cudaMemPrefetchAsync(ptrSize.first, ptrSize.second, stream.device_id(), stream.id()); | ||
} | ||
|
||
pref.store(true); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking through the code again I noticed that here we/I have a single flag to control behaviour for all devices. I think this should be changed to either
atomic<bool>
flag should be changed to per-device flags, orIn principle the latter (2) could make more sense as we expect all devices to run all code (and thus need all GPU conditions) anyway. But async prefetch needs also a CUDA stream, so it needs a bit more thought. Basically it would mean that the CUDAESManaged would have additional CUDA streams (one per device), and then use CUDA events to "synchronize" with the argument stream (otherwise there would be no gain from async). Then the
atomic<bool>
flag would actually be replaced withcudaEventQuery()
for the CUDA event (of a device). I would naively imagineatomic<bool>
to be faster thancudaEventQuery()
, which would favor option 1.