2016-03-24 07:04:01 -05:00
|
|
|
/*
|
2017-03-31 12:11:34 -05:00
|
|
|
Copyright (c) 2015 - present Advanced Micro Devices, Inc. All rights reserved.
|
2016-10-15 22:55:22 +05:30
|
|
|
|
2016-03-24 07:04:01 -05:00
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
furnished to do so, subject to the following conditions:
|
2016-10-15 22:55:22 +05:30
|
|
|
|
2016-03-24 07:04:01 -05:00
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
|
all copies or substantial portions of the Software.
|
2016-10-15 22:55:22 +05:30
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
2016-03-24 07:04:01 -05:00
|
|
|
THE SOFTWARE.
|
|
|
|
|
*/
|
|
|
|
|
|
2016-10-04 22:17:18 +05:30
|
|
|
#include "hip/hip_runtime.h"
|
2017-03-31 12:11:34 -05:00
|
|
|
#include "hip_hcc_internal.h"
|
2016-10-15 20:25:20 -05:00
|
|
|
#include "trace_helper.h"
|
2016-03-24 09:28:46 -05:00
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------------------------------
|
|
|
|
|
//-------------------------------------------------------------------------------------------------
|
|
|
|
|
// Events
|
|
|
|
|
//---
|
|
|
|
|
|
2016-03-26 12:35:04 -05:00
|
|
|
|
2018-03-12 11:29:03 +05:30
|
|
|
ihipEvent_t::ihipEvent_t(unsigned flags) : _criticalData(this) { _flags = flags; };
|
2017-04-06 23:55:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
// Attach to an existing completion future:
|
2018-03-12 11:29:03 +05:30
|
|
|
void ihipEvent_t::attachToCompletionFuture(const hc::completion_future* cf, hipStream_t stream,
|
|
|
|
|
ihipEventType_t eventType) {
|
2017-11-06 23:49:25 +00:00
|
|
|
LockedAccessor_EventCrit_t crit(_criticalData);
|
|
|
|
|
crit->_eventData.marker(*cf);
|
2018-03-12 11:29:03 +05:30
|
|
|
crit->_eventData._type = eventType;
|
2017-11-06 23:49:25 +00:00
|
|
|
crit->_eventData._stream = stream;
|
2018-03-12 11:29:03 +05:30
|
|
|
crit->_eventData._state = hipEventStatusRecording;
|
2017-04-06 23:55:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2018-03-12 11:29:03 +05:30
|
|
|
std::pair<hipEventStatus_t, uint64_t> ihipEvent_t::refreshEventStatus() {
|
2017-11-06 23:49:25 +00:00
|
|
|
auto ecd = locked_copyCrit();
|
|
|
|
|
if (ecd._state == hipEventStatusRecording) {
|
|
|
|
|
bool isReady1 = ecd._stream->locked_eventIsReady(this);
|
|
|
|
|
if (isReady1) {
|
|
|
|
|
LockedAccessor_EventCrit_t eCrit(_criticalData);
|
|
|
|
|
|
2018-03-12 11:29:03 +05:30
|
|
|
if ((eCrit->_eventData._type == hipEventTypeIndependent) ||
|
2017-11-06 23:49:25 +00:00
|
|
|
(eCrit->_eventData._type == hipEventTypeStopCommand)) {
|
2018-03-12 11:29:03 +05:30
|
|
|
eCrit->_eventData._timestamp = eCrit->_eventData.marker().get_end_tick();
|
2017-11-06 23:49:25 +00:00
|
|
|
} else if (eCrit->_eventData._type == hipEventTypeStartCommand) {
|
2018-03-12 11:29:03 +05:30
|
|
|
eCrit->_eventData._timestamp = eCrit->_eventData.marker().get_begin_tick();
|
2017-11-06 23:49:25 +00:00
|
|
|
} else {
|
2018-03-12 11:29:03 +05:30
|
|
|
eCrit->_eventData._timestamp = 0;
|
|
|
|
|
assert(0); // TODO - move to debug assert
|
2017-04-06 23:55:15 +00:00
|
|
|
}
|
2017-05-25 10:37:03 -05:00
|
|
|
|
2017-11-06 23:49:25 +00:00
|
|
|
eCrit->_eventData._state = hipEventStatusComplete;
|
2017-04-06 23:55:15 +00:00
|
|
|
|
2018-03-12 11:29:03 +05:30
|
|
|
return std::pair<hipEventStatus_t, uint64_t>(eCrit->_eventData._state,
|
|
|
|
|
eCrit->_eventData._timestamp);
|
2017-11-06 23:49:25 +00:00
|
|
|
}
|
2018-03-12 11:29:03 +05:30
|
|
|
}
|
2017-04-06 23:55:15 +00:00
|
|
|
|
2017-11-06 23:49:25 +00:00
|
|
|
// Not complete path here:
|
2018-03-12 11:29:03 +05:30
|
|
|
return std::pair<hipEventStatus_t, uint64_t>(ecd._state, ecd._timestamp);
|
2017-08-26 14:39:14 +00:00
|
|
|
}
|
|
|
|
|
|
2017-11-06 23:49:25 +00:00
|
|
|
|
2018-03-12 11:29:03 +05:30
|
|
|
hipError_t ihipEventCreate(hipEvent_t* event, unsigned flags) {
|
2016-03-24 09:28:46 -05:00
|
|
|
hipError_t e = hipSuccess;
|
|
|
|
|
|
2016-10-15 21:15:31 -05:00
|
|
|
// TODO-IPC - support hipEventInterprocess.
|
2018-03-12 11:29:03 +05:30
|
|
|
unsigned supportedFlags = hipEventDefault | hipEventBlockingSync | hipEventDisableTiming |
|
|
|
|
|
hipEventReleaseToDevice | hipEventReleaseToSystem;
|
2017-05-27 16:01:23 -05:00
|
|
|
const unsigned releaseFlags = (hipEventReleaseToDevice | hipEventReleaseToSystem);
|
|
|
|
|
|
2018-03-12 11:29:03 +05:30
|
|
|
const bool illegalFlags =
|
|
|
|
|
(flags & ~supportedFlags) || // can't set any unsupported flags.
|
|
|
|
|
(flags & releaseFlags) == releaseFlags; // can't set both release flags
|
2017-05-27 16:01:23 -05:00
|
|
|
|
|
|
|
|
if (!illegalFlags) {
|
2017-06-05 00:41:18 -05:00
|
|
|
*event = new ihipEvent_t(flags);
|
2016-03-24 09:28:46 -05:00
|
|
|
} else {
|
|
|
|
|
e = hipErrorInvalidValue;
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-26 12:35:04 -05:00
|
|
|
return e;
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-12 11:29:03 +05:30
|
|
|
hipError_t hipEventCreateWithFlags(hipEvent_t* event, unsigned flags) {
|
2016-03-26 12:35:04 -05:00
|
|
|
HIP_INIT_API(event, flags);
|
|
|
|
|
|
|
|
|
|
return ihipLogStatus(ihipEventCreate(event, flags));
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-12 11:29:03 +05:30
|
|
|
hipError_t hipEventCreate(hipEvent_t* event) {
|
2016-03-26 12:35:04 -05:00
|
|
|
HIP_INIT_API(event);
|
2016-03-24 09:28:46 -05:00
|
|
|
|
2016-03-26 12:35:04 -05:00
|
|
|
return ihipLogStatus(ihipEventCreate(event, 0));
|
2016-03-24 09:28:46 -05:00
|
|
|
}
|
|
|
|
|
|
2017-11-06 23:49:25 +00:00
|
|
|
|
2018-03-12 11:29:03 +05:30
|
|
|
hipError_t hipEventRecord(hipEvent_t event, hipStream_t stream) {
|
2017-08-26 14:39:14 +00:00
|
|
|
HIP_INIT_SPECIAL_API(TRACE_SYNC, event, stream);
|
2016-03-24 09:28:46 -05:00
|
|
|
|
2017-11-06 23:49:25 +00:00
|
|
|
auto ecd = event->locked_copyCrit();
|
2017-05-12 17:04:23 -05:00
|
|
|
|
2018-03-12 11:29:03 +05:30
|
|
|
if (event && ecd._state != hipEventStatusUnitialized) {
|
2017-11-06 23:49:25 +00:00
|
|
|
stream = ihipSyncAndResolveStream(stream);
|
2016-03-24 09:28:46 -05:00
|
|
|
|
2017-05-25 10:37:03 -05:00
|
|
|
if (HIP_SYNC_NULL_STREAM && stream->isDefaultStream()) {
|
2017-05-12 17:04:23 -05:00
|
|
|
// TODO-HIP_SYNC_NULL_STREAM : can remove this code when HIP_SYNC_NULL_STREAM = 0
|
2017-11-06 23:49:25 +00:00
|
|
|
//
|
2017-05-25 10:37:03 -05:00
|
|
|
// If default stream , then wait on all queues.
|
2018-03-12 11:29:03 +05:30
|
|
|
ihipCtx_t* ctx = ihipGetTlsDefaultCtx();
|
2017-05-12 17:04:23 -05:00
|
|
|
ctx->locked_syncDefaultStream(true, true);
|
2016-03-24 09:28:46 -05:00
|
|
|
|
2017-11-06 23:49:25 +00:00
|
|
|
{
|
|
|
|
|
LockedAccessor_EventCrit_t eCrit(event->criticalData());
|
2018-03-12 11:29:03 +05:30
|
|
|
eCrit->_eventData.marker(hc::completion_future()); // reset event
|
2017-11-06 23:49:25 +00:00
|
|
|
eCrit->_eventData._stream = stream;
|
|
|
|
|
eCrit->_eventData._timestamp = hc::get_system_ticks();
|
|
|
|
|
eCrit->_eventData._state = hipEventStatusComplete;
|
|
|
|
|
}
|
2016-03-24 09:28:46 -05:00
|
|
|
return ihipLogStatus(hipSuccess);
|
|
|
|
|
} else {
|
2016-08-30 17:29:50 -05:00
|
|
|
// Record the event in the stream:
|
2018-03-12 11:29:03 +05:30
|
|
|
// Keep a copy outside the critical section so we lock stream first, then event - to
|
|
|
|
|
// avoid deadlock
|
2017-11-06 23:49:25 +00:00
|
|
|
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;
|
2018-03-12 11:29:03 +05:30
|
|
|
eCrit->_eventData._state = hipEventStatusRecording;
|
2017-11-06 23:49:25 +00:00
|
|
|
}
|
|
|
|
|
|
2016-03-24 09:28:46 -05:00
|
|
|
return ihipLogStatus(hipSuccess);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
return ihipLogStatus(hipErrorInvalidResourceHandle);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-06 23:49:25 +00:00
|
|
|
|
2018-03-12 11:29:03 +05:30
|
|
|
hipError_t hipEventDestroy(hipEvent_t event) {
|
2016-07-22 15:46:55 +05:30
|
|
|
HIP_INIT_API(event);
|
2016-03-24 09:28:46 -05:00
|
|
|
|
2017-07-18 11:01:02 +05:30
|
|
|
if (event) {
|
|
|
|
|
delete event;
|
2016-03-24 09:28:46 -05:00
|
|
|
|
2017-07-18 11:01:02 +05:30
|
|
|
return ihipLogStatus(hipSuccess);
|
|
|
|
|
} else {
|
|
|
|
|
return ihipLogStatus(hipErrorInvalidResourceHandle);
|
|
|
|
|
}
|
2016-03-24 09:28:46 -05:00
|
|
|
}
|
|
|
|
|
|
2018-03-12 11:29:03 +05:30
|
|
|
hipError_t hipEventSynchronize(hipEvent_t event) {
|
2017-08-16 03:50:04 +00:00
|
|
|
HIP_INIT_SPECIAL_API(TRACE_SYNC, event);
|
2016-03-24 09:28:46 -05:00
|
|
|
|
2017-10-31 22:26:26 +00:00
|
|
|
if (!(event->_flags & hipEventReleaseToSystem)) {
|
2018-03-12 11:29:03 +05:30
|
|
|
tprintf(DB_WARN,
|
|
|
|
|
"hipEventSynchronize on event without system-scope fence ; consider creating with "
|
|
|
|
|
"hipEventReleaseToSystem\n");
|
2017-10-31 22:26:26 +00:00
|
|
|
}
|
2017-11-06 23:49:25 +00:00
|
|
|
auto ecd = event->locked_copyCrit();
|
2017-10-31 22:26:26 +00:00
|
|
|
|
2016-09-01 13:59:55 -05:00
|
|
|
if (event) {
|
2017-11-06 23:49:25 +00:00
|
|
|
if (ecd._state == hipEventStatusUnitialized) {
|
2016-03-24 09:28:46 -05:00
|
|
|
return ihipLogStatus(hipErrorInvalidResourceHandle);
|
2018-03-12 11:29:03 +05:30
|
|
|
} else if (ecd._state == hipEventStatusCreated) {
|
2016-03-24 09:28:46 -05:00
|
|
|
// Created but not actually recorded on any device:
|
|
|
|
|
return ihipLogStatus(hipSuccess);
|
2018-03-12 11:29:03 +05:30
|
|
|
} else if (HIP_SYNC_NULL_STREAM && (ecd._stream->isDefaultStream())) {
|
|
|
|
|
auto* ctx = ihipGetTlsDefaultCtx();
|
2017-05-12 17:04:23 -05:00
|
|
|
// TODO-HIP_SYNC_NULL_STREAM - can remove this code
|
|
|
|
|
ctx->locked_syncDefaultStream(true, true);
|
2016-03-24 09:28:46 -05:00
|
|
|
return ihipLogStatus(hipSuccess);
|
|
|
|
|
} else {
|
2017-11-06 23:49:25 +00:00
|
|
|
ecd._stream->locked_eventWaitComplete(
|
2018-03-12 11:29:03 +05:30
|
|
|
ecd.marker(), (event->_flags & hipEventBlockingSync) ? hc::hcWaitModeBlocked
|
|
|
|
|
: hc::hcWaitModeActive);
|
2017-05-25 10:37:03 -05:00
|
|
|
|
2016-03-24 09:28:46 -05:00
|
|
|
return ihipLogStatus(hipSuccess);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
return ihipLogStatus(hipErrorInvalidResourceHandle);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-12 11:29:03 +05:30
|
|
|
hipError_t hipEventElapsedTime(float* ms, hipEvent_t start, hipEvent_t stop) {
|
2016-07-22 15:46:55 +05:30
|
|
|
HIP_INIT_API(ms, start, stop);
|
2016-03-24 09:28:46 -05:00
|
|
|
|
|
|
|
|
hipError_t status = hipSuccess;
|
2017-06-05 00:41:18 -05:00
|
|
|
|
2016-03-24 09:28:46 -05:00
|
|
|
*ms = 0.0f;
|
2017-11-06 23:42:35 +00:00
|
|
|
|
|
|
|
|
if ((start == nullptr) || (stop == nullptr)) {
|
|
|
|
|
status = hipErrorInvalidResourceHandle;
|
|
|
|
|
} else {
|
|
|
|
|
auto startEcd = start->locked_copyCrit();
|
2018-03-12 11:29:03 +05:30
|
|
|
auto stopEcd = stop->locked_copyCrit();
|
2016-03-24 09:28:46 -05:00
|
|
|
|
2017-11-06 23:42:35 +00:00
|
|
|
if ((start->_flags & hipEventDisableTiming) ||
|
2018-03-12 11:29:03 +05:30
|
|
|
(startEcd._state == hipEventStatusUnitialized) ||
|
|
|
|
|
(startEcd._state == hipEventStatusCreated) || (stop->_flags & hipEventDisableTiming) ||
|
|
|
|
|
(stopEcd._state == hipEventStatusUnitialized) ||
|
|
|
|
|
(stopEcd._state == hipEventStatusCreated)) {
|
2017-11-06 23:42:35 +00:00
|
|
|
// Both events must be at least recorded else return hipErrorInvalidResourceHandle
|
2017-06-05 00:41:18 -05:00
|
|
|
|
2017-11-06 23:42:35 +00:00
|
|
|
status = hipErrorInvalidResourceHandle;
|
2017-06-05 00:41:18 -05:00
|
|
|
|
2017-11-06 23:42:35 +00:00
|
|
|
} else {
|
|
|
|
|
// Refresh status, if still recording...
|
2017-06-05 00:41:18 -05:00
|
|
|
|
2017-11-06 23:42:35 +00:00
|
|
|
auto startStatus = start->refreshEventStatus(); // pair < state, timestamp >
|
2018-03-12 11:29:03 +05:30
|
|
|
auto stopStatus = stop->refreshEventStatus(); // pair < state, timestamp >
|
2017-11-06 23:49:25 +00:00
|
|
|
|
2018-03-12 11:29:03 +05:30
|
|
|
if ((startStatus.first == hipEventStatusComplete) &&
|
|
|
|
|
(stopStatus.first == hipEventStatusComplete)) {
|
|
|
|
|
// Common case, we have good information for both events. 'second" is the
|
|
|
|
|
// timestamp:
|
2017-11-06 23:42:35 +00:00
|
|
|
int64_t tickDiff = (stopStatus.second - startStatus.second);
|
2017-11-06 23:49:25 +00:00
|
|
|
|
2017-11-06 23:42:35 +00:00
|
|
|
uint64_t freqHz;
|
|
|
|
|
hsa_system_get_info(HSA_SYSTEM_INFO_TIMESTAMP_FREQUENCY, &freqHz);
|
|
|
|
|
if (freqHz) {
|
2018-03-12 11:29:03 +05:30
|
|
|
*ms = ((double)(tickDiff) / (double)(freqHz)) * 1000.0f;
|
2017-11-06 23:42:35 +00:00
|
|
|
status = hipSuccess;
|
|
|
|
|
} else {
|
2018-03-12 11:29:03 +05:30
|
|
|
*ms = 0.0f;
|
|
|
|
|
status = hipErrorInvalidValue;
|
|
|
|
|
}
|
2016-03-24 09:28:46 -05:00
|
|
|
|
|
|
|
|
|
2017-11-06 23:42:35 +00:00
|
|
|
} else if ((startStatus.first == hipEventStatusRecording) ||
|
2018-03-12 11:29:03 +05:30
|
|
|
(stopStatus.first == hipEventStatusRecording)) {
|
2017-11-06 23:42:35 +00:00
|
|
|
status = hipErrorNotReady;
|
|
|
|
|
} else {
|
|
|
|
|
assert(0);
|
|
|
|
|
}
|
2016-03-24 09:28:46 -05:00
|
|
|
}
|
2018-03-12 11:29:03 +05:30
|
|
|
}
|
2016-03-24 09:28:46 -05:00
|
|
|
|
|
|
|
|
return ihipLogStatus(status);
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-12 11:29:03 +05:30
|
|
|
hipError_t hipEventQuery(hipEvent_t event) {
|
2017-08-16 03:50:04 +00:00
|
|
|
HIP_INIT_SPECIAL_API(TRACE_QUERY, event);
|
2016-03-24 09:28:46 -05:00
|
|
|
|
2017-10-31 22:26:26 +00:00
|
|
|
if (!(event->_flags & hipEventReleaseToSystem)) {
|
2018-03-12 11:29:03 +05:30
|
|
|
tprintf(DB_WARN,
|
|
|
|
|
"hipEventQuery on event without system-scope fence ; consider creating with "
|
|
|
|
|
"hipEventReleaseToSystem\n");
|
2017-10-31 22:26:26 +00:00
|
|
|
}
|
|
|
|
|
|
2017-11-06 23:49:25 +00:00
|
|
|
auto ecd = event->locked_copyCrit();
|
|
|
|
|
|
|
|
|
|
if ((ecd._state == hipEventStatusRecording) && !ecd._stream->locked_eventIsReady(event)) {
|
2016-03-24 09:28:46 -05:00
|
|
|
return ihipLogStatus(hipErrorNotReady);
|
|
|
|
|
} else {
|
|
|
|
|
return ihipLogStatus(hipSuccess);
|
|
|
|
|
}
|
|
|
|
|
}
|