-
Notifications
You must be signed in to change notification settings - Fork 14.1k
[mlir] SYCL runtime wrapper: add handler for set default device. #90878
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
silee2
wants to merge
1
commit into
llvm:main
Choose a base branch
from
silee2:syclRuntimeWrapper
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
This file contains hidden or 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
@llvm/pr-subscribers-mlir @llvm/pr-subscribers-mlir-execution-engine Author: Sang Ik Lee (silee2) ChangesAdd missing support for set default device Full diff: https://github.com/llvm/llvm-project/pull/90878.diff 1 Files Affected:
diff --git a/mlir/lib/ExecutionEngine/SyclRuntimeWrappers.cpp b/mlir/lib/ExecutionEngine/SyclRuntimeWrappers.cpp
index c250340c38fc77..806d49f342d292 100644
--- a/mlir/lib/ExecutionEngine/SyclRuntimeWrappers.cpp
+++ b/mlir/lib/ExecutionEngine/SyclRuntimeWrappers.cpp
@@ -11,8 +11,11 @@
//===----------------------------------------------------------------------===//
#include <CL/sycl.hpp>
+#include <cstdio>
+#include <cstdlib>
#include <level_zero/ze_api.h>
#include <sycl/ext/oneapi/backend/level_zero.hpp>
+#include <vector>
#ifdef _WIN32
#define SYCL_RUNTIME_EXPORT __declspec(dllexport)
@@ -49,39 +52,53 @@ auto catchAll(F &&func) {
} // namespace
-static sycl::device getDefaultDevice() {
- static sycl::device syclDevice;
- static bool isDeviceInitialised = false;
- if (!isDeviceInitialised) {
- auto platformList = sycl::platform::get_platforms();
- for (const auto &platform : platformList) {
- auto platformName = platform.get_info<sycl::info::platform::name>();
- bool isLevelZero = platformName.find("Level-Zero") != std::string::npos;
- if (!isLevelZero)
- continue;
-
- syclDevice = platform.get_devices()[0];
- isDeviceInitialised = true;
- return syclDevice;
+thread_local static int32_t defaultDevice = 0;
+thread_local static bool isGpuPoolInitialized = false;
+thread_local static bool isDefaultContextInitialized = false;
+thread_local static std::vector<sycl::device> *pGpuPool = nullptr;
+thread_local static sycl::context *pDefaultContext = nullptr;
+
+static void initGpuPool() {
+ if (isGpuPoolInitialized)
+ return;
+ auto platformList = sycl::platform::get_platforms();
+ for (const auto &platform : platformList) {
+ if (platform.get_backend() == sycl::backend::ext_oneapi_level_zero) {
+ auto gpuDevices = platform.get_devices(sycl::info::device_type::gpu);
+ if (gpuDevices.empty()) {
+ throw std::runtime_error("SyclRuntime: No GPU devices found!");
+ }
+ pGpuPool = new std::vector<sycl::device>{gpuDevices};
+ isGpuPoolInitialized = true;
+ return;
}
- throw std::runtime_error("getDefaultDevice failed");
- } else
- return syclDevice;
+ }
+ throw std::runtime_error("SyclRuntime: No GPU devices found!");
}
-static sycl::context getDefaultContext() {
- static sycl::context syclContext{getDefaultDevice()};
- return syclContext;
+static sycl::device *getDefaultDevicePtr() {
+ initGpuPool();
+ return &((*pGpuPool)[defaultDevice]);
+}
+
+static sycl::context *getDefaultContextPtr() {
+ if (isDefaultContextInitialized) {
+ return pDefaultContext;
+ }
+ initGpuPool();
+ pDefaultContext = new sycl::context(*pGpuPool);
+ isDefaultContextInitialized = true;
+ return pDefaultContext;
}
static void *allocDeviceMemory(sycl::queue *queue, size_t size, bool isShared) {
void *memPtr = nullptr;
if (isShared) {
- memPtr = sycl::aligned_alloc_shared(64, size, getDefaultDevice(),
- getDefaultContext());
+ memPtr = sycl::aligned_alloc_shared(64, size, *getDefaultDevicePtr(),
+ *getDefaultContextPtr());
} else {
- memPtr = sycl::aligned_alloc_device(64, size, getDefaultDevice(),
- getDefaultContext());
+ memPtr = sycl::aligned_alloc_device(64, size, *getDefaultDevicePtr(),
+ *getDefaultContextPtr());
}
if (memPtr == nullptr) {
throw std::runtime_error("mem allocation failed!");
@@ -90,7 +107,13 @@ static void *allocDeviceMemory(sycl::queue *queue, size_t size, bool isShared) {
}
static void deallocDeviceMemory(sycl::queue *queue, void *ptr) {
- sycl::free(ptr, *queue);
+ if (queue == nullptr) {
+ queue = new sycl::queue(*getDefaultContextPtr(), *getDefaultDevicePtr());
+ sycl::free(ptr, *queue);
+ delete queue;
+ } else {
+ sycl::free(ptr, *queue);
+ }
}
static ze_module_handle_t loadModule(const void *data, size_t dataSize) {
@@ -104,9 +127,9 @@ static ze_module_handle_t loadModule(const void *data, size_t dataSize) {
nullptr,
nullptr};
auto zeDevice = sycl::get_native<sycl::backend::ext_oneapi_level_zero>(
- getDefaultDevice());
+ *getDefaultDevicePtr());
auto zeContext = sycl::get_native<sycl::backend::ext_oneapi_level_zero>(
- getDefaultContext());
+ *getDefaultContextPtr());
L0_SAFE_CALL(zeModuleCreate(zeContext, zeDevice, &desc, &zeModule, nullptr));
return zeModule;
}
@@ -115,17 +138,33 @@ static sycl::kernel *getKernel(ze_module_handle_t zeModule, const char *name) {
assert(zeModule);
assert(name);
ze_kernel_handle_t zeKernel;
- ze_kernel_desc_t desc = {};
- desc.pKernelName = name;
+ ze_kernel_desc_t desc = {ZE_STRUCTURE_TYPE_KERNEL_DESC, nullptr,
+ 0, // flags
+ name};
+
+ ze_result_t result = zeKernelCreate(zeModule, &desc, &zeKernel);
+
+ // Check if there are unresolved imports
+ if (result == ZE_RESULT_ERROR_INVALID_MODULE_UNLINKED) {
+ fprintf(stdout, "Unresolved imports!!!\n");
+ fflush(stdout);
+ abort();
+ }
+
+ // Check to see if the kernel name was found in the supplied module
+ if (result == ZE_RESULT_ERROR_INVALID_KERNEL_NAME) {
+ fprintf(stdout, "Invalid kernel name: %s !!!\n", name);
+ fflush(stdout);
+ abort();
+ }
- L0_SAFE_CALL(zeKernelCreate(zeModule, &desc, &zeKernel));
sycl::kernel_bundle<sycl::bundle_state::executable> kernelBundle =
sycl::make_kernel_bundle<sycl::backend::ext_oneapi_level_zero,
sycl::bundle_state::executable>(
- {zeModule}, getDefaultContext());
+ {zeModule}, *getDefaultContextPtr());
auto kernel = sycl::make_kernel<sycl::backend::ext_oneapi_level_zero>(
- {kernelBundle, zeKernel}, getDefaultContext());
+ {kernelBundle, zeKernel}, *getDefaultContextPtr());
return new sycl::kernel(kernel);
}
@@ -152,7 +191,7 @@ extern "C" SYCL_RUNTIME_EXPORT sycl::queue *mgpuStreamCreate() {
return catchAll([&]() {
sycl::queue *queue =
- new sycl::queue(getDefaultContext(), getDefaultDevice());
+ new sycl::queue(*getDefaultContextPtr(), *getDefaultDevicePtr());
return queue;
});
}
@@ -207,3 +246,11 @@ mgpuModuleUnload(ze_module_handle_t module) {
catchAll([&]() { L0_SAFE_CALL(zeModuleDestroy(module)); });
}
+
+extern "C" SYCL_RUNTIME_EXPORT void mgpuSetDefaultDevice(int32_t device) {
+ initGpuPool();
+ if (device >= pGpuPool->size()) {
+ throw std::runtime_error("SyclRuntime: Invalid device index!");
+ }
+ defaultDevice = device;
+}
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Add missing support for set default device
Thread local storage is used for keeping per thread default device.
Default context is redefined as a context with all GPU devices.