-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
device_guard
anywhere a CUDA
function is called.
- Loading branch information
Showing
3 changed files
with
65 additions
and
30 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
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,34 @@ | ||
|
||
#include <cuda_runtime.h> | ||
|
||
|
||
#include <ginkgo/core/base/exception_helpers.hpp> | ||
|
||
|
||
namespace gko { | ||
|
||
|
||
class device_guard { | ||
public: | ||
device_guard(int device_id) | ||
{ | ||
GKO_ASSERT_NO_CUDA_ERRORS(cudaGetDevice(&original_device_id)); | ||
GKO_ASSERT_NO_CUDA_ERRORS(cudaSetDevice(device_id)); | ||
} | ||
|
||
~device_guard() noexcept(false) | ||
{ | ||
/* Ignore the error during stack unwinding for this call */ | ||
if (std::uncaught_exception()) { | ||
cudaSetDevice(original_device_id); | ||
} else { | ||
GKO_ASSERT_NO_CUDA_ERRORS(cudaSetDevice(original_device_id)); | ||
} | ||
} | ||
|
||
private: | ||
int original_device_id{}; | ||
}; | ||
|
||
|
||
} // namespace gko |
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