Skip to content

Commit e5827d4

Browse files
author
Konrad Kusiak
committed
Moved hip EvBase to be owned by device
1 parent 4d16a65 commit e5827d4

File tree

6 files changed

+28
-59
lines changed

6 files changed

+28
-59
lines changed

source/adapters/hip/context.cpp

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,6 @@ UR_APIEXPORT ur_result_t UR_APICALL urContextCreate(
4747
// Create a scoped context.
4848
ContextPtr = std::unique_ptr<ur_context_handle_t_>(
4949
new ur_context_handle_t_{phDevices, DeviceCount});
50-
51-
static std::once_flag InitFlag;
52-
std::call_once(
53-
InitFlag,
54-
[](ur_result_t &) {
55-
// Use default stream to record base event counter
56-
UR_CHECK_ERROR(hipEventCreateWithFlags(&ur_platform_handle_t_::EvBase,
57-
hipEventDefault));
58-
UR_CHECK_ERROR(hipEventRecord(ur_platform_handle_t_::EvBase, 0));
59-
},
60-
RetErr);
61-
6250
*phContext = ContextPtr.release();
6351
} catch (ur_result_t Err) {
6452
RetErr = Err;

source/adapters/hip/device.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,18 @@ int getAttribute(ur_device_handle_t Device, hipDeviceAttribute_t Attribute) {
2020
return Value;
2121
}
2222

23+
uint64_t ur_device_handle_t_::getElapsedTime(hipEvent_t ev) const {
24+
float Milliseconds = 0.0f;
25+
26+
// hipEventSynchronize waits till the event is ready for call to
27+
// hipEventElapsedTime.
28+
UR_CHECK_ERROR(hipEventSynchronize(EvBase));
29+
UR_CHECK_ERROR(hipEventSynchronize(ev));
30+
UR_CHECK_ERROR(hipEventElapsedTime(&Milliseconds, EvBase, ev));
31+
32+
return static_cast<uint64_t>(Milliseconds * 1.0e6);
33+
}
34+
2335
UR_APIEXPORT ur_result_t UR_APICALL urDeviceGetInfo(ur_device_handle_t hDevice,
2436
ur_device_info_t propName,
2537
size_t propSize,
@@ -995,11 +1007,7 @@ ur_result_t UR_APICALL urDeviceGetGlobalTimestamps(ur_device_handle_t hDevice,
9951007
if (pDeviceTimestamp) {
9961008
UR_CHECK_ERROR(hipEventCreateWithFlags(&Event, hipEventDefault));
9971009
UR_CHECK_ERROR(hipEventRecord(Event));
998-
UR_CHECK_ERROR(hipEventSynchronize(Event));
999-
float ElapsedTime = 0.0f;
1000-
UR_CHECK_ERROR(hipEventElapsedTime(&ElapsedTime,
1001-
ur_platform_handle_t_::EvBase, Event));
1002-
*pDeviceTimestamp = (uint64_t)(ElapsedTime * (double)1e6);
1010+
*pDeviceTimestamp = hDevice->getElapsedTime(Event);
10031011
}
10041012

10051013
if (pHostTimestamp) {

source/adapters/hip/device.hpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ struct ur_device_handle_t_ {
2525
std::atomic_uint32_t RefCount;
2626
ur_platform_handle_t Platform;
2727
hipCtx_t HIPContext;
28+
hipEvent_t EvBase; // HIP event used as base counter
2829
uint32_t DeviceIndex;
30+
2931
int MaxWorkGroupSize{0};
3032
int MaxBlockDimX{0};
3133
int MaxBlockDimY{0};
@@ -36,9 +38,10 @@ struct ur_device_handle_t_ {
3638

3739
public:
3840
ur_device_handle_t_(native_type HipDevice, hipCtx_t Context,
39-
ur_platform_handle_t Platform, uint32_t DeviceIndex)
41+
hipEvent_t EvBase, ur_platform_handle_t Platform,
42+
uint32_t DeviceIndex)
4043
: HIPDevice(HipDevice), RefCount{1}, Platform(Platform),
41-
HIPContext(Context), DeviceIndex(DeviceIndex) {
44+
HIPContext(Context), EvBase(EvBase), DeviceIndex(DeviceIndex) {
4245

4346
UR_CHECK_ERROR(hipDeviceGetAttribute(
4447
&MaxWorkGroupSize, hipDeviceAttributeMaxThreadsPerBlock, HIPDevice));
@@ -68,6 +71,8 @@ struct ur_device_handle_t_ {
6871

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

74+
uint64_t getElapsedTime(hipEvent_t) const;
75+
7176
hipCtx_t getNativeContext() const noexcept { return HIPContext; };
7277

7378
// Returns the index of the device relative to the other devices in the same

source/adapters/hip/event.cpp

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -90,44 +90,18 @@ bool ur_event_handle_t_::isCompleted() const {
9090
}
9191

9292
uint64_t ur_event_handle_t_::getQueuedTime() const {
93-
float MilliSeconds = 0.0f;
9493
assert(isStarted());
95-
96-
// hipEventSynchronize waits till the event is ready for call to
97-
// hipEventElapsedTime.
98-
UR_CHECK_ERROR(hipEventSynchronize(EvStart));
99-
UR_CHECK_ERROR(hipEventSynchronize(EvEnd));
100-
101-
UR_CHECK_ERROR(hipEventElapsedTime(&MilliSeconds, EvStart, EvEnd));
102-
return static_cast<uint64_t>(MilliSeconds * 1.0e6);
94+
return Queue->getDevice()->getElapsedTime(EvQueued);
10395
}
10496

10597
uint64_t ur_event_handle_t_::getStartTime() const {
106-
float MiliSeconds = 0.0f;
10798
assert(isStarted());
108-
109-
// hipEventSynchronize waits till the event is ready for call to
110-
// hipEventElapsedTime.
111-
UR_CHECK_ERROR(hipEventSynchronize(ur_platform_handle_t_::EvBase));
112-
UR_CHECK_ERROR(hipEventSynchronize(EvStart));
113-
114-
UR_CHECK_ERROR(hipEventElapsedTime(&MiliSeconds,
115-
ur_platform_handle_t_::EvBase, EvStart));
116-
return static_cast<uint64_t>(MiliSeconds * 1.0e6);
99+
return Queue->getDevice()->getElapsedTime(EvStart);
117100
}
118101

119102
uint64_t ur_event_handle_t_::getEndTime() const {
120-
float MiliSeconds = 0.0f;
121103
assert(isStarted() && isRecorded());
122-
123-
// hipEventSynchronize waits till the event is ready for call to
124-
// hipEventElapsedTime.
125-
UR_CHECK_ERROR(hipEventSynchronize(ur_platform_handle_t_::EvBase));
126-
UR_CHECK_ERROR(hipEventSynchronize(EvEnd));
127-
128-
UR_CHECK_ERROR(
129-
hipEventElapsedTime(&MiliSeconds, ur_platform_handle_t_::EvBase, EvEnd));
130-
return static_cast<uint64_t>(MiliSeconds * 1.0e6);
104+
return Queue->getDevice()->getElapsedTime(EvEnd);
131105
}
132106

133107
ur_result_t ur_event_handle_t_::record() {

source/adapters/hip/platform.cpp

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
#include "platform.hpp"
1212
#include "context.hpp"
1313

14-
hipEvent_t ur_platform_handle_t_::EvBase{nullptr};
15-
1614
UR_APIEXPORT ur_result_t UR_APICALL
1715
urPlatformGetInfo(ur_platform_handle_t, ur_platform_info_t propName,
1816
size_t propSize, void *pPropValue, size_t *pSizeRet) {
@@ -81,18 +79,15 @@ urPlatformGet(ur_adapter_handle_t *, uint32_t, uint32_t NumEntries,
8179
UR_CHECK_ERROR(hipDeviceGet(&Device, i));
8280
hipCtx_t Context;
8381
UR_CHECK_ERROR(hipDevicePrimaryCtxRetain(&Context, Device));
84-
Platform.Devices.emplace_back(
85-
new ur_device_handle_t_{Device, Context, &Platform, i});
86-
}
87-
88-
// Setup EvBase
89-
{
90-
ScopedContext Active(Platform.Devices.front().get());
9182
hipEvent_t EvBase;
9283
UR_CHECK_ERROR(hipEventCreate(&EvBase));
84+
85+
// Use the default stream to record base event counter
9386
UR_CHECK_ERROR(hipEventRecord(EvBase, 0));
87+
Platform.Devices.emplace_back(new ur_device_handle_t_{
88+
Device, Context, EvBase, &Platform, i});
9489

95-
ur_platform_handle_t_::EvBase = EvBase;
90+
ScopedContext Active(Platform.Devices.front().get());
9691
}
9792
} catch (const std::bad_alloc &) {
9893
// Signal out-of-memory situation

source/adapters/hip/platform.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,5 @@
2020
/// when devices are used.
2121
///
2222
struct ur_platform_handle_t_ {
23-
static hipEvent_t EvBase; // HIP event used as base counter
2423
std::vector<std::unique_ptr<ur_device_handle_t_>> Devices;
2524
};

0 commit comments

Comments
 (0)