Skip to content

Commit 2062c1b

Browse files
committed
initial v2 event pool implementation
1 parent 7e38af7 commit 2062c1b

16 files changed

+848
-1
lines changed

source/adapters/level_zero/device.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <ze_api.h>
2424
#include <zes_api.h>
2525

26+
#include "adapters/level_zero/platform.hpp"
2627
#include "common.hpp"
2728

2829
enum EventsScope {
@@ -219,4 +220,7 @@ struct ur_device_handle_t_ : _ur_object {
219220
ZeCache<struct ze_global_memsize> ZeGlobalMemSize;
220221
ZeCache<ZeStruct<ze_mutable_command_list_exp_properties_t>>
221222
ZeDeviceMutableCmdListsProperties;
223+
224+
// unique ephemeral identifer of the device in the adapter
225+
DeviceId Id;
222226
};

source/adapters/level_zero/platform.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,9 +433,24 @@ ur_result_t ur_platform_handle_t_::populateDeviceCacheIfNeeded() {
433433
return UR_RESULT_ERROR_UNKNOWN;
434434
}
435435
DeviceCachePopulated = true;
436+
437+
size_t id = 0;
438+
for (auto &dev : URDevicesCache) {
439+
dev->Id = id++;
440+
}
441+
436442
return UR_RESULT_SUCCESS;
437443
}
438444

445+
ur_device_handle_t ur_platform_handle_t_::getDeviceById(DeviceId id) {
446+
for (auto &dev : URDevicesCache) {
447+
if (dev->Id == id) {
448+
return dev.get();
449+
}
450+
}
451+
return nullptr;
452+
}
453+
439454
// Returns plugin specific backend option.
440455
// Current support is only for optimization options.
441456
// Return '-ze-opt-disable' for frontend_option = -O0.

source/adapters/level_zero/platform.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,13 @@
1010
#pragma once
1111

1212
#include "common.hpp"
13+
#include "ur_api.h"
1314
#include "ze_api.h"
1415

1516
struct ur_device_handle_t_;
1617

18+
typedef size_t DeviceId;
19+
1720
struct ur_platform_handle_t_ : public _ur_platform {
1821
ur_platform_handle_t_(ze_driver_handle_t Driver)
1922
: ZeDriver{Driver}, ZeApiVersion{ZE_API_VERSION_CURRENT} {}
@@ -53,6 +56,8 @@ struct ur_platform_handle_t_ : public _ur_platform {
5356
// Check the device cache and load it if necessary.
5457
ur_result_t populateDeviceCacheIfNeeded();
5558

59+
ur_device_handle_t getDeviceById(DeviceId);
60+
5661
// Return the PI device from cache that represents given native device.
5762
// If not found, then nullptr is returned.
5863
ur_device_handle_t getDeviceFromNativeHandle(ze_device_handle_t);
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//===--------- event.cpp - Level Zero Adapter -----------------------------===//
2+
//
3+
// Copyright (C) 2024 Intel Corporation
4+
//
5+
// Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM
6+
// Exceptions. See LICENSE.TXT
7+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8+
//
9+
//===----------------------------------------------------------------------===//
10+
#include "event.hpp"
11+
#include "adapters/level_zero/v2/event_provider.hpp"
12+
#include "ze_api.h"
13+
14+
namespace v2 {
15+
void ur_event::attachZeHandle(event_allocation event) {
16+
type = event.type;
17+
zeEvent = std::move(event.borrow);
18+
}
19+
20+
event_borrowed ur_event::detachZeHandle() {
21+
// consider make an abstraction for regular/counter based
22+
// events if there's more of this type of conditions
23+
if (type == event_type::EVENT_REGULAR) {
24+
zeEventHostReset(zeEvent.get());
25+
}
26+
auto e = std::move(zeEvent);
27+
zeEvent = nullptr;
28+
29+
return e;
30+
}
31+
32+
ze_event_handle_t ur_event::getZeEvent() { return zeEvent.get(); }
33+
34+
} // namespace v2
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//===--------- event.hpp - Level Zero Adapter -----------------------------===//
2+
//
3+
// Copyright (C) 2024 Intel Corporation
4+
//
5+
// Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM
6+
// Exceptions. See LICENSE.TXT
7+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8+
//
9+
//===----------------------------------------------------------------------===//
10+
#pragma once
11+
12+
#include <stack>
13+
14+
#include <ur/ur.hpp>
15+
#include <ur_api.h>
16+
#include <ze_api.h>
17+
18+
#include "event_provider.hpp"
19+
20+
namespace v2 {
21+
22+
class ur_event {
23+
public:
24+
void attachZeHandle(event_allocation);
25+
event_borrowed detachZeHandle();
26+
27+
ze_event_handle_t getZeEvent();
28+
29+
private:
30+
event_type type;
31+
event_borrowed zeEvent;
32+
};
33+
34+
} // namespace v2
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//===--------- event_pool.cpp - Level Zero Adapter ------------------------===//
2+
//
3+
// Copyright (C) 2024 Intel Corporation
4+
//
5+
// Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM
6+
// Exceptions. See LICENSE.TXT
7+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8+
//
9+
//===----------------------------------------------------------------------===//
10+
#include "ur_api.h"
11+
#include <event_pool.hpp>
12+
13+
namespace v2 {
14+
15+
static constexpr size_t EVENTS_BURST = 64;
16+
17+
ur_event *event_pool::allocate() {
18+
if (freelist.empty()) {
19+
auto start = events.size();
20+
auto end = start + EVENTS_BURST;
21+
events.resize(end);
22+
for (; start < end; ++start) {
23+
freelist.push_back(&events.at(start));
24+
}
25+
}
26+
27+
auto event = freelist.back();
28+
29+
auto ZeEvent = provider->allocate();
30+
event->attachZeHandle(std::move(ZeEvent));
31+
32+
freelist.pop_back();
33+
34+
return event;
35+
}
36+
37+
void event_pool::free(ur_event *event) {
38+
auto _ = event->detachZeHandle();
39+
40+
freelist.push_back(event);
41+
}
42+
43+
} // namespace v2
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//===--------- event_pool.hpp - Level Zero Adapter ------------------------===//
2+
//
3+
// Copyright (C) 2024 Intel Corporation
4+
//
5+
// Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM
6+
// Exceptions. See LICENSE.TXT
7+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8+
//
9+
//===----------------------------------------------------------------------===//
10+
#pragma once
11+
12+
#include <memory>
13+
#include <mutex>
14+
#include <stack>
15+
16+
#include <unordered_map>
17+
#include <ur/ur.hpp>
18+
#include <ur_api.h>
19+
#include <vector>
20+
#include <ze_api.h>
21+
22+
#include "../device.hpp"
23+
#include "common.hpp"
24+
#include "event.hpp"
25+
#include "event_provider.hpp"
26+
27+
namespace v2 {
28+
29+
class event_pool {
30+
public:
31+
event_pool(std::unique_ptr<event_provider> Provider)
32+
: provider(std::move(Provider)){};
33+
34+
event_pool(event_pool &&other) = default;
35+
event_pool &operator=(event_pool &&other) = default;
36+
37+
event_pool(const event_pool &) = delete;
38+
event_pool &operator=(const event_pool &) = delete;
39+
40+
DeviceId Id() { return provider->device()->Id; };
41+
42+
ur_event *allocate();
43+
void free(ur_event *event);
44+
45+
private:
46+
std::deque<ur_event> events;
47+
std::vector<ur_event *> freelist;
48+
49+
std::unique_ptr<event_provider> provider;
50+
};
51+
52+
} // namespace v2
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//===--------- event_pool_cache.cpp - Level Zero Adapter ------------------===//
2+
//
3+
// Copyright (C) 2024 Intel Corporation
4+
//
5+
// Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM
6+
// Exceptions. See LICENSE.TXT
7+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8+
//
9+
//===----------------------------------------------------------------------===//
10+
#include "event_pool_cache.hpp"
11+
#include "device.hpp"
12+
#include "platform.hpp"
13+
14+
namespace v2 {
15+
16+
event_pool_cache::event_pool_cache(size_t max_devices,
17+
ProviderCreateFunc ProviderCreate)
18+
: providerCreate(ProviderCreate) {
19+
pools.resize(max_devices);
20+
}
21+
22+
event_pool_cache::~event_pool_cache() {}
23+
24+
event_pool_borrowed event_pool_cache::borrow(DeviceId id) {
25+
std::unique_lock<ur_mutex> Lock(mutex);
26+
27+
if (id >= pools.size()) {
28+
return nullptr;
29+
}
30+
31+
auto &vec = pools[id];
32+
if (vec.empty()) {
33+
vec.emplace_back(std::make_unique<event_pool>(providerCreate(id)));
34+
}
35+
36+
auto pool = vec.back().release();
37+
vec.pop_back();
38+
39+
return event_pool_borrowed(pool, [this](event_pool *pool) {
40+
std::unique_lock<ur_mutex> Lock(mutex);
41+
pools[pool->Id()].emplace_back(pool);
42+
});
43+
}
44+
45+
} // namespace v2
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//===--------- event_pool_cache.hpp - Level Zero Adapter ------------------===//
2+
//
3+
// Copyright (C) 2024 Intel Corporation
4+
//
5+
// Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM
6+
// Exceptions. See LICENSE.TXT
7+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8+
//
9+
//===----------------------------------------------------------------------===//
10+
#pragma once
11+
12+
#include <functional>
13+
#include <memory>
14+
#include <mutex>
15+
#include <stack>
16+
17+
#include <unordered_map>
18+
#include <ur/ur.hpp>
19+
#include <ur_api.h>
20+
#include <ze_api.h>
21+
22+
#include "../device.hpp"
23+
#include "event_pool.hpp"
24+
#include "event_provider.hpp"
25+
26+
namespace v2 {
27+
28+
using event_pool_borrowed =
29+
std::unique_ptr<event_pool, std::function<void(event_pool *)>>;
30+
31+
class event_pool_cache {
32+
public:
33+
using ProviderCreateFunc =
34+
std::function<std::unique_ptr<event_provider>(DeviceId)>;
35+
36+
event_pool_cache(size_t max_devices, ProviderCreateFunc);
37+
~event_pool_cache();
38+
39+
event_pool_borrowed borrow(DeviceId);
40+
41+
private:
42+
ur_mutex mutex;
43+
ProviderCreateFunc providerCreate;
44+
std::vector<std::vector<std::unique_ptr<event_pool>>> pools;
45+
};
46+
47+
} // namespace v2
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//===--------- command_list_cache.hpp - Level Zero Adapter ---------------===//
2+
//
3+
// Copyright (C) 2024 Intel Corporation
4+
//
5+
// Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM
6+
// Exceptions. See LICENSE.TXT
7+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8+
//
9+
//===----------------------------------------------------------------------===//
10+
#pragma once
11+
12+
#include <memory>
13+
#include <mutex>
14+
#include <stack>
15+
16+
#include <unordered_map>
17+
#include <ur/ur.hpp>
18+
#include <ur_api.h>
19+
#include <vector>
20+
#include <ze_api.h>
21+
22+
namespace v2 {
23+
24+
enum event_type { EVENT_REGULAR, EVENT_COUNTER };
25+
26+
using event_borrowed =
27+
std::unique_ptr<_ze_event_handle_t, std::function<void(ze_event_handle_t)>>;
28+
29+
struct event_allocation {
30+
event_type type;
31+
event_borrowed borrow;
32+
};
33+
34+
class event_provider {
35+
public:
36+
virtual ~event_provider() = default;
37+
virtual event_allocation allocate() = 0;
38+
virtual ur_device_handle_t device() = 0;
39+
};
40+
41+
} // namespace v2

0 commit comments

Comments
 (0)