Skip to content

Commit

Permalink
fix a problem with UserEvents released too early
Browse files Browse the repository at this point in the history
  • Loading branch information
franz committed May 14, 2023
1 parent 10b2fa1 commit 781d7a9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
11 changes: 6 additions & 5 deletions src/backend/OpenCL/CHIPBackendOpenCL.cc
Original file line number Diff line number Diff line change
Expand Up @@ -967,7 +967,9 @@ void CL_CALLBACK pfn_notify(cl_event Event, cl_int CommandExecStatus,
return;
Cbo->Callback(Cbo->Stream, Cbo->Status, Cbo->UserData);
if (Cbo->CallbackFinishEvent != nullptr) {
static_cast<cl::UserEvent &>(Cbo->CallbackFinishEvent->get()).setStatus(CL_COMPLETE);
logTrace("Cbo->CallbackFinishEvent: {}", (void *)Cbo->CallbackFinishEvent);
cl_event Ev = Cbo->CallbackFinishEvent->get().get();
clSetUserEventStatus(Ev, CL_COMPLETE);
Cbo->CallbackFinishEvent->decreaseRefCount("Notified finished.");
}
delete Cbo;
Expand Down Expand Up @@ -1016,7 +1018,6 @@ void CHIPQueueOpenCL::MemUnmap(const AllocationInfo *AllocInfo) {

void CHIPQueueOpenCL::addCallback(hipStreamCallback_t Callback,
void *UserData) {
logTrace("CHIPQueueOpenCL::addCallback()");

cl::Context &ClContext_ = ((CHIPContextOpenCL *)ChipContext_)->get();
cl_int Err;
Expand All @@ -1025,7 +1026,7 @@ void CHIPQueueOpenCL::addCallback(hipStreamCallback_t Callback,
(CHIPEventOpenCL *)Backend->createCHIPEvent(ChipContext_);
cl::UserEvent HoldBackClEvent(ClContext_, &Err);
CHIPERR_CHECK_LOG_AND_THROW(Err, CL_SUCCESS, hipErrorTbd);
HoldBackEvent->reset(HoldBackClEvent());
HoldBackEvent->reset(std::move(HoldBackClEvent));

std::vector<CHIPEvent *> WaitForEvents{HoldBackEvent};
auto LastEvent = getLastEvent();
Expand All @@ -1046,7 +1047,7 @@ void CHIPQueueOpenCL::addCallback(hipStreamCallback_t Callback,
(CHIPEventOpenCL *)Backend->createCHIPEvent(ChipContext_);
cl::UserEvent CallbackClEvent(ClContext_, &Err);
CHIPERR_CHECK_LOG_AND_THROW(Err, CL_SUCCESS, hipErrorTbd);
CallbackEvent->reset(CallbackClEvent());
CallbackEvent->reset(std::move(CallbackClEvent));

// Make the succeeding commands wait for the user event which will be
// set CL_COMPLETE by the callback trampoline function pfn_notify after
Expand All @@ -1067,7 +1068,7 @@ void CHIPQueueOpenCL::addCallback(hipStreamCallback_t Callback,
ClQueue.flush();

// Now the CB can start executing in the background:
HoldBackClEvent.setStatus(CL_COMPLETE);
HoldBackEvent->getAsUserEv().setStatus(CL_COMPLETE);

return;
};
Expand Down
19 changes: 17 additions & 2 deletions src/backend/OpenCL/CHIPBackendOpenCL.hh
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,23 @@ public:

virtual bool updateFinishStatus(bool ThrowErrorIfNotReady = true) override;
cl::Event &get() { return ClEvent; }
void reset(cl::Event &&Ev) { ClEvent = Ev; }
void reset(cl_event Ev) { ClEvent = Ev; }
cl::UserEvent &getAsUserEv() { return static_cast<cl::UserEvent &>(ClEvent); }
void reset(cl::UserEvent &&Ev) {
ClEvent = Ev;
logTrace("UserEvent {} Moved into {} || NOW: {}", (void *)Ev.get(),
(void *)this, (void *)ClEvent.get());
}

void reset(cl::Event &&Ev) {
ClEvent = Ev;
logTrace("Event {} Moved into {} || NOW: {}", (void *)Ev.get(),
(void *)this, (void *)ClEvent.get());
}
void reset(cl_event Ev) {
ClEvent = Ev;
logTrace("Event {} Moved into {} ||| NOW: {}", (void *)Ev, (void *)this,
(void *)ClEvent.get());
}

// for elapsedTime
uint64_t getFinishTime();
Expand Down

0 comments on commit 781d7a9

Please sign in to comment.