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
[RFC] Add make_device_unique() functions to ScopedContextBase #487
Draft
makortel
wants to merge
1
commit into
cms-patatrack:CMSSW_11_1_X_Patatrack
Choose a base branch
from
makortel:cudaCtxAlloc
base: CMSSW_11_1_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.
Draft
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#ifndef HeterogeneousCore_CUDACore_ScopedContextBase_h | ||
#define HeterogeneousCore_CUDACore_ScopedContextBase_h | ||
|
||
#include "FWCore/Utilities/interface/StreamID.h" | ||
#include "HeterogeneousCore/CUDAUtilities/interface/device_unique_ptr.h" | ||
#include "HeterogeneousCore/CUDAUtilities/interface/SharedStreamPtr.h" | ||
|
||
namespace cms { | ||
namespace cuda { | ||
class ProductBase; | ||
|
||
class ScopedContextBase { | ||
public: | ||
ScopedContextBase(ScopedContextBase const&) = delete; | ||
ScopedContextBase& operator=(ScopedContextBase const&) = delete; | ||
ScopedContextBase(ScopedContextBase&&) = delete; | ||
ScopedContextBase& operator=(ScopedContextBase&&) = delete; | ||
|
||
int device() const { return currentDevice_; } | ||
|
||
// cudaStream_t is a pointer to a thread-safe object, for which a | ||
// mutable access is needed even if the ScopedContext itself | ||
// would be const. Therefore it is ok to return a non-const | ||
// pointer from a const method here. | ||
cudaStream_t stream() const { return stream_.get(); } | ||
const SharedStreamPtr& streamPtr() const { return stream_; } | ||
|
||
template <typename T> | ||
typename cms::cuda::device::impl::make_device_unique_selector<T>::non_array make_device_unique() { | ||
return cms::cuda::make_device_unique<T>(stream()); | ||
} | ||
|
||
template <typename T> | ||
typename cms::cuda::device::impl::make_device_unique_selector<T>::unbounded_array make_device_unique(size_t n) { | ||
return cms::cuda::make_device_unique<T>(n, stream()); | ||
} | ||
|
||
template <typename T, typename... Args> | ||
typename cms::cuda::device::impl::make_device_unique_selector<T>::bounded_array make_device_unique(Args&&...) = | ||
delete; | ||
|
||
protected: | ||
// The constructors set the current device, but the device | ||
// is not set back to the previous value at the destructor. This | ||
// should be sufficient (and tiny bit faster) as all CUDA API | ||
// functions relying on the current device should be called from | ||
// the scope where this context is. The current device doesn't | ||
// really matter between modules (or across TBB tasks). | ||
explicit ScopedContextBase(edm::StreamID streamID); | ||
|
||
explicit ScopedContextBase(const ProductBase& data); | ||
|
||
explicit ScopedContextBase(int device, SharedStreamPtr stream); | ||
|
||
private: | ||
int currentDevice_; | ||
SharedStreamPtr stream_; | ||
}; | ||
} // namespace cuda | ||
} // namespace cms | ||
|
||
#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
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,27 @@ | ||
#include "CUDADataFormats/Common/interface/ProductBase.h" | ||
#include "HeterogeneousCore/CUDACore/interface/ScopedContextBase.h" | ||
#include "HeterogeneousCore/CUDAUtilities/interface/cudaCheck.h" | ||
#include "HeterogeneousCore/CUDAUtilities/interface/StreamCache.h" | ||
|
||
#include "chooseDevice.h" | ||
|
||
namespace cms::cuda { | ||
ScopedContextBase::ScopedContextBase(edm::StreamID streamID) : currentDevice_(chooseDevice(streamID)) { | ||
cudaCheck(cudaSetDevice(currentDevice_)); | ||
stream_ = getStreamCache().get(); | ||
} | ||
|
||
ScopedContextBase::ScopedContextBase(const ProductBase& data) : currentDevice_(data.device()) { | ||
cudaCheck(cudaSetDevice(currentDevice_)); | ||
if (data.mayReuseStream()) { | ||
stream_ = data.streamPtr(); | ||
} else { | ||
stream_ = getStreamCache().get(); | ||
} | ||
} | ||
|
||
ScopedContextBase::ScopedContextBase(int device, SharedStreamPtr stream) | ||
: currentDevice_(device), stream_(std::move(stream)) { | ||
cudaCheck(cudaSetDevice(currentDevice_)); | ||
} | ||
} // namespace cms::cuda |
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
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
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.
If this (base) class gets exposed to users, I'm tempted to rename it to just
ScopedContext
. (Even if I would then have to figure out what to do for currentScopedContext.h
. One option is to merge the headers back together and make it C++14-compatible).