schwz  Generated automatically from develop
device_guard.hpp (5a15602)
1 
2 /*******************************<SCHWARZ LIB LICENSE>***********************
3 Copyright (c) 2019, the SCHWARZ LIB authors
4 All rights reserved.
5 
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
8 are met:
9 
10 1. Redistributions of source code must retain the above copyright
11 notice, this list of conditions and the following disclaimer.
12 
13 2. Redistributions in binary form must reproduce the above copyright
14 notice, this list of conditions and the following disclaimer in the
15 documentation and/or other materials provided with the distribution.
16 
17 3. Neither the name of the copyright holder nor the names of its
18 contributors may be used to endorse or promote products derived from
19 this software without specific prior written permission.
20 
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
22 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
24 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 ******************************<SCHWARZ LIB LICENSE>*************************/
33 
34 
35 #ifndef SCHWARZ_DEVICE_GUARD_HPP_
36 #define SCHWARZ_DEVICE_GUARD_HPP_
37 
38 
39 #include <exception>
40 
41 #include <schwarz/config.hpp>
42 
43 #if SCHW_HAVE_CUDA
44 #include <cuda_runtime.h>
45 #endif
46 
47 
48 #include <exception_helpers.hpp>
49 
50 
51 namespace schwz {
52 
53 
64 class device_guard {
65 public:
66  device_guard(int device_id)
67  {
68 #if SCHW_HAVE_CUDA
69  SCHWARZ_ASSERT_NO_CUDA_ERRORS(cudaGetDevice(&original_device_id));
70  SCHWARZ_ASSERT_NO_CUDA_ERRORS(cudaSetDevice(device_id));
71 #endif
72  }
73 
74  device_guard(device_guard &other) = delete;
75 
76  device_guard &operator=(const device_guard &other) = delete;
77 
78  device_guard(device_guard &&other) = delete;
79 
80  device_guard const &operator=(device_guard &&other) = delete;
81 
82  ~device_guard() noexcept(false)
83  {
84  /* Ignore the error during stack unwinding for this call */
85  if (std::uncaught_exception()) {
86 #if SCHW_HAVE_CUDA
87  cudaSetDevice(original_device_id);
88 #endif
89  } else {
90 #if SCHW_HAVE_CUDA
91  SCHWARZ_ASSERT_NO_CUDA_ERRORS(cudaSetDevice(original_device_id));
92 #endif
93  }
94  }
95 
96 private:
97  int original_device_id{};
98 };
99 
100 
101 } // namespace schwz
102 
103 
104 #endif // SCHWARZ_DEVICE_GUARD_HPP_
This class defines a device guard for the cuda functions and the cuda module.
Definition: device_guard.hpp:64
The Schwarz wrappers namespace.
Definition: comm_helpers.hpp:49