2018-03-01 22:57:20 -05:00
|
|
|
/*
|
|
|
|
|
Copyright (c) 2015 - present Advanced Micro Devices, Inc. All rights reserved.
|
|
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
|
THE SOFTWARE.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <hip/hip_runtime.h>
|
|
|
|
|
#include "hip_internal.hpp"
|
|
|
|
|
#include "platform/runtime.hpp"
|
2018-03-02 17:55:48 -05:00
|
|
|
#include "utils/versions.hpp"
|
2018-03-01 22:57:20 -05:00
|
|
|
|
2018-05-01 18:10:09 -04:00
|
|
|
std::vector<amd::Context*> g_devices;
|
|
|
|
|
|
|
|
|
|
namespace hip {
|
|
|
|
|
|
2018-03-19 13:52:17 -04:00
|
|
|
thread_local amd::Context* g_context = nullptr;
|
2018-03-23 14:18:27 -04:00
|
|
|
thread_local std::stack<amd::Context*> g_ctxtStack;
|
2018-05-04 14:13:13 -04:00
|
|
|
std::once_flag g_ihipInitialized;
|
2018-03-23 14:18:27 -04:00
|
|
|
|
2018-05-08 19:04:35 -04:00
|
|
|
std::map<amd::Context*, amd::HostQueue*> g_nullStreams;
|
2018-03-01 22:57:20 -05:00
|
|
|
|
2018-05-01 18:10:09 -04:00
|
|
|
void init() {
|
2018-03-01 22:57:20 -05:00
|
|
|
if (!amd::Runtime::initialized()) {
|
|
|
|
|
amd::Runtime::init();
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-19 13:52:17 -04:00
|
|
|
const std::vector<amd::Device*>& devices = amd::Device::getDevices(CL_DEVICE_TYPE_GPU, false);
|
2018-03-01 22:57:20 -05:00
|
|
|
|
2018-03-19 13:52:17 -04:00
|
|
|
for (unsigned int i=0; i<devices.size(); i++) {
|
|
|
|
|
const std::vector<amd::Device*> device(1, devices[i]);
|
|
|
|
|
amd::Context* context = new amd::Context(device, amd::Context::Info());
|
2018-03-28 19:23:57 -04:00
|
|
|
if (!context) return;
|
2018-03-19 13:52:17 -04:00
|
|
|
|
|
|
|
|
if (context && CL_SUCCESS != context->create(nullptr)) {
|
|
|
|
|
context->release();
|
|
|
|
|
} else {
|
|
|
|
|
g_devices.push_back(context);
|
|
|
|
|
}
|
2018-03-01 22:57:20 -05:00
|
|
|
}
|
2018-03-28 19:23:57 -04:00
|
|
|
}
|
|
|
|
|
|
2018-05-01 18:10:09 -04:00
|
|
|
amd::Context* getCurrentContext() {
|
|
|
|
|
return g_context;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void setCurrentContext(unsigned int index) {
|
|
|
|
|
assert(index<g_devices.size());
|
|
|
|
|
g_context = g_devices[index];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
amd::HostQueue* getNullStream() {
|
|
|
|
|
auto stream = g_nullStreams.find(getCurrentContext());
|
|
|
|
|
if (stream == g_nullStreams.end()) {
|
|
|
|
|
amd::Device* device = getCurrentContext()->devices()[0];
|
|
|
|
|
amd::HostQueue* queue = new amd::HostQueue(*hip::getCurrentContext(), *device, 0,
|
|
|
|
|
amd::CommandQueue::RealTimeDisabled,
|
|
|
|
|
amd::CommandQueue::Priority::Normal);
|
|
|
|
|
g_nullStreams[getCurrentContext()] = queue;
|
|
|
|
|
return queue;
|
|
|
|
|
}
|
2018-05-08 19:04:35 -04:00
|
|
|
syncStreams();
|
2018-05-01 18:10:09 -04:00
|
|
|
return stream->second;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
using namespace hip;
|
|
|
|
|
|
2018-04-05 15:02:43 -04:00
|
|
|
hipError_t hipInit(unsigned int flags) {
|
2018-03-28 19:23:57 -04:00
|
|
|
HIP_INIT_API(flags);
|
2018-03-01 22:57:20 -05:00
|
|
|
|
|
|
|
|
return hipSuccess;
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-05 15:02:43 -04:00
|
|
|
hipError_t hipCtxCreate(hipCtx_t *ctx, unsigned int flags, hipDevice_t device) {
|
2018-03-01 22:57:20 -05:00
|
|
|
HIP_INIT_API(ctx, flags, device);
|
|
|
|
|
|
2018-03-19 13:52:17 -04:00
|
|
|
if (static_cast<size_t>(device) >= g_devices.size()) {
|
|
|
|
|
return hipErrorInvalidValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*ctx = reinterpret_cast<hipCtx_t>(g_devices[device]);
|
|
|
|
|
|
2018-03-23 14:18:27 -04:00
|
|
|
// Increment ref count for device primary context
|
|
|
|
|
g_devices[device]->retain();
|
2018-04-24 16:49:38 -04:00
|
|
|
g_ctxtStack.push(g_devices[device]);
|
2018-03-23 14:18:27 -04:00
|
|
|
|
2018-03-19 13:52:17 -04:00
|
|
|
return hipSuccess;
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-05 15:02:43 -04:00
|
|
|
hipError_t hipCtxSetCurrent(hipCtx_t ctx) {
|
2018-03-19 13:52:17 -04:00
|
|
|
HIP_INIT_API(ctx);
|
|
|
|
|
|
2018-03-23 14:18:27 -04:00
|
|
|
if (ctx == nullptr) {
|
|
|
|
|
if(!g_ctxtStack.empty()) {
|
|
|
|
|
g_ctxtStack.pop();
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2018-05-01 18:10:09 -04:00
|
|
|
hip::g_context = reinterpret_cast<amd::Context*>(as_amd(ctx));
|
2018-03-23 14:18:27 -04:00
|
|
|
if(!g_ctxtStack.empty()) {
|
|
|
|
|
g_ctxtStack.pop();
|
|
|
|
|
}
|
2018-05-01 18:10:09 -04:00
|
|
|
g_ctxtStack.push(hip::getCurrentContext());
|
2018-03-23 14:18:27 -04:00
|
|
|
}
|
2018-03-19 13:52:17 -04:00
|
|
|
|
|
|
|
|
return hipSuccess;
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-05 15:02:43 -04:00
|
|
|
hipError_t hipCtxGetCurrent(hipCtx_t* ctx) {
|
2018-03-19 13:52:17 -04:00
|
|
|
HIP_INIT_API(ctx);
|
|
|
|
|
|
2018-05-01 18:10:09 -04:00
|
|
|
*ctx = reinterpret_cast<hipCtx_t>(hip::getCurrentContext());
|
2018-03-19 13:52:17 -04:00
|
|
|
|
2018-03-01 22:57:20 -05:00
|
|
|
return hipSuccess;
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-05 15:02:43 -04:00
|
|
|
hipError_t hipRuntimeGetVersion(int *runtimeVersion) {
|
2018-03-02 17:55:48 -05:00
|
|
|
HIP_INIT_API(runtimeVersion);
|
|
|
|
|
|
|
|
|
|
if (!runtimeVersion) {
|
|
|
|
|
return hipErrorInvalidValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*runtimeVersion = AMD_PLATFORM_BUILD_NUMBER;
|
|
|
|
|
|
|
|
|
|
return hipSuccess;
|
|
|
|
|
}
|
2018-03-23 14:18:27 -04:00
|
|
|
|
2018-04-05 15:02:43 -04:00
|
|
|
hipError_t hipCtxDestroy(hipCtx_t ctx) {
|
2018-03-23 14:18:27 -04:00
|
|
|
HIP_INIT_API(ctx);
|
|
|
|
|
|
|
|
|
|
amd::Context* amdContext = reinterpret_cast<amd::Context*>(as_amd(ctx));
|
|
|
|
|
if (amdContext == nullptr) {
|
|
|
|
|
return hipErrorInvalidValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Need to remove the ctx of calling thread if its the top one
|
2018-04-24 16:49:38 -04:00
|
|
|
if (!g_ctxtStack.empty() && g_ctxtStack.top() == amdContext) {
|
2018-03-23 14:18:27 -04:00
|
|
|
g_ctxtStack.pop();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Remove context from global context list
|
|
|
|
|
for (unsigned int i = 0; i < g_devices.size(); i++) {
|
|
|
|
|
if (g_devices[i] == amdContext) {
|
|
|
|
|
// Decrement ref count for device primary context
|
|
|
|
|
amdContext->release();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return hipSuccess;
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-05 15:02:43 -04:00
|
|
|
hipError_t hipCtxPopCurrent(hipCtx_t* ctx) {
|
2018-03-23 14:18:27 -04:00
|
|
|
HIP_INIT_API(ctx);
|
|
|
|
|
|
|
|
|
|
amd::Context* amdContext = reinterpret_cast<amd::Context*>(as_amd(ctx));
|
|
|
|
|
if (amdContext == nullptr) {
|
|
|
|
|
return hipErrorInvalidContext;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!g_ctxtStack.empty()) {
|
|
|
|
|
amdContext = g_ctxtStack.top();
|
|
|
|
|
g_ctxtStack.pop();
|
|
|
|
|
} else {
|
|
|
|
|
return hipErrorInvalidContext;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return hipSuccess;
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-05 15:02:43 -04:00
|
|
|
hipError_t hipCtxPushCurrent(hipCtx_t ctx) {
|
2018-03-23 14:18:27 -04:00
|
|
|
HIP_INIT_API(ctx);
|
|
|
|
|
|
|
|
|
|
amd::Context* amdContext = reinterpret_cast<amd::Context*>(as_amd(ctx));
|
|
|
|
|
if (amdContext == nullptr) {
|
|
|
|
|
return hipErrorInvalidContext;
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-01 18:10:09 -04:00
|
|
|
hip::g_context = amdContext;
|
|
|
|
|
g_ctxtStack.push(hip::getCurrentContext());
|
2018-03-23 14:18:27 -04:00
|
|
|
|
|
|
|
|
return hipSuccess;
|
2018-03-28 19:23:57 -04:00
|
|
|
}
|
2018-04-05 15:02:43 -04:00
|
|
|
|
2018-04-10 17:41:24 -04:00
|
|
|
hipError_t hipDriverGetVersion(int* driverVersion) {
|
|
|
|
|
HIP_INIT_API(driverVersion);
|
|
|
|
|
|
|
|
|
|
auto* deviceHandle = g_devices[0]->devices()[0];
|
|
|
|
|
const auto& info = deviceHandle->info();
|
|
|
|
|
|
|
|
|
|
if (driverVersion) {
|
|
|
|
|
*driverVersion = AMD_PLATFORM_BUILD_NUMBER * 100 +
|
|
|
|
|
AMD_PLATFORM_REVISION_NUMBER;
|
|
|
|
|
} else {
|
|
|
|
|
return hipErrorInvalidValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return hipSuccess;;
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-05 15:02:43 -04:00
|
|
|
hipError_t hipCtxGetDevice(hipDevice_t* device) {
|
|
|
|
|
HIP_INIT_API(device);
|
|
|
|
|
|
2018-04-24 16:49:38 -04:00
|
|
|
if (device != nullptr) {
|
|
|
|
|
for (unsigned int i = 0; i < g_devices.size(); i++) {
|
2018-05-01 18:10:09 -04:00
|
|
|
if (g_devices[i] == hip::getCurrentContext()) {
|
2018-04-24 16:49:38 -04:00
|
|
|
*device = static_cast<hipDevice_t>(i);
|
|
|
|
|
return hipSuccess;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
return hipErrorInvalidValue;
|
|
|
|
|
}
|
2018-04-05 15:02:43 -04:00
|
|
|
|
|
|
|
|
return hipErrorUnknown;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hipError_t hipCtxGetApiVersion(hipCtx_t ctx, int* apiVersion) {
|
|
|
|
|
HIP_INIT_API(apiVersion);
|
|
|
|
|
|
|
|
|
|
assert(0 && "Unimplemented");
|
|
|
|
|
|
|
|
|
|
return hipErrorUnknown;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hipError_t hipCtxGetCacheConfig(hipFuncCache_t* cacheConfig) {
|
|
|
|
|
HIP_INIT_API(cacheConfig);
|
|
|
|
|
|
|
|
|
|
assert(0 && "Unimplemented");
|
|
|
|
|
|
|
|
|
|
return hipErrorUnknown;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hipError_t hipCtxSetCacheConfig(hipFuncCache_t cacheConfig) {
|
|
|
|
|
HIP_INIT_API(cacheConfig);
|
|
|
|
|
|
|
|
|
|
assert(0 && "Unimplemented");
|
|
|
|
|
|
|
|
|
|
return hipErrorUnknown;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hipError_t hipCtxSetSharedMemConfig(hipSharedMemConfig config) {
|
|
|
|
|
HIP_INIT_API(config);
|
|
|
|
|
|
|
|
|
|
assert(0 && "Unimplemented");
|
|
|
|
|
|
|
|
|
|
return hipErrorUnknown;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hipError_t hipCtxSynchronize(void) {
|
|
|
|
|
HIP_INIT_API(1);
|
|
|
|
|
|
|
|
|
|
assert(0 && "Unimplemented");
|
|
|
|
|
|
|
|
|
|
return hipErrorUnknown;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hipError_t hipCtxGetFlags(unsigned int* flags) {
|
|
|
|
|
HIP_INIT_API(flags);
|
|
|
|
|
|
|
|
|
|
assert(0 && "Unimplemented");
|
|
|
|
|
|
|
|
|
|
return hipErrorUnknown;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hipError_t hipDevicePrimaryCtxGetState(hipDevice_t dev, unsigned int* flags, int* active) {
|
|
|
|
|
HIP_INIT_API(dev, flags, active);
|
|
|
|
|
|
|
|
|
|
assert(0 && "Unimplemented");
|
|
|
|
|
|
|
|
|
|
return hipErrorUnknown;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hipError_t hipDevicePrimaryCtxRelease(hipDevice_t dev) {
|
|
|
|
|
HIP_INIT_API(dev);
|
|
|
|
|
|
|
|
|
|
assert(0 && "Unimplemented");
|
|
|
|
|
|
|
|
|
|
return hipErrorUnknown;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hipError_t hipDevicePrimaryCtxRetain(hipCtx_t* pctx, hipDevice_t dev) {
|
|
|
|
|
HIP_INIT_API(pctx, dev);
|
|
|
|
|
|
|
|
|
|
assert(0 && "Unimplemented");
|
|
|
|
|
|
|
|
|
|
return hipErrorUnknown;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hipError_t hipDevicePrimaryCtxReset(hipDevice_t dev) {
|
|
|
|
|
HIP_INIT_API(dev);
|
|
|
|
|
|
|
|
|
|
assert(0 && "Unimplemented");
|
|
|
|
|
|
|
|
|
|
return hipErrorUnknown;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hipError_t hipDevicePrimaryCtxSetFlags(hipDevice_t dev, unsigned int flags) {
|
|
|
|
|
HIP_INIT_API(dev, flags);
|
|
|
|
|
|
|
|
|
|
assert(0 && "Unimplemented");
|
|
|
|
|
|
|
|
|
|
return hipErrorUnknown;
|
2018-04-24 16:49:38 -04:00
|
|
|
}
|