Ginkgo  Generated from pipelines/1330831941 branch based on master. Ginkgo version 1.8.0
A numerical linear algebra library targeting many-core architectures
scoped_device_id_guard.hpp
1 // SPDX-FileCopyrightText: 2017 - 2024 The Ginkgo authors
2 //
3 // SPDX-License-Identifier: BSD-3-Clause
4 
5 #ifndef GKO_PUBLIC_CORE_BASE_SCOPED_DEVICE_ID_GUARD_HPP_
6 #define GKO_PUBLIC_CORE_BASE_SCOPED_DEVICE_ID_GUARD_HPP_
7 
8 
9 #include <memory>
10 
11 
12 namespace gko {
13 
14 
15 class OmpExecutor;
16 class ReferenceExecutor;
17 class CudaExecutor;
18 class HipExecutor;
19 class DpcppExecutor;
20 
21 
22 namespace detail {
23 
24 
29 class generic_scoped_device_id_guard {
30 public:
31  generic_scoped_device_id_guard() = default;
32 
33  // TODO: this should be a purely virtual function, but somehow that leads to
34  // linker errors
35  virtual ~generic_scoped_device_id_guard() = default;
36 
37  // Prohibit copy construction
38  generic_scoped_device_id_guard(
39  const generic_scoped_device_id_guard& other) = delete;
40 
41  // Prohibit copy assignment
42  generic_scoped_device_id_guard& operator=(
43  const generic_scoped_device_id_guard& other) = delete;
44 };
45 
46 
47 } // namespace detail
48 
49 
77 public:
86  scoped_device_id_guard(const ReferenceExecutor* exec, int device_id);
87 
96  scoped_device_id_guard(const OmpExecutor* exec, int device_id);
97 
106  scoped_device_id_guard(const CudaExecutor* exec, int device_id);
107 
116  scoped_device_id_guard(const HipExecutor* exec, int device_id);
117 
126  scoped_device_id_guard(const DpcppExecutor* exec, int device_id);
127 
128  scoped_device_id_guard() = default;
129 
130  // Prohibit copy construction.
132 
133  // Allow move construction.
134  // These are needed, since C++14 does not guarantee copy elision.
136 
137  // Prohibit copy assignment.
138  scoped_device_id_guard& operator=(const scoped_device_id_guard&) = delete;
139 
140  // Allow move construction.
141  // These are needed, since C++14 does not guarantee copy elision.
142  scoped_device_id_guard& operator=(scoped_device_id_guard&&) = default;
143 
144  ~scoped_device_id_guard() = default;
145 
146 private:
147  std::unique_ptr<detail::generic_scoped_device_id_guard> scope_;
148 };
149 
150 
151 } // namespace gko
152 
153 #endif // GKO_PUBLIC_CORE_BASE_SCOPED_DEVICE_ID_GUARD_HPP_
gko::scoped_device_id_guard
This move-only class uses RAII to set the device id within a scoped block, if necessary.
Definition: scoped_device_id_guard.hpp:76
gko::HipExecutor
This is the Executor subclass which represents the HIP enhanced device.
Definition: executor.hpp:1701
gko::ReferenceExecutor
This is a specialization of the OmpExecutor, which runs the reference implementations of the kernels ...
Definition: executor.hpp:1415
gko::CudaExecutor
This is the Executor subclass which represents the CUDA device.
Definition: executor.hpp:1483
gko
The Ginkgo namespace.
Definition: abstract_factory.hpp:20
gko::DpcppExecutor
This is the Executor subclass which represents a DPC++ enhanced device.
Definition: executor.hpp:1900
gko::OmpExecutor
This is the Executor subclass which represents the OpenMP device (typically CPU).
Definition: executor.hpp:1337