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
15
19#include "shambase/string.hpp"
21#include "shambackends/sycl.hpp"
26
27shamsys::DeviceSelectRet_t init_queues_auto(std::string search_key) {
28
29 StackEntry stack_loc{false};
30
32
33 std::optional<u32> local_id = shamcomm::node_local_rank();
34
35 if (local_id) {
36
37 u32 valid_dev_cnt = 0;
38
39 shamsys::for_each_device([&](u32 key_global,
40 const sycl::platform &plat,
41 const sycl::device &dev) {
42 if (shambase::contain_substr(plat.get_info<sycl::info::platform::name>(), search_key)) {
43 valid_dev_cnt++;
44 }
45 });
46
47 u32 valid_dev_id = 0;
48
49 shamsys::for_each_device([&](u32 key_global,
50 const sycl::platform &plat,
51 const sycl::device &dev) {
52 if (shambase::contain_substr(plat.get_info<sycl::info::platform::name>(), search_key)) {
53
54 if ((*local_id) % valid_dev_cnt == valid_dev_id) {
55 shamlog_debug_sycl_ln(
56 "Sys",
57 "create queue :\n",
58 "Local ID :",
59 *local_id,
60 "\n Queue id :",
61 key_global);
62
63 auto PlatformName = plat.get_info<sycl::info::platform::name>();
64 auto DeviceName = dev.get_info<sycl::info::device::name>();
65 shamlog_debug_sycl_ln(
66 "NodeInstance",
67 "init alt queue : ",
68 "|",
69 DeviceName,
70 "|",
71 PlatformName,
72 "|",
74 "|");
75
76 ret.device_alt = std::make_shared<sham::Device>(
77 sham::sycl_dev_to_sham_dev(key_global, dev));
78
79 shamlog_debug_sycl_ln(
80 "NodeInstance",
81 "init comp queue : ",
82 "|",
83 DeviceName,
84 "|",
85 PlatformName,
86 "|",
88 "|");
89 ret.device_compute = std::make_shared<sham::Device>(
90 sham::sycl_dev_to_sham_dev(key_global, dev));
91 }
92
93 valid_dev_id++;
94 }
95 });
96
97 } else {
98 logger::err_ln("Sys", "cannot query local rank cannot use autodetect");
100 "cannot query local rank cannot use autodetect");
101 }
102
103 return ret;
104}
105
106shamsys::DeviceSelectRet_t init_queues(u32 alt_id, u32 compute_id) {
107
108 StackEntry stack_loc{false};
109
111
113 [&](u32 key_global, const sycl::platform &plat, const sycl::device &dev) {});
114
115 if (alt_id >= cnt_dev) {
117 "the alt queue id is larger than the number of queue");
118 }
119
120 if (compute_id >= cnt_dev) {
122 "the compute queue id is larger than the number of queue");
123 }
124
126 [&](u32 key_global, const sycl::platform &plat, const sycl::device &dev) {
127 auto PlatformName = plat.get_info<sycl::info::platform::name>();
128 auto DeviceName = dev.get_info<sycl::info::device::name>();
129
130 if (key_global == alt_id) {
131 shamlog_debug_sycl_ln(
132 "NodeInstance",
133 "init alt queue : ",
134 "|",
135 DeviceName,
136 "|",
137 PlatformName,
138 "|",
140 "|");
141 ret.device_alt
142 = std::make_shared<sham::Device>(sham::sycl_dev_to_sham_dev(key_global, dev));
143 }
144
145 if (key_global == compute_id) {
146 shamlog_debug_sycl_ln(
147 "NodeInstance",
148 "init comp queue : ",
149 "|",
150 DeviceName,
151 "|",
152 PlatformName,
153 "|",
155 "|");
156 ret.device_compute
157 = std::make_shared<sham::Device>(sham::sycl_dev_to_sham_dev(key_global, dev));
158 }
159 });
160
161 return ret;
162}
163namespace shamsys {
164
177 DeviceSelectRet_t select_devices(std::string sycl_cfg) {
178
179 if (shambase::contain_substr(sycl_cfg, "auto:")) {
180
181 std::string search = sycl_cfg.substr(5);
182 return init_queues_auto(search);
183
184 } else {
185
186 size_t split_alt_comp = 0;
187 split_alt_comp = sycl_cfg.find(":");
188
189 if (split_alt_comp == std::string::npos) {
190 logger::err_ln("NodeInstance", "sycl-cfg layout should be x:x");
191 shambase::throw_with_loc<std::runtime_error>("sycl-cfg layout should be x:x");
192 }
193
194 std::string alt_cfg = sycl_cfg.substr(0, split_alt_comp);
195 std::string comp_cfg = sycl_cfg.substr(split_alt_comp + 1, sycl_cfg.length());
196
197 u32 ialt, icomp;
198 try {
199 try {
200 ialt = std::stoi(alt_cfg);
201 } catch (const std::invalid_argument &a) {
202 logger::err_ln("NodeInstance", "alt config is not an int");
203 shambase::throw_with_loc<std::runtime_error>("alt config is not an int");
204 }
205 } catch (const std::out_of_range &a) {
206 logger::err_ln("NodeInstance", "alt config is to big for an integer");
207 shambase::throw_with_loc<std::runtime_error>("alt config is to big for an integer");
208 }
209
210 try {
211 try {
212 icomp = std::stoi(comp_cfg);
213 } catch (const std::invalid_argument &a) {
214 logger::err_ln("NodeInstance", "compute config is not an int");
215 shambase::throw_with_loc<std::runtime_error>("compute config is not an int");
216 }
217 } catch (const std::out_of_range &a) {
218 logger::err_ln("NodeInstance", "compute config is to big for an integer");
220 "compute config is to big for an integer");
221 }
222
223 return init_queues(ialt, icomp);
224 }
225 }
226
227} // 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:454
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.
ExcptTypes make_except_with_loc(std::string message, SourceLocation loc=SourceLocation{})
Create an exception with a message and a location.
bool contain_substr(std::string str, std::string what)
Check if a substring is present in a given string.
Definition string.hpp:181
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.
void err_ln(std::string module_name, Types... var2)
Prints a log message with multiple arguments followed by a newline.
Definition logs.hpp:132
This file contains the definition for the stacktrace related functionality.
shambase::details::BasicStackEntry StackEntry
Alias for shambase::details::BasicStackEntry.