diff --git a/hipamd/api/hip/hip_context.cpp b/hipamd/api/hip/hip_context.cpp index 78e65e99c5..2a67898bf3 100644 --- a/hipamd/api/hip/hip_context.cpp +++ b/hipamd/api/hip/hip_context.cpp @@ -27,7 +27,8 @@ THE SOFTWARE. #include "utils/versions.hpp" -amd::Context* g_context = nullptr; +thread_local amd::Context* g_context = nullptr; +std::vector g_devices; hipError_t hipInit(unsigned int flags) { @@ -37,14 +38,18 @@ hipError_t hipInit(unsigned int flags) amd::Runtime::init(); } - // FIXME: move the global VDI context to hipInit. - g_context = new amd::Context( - amd::Device::getDevices(CL_DEVICE_TYPE_GPU, false), amd::Context::Info()); - if (!g_context) return hipErrorOutOfMemory; + const std::vector& devices = amd::Device::getDevices(CL_DEVICE_TYPE_GPU, false); - if (g_context && CL_SUCCESS != g_context->create(nullptr)) { - g_context->release(); - return hipErrorUnknown; + for (unsigned int i=0; i device(1, devices[i]); + amd::Context* context = new amd::Context(device, amd::Context::Info()); + if (!context) return hipErrorOutOfMemory; + + if (context && CL_SUCCESS != context->create(nullptr)) { + context->release(); + } else { + g_devices.push_back(context); + } } return hipSuccess; @@ -54,6 +59,30 @@ hipError_t hipCtxCreate(hipCtx_t *ctx, unsigned int flags, hipDevice_t device) { HIP_INIT_API(ctx, flags, device); + if (static_cast(device) >= g_devices.size()) { + return hipErrorInvalidValue; + } + + *ctx = reinterpret_cast(g_devices[device]); + + return hipSuccess; +} + +hipError_t hipCtxSetCurrent(hipCtx_t ctx) +{ + HIP_INIT_API(ctx); + + g_context = reinterpret_cast(ctx); + + return hipSuccess; +} + +hipError_t hipCtxGetCurrent(hipCtx_t* ctx) +{ + HIP_INIT_API(ctx); + + *ctx = reinterpret_cast(g_context); + return hipSuccess; } diff --git a/hipamd/api/hip/hip_device.cpp b/hipamd/api/hip/hip_device.cpp index f646933bb9..66bae6174e 100644 --- a/hipamd/api/hip/hip_device.cpp +++ b/hipamd/api/hip/hip_device.cpp @@ -50,7 +50,7 @@ hipError_t hipDeviceTotalMem (size_t *bytes, hipDevice_t device) { HIP_INIT_API(bytes, device); - if (device < 0 || device > (cl_int)g_context->devices().size()) { + if (device < 0 || static_cast(device) >= g_devices.size()) { return hipErrorInvalidDevice; } @@ -58,7 +58,7 @@ hipError_t hipDeviceTotalMem (size_t *bytes, hipDevice_t device) { return hipErrorInvalidValue; } - auto* deviceHandle = g_context->devices()[device]; + auto* deviceHandle = g_devices[device]->devices()[0]; const auto& info = deviceHandle->info(); *bytes = info.globalMemSize_; @@ -70,7 +70,7 @@ hipError_t hipDeviceComputeCapability(int *major, int *minor, hipDevice_t device HIP_INIT_API(major, minor, device); - if (device < 0 || device > (cl_int)g_context->devices().size()) { + if (device < 0 || static_cast(device) >= g_devices.size()) { return hipErrorInvalidDevice; } @@ -78,7 +78,7 @@ hipError_t hipDeviceComputeCapability(int *major, int *minor, hipDevice_t device return hipErrorInvalidValue; } - auto* deviceHandle = g_context->devices()[device]; + auto* deviceHandle = g_devices[device]->devices()[0]; const auto& info = deviceHandle->info(); *major = info.gfxipVersion_ / 100; *minor = info.gfxipVersion_ % 100; @@ -94,7 +94,7 @@ hipError_t hipDeviceGetCount(int* count) { } // Get all available devices - *count = g_context->devices().size(); + *count = g_devices.size(); return hipSuccess; } @@ -103,7 +103,7 @@ hipError_t hipDeviceGetName(char *name, int len, hipDevice_t device) { HIP_INIT_API((void*)name, len, device); - if (device < 0 || device > (cl_int)g_context->devices().size()) { + if (device < 0 || static_cast(device) >= g_devices.size()) { return hipErrorInvalidDevice; } @@ -111,7 +111,7 @@ hipError_t hipDeviceGetName(char *name, int len, hipDevice_t device) { return hipErrorInvalidValue; } - auto* deviceHandle = g_context->devices()[device]; + auto* deviceHandle = g_devices[device]->devices()[0]; const auto& info = deviceHandle->info(); len = ((cl_uint)len < ::strlen(info.boardName_)) ? len : 128; @@ -127,10 +127,10 @@ hipError_t hipGetDeviceProperties ( hipDeviceProp_t* props, hipDevice_t device ) return hipErrorInvalidValue; } - if (unsigned(device) >= g_context->devices().size()) { + if (unsigned(device) >= g_devices.size()) { return hipErrorInvalidDevice; } - auto* deviceHandle = g_context->devices()[device]; + auto* deviceHandle = g_devices[device]->devices()[0]; hipDeviceProp_t deviceProps = {0}; diff --git a/hipamd/api/hip/hip_internal.hpp b/hipamd/api/hip/hip_internal.hpp index 159c7671ec..ba9446f2c1 100644 --- a/hipamd/api/hip/hip_internal.hpp +++ b/hipamd/api/hip/hip_internal.hpp @@ -36,7 +36,8 @@ THE SOFTWARE. #define HIP_INIT_API(...) \ HIP_INIT() -extern amd::Context* g_context; +extern thread_local amd::Context* g_context; +extern std::vector g_devices; hipError_t hipDeviceGetCount(int* count);