Skip to content
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

Fix synchronization mistake in CUDAScopedContext #327

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions CUDADataFormats/Common/interface/CUDAProduct.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ class CUDAProduct: public CUDAProductBase {
friend class CUDAScopedContext;
friend class edm::Wrapper<CUDAProduct<T>>;

explicit CUDAProduct(int device, std::shared_ptr<cuda::stream_t<>> stream, std::shared_ptr<cuda::event_t> event, T data):
CUDAProductBase(device, std::move(stream), std::move(event)),
explicit CUDAProduct(int device, std::shared_ptr<cuda::stream_t<>> stream, T data):
CUDAProductBase(device, std::move(stream)),
data_(std::move(data))
{}

template <typename... Args>
explicit CUDAProduct(int device, std::shared_ptr<cuda::stream_t<>> stream, std::shared_ptr<cuda::event_t> event, Args&&... args):
CUDAProductBase(device, std::move(stream), std::move(event)),
explicit CUDAProduct(int device, std::shared_ptr<cuda::stream_t<>> stream, Args&&... args):
CUDAProductBase(device, std::move(stream)),
data_(std::forward<Args>(args)...)
{}

Expand Down
10 changes: 8 additions & 2 deletions CUDADataFormats/Common/interface/CUDAProductBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,18 @@ class CUDAProductBase {
cuda::event_t *event() { return event_.get(); }

protected:
explicit CUDAProductBase(int device, std::shared_ptr<cuda::stream_t<>> stream, std::shared_ptr<cuda::event_t> event);
explicit CUDAProductBase(int device, std::shared_ptr<cuda::stream_t<>> stream):
stream_{std::move(stream)},
device_{device}
{}

private:
friend class CUDAScopedContext;

// Intended to be used only from CUDAScopedContext
// The following functions are intended to be used only from CUDAScopedContext
void setEvent(std::shared_ptr<cuda::event_t> event) {
event_ = std::move(event);
}
const std::shared_ptr<cuda::stream_t<>>& streamPtr() const { return stream_; }

bool mayReuseStream() const {
Expand Down
6 changes: 0 additions & 6 deletions CUDADataFormats/Common/src/CUDAProductBase.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@
#include "FWCore/ServiceRegistry/interface/Service.h"
#include "HeterogeneousCore/CUDAServices/interface/CUDAService.h"

CUDAProductBase::CUDAProductBase(int device, std::shared_ptr<cuda::stream_t<>> stream, std::shared_ptr<cuda::event_t> event):
stream_(std::move(stream)),
event_(std::move(event)),
device_(device)
{}

bool CUDAProductBase::isAvailable() const {
// In absence of event, the product was available already at the end
// of produce() of the producer.
Expand Down
10 changes: 7 additions & 3 deletions HeterogeneousCore/CUDACore/interface/CUDAScopedContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,23 @@ class CUDAScopedContext {

template <typename T>
std::unique_ptr<CUDAProduct<T> > wrap(T data) {
createEventIfStreamBusy();
// make_unique doesn't work because of private constructor
//
// CUDAProduct<T> constructor records CUDA event to the CUDA
// stream. The event will become "occurred" after all work queued
// to the stream before this point has been finished.
return std::unique_ptr<CUDAProduct<T> >(new CUDAProduct<T>(device(), streamPtr(), event_, std::move(data)));
std::unique_ptr<CUDAProduct<T> > ret(new CUDAProduct<T>(device(), streamPtr(), std::move(data)));
createEventIfStreamBusy();
ret->setEvent(event_);
return ret;
}

template <typename T, typename... Args>
auto emplace(edm::Event& iEvent, edm::EDPutTokenT<T> token, Args&&... args) {
auto ret = iEvent.emplace(token, device(), streamPtr(), std::forward<Args>(args)...);
createEventIfStreamBusy();
return iEvent.emplace(token, device(), streamPtr(), event_, std::forward<Args>(args)...);
const_cast<T&>(*ret).setEvent(event_);
return ret;
}

private:
Expand Down