Skip to content

Commit

Permalink
Move base event into device
Browse files Browse the repository at this point in the history
  • Loading branch information
hdelan committed Sep 25, 2023
1 parent 0de2831 commit c2a3cf7
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 42 deletions.
12 changes: 12 additions & 0 deletions sycl/plugins/unified_runtime/ur/adapters/hip/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ int getAttribute(ur_device_handle_t Device, hipDeviceAttribute_t Attribute) {
return Value;
}

uint64_t ur_device_handle_t_::getElapsedTime(hipEvent_t Event) const {
float MilliSeconds = 0.0f;

// hipEventSynchronize waits till the event is ready for call to
// hipEventElapsedTime.
UR_CHECK_ERROR(hipEventSynchronize(EvBase));
UR_CHECK_ERROR(hipEventSynchronize(Event));
UR_CHECK_ERROR(hipEventElapsedTime(&MilliSeconds, EvBase, Event));

return static_cast<uint64_t>(MilliSeconds * 1.0e6);
}

UR_APIEXPORT ur_result_t UR_APICALL urDeviceGetInfo(ur_device_handle_t hDevice,
ur_device_info_t propName,
size_t propSize,
Expand Down
10 changes: 7 additions & 3 deletions sycl/plugins/unified_runtime/ur/adapters/hip/device.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ struct ur_device_handle_t_ {
std::atomic_uint32_t RefCount;
ur_platform_handle_t Platform;
hipCtx_t HIPContext;
hipEvent_t EvBase;
size_t DeviceIndex; // The index of the device in the UR context

public:
ur_device_handle_t_(native_type HipDevice, hipCtx_t Context,
ur_platform_handle_t Platform, size_t DeviceIndex)
ur_platform_handle_t Platform, size_t DeviceIndex,
hipEvent_t EvBase)
: HIPDevice(HipDevice), RefCount{1}, Platform(Platform),
HIPContext(Context), DeviceIndex(DeviceIndex) {}
HIPContext(Context), EvBase(EvBase), DeviceIndex(DeviceIndex) {}

~ur_device_handle_t_() {
UR_CHECK_ERROR(hipDevicePrimaryCtxRelease(HIPDevice));
Expand All @@ -41,7 +43,9 @@ struct ur_device_handle_t_ {

ur_platform_handle_t getPlatform() const noexcept { return Platform; };

hipCtx_t getNativeContext() { return HIPContext; };
uint64_t getElapsedTime(hipEvent_t) const;

hipCtx_t getNativeContext() const { return HIPContext; };

// Returns the index of the device in question relative to the other devices
// in the platform
Expand Down
40 changes: 4 additions & 36 deletions sycl/plugins/unified_runtime/ur/adapters/hip/event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,44 +88,13 @@ bool ur_event_handle_t_::isCompleted() const noexcept {
}

uint64_t ur_event_handle_t_::getQueuedTime() const {
float MilliSeconds = 0.0f;
assert(isStarted());

// hipEventSynchronize waits till the event is ready for call to
// hipEventElapsedTime.
UR_CHECK_ERROR(hipEventSynchronize(EvStart));
UR_CHECK_ERROR(hipEventSynchronize(EvEnd));

UR_CHECK_ERROR(hipEventElapsedTime(&MilliSeconds, EvStart, EvEnd));
return static_cast<uint64_t>(MilliSeconds * 1.0e6);
}

uint64_t ur_event_handle_t_::getStartTime() const {
float MiliSeconds = 0.0f;
assert(isStarted());

// hipEventSynchronize waits till the event is ready for call to
// hipEventElapsedTime.
UR_CHECK_ERROR(hipEventSynchronize(ur_platform_handle_t_::EvBase));
UR_CHECK_ERROR(hipEventSynchronize(EvStart));

UR_CHECK_ERROR(hipEventElapsedTime(&MiliSeconds,
ur_platform_handle_t_::EvBase, EvStart));
return static_cast<uint64_t>(MiliSeconds * 1.0e6);
return Queue->getDevice()->getElapsedTime(EvStart);
}

uint64_t ur_event_handle_t_::getEndTime() const {
float MiliSeconds = 0.0f;
assert(isStarted() && isRecorded());

// hipEventSynchronize waits till the event is ready for call to
// hipEventElapsedTime.
UR_CHECK_ERROR(hipEventSynchronize(ur_platform_handle_t_::EvBase));
UR_CHECK_ERROR(hipEventSynchronize(EvEnd));

UR_CHECK_ERROR(
hipEventElapsedTime(&MiliSeconds, ur_platform_handle_t_::EvBase, EvEnd));
return static_cast<uint64_t>(MiliSeconds * 1.0e6);
return Queue->getDevice()->getElapsedTime(EvEnd);
}

ur_result_t ur_event_handle_t_::record() {
Expand Down Expand Up @@ -249,11 +218,10 @@ UR_APIEXPORT ur_result_t UR_APICALL urEventGetProfilingInfo(
UrReturnHelper ReturnValue(propValueSize, pPropValue, pPropValueSizeRet);
switch (propName) {
case UR_PROFILING_INFO_COMMAND_QUEUED:
case UR_PROFILING_INFO_COMMAND_SUBMIT:
// Note: No user for this case
return ReturnValue(static_cast<uint64_t>(hEvent->getQueuedTime()));
case UR_PROFILING_INFO_COMMAND_SUBMIT:
case UR_PROFILING_INFO_COMMAND_START:
return ReturnValue(static_cast<uint64_t>(hEvent->getStartTime()));
return ReturnValue(static_cast<uint64_t>(hEvent->getQueuedTime()));
case UR_PROFILING_INFO_COMMAND_END:
return ReturnValue(static_cast<uint64_t>(hEvent->getEndTime()));
default:
Expand Down
11 changes: 8 additions & 3 deletions sycl/plugins/unified_runtime/ur/adapters/hip/platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,17 @@ urPlatformGet(ur_adapter_handle_t *, uint32_t, uint32_t NumEntries,
try {
for (int i = 0; i < NumDevices; ++i) {
hipDevice_t Device;
Err = UR_CHECK_ERROR(hipDeviceGet(&Device, i));
UR_CHECK_ERROR(hipDeviceGet(&Device, i));
hipCtx_t Context;
Err = UR_CHECK_ERROR(hipDevicePrimaryCtxRetain(&Context, Device));
UR_CHECK_ERROR(hipDevicePrimaryCtxRetain(&Context, Device));
UR_CHECK_ERROR(hipCtxSetCurrent(Context));
hipEvent_t EvBase;
UR_CHECK_ERROR(hipEventCreateWithFlags(&EvBase, hipEventDefault));
UR_CHECK_ERROR(hipEventRecord(EvBase, 0));

Platform.Devices.emplace_back(
std::make_unique<ur_device_handle_t_>(Device, Context,
&Platform, i));
&Platform, i, EvBase));
}
} catch (const std::bad_alloc &) {
Platform.Devices.clear();
Expand Down

0 comments on commit c2a3cf7

Please sign in to comment.