From 6fcff0118ea4cbcacf13183bf320d321a3839770 Mon Sep 17 00:00:00 2001 From: Jeff Daily Date: Mon, 4 Nov 2019 17:45:11 +0000 Subject: [PATCH] fix race condition in hipEventRecord --- src/hip_event.cpp | 65 +++++++++++++++-------------------------------- 1 file changed, 21 insertions(+), 44 deletions(-) diff --git a/src/hip_event.cpp b/src/hip_event.cpp index 73e296dae3..b74efeb4f1 100644 --- a/src/hip_event.cpp +++ b/src/hip_event.cpp @@ -109,51 +109,28 @@ hipError_t hipEventCreate(hipEvent_t* event) { hipError_t hipEventRecord(hipEvent_t event, hipStream_t stream) { HIP_INIT_SPECIAL_API(hipEventRecord, TRACE_SYNC, event, stream); - - hipError_t status; - if (event){ - auto ecd = event->locked_copyCrit(); - if( ecd._state != hipEventStatusUnitialized) { - stream = ihipSyncAndResolveStream(stream); - - if (HIP_SYNC_NULL_STREAM && stream->isDefaultStream()) { - // TODO-HIP_SYNC_NULL_STREAM : can remove this code when HIP_SYNC_NULL_STREAM = 0 - // - // If default stream , then wait on all queues. - ihipCtx_t* ctx = ihipGetTlsDefaultCtx(); - ctx->locked_syncDefaultStream(true, true); - - { - LockedAccessor_EventCrit_t eCrit(event->criticalData()); - eCrit->_eventData.marker(hc::completion_future()); // reset event - eCrit->_eventData._stream = stream; - eCrit->_eventData._timestamp = hc::get_system_ticks(); - eCrit->_eventData._state = hipEventStatusComplete; - } - status = hipSuccess; - } else { - // Record the event in the stream: - // Keep a copy outside the critical section so we lock stream first, then event - to - // avoid deadlock - hc::completion_future cf = stream->locked_recordEvent(event); - - { - LockedAccessor_EventCrit_t eCrit(event->criticalData()); - eCrit->_eventData.marker(cf); - eCrit->_eventData._stream = stream; - eCrit->_eventData._timestamp = 0; - eCrit->_eventData._state = hipEventStatusRecording; - } - - status = hipSuccess; - } - } else { - status = hipErrorInvalidResourceHandle; - } - } else { - status = hipErrorInvalidResourceHandle; + if (!event) return ihipLogStatus(hipErrorInvalidResourceHandle); + stream = ihipSyncAndResolveStream(stream); + LockedAccessor_EventCrit_t eCrit(event->criticalData()); + if (eCrit->_eventData._state == hipEventStatusUnitialized) return ihipLogStatus(hipErrorInvalidResourceHandle); + if (HIP_SYNC_NULL_STREAM && stream->isDefaultStream()) { + // TODO-HIP_SYNC_NULL_STREAM : can remove this code when HIP_SYNC_NULL_STREAM = 0 + // If default stream , then wait on all queues. + ihipCtx_t* ctx = ihipGetTlsDefaultCtx(); + ctx->locked_syncDefaultStream(true, true); + eCrit->_eventData.marker(hc::completion_future()); // reset event + eCrit->_eventData._stream = stream; + eCrit->_eventData._timestamp = hc::get_system_ticks(); + eCrit->_eventData._state = hipEventStatusComplete; } - return ihipLogStatus(status); + else { + // Record the event in the stream: + eCrit->_eventData.marker(stream->locked_recordEvent(event)); + eCrit->_eventData._stream = stream; + eCrit->_eventData._timestamp = 0; + eCrit->_eventData._state = hipEventStatusRecording; + } + return ihipLogStatus(hipSuccess); }