From 5e91fe9af3179554ec775710ec7b95605bb99df7 Mon Sep 17 00:00:00 2001 From: Rahul Garg Date: Sat, 13 Aug 2016 00:09:08 +0530 Subject: [PATCH] First implementation of hipCtxXXX functions Change-Id: I4609cbe6bd90a1fff8655bff4fdd773864397aba [ROCm/hip commit: 62d390da58ef2a6200399fc914e18546939b1a6e] --- .../hip/include/hcc_detail/hip_runtime_api.h | 10 +++ projects/hip/include/hip_runtime_api.h | 1 + projects/hip/src/hip_context.cpp | 65 +++++++++++++++++++ .../hip/tests/src/context/hipCtx_simple.cpp | 7 ++ 4 files changed, 83 insertions(+) diff --git a/projects/hip/include/hcc_detail/hip_runtime_api.h b/projects/hip/include/hcc_detail/hip_runtime_api.h index 5f0b6bec54..ae10a89df9 100644 --- a/projects/hip/include/hcc_detail/hip_runtime_api.h +++ b/projects/hip/include/hcc_detail/hip_runtime_api.h @@ -1053,6 +1053,16 @@ hipError_t hipInit(unsigned int flags) ; // TODO-ctx hipError_t hipCtxCreate(hipCtx_t *ctx, unsigned int flags, hipDevice_t device); +hipError_t hipCtxDestroy(hipCtx_t ctx); + +hipError_t hipCtxPopCurrent(hipCtx_t* ctx); + +hipError_t hipCtxPushCurrent(hipCtx_t ctx); + +hipError_t hipCtxSetCurrent(hipCtx_t ctx); + +hipError_t hipCtxGetCurrent(hipCtx_t* ctx); + // TODO-ctx /** diff --git a/projects/hip/include/hip_runtime_api.h b/projects/hip/include/hip_runtime_api.h index e4719ec5b1..0963b469da 100644 --- a/projects/hip/include/hip_runtime_api.h +++ b/projects/hip/include/hip_runtime_api.h @@ -161,6 +161,7 @@ typedef enum hipError_t { ,hipErrorRuntimeOther ///< HSA runtime call other than memory returned error. Typically not seen in production systems. ,hipErrorHostMemoryAlreadyRegistered ///< Produced when trying to lock a page-locked memory. ,hipErrorHostMemoryNotRegistered ///< Produced when trying to unlock a non-page-locked memory. + ,hipErrorInvalidContext ///< Produced when input context is invalid. ,hipErrorTbd ///< Marker that more error codes are needed. } hipError_t; diff --git a/projects/hip/src/hip_context.cpp b/projects/hip/src/hip_context.cpp index c6c04f1f52..eb47b9bcd2 100644 --- a/projects/hip/src/hip_context.cpp +++ b/projects/hip/src/hip_context.cpp @@ -87,3 +87,68 @@ hipError_t hipDriverGetVersion(int *driverVersion) return ihipLogStatus(hipSuccess); } + +hipError_t hipCtxDestroy(hipCtx_t ctx) +{ + hipError_t e = hipSuccess; + ihipCtx_t* currentCtx= ihipGetTlsDefaultCtx(); + if(currentCtx == ctx) { + //need to destroy the ctx associated with calling thread + tls_ctxStack.pop(); + } + delete ctx; //As per CUDA docs , attempting to access ctx from those threads which has this ctx as current, will result in the error HIP_ERROR_CONTEXT_IS_DESTROYED. + return ihipLogStatus(e); +} + +hipError_t hipCtxPopCurrent(hipCtx_t* ctx) +{ + hipError_t e = hipSuccess; + tls_ctxStack.pop(); + if(!tls_ctxStack.empty()) { + *ctx= tls_ctxStack.top(); + } + else { + *ctx = nullptr; + } + + ihipSetTlsDefaultCtx(*ctx); //TOD0 - Shall check for NULL? + return ihipLogStatus(e); +} + +hipError_t hipCtxPushCurrent(hipCtx_t ctx) +{ + hipError_t e = hipSuccess; + if(ctx != NULL) { //TODO- is this check needed? + ihipSetTlsDefaultCtx(ctx); + tls_ctxStack.push(ctx); + } + else { + e = hipErrorInvalidContext; + } + return ihipLogStatus(e); +} + +hipError_t hipCtxGetCurrent(hipCtx_t* ctx) +{ + hipError_t e = hipSuccess; + + *ctx = ihipGetTlsDefaultCtx(); + if(*ctx == nullptr) { + *ctx = NULL; //TODO - is it required? Can return nullptr? + } + return ihipLogStatus(e); +} + +hipError_t hipCtxSetCurrent(hipCtx_t ctx) +{ + hipError_t e = hipSuccess; + if(ctx == NULL) { + tls_ctxStack.pop(); + } + else { + ihipSetTlsDefaultCtx(ctx); + tls_ctxStack.push(ctx); + } + return ihipLogStatus(e); +} + diff --git a/projects/hip/tests/src/context/hipCtx_simple.cpp b/projects/hip/tests/src/context/hipCtx_simple.cpp index 453021aba5..7d634d36fe 100644 --- a/projects/hip/tests/src/context/hipCtx_simple.cpp +++ b/projects/hip/tests/src/context/hipCtx_simple.cpp @@ -31,9 +31,16 @@ int main(int argc, char *argv[]) hipDevice_t device; hipCtx_t ctx; + hipCtx_t ctx1; HIPCHECK(hipDeviceGetFromId(&device, 0)); HIPCHECK(hipCtxCreate(&ctx, 0, device)); + HIPCHECK(hipCtxGetCurrent(&ctx1)); + + HIPCHECK(hipCtxPopCurrent(&ctx1)); + HIPCHECK(hipCtxGetCurrent(&ctx1)); + + HIPCHECK(hipCtxDestroy(ctx)); passed(); };