Skip to main content

Rocky Solver SDK 2025 R2

api_cuda_cfd_coupling

Last update: 16.07.2025
1// (C) 2024 ANSYS, Inc. Unauthorized use, distribution, or duplication is prohibited.
2#pragma once
3
4#include <rocky20/api/device/api_cuda.hpp>
5#include <rocky20/api/device/api_device_cfd_coupling.hpp>
6
7namespace rocky::cuda
8{
9
10template <class _Plugin= device::EmptyCustomCouplingPlugin>
11void select_and_dispatch_cfd_coupling_kernel(
12 IRockyModel& model,
13 DeviceParticleGroupData* device_group_data,
14 void** udf_devices_data
15)
16{
17 if (model.get_number_of_active_particles() <= 0)
18 return;
19
20 // It will be enough to have a data recorder
21 bool collect_cfd_data = model._impl->GetDeviceModel(0).cfd_data_recorder != nullptr;
22
23 for (int device_index = 0; device_index<model.get_number_of_devices(); device_index++)
24 {
25 void* udf_device_data = (udf_devices_data != nullptr ? udf_devices_data[device_index] : nullptr);
26 SDeviceModel* device_data = model.get_device_data(device_index);
27 const auto& group_device_data = device_group_data[device_index];
28
29 CUDA_SET_DEVICE(model.get_device_id(device_index));
30
31 unsigned int* group_particle_indexes = group_device_data.d_group_2_particles;
32 for (auto j = 0; j < group_device_data.group_indexes.size(); ++j)
33 {
34 const int particle_group_index = group_device_data.group_indexes[j];
35 device::select_and_dispatch_coupling_kernel<_Plugin>(
36 true,
37 collect_cfd_data,
38 device_data,
39 group_particle_indexes,
40 group_device_data.n_particles_per_group[j],
41 udf_device_data,
42 model._impl->cfd_data.gen_cfd,
43 model._impl->cfd_data.cfd_particle_groups[particle_group_index]);
44 group_particle_indexes += group_device_data.n_particles_per_group[j];
45 }
46 }
47
48 //Sync all GPUs
49 sync_devices(model);
50}
51
52template <typename _Op>
53#ifndef GPU_COMPILER
54inline
55#endif
56__global__ void cuda_dispatch_fluid_kernel(SDeviceModel* device_data, double current_time, _Op op, void* udf_data)
57{
58 device::api_compute_cfd_data_coupling<CudaBackend>(device_data, current_time, op, udf_data);
59}
60
61template <typename _Op>
62inline
63void dispatch_fluid_kernel(IRockyModel& model, _Op op, void** udf_devices_data)
64{
65 if (model.get_number_of_active_particles() <= 0)
66 return;
67
68 for (int i = 0; i<model.get_number_of_devices(); i++)
69 {
70 CUDA_SET_DEVICE(model.get_device_id(i));
71
72 cuda_dispatch_fluid_kernel<<<CD_NBLOCKS, CD_NTHREADS>>>(
73 model.get_device_data(i),
74 model.get_current_time(),
75 op,
76 (udf_devices_data != nullptr ? udf_devices_data[i] : nullptr)
77 );
78 }
79 //Sync all GPUs
80 sync_devices(model);
81}
82
83}
Definition rocky_api.h:58
int get_number_of_devices() const
Definition rocky_api.cpp:173
int get_number_of_active_particles() const
Definition rocky_api.cpp:153
double get_current_time() const
Definition rocky_api.cpp:143
int get_device_id(int device_index) const
Definition rocky_api.cpp:178

Connect with Ansys