Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
device_select.cpp
Go to the documentation of this file.
1// -------------------------------------------------------//
2//
3// SHAMROCK code for hydrodynamics
4// Copyright (c) 2021-2026 Timothée David--Cléris <tim.shamrock@proton.me>
5// SPDX-License-Identifier: CeCILL Free Software License Agreement v2.1
6// Shamrock is licensed under the CeCILL 2.1 License, see LICENSE for more information
7//
8// -------------------------------------------------------//
9
20#include "shambackends/sycl.hpp"
25
26shamsys::DeviceSelectRet_t init_queues_auto(std::string search_key) {
27
28 StackEntry stack_loc{false};
29
31
32 std::optional<u32> local_id = shamcomm::node_local_rank();
33
34 if (local_id) {
35
36 u32 valid_dev_cnt = 0;
37
38 shamsys::for_each_device([&](u32 key_global,
39 const sycl::platform &plat,
40 const sycl::device &dev) {
41 if (shambase::contain_substr(plat.get_info<sycl::info::platform::name>(), search_key)) {
42 valid_dev_cnt++;
43 }
44 });
45
46 u32 valid_dev_id = 0;
47
48 shamsys::for_each_device([&](u32 key_global,
49 const sycl::platform &plat,
50 const sycl::device &dev) {
51 if (shambase::contain_substr(plat.get_info<sycl::info::platform::name>(), search_key)) {
52
53 if ((*local_id) % valid_dev_cnt == valid_dev_id) {
54 shamlog_debug_sycl_ln(
55 "Sys",
56 "create queue :\n",
57 "Local ID :",
58 *local_id,
59 "\n Queue id :",
60 key_global);
61
62 auto PlatformName = plat.get_info<sycl::info::platform::name>();
63 auto DeviceName = dev.get_info<sycl::info::device::name>();
64 shamlog_debug_sycl_ln(
65 "NodeInstance",
66 "init alt queue : ",
67 "|",
68 DeviceName,
69 "|",
70 PlatformName,
71 "|",
72 shambase::getDevice_type(dev),
73 "|");
74
75 ret.device_alt = std::make_shared<sham::Device>(
76 sham::sycl_dev_to_sham_dev(key_global, dev));
77
78 shamlog_debug_sycl_ln(
79 "NodeInstance",
80 "init comp queue : ",
81 "|",
82 DeviceName,
83 "|",
84 PlatformName,
85 "|",
86 shambase::getDevice_type(dev),
87 "|");
88 ret.device_compute = std::make_shared<sham::Device>(
89 sham::sycl_dev_to_sham_dev(key_global, dev));
90 }
91
92 valid_dev_id++;
93 }
94 });
95
96 } else {
97 logger::err_ln("Sys", "cannot query local rank cannot use autodetect");
99 "cannot query local rank cannot use autodetect");
100 }
101
102 return ret;
103}
104
105shamsys::DeviceSelectRet_t init_queues(u32 alt_id, u32 compute_id) {
106
107 StackEntry stack_loc{false};
108
110
112 [&](u32 key_global, const sycl::platform &plat, const sycl::device &dev) {});
113
114 if (alt_id >= cnt_dev) {
116 "the alt queue id is larger than the number of queue");
117 }
118
119 if (compute_id >= cnt_dev) {
121 "the compute queue id is larger than the number of queue");
122 }
123
125 [&](u32 key_global, const sycl::platform &plat, const sycl::device &dev) {
126 auto PlatformName = plat.get_info<sycl::info::platform::name>();
127 auto DeviceName = dev.get_info<sycl::info::device::name>();
128
129 if (key_global == alt_id) {
130 shamlog_debug_sycl_ln(
131 "NodeInstance",
132 "init alt queue : ",
133 "|",
134 DeviceName,
135 "|",
136 PlatformName,
137 "|",
139 "|");
140 ret.device_alt
141 = std::make_shared<sham::Device>(sham::sycl_dev_to_sham_dev(key_global, dev));
142 }
143
144 if (key_global == compute_id) {
145 shamlog_debug_sycl_ln(
146 "NodeInstance",
147 "init comp queue : ",
148 "|",
149 DeviceName,
150 "|",
151 PlatformName,
152 "|",
154 "|");
155 ret.device_compute
156 = std::make_shared<sham::Device>(sham::sycl_dev_to_sham_dev(key_global, dev));
157 }
158 });
159
160 return ret;
161}
162namespace shamsys {
163
176 DeviceSelectRet_t select_devices(std::string sycl_cfg) {
177
178 if (shambase::contain_substr(sycl_cfg, "auto:")) {
179
180 std::string search = sycl_cfg.substr(5);
181 return init_queues_auto(search);
182
183 } else {
184
185 size_t split_alt_comp = 0;
186 split_alt_comp = sycl_cfg.find(":");
187
188 if (split_alt_comp == std::string::npos) {
189 logger::err_ln("NodeInstance", "sycl-cfg layout should be x:x");
190 shambase::throw_with_loc<std::runtime_error>("sycl-cfg layout should be x:x");
191 }
192
193 std::string alt_cfg = sycl_cfg.substr(0, split_alt_comp);
194 std::string comp_cfg = sycl_cfg.substr(split_alt_comp + 1, sycl_cfg.length());
195
196 u32 ialt, icomp;
197 try {
198 try {
199 ialt = std::stoi(alt_cfg);
200 } catch (const std::invalid_argument &a) {
201 logger::err_ln("NodeInstance", "alt config is not an int");
202 shambase::throw_with_loc<std::runtime_error>("alt config is not an int");
203 }
204 } catch (const std::out_of_range &a) {
205 logger::err_ln("NodeInstance", "alt config is to big for an integer");
206 shambase::throw_with_loc<std::runtime_error>("alt config is to big for an integer");
207 }
208
209 try {
210 try {
211 icomp = std::stoi(comp_cfg);
212 } catch (const std::invalid_argument &a) {
213 logger::err_ln("NodeInstance", "compute config is not an int");
214 shambase::throw_with_loc<std::runtime_error>("compute config is not an int");
215 }
216 } catch (const std::out_of_range &a) {
217 logger::err_ln("NodeInstance", "compute config is to big for an integer");
219 "compute config is to big for an integer");
220 }
221
222 return init_queues(ialt, icomp);
223 }
224 }
225
226} // namespace shamsys
std::uint32_t u32
32 bit unsigned integer
This header file contains utility functions related to exception handling in the code.
Functions related to the MPI communicator.
Device sycl_dev_to_sham_dev(usize i, const sycl::device &dev)
Convert a SYCL device to a shamrock backend device.
Definition Device.cpp:453
void throw_with_loc(std::string message, SourceLocation loc=SourceLocation{})
Throw an exception and append the source location to it.
std::string getDevice_type(const sycl::device &Device)
Get the Device Type Name.
bool contain_substr(std::string str, std::string what)
Check if a substring is present in a given string.
Definition string.hpp:258
namespace for the system handling
DeviceSelectRet_t select_devices(std::string sycl_cfg)
Select the devices for the queues.
u32 for_each_device(std::function< void(u32, const sycl::platform &, const sycl::device &)> fct)
Iterate over all SYCL devices and perform a given function.
This file contains the definition for the stacktrace related functionality.