Ginkgo Generated from branch based on master. Ginkgo version 1.7.0
A numerical linear algebra library targeting many-core architectures
Loading...
Searching...
No Matches
scoped_device_id_guard.hpp
1/*******************************<GINKGO LICENSE>******************************
2Copyright (c) 2017-2023, the Ginkgo authors
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions
7are met:
8
91. Redistributions of source code must retain the above copyright
10notice, this list of conditions and the following disclaimer.
11
122. Redistributions in binary form must reproduce the above copyright
13notice, this list of conditions and the following disclaimer in the
14documentation and/or other materials provided with the distribution.
15
163. Neither the name of the copyright holder nor the names of its
17contributors may be used to endorse or promote products derived from
18this software without specific prior written permission.
19
20THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31******************************<GINKGO LICENSE>*******************************/
32
33#ifndef GKO_PUBLIC_CORE_BASE_SCOPED_DEVICE_ID_GUARD_HPP_
34#define GKO_PUBLIC_CORE_BASE_SCOPED_DEVICE_ID_GUARD_HPP_
35
36
37#include <memory>
38
39
40namespace gko {
41
42
43class OmpExecutor;
44class ReferenceExecutor;
45class CudaExecutor;
46class HipExecutor;
47class DpcppExecutor;
48
49
50namespace detail {
51
52
57class generic_scoped_device_id_guard {
58public:
59 generic_scoped_device_id_guard() = default;
60
61 // TODO: this should be a purely virtual function, but somehow that leads to
62 // linker errors
63 virtual ~generic_scoped_device_id_guard() = default;
64
65 // Prohibit copy construction
66 generic_scoped_device_id_guard(
67 const generic_scoped_device_id_guard& other) = delete;
68
69 // Prohibit copy assignment
70 generic_scoped_device_id_guard& operator=(
71 const generic_scoped_device_id_guard& other) = delete;
72};
73
74
75} // namespace detail
76
77
105public:
114 scoped_device_id_guard(const ReferenceExecutor* exec, int device_id);
115
124 scoped_device_id_guard(const OmpExecutor* exec, int device_id);
125
134 scoped_device_id_guard(const CudaExecutor* exec, int device_id);
135
144 scoped_device_id_guard(const HipExecutor* exec, int device_id);
145
154 scoped_device_id_guard(const DpcppExecutor* exec, int device_id);
155
156 scoped_device_id_guard() = default;
157
158 // Prohibit copy construction.
160
161 // Allow move construction.
162 // These are needed, since C++14 does not guarantee copy elision.
164
165 // Prohibit copy assignment.
166 scoped_device_id_guard& operator=(const scoped_device_id_guard&) = delete;
167
168 // Allow move construction.
169 // These are needed, since C++14 does not guarantee copy elision.
170 scoped_device_id_guard& operator=(scoped_device_id_guard&&) = default;
171
172 ~scoped_device_id_guard() = default;
173
174private:
175 std::unique_ptr<detail::generic_scoped_device_id_guard> scope_;
176};
177
178
179} // namespace gko
180
181#endif // GKO_PUBLIC_CORE_BASE_SCOPED_DEVICE_ID_GUARD_HPP_
This is the Executor subclass which represents the CUDA device.
Definition executor.hpp:1513
This is the Executor subclass which represents a DPC++ enhanced device.
Definition executor.hpp:1925
This is the Executor subclass which represents the HIP enhanced device.
Definition executor.hpp:1727
This is the Executor subclass which represents the OpenMP device (typically CPU).
Definition executor.hpp:1366
This is a specialization of the OmpExecutor, which runs the reference implementations of the kernels ...
Definition executor.hpp:1443
This move-only class uses RAII to set the device id within a scoped block, if necessary.
Definition scoped_device_id_guard.hpp:104
scoped_device_id_guard(const DpcppExecutor *exec, int device_id)
Create a scoped device id from an DpcppExecutor.
scoped_device_id_guard(const OmpExecutor *exec, int device_id)
Create a scoped device id from an OmpExecutor.
scoped_device_id_guard(const HipExecutor *exec, int device_id)
Create a scoped device id from an HipExecutor.
scoped_device_id_guard(const CudaExecutor *exec, int device_id)
Create a scoped device id from an CudaExecutor.
scoped_device_id_guard(const ReferenceExecutor *exec, int device_id)
Create a scoped device id from an Reference.
The Ginkgo namespace.
Definition abstract_factory.hpp:48
constexpr T one()
Returns the multiplicative identity for T.
Definition math.hpp:803