diff --git a/projects/clr/hipamd/include/hip/hcc_detail/hip_runtime_api.h b/projects/clr/hipamd/include/hip/hcc_detail/hip_runtime_api.h index 2ff4a70802..da94ad5e5e 100644 --- a/projects/clr/hipamd/include/hip/hcc_detail/hip_runtime_api.h +++ b/projects/clr/hipamd/include/hip/hcc_detail/hip_runtime_api.h @@ -1696,6 +1696,66 @@ hipError_t hipCtxEnablePeerAccess (hipCtx_t peerCtx, unsigned int flags); */ hipError_t hipCtxDisablePeerAccess (hipCtx_t peerCtx); +/** + * @brief Get the state of the primary context. + * + * @param [in] Device to get primary context flags for + * @param [out] Pointer to store flags + * @param [out] Pointer to store context state; 0 = inactive, 1 = active + * + * @returns #hipSuccess + * + * @see hipCtxCreate, hipCtxDestroy, hipCtxGetFlags, hipCtxPopCurrent, hipCtxGetCurrent, hipCtxSetCurrent, hipCtxPushCurrent, hipCtxSetCacheConfig, hipCtxSynchronize, hipCtxGetDevice + */ +hipError_t hipDevicePrimaryCtxGetState ( hipDevice_t dev, unsigned int* flags, int* active ); + +/** + * @brief Release the primary context on the GPU. + * + * @param [in] Device which primary context is released + * + * @returns #hipSuccess + * + * @see hipCtxCreate, hipCtxDestroy, hipCtxGetFlags, hipCtxPopCurrent, hipCtxGetCurrent, hipCtxSetCurrent, hipCtxPushCurrent, hipCtxSetCacheConfig, hipCtxSynchronize, hipCtxGetDevice + * @warning This function return #hipSuccess though doesn't release the primaryCtx by design on HIP/HCC path. + */ +hipError_t hipDevicePrimaryCtxRelease ( hipDevice_t dev); + +/** + * @brief Retain the primary context on the GPU. + * + * @param [out] Returned context handle of the new context + * @param [in] Device which primary context is released + * + * @returns #hipSuccess + * + * @see hipCtxCreate, hipCtxDestroy, hipCtxGetFlags, hipCtxPopCurrent, hipCtxGetCurrent, hipCtxSetCurrent, hipCtxPushCurrent, hipCtxSetCacheConfig, hipCtxSynchronize, hipCtxGetDevice + */ +hipError_t hipDevicePrimaryCtxRetain ( hipCtx_t* pctx, hipDevice_t dev ); + +/** + * @brief Resets the primary context on the GPU. + * + * @param [in] Device which primary context is reset + * + * @returns #hipSuccess + * + * @see hipCtxCreate, hipCtxDestroy, hipCtxGetFlags, hipCtxPopCurrent, hipCtxGetCurrent, hipCtxSetCurrent, hipCtxPushCurrent, hipCtxSetCacheConfig, hipCtxSynchronize, hipCtxGetDevice + */ +hipError_t hipDevicePrimaryCtxReset ( hipDevice_t dev ); + +/** + * @brief Set flags for the primary context. + * + * @param [in] Device for which the primary context flags are set + * @param [in] New flags for the device + * + * @returns #hipSuccess, #hipErrorContextAlreadyInUse + * + * @see hipCtxCreate, hipCtxDestroy, hipCtxGetFlags, hipCtxPopCurrent, hipCtxGetCurrent, hipCtxSetCurrent, hipCtxPushCurrent, hipCtxSetCacheConfig, hipCtxSynchronize, hipCtxGetDevice + */ +hipError_t hipDevicePrimaryCtxSetFlags ( hipDevice_t dev, unsigned int flags ); + // doxygen end Context Management /** * @} diff --git a/projects/clr/hipamd/include/hip/nvcc_detail/hip_runtime_api.h b/projects/clr/hipamd/include/hip/nvcc_detail/hip_runtime_api.h index 8c3e0da639..a9963fdfe3 100644 --- a/projects/clr/hipamd/include/hip/nvcc_detail/hip_runtime_api.h +++ b/projects/clr/hipamd/include/hip/nvcc_detail/hip_runtime_api.h @@ -670,6 +670,31 @@ inline static hipError_t hipCtxEnablePeerAccess ( hipCtx_t peerCtx, unsigned in return hipCUResultTohipError(cuCtxEnablePeerAccess(peerCtx, flags)); } +inline static hipError_t hipDevicePrimaryCtxGetState ( hipDevice_t dev, unsigned int* flags, int* active ) +{ + return hipCUResultTohipError(cuDevicePrimaryCtxGetState(dev, flags, active)); +} + +inline static hipError_t hipDevicePrimaryCtxRelease ( hipDevice_t dev) +{ + return hipCUResultTohipError(cuDevicePrimaryCtxRelease(dev)); +} + +inline static hipError_t hipDevicePrimaryCtxRetain ( hipCtx_t* pctx, hipDevice_t dev ) +{ + return hipCUResultTohipError(cuDevicePrimaryCtxRetain(pctx, dev)); +} + +inline static hipError_t hipDevicePrimaryCtxReset ( hipDevice_t dev ) +{ + return hipCUResultTohipError(cuDevicePrimaryCtxReset(dev)); +} + +inline static hipError_t hipDevicePrimaryCtxSetFlags ( hipDevice_t dev, unsigned int flags ) +{ + return hipCUResultTohipError(cuDevicePrimaryCtxSetFlags(dev, flags)); +} + inline static hipError_t hipMemGetAddressRange ( hipDeviceptr_t* pbase, size_t* psize, hipDeviceptr_t dptr ) { return hipCUResultTohipError(cuMemGetAddressRange( pbase , psize , dptr)); diff --git a/projects/clr/hipamd/src/hip_context.cpp b/projects/clr/hipamd/src/hip_context.cpp index 835cf6e795..4c39abb738 100644 --- a/projects/clr/hipamd/src/hip_context.cpp +++ b/projects/clr/hipamd/src/hip_context.cpp @@ -283,3 +283,79 @@ hipError_t hipCtxGetFlags ( unsigned int* flags ) *flags = tempCtx->_ctxFlags; return ihipLogStatus(e); } + +hipError_t hipDevicePrimaryCtxGetState ( hipDevice_t dev, unsigned int* flags, int* active ) +{ + HIP_INIT_API(dev, flags, active); + hipError_t e = hipSuccess; + auto deviceHandle = ihipGetDevice(dev); + + if (deviceHandle == NULL) { + e = hipErrorInvalidDevice; + } + + ihipCtx_t* tempCtx; + tempCtx = ihipGetTlsDefaultCtx(); + ihipCtx_t* primaryCtx = deviceHandle->_primaryCtx; + if(tempCtx == primaryCtx) { + *active = 1; + *flags = tempCtx->_ctxFlags; + } else { + *active = 0; + *flags = primaryCtx->_ctxFlags; + } + return ihipLogStatus(e); +} + +hipError_t hipDevicePrimaryCtxRelease ( hipDevice_t dev) +{ + HIP_INIT_API(dev); + hipError_t e = hipSuccess; + auto deviceHandle = ihipGetDevice(dev); + + if (deviceHandle == NULL) { + e = hipErrorInvalidDevice; + } + return ihipLogStatus(e); +} + +hipError_t hipDevicePrimaryCtxRetain ( hipCtx_t* pctx, hipDevice_t dev ) +{ + HIP_INIT_API(pctx, dev); + hipError_t e = hipSuccess; + auto deviceHandle = ihipGetDevice(dev); + + if (deviceHandle == NULL) { + e = hipErrorInvalidDevice; + } + *pctx = deviceHandle->_primaryCtx; + return ihipLogStatus(e); +} + +hipError_t hipDevicePrimaryCtxReset ( hipDevice_t dev ) +{ + HIP_INIT_API(dev); + hipError_t e = hipSuccess; + auto deviceHandle = ihipGetDevice(dev); + + if (deviceHandle == NULL) { + e = hipErrorInvalidDevice; + } + ihipCtx_t* primaryCtx = deviceHandle->_primaryCtx; + primaryCtx->locked_reset(); + return ihipLogStatus(e); +} + +hipError_t hipDevicePrimaryCtxSetFlags ( hipDevice_t dev, unsigned int flags ) +{ + HIP_INIT_API(dev, flags); + hipError_t e = hipSuccess; + auto deviceHandle = ihipGetDevice(dev); + + if (deviceHandle == NULL) { + e = hipErrorInvalidDevice; + } else { + e = hipErrorContextAlreadyInUse; + } + return ihipLogStatus(e); +}