Skip to main content

Rocky Solver SDK 2025 R2

api_cuda_sph

Last update: 16.07.2025
1// (C) 2024 ANSYS, Inc. Unauthorized use, distribution, or duplication is prohibited.
2#pragma once
3
4// Includes ---------------------------------------------------------------------------------------
5#include <rocky20/api/device/api_cuda.hpp>
6#include <rocky20/api/sph/api_device_sph.hpp>
7#include <rocky20/sph/sph_model.h>
8
9namespace rocky::cuda {
10
11template <typename Operator, typename Functor>
12__global__ void execute_sph_particles_function(
13 SDeviceModel* device_data,
14 SPHDeviceModel* sph_device,
15 double current_time,
16 Functor sph_particles_f,
17 void* udf_device_data)
18{
19 device::launch_sph_particles_function<CudaBackend, Operator>(
20 device_data, sph_device, current_time, sph_particles_f, udf_device_data);
21}
22
23template <typename Operator, typename Functor, class SPHNeighborsHandler>
24__global__ void execute_sph_interactions_function(
25 SDeviceModel* device_data,
26 SPHDeviceModel* sph_device,
27 double current_time,
28 Functor sph_particle_interaction_f,
29 void* udf_device_data,
30 IterationType it_type)
31{
32 // SPH -> SPH
33 SPHNeighborsHandler handler { device_data, sph_device, (float) current_time, it_type };
34 device::launch_sph_interactions_particles<CudaBackend, Operator, Functor>(
35 device_data,
36 sph_device,
37 current_time,
38 sph_particle_interaction_f,
39 udf_device_data,
40 handler);
41}
42
43template <typename Operator, typename Functor>
44__global__ void execute_sph_interactions_triangle_function(
45 SDeviceModel* device_data,
46 SPHDeviceModel* sph_device,
47 double current_time,
48 Functor sph_triangle_interaction_f,
49 void* udf_device_data)
50{
51 // SPH -> triangles
52 device::launch_sph_interactions_triangles<CudaBackend, Operator>(
53 device_data, sph_device, current_time, sph_triangle_interaction_f, udf_device_data);
54}
55
56// Dispatchers ------------------------------------------------------------------------------------
57template <typename Operator = device::DefaultOperator<CudaBackend>, typename Functor>
58void dispatch_sph_elements_kernel(
59 IRockyModel& model, Functor& sph_particles_f, void** udf_devices_data)
60{
61 SPHModel* sph_model = &(*model._impl->sph_model);
62 cuda_host& cu_hos = *model._impl;
63
64 if (sph_model->gen_sph.nprt_active <= 0)
65 return;
66
67 void* udf_device_data = nullptr;
68
69 // Get the number of active particles to launch kernel
70 std::vector<int> n_active_particles(model.get_number_of_devices(), 0);
71 #pragma omp parallel for num_threads(model.get_number_of_devices()) if (model._impl->gpu_parallel)
72 for (int i = 0; i < model.get_number_of_devices(); ++i)
73 {
74 cuda_general_sph gen_sph_tmp;
75 CUDA_SET_DEVICE(model.get_device_id(i));
76 CUDA_COPY_D2H(&gen_sph_tmp, sph_model->get_device_general(i), 1);
77 n_active_particles[i] = gen_sph_tmp.nprt_active;
78 }
79
80 // Launching user kernel forces
81 for (int i = 0; i < model.get_number_of_devices(); ++i)
82 {
83 CUDA_SET_DEVICE(model.get_device_id(i));
84
85 SPHDeviceModel* sph_device = model._impl->sph_model->get_device_data(i);
86 SDeviceModel* device_data = model.get_device_data(i);
87 if (udf_devices_data != nullptr)
88 {
89 udf_device_data = udf_devices_data[i];
90 }
91
92 auto [blocks, threads] = CudaBackend::get_launch_params(n_active_particles[i]);
93 rocky::cuda::execute_sph_particles_function<Operator><<<blocks, threads>>>(
94 device_data,
95 sph_device,
96 cu_hos.iteration.current_time,
97 sph_particles_f,
98 udf_device_data);
99 }
100
101 // Syncing all GPUs
102 sync_devices(model);
103}
104
105template <typename Operator = device::DefaultElementsOperator<CudaBackend>, typename Functor>
106void dispatch_sph_interactions_elements_kernel(
107 IRockyModel& model,
108 Functor& sph_particle_interaction_f,
109 void** udf_devices_data,
110 IterationType it_type = itAll)
111{
112 SPHModel* sph_model = &(*model._impl->sph_model);
113 cuda_host& cu_hos = *model._impl;
114
115 if (sph_model->gen_sph.nprt_active <= 0)
116 return;
117
118 void* udf_device_data = nullptr;
119
120 // Get the number of interacting particle x triangle pairs to launch kernel
121 std::vector<size_t> n_particle_interactions(model.get_number_of_devices(), 0);
122 if (sph_model->gen_sph.use_particles_neighbors_list)
123 {
124 #pragma omp parallel for num_threads(model.get_number_of_devices()) if (model._impl->gpu_parallel)
125 for (int i = 0; i < model.get_number_of_devices(); ++i)
126 {
127 cuda_general_sph gen_sph_tmp;
128 CUDA_SET_DEVICE(model.get_device_id(i));
129 CUDA_COPY_D2H(&gen_sph_tmp, sph_model->get_device_general(i), 1);
130 if (it_type == itFreeAndFreeCoupled)
131 n_particle_interactions[i] =
132 gen_sph_tmp.ncont_ff_p_int + gen_sph_tmp.ncont_fc_p_int;
133 else if (it_type == itFreeCoupledOnly)
134 n_particle_interactions[i] = gen_sph_tmp.ncont_fc_p_int;
135 else // itAll
136 n_particle_interactions[i] = gen_sph_tmp.ncont_ff_p_int + gen_sph_tmp.ncont_fc_p_int
137 + gen_sph_tmp.ncont_cc_p_int;
138 }
139 }
140
141 // Launching user kernel forces
142 for (int i = 0; i < model.get_number_of_devices(); ++i)
143 {
144 CUDA_SET_DEVICE(model.get_device_id(i));
145
146 if (udf_devices_data != nullptr)
147 {
148 udf_device_data = udf_devices_data[i];
149 }
150
151 const auto [blocks, threads] = CudaBackend::get_launch_params(
152 sph_model->gen_sph.use_particles_neighbors_list
153 ? n_particle_interactions[i]
154 : model._impl->sph_model->gen_sph.nprt_active / model.get_number_of_devices());
155
156 // Dispatch the SPH elements interaction function using the SPH Iterators.
157 if (cu_hos.sph_model->gen_sph.use_particles_neighbors_list)
158 {
159 if (cu_hos.gen.periodic_domain.enabled())
160 {
161 rocky::cuda::execute_sph_interactions_function<
162 Operator,
163 decltype(sph_particle_interaction_f),
164 SPHExplicitHandler<CudaBackend, true>><<<blocks, threads>>>(
165 cu_hos.GetDeviceData(i),
166 cu_hos.sph_model->get_device_data(i),
167 cu_hos.iteration.current_time,
168 sph_particle_interaction_f,
169 udf_device_data,
170 it_type);
171 }
172 else
173 {
174 rocky::cuda::execute_sph_interactions_function<
175 Operator,
176 decltype(sph_particle_interaction_f),
177 SPHExplicitHandler<CudaBackend>><<<blocks, threads>>>(
178 cu_hos.GetDeviceData(i),
179 cu_hos.sph_model->get_device_data(i),
180 cu_hos.iteration.current_time,
181 sph_particle_interaction_f,
182 udf_device_data,
183 it_type);
184 }
185 }
186 else
187 {
188 if (cu_hos.gen.nprt_active > 0)
189 {
190 if (cu_hos.gen.periodic_domain.enabled())
191 {
192 rocky::cuda::execute_sph_interactions_function<
193 Operator,
194 decltype(sph_particle_interaction_f),
195 SPHApplyHandler<CudaBackend, true, true>><<<blocks, threads>>>(
196 cu_hos.GetDeviceData(i),
197 cu_hos.sph_model->get_device_data(i),
198 cu_hos.iteration.current_time,
199 sph_particle_interaction_f,
200 udf_device_data,
201 it_type);
202 if (i < model._impl->get_n_gpus() - 1)
203 {
204 rocky::cuda::execute_sph_interactions_function<
205 Operator,
206 decltype(sph_particle_interaction_f),
207 SPHApplyHandler<CudaBackend, true, true, true>>
208 <<<sph::iterator::BLOCKS, sph::iterator::THREADS>>>(
209 model._impl->GetDeviceData(i),
210 model._impl->sph_model->get_device_data(i),
211 cu_hos.iteration.current_time,
212 sph_particle_interaction_f,
213 udf_device_data,
214 it_type);
215 }
216 }
217 else
218 {
219 rocky::cuda::execute_sph_interactions_function<
220 Operator,
221 decltype(sph_particle_interaction_f),
222 SPHApplyHandler<CudaBackend, true>><<<blocks, threads>>>(
223 cu_hos.GetDeviceData(i),
224 cu_hos.sph_model->get_device_data(i),
225 cu_hos.iteration.current_time,
226 sph_particle_interaction_f,
227 udf_device_data,
228 it_type);
229 if (i < model._impl->get_n_gpus() - 1)
230 {
231 rocky::cuda::execute_sph_interactions_function<
232 Operator,
233 decltype(sph_particle_interaction_f),
234 SPHApplyHandler<CudaBackend, true, false, true>>
235 <<<sph::iterator::BLOCKS, sph::iterator::THREADS>>>(
236 model._impl->GetDeviceData(i),
237 model._impl->sph_model->get_device_data(i),
238 cu_hos.iteration.current_time,
239 sph_particle_interaction_f,
240 udf_device_data,
241 it_type);
242 }
243 }
244 }
245 else
246 {
247 if (cu_hos.gen.periodic_domain.enabled())
248 {
249 rocky::cuda::execute_sph_interactions_function<
250 Operator,
251 decltype(sph_particle_interaction_f),
252 SPHApplyHandler<CudaBackend, false, true>><<<blocks, threads>>>(
253 cu_hos.GetDeviceData(i),
254 cu_hos.sph_model->get_device_data(i),
255 cu_hos.iteration.current_time,
256 sph_particle_interaction_f,
257 udf_device_data,
258 it_type);
259 if (i < model._impl->get_n_gpus() - 1)
260 {
261 rocky::cuda::execute_sph_interactions_function<
262 Operator,
263 decltype(sph_particle_interaction_f),
264 SPHApplyHandler<CudaBackend, false, true, true>>
265 <<<sph::iterator::BLOCKS, sph::iterator::THREADS>>>(
266 model._impl->GetDeviceData(i),
267 model._impl->sph_model->get_device_data(i),
268 cu_hos.iteration.current_time,
269 sph_particle_interaction_f,
270 udf_device_data,
271 it_type);
272 }
273 }
274 else
275 {
276 rocky::cuda::execute_sph_interactions_function<
277 Operator,
278 decltype(sph_particle_interaction_f),
279 SPHApplyHandler<CudaBackend, false, false>><<<blocks, threads>>>(
280 cu_hos.GetDeviceData(i),
281 cu_hos.sph_model->get_device_data(i),
282 cu_hos.iteration.current_time,
283 sph_particle_interaction_f,
284 udf_device_data,
285 it_type);
286 if (i < model._impl->get_n_gpus() - 1)
287 {
288 rocky::cuda::execute_sph_interactions_function<
289 Operator,
290 decltype(sph_particle_interaction_f),
291 SPHApplyHandler<CudaBackend, false, false, true>>
292 <<<sph::iterator::BLOCKS, sph::iterator::THREADS>>>(
293 model._impl->GetDeviceData(i),
294 model._impl->sph_model->get_device_data(i),
295 cu_hos.iteration.current_time,
296 sph_particle_interaction_f,
297 udf_device_data,
298 it_type);
299 }
300 }
301 }
302 }
303 }
304
305 // Syncing all GPUs
306 sync_devices(model);
307}
308
309template <typename Operator = device::DefaultTrianglesOperator<CudaBackend>, typename Functor>
310void dispatch_sph_interactions_triangles_kernel(
311 IRockyModel& model, Functor& sph_triangle_interaction_f, void** udf_devices_data)
312{
313 SPHModel* sph_model = &(*model._impl->sph_model);
314 cuda_host& cu_hos = *model._impl;
315
316 if (sph_model->gen_sph.nprt_active <= 0)
317 return;
318
319 void* udf_device_data = nullptr;
320
321 // Get the number of interacting particle x triangle pairs to launch kernel
322 std::vector<int> n_triangle_force_pairs(model.get_number_of_devices(), 0);
323 #pragma omp parallel for num_threads(model.get_number_of_devices()) if (model._impl->gpu_parallel)
324 for (int i = 0; i < model.get_number_of_devices(); ++i)
325 {
326 cuda_general_sph gen_sph_tmp;
327 CUDA_SET_DEVICE(model.get_device_id(i));
328 CUDA_COPY_D2H(&gen_sph_tmp, sph_model->get_device_general(i), 1);
329 n_triangle_force_pairs[i] = gen_sph_tmp.n_triangle_force_pairs;
330 }
331
332 // Launching user kernel forces
333 for (int i = 0; i < model.get_number_of_devices(); ++i)
334 {
335 CUDA_SET_DEVICE(model.get_device_id(i));
336
337 SPHDeviceModel* sph_device = model._impl->sph_model->get_device_data(i);
338 SDeviceModel* device_data = model.get_device_data(i);
339 if (udf_devices_data != nullptr)
340 {
341 udf_device_data = udf_devices_data[i];
342 }
343
344 auto [blocks, threads] = CudaBackend::get_launch_params(n_triangle_force_pairs[i]);
345 rocky::cuda::execute_sph_interactions_triangle_function<Operator>
346 <<<blocks, threads>>>(
347 device_data,
348 sph_device,
349 cu_hos.iteration.current_time,
350 sph_triangle_interaction_f,
351 udf_device_data);
352 }
353
354 // Syncing all GPUs
355 sync_devices(model);
356}
357
358} // namespace rocky::cuda
Definition rocky_api.h:58
int get_number_of_devices() const
Definition rocky_api.cpp:173
int get_device_id(int device_index) const
Definition rocky_api.cpp:178

Connect with Ansys