Index: openmp/libomptarget/plugins/cuda/src/rtl.cpp =================================================================== --- openmp/libomptarget/plugins/cuda/src/rtl.cpp +++ openmp/libomptarget/plugins/cuda/src/rtl.cpp @@ -385,9 +385,15 @@ for (DeviceDataTy &D : DeviceData) { // Destroy context - if (D.Context) - checkResult(cuCtxDestroy(D.Context), - "Error returned from cuCtxDestroy\n"); + if (D.Context) { + checkResult(cuCtxSetCurrent(D.Context), + "Error returned from cuCtxSetCurrent\n"); + CUdevice Device; + checkResult(cuCtxGetDevice(&Device), + "Error returned from cuCtxGetDevice\n"); + checkResult(cuDevicePrimaryCtxRelease(Device), + "Error returned from cuDevicePrimaryCtxRelease\n"); + } } } @@ -408,10 +414,10 @@ if (!checkResult(Err, "Error returned from cuDeviceGet\n")) return OFFLOAD_FAIL; - // Create the context and save it to use whenever this device is selected. - Err = cuCtxCreate(&DeviceData[DeviceId].Context, CU_CTX_SCHED_BLOCKING_SYNC, - Device); - if (!checkResult(Err, "Error returned from cuCtxCreate\n")) + // Retain the per device primary context and save it to use whenever this + // device is selected. + Err = cuDevicePrimaryCtxRetain(&DeviceData[DeviceId].Context, Device); + if (!checkResult(Err, "Error returned from cuDevicePrimaryCtxRetain\n")) return OFFLOAD_FAIL; Err = cuCtxSetCurrent(DeviceData[DeviceId].Context);