Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rework DeviceGuard to restore original context upon the exit (#882)
- some libraries are not using PrimiarContext while DALI does. When cudaSetDevice is called PrimaryContext is created and is set as the current one, the old one is lost while other apps may still need it. Adds saving of current context and restores it when DeviceGuard is destroyed - removed CUContext as it is not needed and can be replaced by the DeviceGuard Signed-off-by: Janusz Lisiecki <jlisiecki@nvidia.com>
- Loading branch information
Showing
24 changed files
with
347 additions
and
267 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include "dali/core/device_guard.h" | ||
#include "dali/core/error_handling.h" | ||
|
||
namespace dali { | ||
|
||
DeviceGuard::DeviceGuard() : | ||
old_context_(NULL) { | ||
DALI_ENFORCE(cuInitChecked(), | ||
"Failed to load libcuda.so. " | ||
"Check your library paths and if the driver is installed correctly."); | ||
CUDA_CALL(cuCtxGetCurrent(&old_context_)); | ||
} | ||
|
||
DeviceGuard::DeviceGuard(int new_device) : | ||
old_context_(NULL) { | ||
if (new_device >= 0) { | ||
DALI_ENFORCE(cuInitChecked(), | ||
"Failed to load libcuda.so. " | ||
"Check your library paths and if the driver is installed correctly."); | ||
CUDA_CALL(cuCtxGetCurrent(&old_context_)); | ||
CUDA_CALL(cudaSetDevice(new_device)); | ||
} | ||
} | ||
|
||
DeviceGuard::~DeviceGuard() { | ||
if (old_context_ != NULL) { | ||
CUresult err = cuCtxSetCurrent(old_context_); | ||
if (err != CUDA_SUCCESS) { | ||
std::cerr << "Failed to recover from DeviceGuard: " << err << std::endl; | ||
std::terminate(); | ||
} | ||
} | ||
} | ||
|
||
} // namespace dali |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
// Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include <gtest/gtest.h> | ||
#include "dali/core/dynlink_cuda.h" | ||
#include "dali/core/cuda_utils.h" | ||
#include "dali/core/device_guard.h" | ||
|
||
namespace dali { | ||
|
||
TEST(DeviceGuard, ConstructorWithDevice) { | ||
int test_device = 0; | ||
int guard_device = 0; | ||
int current_device; | ||
int count = 1; | ||
|
||
EXPECT_EQ(cuInitChecked(), true); | ||
EXPECT_EQ(cudaGetDeviceCount(&count), cudaSuccess); | ||
if (count > 1) { | ||
guard_device = 1; | ||
} | ||
|
||
EXPECT_EQ(cudaSetDevice(test_device), cudaSuccess); | ||
EXPECT_EQ(cudaGetDevice(¤t_device), cudaSuccess); | ||
EXPECT_EQ(current_device, test_device); | ||
{ | ||
DeviceGuard g(guard_device); | ||
EXPECT_EQ(cudaGetDevice(¤t_device), cudaSuccess); | ||
EXPECT_EQ(current_device, guard_device); | ||
} | ||
EXPECT_EQ(cudaGetDevice(¤t_device), cudaSuccess); | ||
EXPECT_EQ(current_device, test_device); | ||
} | ||
|
||
TEST(DeviceGuard, ConstructorNoArgs) { | ||
int test_device = 0; | ||
int guard_device = 0; | ||
int current_device; | ||
int count = 1; | ||
|
||
EXPECT_EQ(cuInitChecked(), true); | ||
EXPECT_EQ(cudaGetDeviceCount(&count), cudaSuccess); | ||
if (count > 1) { | ||
guard_device = 1; | ||
} | ||
|
||
EXPECT_EQ(cudaSetDevice(test_device), cudaSuccess); | ||
EXPECT_EQ(cudaGetDevice(¤t_device), cudaSuccess); | ||
EXPECT_EQ(current_device, test_device); | ||
{ | ||
DeviceGuard g; | ||
EXPECT_EQ(cudaSetDevice(guard_device), cudaSuccess); | ||
EXPECT_EQ(cudaGetDevice(¤t_device), cudaSuccess); | ||
EXPECT_EQ(current_device, guard_device); | ||
} | ||
EXPECT_EQ(cudaGetDevice(¤t_device), cudaSuccess); | ||
EXPECT_EQ(current_device, test_device); | ||
} | ||
|
||
TEST(DeviceGuard, Checkcontext) { | ||
int test_device = 0; | ||
CUdevice cu_test_device; | ||
CUcontext cu_test_ctx; | ||
int guard_device = 0; | ||
int current_device; | ||
CUdevice cu_current_device; | ||
CUcontext cu_current_ctx; | ||
int count = 1; | ||
|
||
EXPECT_EQ(cuInitChecked(), true); | ||
EXPECT_EQ(cudaGetDeviceCount(&count), cudaSuccess); | ||
if (count > 1) { | ||
guard_device = 1; | ||
} | ||
|
||
EXPECT_EQ(cuDeviceGet(&cu_test_device, test_device), CUDA_SUCCESS); | ||
EXPECT_EQ(cuCtxCreate(&cu_test_ctx, 0, cu_test_device), CUDA_SUCCESS); | ||
EXPECT_EQ(cuCtxSetCurrent(cu_test_ctx), CUDA_SUCCESS); | ||
EXPECT_EQ(cuCtxGetCurrent(&cu_current_ctx), CUDA_SUCCESS); | ||
EXPECT_EQ(cuCtxGetDevice(&cu_current_device), CUDA_SUCCESS); | ||
EXPECT_EQ(cu_current_ctx, cu_test_ctx); | ||
EXPECT_EQ(cu_current_device, cu_test_device); | ||
{ | ||
DeviceGuard g(guard_device); | ||
EXPECT_EQ(cudaGetDevice(¤t_device), cudaSuccess); | ||
EXPECT_EQ(current_device, guard_device); | ||
EXPECT_EQ(cuCtxGetCurrent(&cu_current_ctx), CUDA_SUCCESS); | ||
EXPECT_NE(cu_current_ctx, cu_test_ctx); | ||
} | ||
EXPECT_EQ(cuCtxGetCurrent(&cu_current_ctx), CUDA_SUCCESS); | ||
EXPECT_EQ(cuCtxGetDevice(&cu_current_device), CUDA_SUCCESS); | ||
EXPECT_EQ(cu_current_ctx, cu_test_ctx); | ||
EXPECT_EQ(cu_current_device, cu_test_device); | ||
cuCtxDestroy(cu_test_ctx); | ||
} | ||
|
||
TEST(DeviceGuard, CheckcontextNoArgs) { | ||
int test_device = 0; | ||
CUdevice cu_test_device; | ||
CUcontext cu_test_ctx; | ||
int guard_device = 0; | ||
int current_device; | ||
CUdevice cu_current_device; | ||
CUcontext cu_current_ctx; | ||
int count = 1; | ||
|
||
EXPECT_EQ(cuInitChecked(), true); | ||
EXPECT_EQ(cudaGetDeviceCount(&count), cudaSuccess); | ||
if (count > 1) { | ||
guard_device = 1; | ||
} | ||
|
||
EXPECT_EQ(cuDeviceGet(&cu_test_device, test_device), CUDA_SUCCESS); | ||
EXPECT_EQ(cuCtxCreate(&cu_test_ctx, 0, cu_test_device), CUDA_SUCCESS); | ||
EXPECT_EQ(cuCtxSetCurrent(cu_test_ctx), CUDA_SUCCESS); | ||
EXPECT_EQ(cuCtxGetCurrent(&cu_current_ctx), CUDA_SUCCESS); | ||
EXPECT_EQ(cuCtxGetDevice(&cu_current_device), CUDA_SUCCESS); | ||
EXPECT_EQ(cu_current_ctx, cu_test_ctx); | ||
EXPECT_EQ(cu_current_device, cu_test_device); | ||
{ | ||
DeviceGuard g; | ||
EXPECT_EQ(cudaSetDevice(guard_device), cudaSuccess); | ||
EXPECT_EQ(cudaGetDevice(¤t_device), cudaSuccess); | ||
EXPECT_EQ(cuCtxGetCurrent(&cu_current_ctx), CUDA_SUCCESS); | ||
EXPECT_NE(cu_current_ctx, cu_test_ctx); | ||
} | ||
EXPECT_EQ(cuCtxGetCurrent(&cu_current_ctx), CUDA_SUCCESS); | ||
EXPECT_EQ(cuCtxGetDevice(&cu_current_device), CUDA_SUCCESS); | ||
EXPECT_EQ(cu_current_ctx, cu_test_ctx); | ||
EXPECT_EQ(cu_current_device, cu_test_device); | ||
cuCtxDestroy(cu_test_ctx); | ||
} | ||
|
||
} // namespace dali |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.