-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1867 from pbalcer/v2-events
initial v2 event pool implementation
- Loading branch information
Showing
16 changed files
with
848 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
//===--------- event.cpp - Level Zero Adapter -----------------------------===// | ||
// | ||
// Copyright (C) 2024 Intel Corporation | ||
// | ||
// Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM | ||
// Exceptions. See LICENSE.TXT | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
#include "event.hpp" | ||
#include "adapters/level_zero/v2/event_provider.hpp" | ||
#include "ze_api.h" | ||
|
||
namespace v2 { | ||
void ur_event::attachZeHandle(event_allocation event) { | ||
type = event.type; | ||
zeEvent = std::move(event.borrow); | ||
} | ||
|
||
event_borrowed ur_event::detachZeHandle() { | ||
// consider make an abstraction for regular/counter based | ||
// events if there's more of this type of conditions | ||
if (type == event_type::EVENT_REGULAR) { | ||
zeEventHostReset(zeEvent.get()); | ||
} | ||
auto e = std::move(zeEvent); | ||
zeEvent = nullptr; | ||
|
||
return e; | ||
} | ||
|
||
ze_event_handle_t ur_event::getZeEvent() { return zeEvent.get(); } | ||
|
||
} // namespace v2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
//===--------- event.hpp - Level Zero Adapter -----------------------------===// | ||
// | ||
// Copyright (C) 2024 Intel Corporation | ||
// | ||
// Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM | ||
// Exceptions. See LICENSE.TXT | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
#pragma once | ||
|
||
#include <stack> | ||
|
||
#include <ur/ur.hpp> | ||
#include <ur_api.h> | ||
#include <ze_api.h> | ||
|
||
#include "event_provider.hpp" | ||
|
||
namespace v2 { | ||
|
||
class ur_event { | ||
public: | ||
void attachZeHandle(event_allocation); | ||
event_borrowed detachZeHandle(); | ||
|
||
ze_event_handle_t getZeEvent(); | ||
|
||
private: | ||
event_type type; | ||
event_borrowed zeEvent; | ||
}; | ||
|
||
} // namespace v2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
//===--------- event_pool.cpp - Level Zero Adapter ------------------------===// | ||
// | ||
// Copyright (C) 2024 Intel Corporation | ||
// | ||
// Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM | ||
// Exceptions. See LICENSE.TXT | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
#include "ur_api.h" | ||
#include <event_pool.hpp> | ||
|
||
namespace v2 { | ||
|
||
static constexpr size_t EVENTS_BURST = 64; | ||
|
||
ur_event *event_pool::allocate() { | ||
if (freelist.empty()) { | ||
auto start = events.size(); | ||
auto end = start + EVENTS_BURST; | ||
events.resize(end); | ||
for (; start < end; ++start) { | ||
freelist.push_back(&events.at(start)); | ||
} | ||
} | ||
|
||
auto event = freelist.back(); | ||
|
||
auto ZeEvent = provider->allocate(); | ||
event->attachZeHandle(std::move(ZeEvent)); | ||
|
||
freelist.pop_back(); | ||
|
||
return event; | ||
} | ||
|
||
void event_pool::free(ur_event *event) { | ||
auto _ = event->detachZeHandle(); | ||
|
||
freelist.push_back(event); | ||
} | ||
|
||
} // namespace v2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
//===--------- event_pool.hpp - Level Zero Adapter ------------------------===// | ||
// | ||
// Copyright (C) 2024 Intel Corporation | ||
// | ||
// Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM | ||
// Exceptions. See LICENSE.TXT | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
#pragma once | ||
|
||
#include <memory> | ||
#include <mutex> | ||
#include <stack> | ||
|
||
#include <unordered_map> | ||
#include <ur/ur.hpp> | ||
#include <ur_api.h> | ||
#include <vector> | ||
#include <ze_api.h> | ||
|
||
#include "../device.hpp" | ||
#include "common.hpp" | ||
#include "event.hpp" | ||
#include "event_provider.hpp" | ||
|
||
namespace v2 { | ||
|
||
class event_pool { | ||
public: | ||
event_pool(std::unique_ptr<event_provider> Provider) | ||
: provider(std::move(Provider)){}; | ||
|
||
event_pool(event_pool &&other) = default; | ||
event_pool &operator=(event_pool &&other) = default; | ||
|
||
event_pool(const event_pool &) = delete; | ||
event_pool &operator=(const event_pool &) = delete; | ||
|
||
DeviceId Id() { return provider->device()->Id; }; | ||
|
||
ur_event *allocate(); | ||
void free(ur_event *event); | ||
|
||
private: | ||
std::deque<ur_event> events; | ||
std::vector<ur_event *> freelist; | ||
|
||
std::unique_ptr<event_provider> provider; | ||
}; | ||
|
||
} // namespace v2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
//===--------- event_pool_cache.cpp - Level Zero Adapter ------------------===// | ||
// | ||
// Copyright (C) 2024 Intel Corporation | ||
// | ||
// Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM | ||
// Exceptions. See LICENSE.TXT | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
#include "event_pool_cache.hpp" | ||
#include "device.hpp" | ||
#include "platform.hpp" | ||
|
||
namespace v2 { | ||
|
||
event_pool_cache::event_pool_cache(size_t max_devices, | ||
ProviderCreateFunc ProviderCreate) | ||
: providerCreate(ProviderCreate) { | ||
pools.resize(max_devices); | ||
} | ||
|
||
event_pool_cache::~event_pool_cache() {} | ||
|
||
event_pool_borrowed event_pool_cache::borrow(DeviceId id) { | ||
std::unique_lock<ur_mutex> Lock(mutex); | ||
|
||
if (id >= pools.size()) { | ||
return nullptr; | ||
} | ||
|
||
auto &vec = pools[id]; | ||
if (vec.empty()) { | ||
vec.emplace_back(std::make_unique<event_pool>(providerCreate(id))); | ||
} | ||
|
||
auto pool = vec.back().release(); | ||
vec.pop_back(); | ||
|
||
return event_pool_borrowed(pool, [this](event_pool *pool) { | ||
std::unique_lock<ur_mutex> Lock(mutex); | ||
pools[pool->Id()].emplace_back(pool); | ||
}); | ||
} | ||
|
||
} // namespace v2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
//===--------- event_pool_cache.hpp - Level Zero Adapter ------------------===// | ||
// | ||
// Copyright (C) 2024 Intel Corporation | ||
// | ||
// Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM | ||
// Exceptions. See LICENSE.TXT | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
#pragma once | ||
|
||
#include <functional> | ||
#include <memory> | ||
#include <mutex> | ||
#include <stack> | ||
|
||
#include <unordered_map> | ||
#include <ur/ur.hpp> | ||
#include <ur_api.h> | ||
#include <ze_api.h> | ||
|
||
#include "../device.hpp" | ||
#include "event_pool.hpp" | ||
#include "event_provider.hpp" | ||
|
||
namespace v2 { | ||
|
||
using event_pool_borrowed = | ||
std::unique_ptr<event_pool, std::function<void(event_pool *)>>; | ||
|
||
class event_pool_cache { | ||
public: | ||
using ProviderCreateFunc = | ||
std::function<std::unique_ptr<event_provider>(DeviceId)>; | ||
|
||
event_pool_cache(size_t max_devices, ProviderCreateFunc); | ||
~event_pool_cache(); | ||
|
||
event_pool_borrowed borrow(DeviceId); | ||
|
||
private: | ||
ur_mutex mutex; | ||
ProviderCreateFunc providerCreate; | ||
std::vector<std::vector<std::unique_ptr<event_pool>>> pools; | ||
}; | ||
|
||
} // namespace v2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
//===--------- command_list_cache.hpp - Level Zero Adapter ---------------===// | ||
// | ||
// Copyright (C) 2024 Intel Corporation | ||
// | ||
// Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM | ||
// Exceptions. See LICENSE.TXT | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
#pragma once | ||
|
||
#include <memory> | ||
#include <mutex> | ||
#include <stack> | ||
|
||
#include <unordered_map> | ||
#include <ur/ur.hpp> | ||
#include <ur_api.h> | ||
#include <vector> | ||
#include <ze_api.h> | ||
|
||
namespace v2 { | ||
|
||
enum event_type { EVENT_REGULAR, EVENT_COUNTER }; | ||
|
||
using event_borrowed = | ||
std::unique_ptr<_ze_event_handle_t, std::function<void(ze_event_handle_t)>>; | ||
|
||
struct event_allocation { | ||
event_type type; | ||
event_borrowed borrow; | ||
}; | ||
|
||
class event_provider { | ||
public: | ||
virtual ~event_provider() = default; | ||
virtual event_allocation allocate() = 0; | ||
virtual ur_device_handle_t device() = 0; | ||
}; | ||
|
||
} // namespace v2 |
Oops, something went wrong.