Skip to content

Commit

Permalink
Merge pull request ROCm#167 from bensander/event_safety
Browse files Browse the repository at this point in the history
Event safety
  • Loading branch information
bensander authored Aug 29, 2017
2 parents 3c172aa + 6ff74d0 commit cc08b36
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 14 deletions.
23 changes: 16 additions & 7 deletions src/hip_event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ void ihipEvent_t::attachToCompletionFuture(const hc::completion_future *cf,

void ihipEvent_t::refereshEventStatus()
{
bool isReady0 = _marker.is_ready();
bool isReady0 = locked_isReady();
bool isReady1;
int val = 0;
if (_state == hipEventStatusRecording) {
// TODO - use completion-future functions to obtain ticks and timestamps:
hsa_signal_t *sig = static_cast<hsa_signal_t*> (_marker.get_native_handle());
isReady1 = _marker.is_ready();
isReady1 = locked_isReady();
if (sig) {
val = hsa_signal_load_acquire(*sig);
if (val == 0) {
Expand All @@ -86,6 +86,17 @@ void ihipEvent_t::refereshEventStatus()
}


bool ihipEvent_t::locked_isReady()
{
return _stream->locked_eventIsReady(this);
}

void ihipEvent_t::locked_waitComplete(hc::hcWaitMode waitMode)
{
return _stream->locked_eventWaitComplete(this, waitMode);
}


hipError_t ihipEventCreate(hipEvent_t* event, unsigned flags)
{
hipError_t e = hipSuccess;
Expand Down Expand Up @@ -127,7 +138,7 @@ hipError_t hipEventCreate(hipEvent_t* event)

hipError_t hipEventRecord(hipEvent_t event, hipStream_t stream)
{
HIP_INIT_SPECIAL_API(TRACE_QUERY, event, stream);
HIP_INIT_SPECIAL_API(TRACE_SYNC, event, stream);

if (event && event->_state != hipEventStatusUnitialized) {
stream = ihipSyncAndResolveStream(stream);
Expand Down Expand Up @@ -192,9 +203,7 @@ hipError_t hipEventSynchronize(hipEvent_t event)
ctx->locked_syncDefaultStream(true, true);
return ihipLogStatus(hipSuccess);
} else {
event->_marker.wait((event->_flags & hipEventBlockingSync) ? hc::hcWaitModeBlocked : hc::hcWaitModeActive);

assert (event->_marker.is_ready());
event->locked_waitComplete((event->_flags & hipEventBlockingSync) ? hc::hcWaitModeBlocked : hc::hcWaitModeActive);

return ihipLogStatus(hipSuccess);
}
Expand Down Expand Up @@ -259,7 +268,7 @@ hipError_t hipEventQuery(hipEvent_t event)
{
HIP_INIT_SPECIAL_API(TRACE_QUERY, event);

if ((event->_state == hipEventStatusRecording) && (!event->_marker.is_ready())) {
if ((event->_state == hipEventStatusRecording) && !event->locked_isReady()) {
return ihipLogStatus(hipErrorNotReady);
} else {
return ihipLogStatus(hipSuccess);
Expand Down
26 changes: 23 additions & 3 deletions src/hip_hcc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -328,14 +328,34 @@ void ihipStream_t::locked_wait()

// Causes current stream to wait for specified event to complete:
// Note this does not provide any kind of host serialization.
void ihipStream_t::locked_waitEvent(hipEvent_t event)
void ihipStream_t::locked_streamWaitEvent(hipEvent_t event)
{
LockedAccessor_StreamCrit_t crit(_criticalData);


crit->_av.create_blocking_marker(event->_marker, hc::accelerator_scope);
crit->_av.create_blocking_marker(event->marker(), hc::accelerator_scope);
}


// Causes current stream to wait for specified event to complete:
// Note this does not provide any kind of host serialization.
bool ihipStream_t::locked_eventIsReady(hipEvent_t event)
{
// Event query that returns "Complete" may cause HCC to manipulate
// internal queue state so lock the stream's queue here.
LockedAccessor_StreamCrit_t crit(_criticalData);

return (event->marker().is_ready());
}

void ihipStream_t::locked_eventWaitComplete(hipEvent_t event, hc::hcWaitMode waitMode)
{
LockedAccessor_StreamCrit_t crit(_criticalData);

event->marker().wait(waitMode);
}


// Create a marker in this stream.
// Save state in the event so it can track the status of the event.
void ihipStream_t::locked_recordEvent(hipEvent_t event)
Expand All @@ -354,7 +374,7 @@ void ihipStream_t::locked_recordEvent(hipEvent_t event)
scopeFlag = HIP_EVENT_SYS_RELEASE ? hc::system_scope : hc::accelerator_scope;
}

event->_marker = crit->_av.create_marker(scopeFlag);
event->marker(crit->_av.create_marker(scopeFlag));
};

//=============================================================================
Expand Down
15 changes: 12 additions & 3 deletions src/hip_hcc_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -517,9 +517,12 @@ class ihipStream_t {

hc::accelerator_view* locked_getAv() { LockedAccessor_StreamCrit_t crit(_criticalData); return &(crit->_av); };

void locked_waitEvent(hipEvent_t event);
void locked_streamWaitEvent(hipEvent_t event);
void locked_recordEvent(hipEvent_t event);

bool locked_eventIsReady(hipEvent_t event);
void locked_eventWaitComplete(hipEvent_t event, hc::hcWaitMode waitMode);

ihipStreamCritical_t &criticalData() { return _criticalData; };

//---
Expand Down Expand Up @@ -608,18 +611,24 @@ class ihipEvent_t {
ihipEvent_t(unsigned flags);
void attachToCompletionFuture(const hc::completion_future *cf, hipStream_t stream, ihipEventType_t eventType);
void refereshEventStatus();
hc::completion_future & marker() { return _marker; }
void marker(hc::completion_future cf) { _marker = cf; };

bool locked_isReady();
void locked_waitComplete(hc::hcWaitMode waitMode);

uint64_t timestamp() const { return _timestamp; } ;
ihipEventType_t type() const { return _type; };

public:
hipEventStatus_t _state;

hipStream_t _stream; // Stream where the event is recorded, or NULL if all streams.
hipStream_t _stream; // Stream where the event is recorded. Null stream is resolved to actual stream when recorded
unsigned _flags;

hc::completion_future _marker;

private:
hc::completion_future _marker;
ihipEventType_t _type;
uint64_t _timestamp; // store timestamp, may be set on host or by marker.
friend hipError_t hipEventRecord(hipEvent_t event, hipStream_t stream);
Expand Down
2 changes: 1 addition & 1 deletion src/hip_stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ hipError_t hipStreamWaitEvent(hipStream_t stream, hipEvent_t event, unsigned int
if (stream != hipStreamNull) {

// This will user create_blocking_marker to wait on the specified queue.
stream->locked_waitEvent(event);
stream->locked_streamWaitEvent(event);

} else {
// TODO-hcc Convert to use create_blocking_marker(...) functionality.
Expand Down

0 comments on commit cc08b36

Please sign in to comment.