    

 ## On this page

  

 

 # api\_cuda\_sph

 Last update: 17.07.2025 

<a id="l00001" name="l00001"></a> 1\#pragma once

<a id="l00002" name="l00002"></a> 2 

<a id="l00003" name="l00003"></a> 3// Includes ---------------------------------------------------------------------------------------

<a id="l00004" name="l00004"></a> 4\#include &lt;rocky20/api/device/api\_cuda.hpp&gt;

<a id="l00005" name="l00005"></a> 5\#include &lt;rocky20/api/sph/api\_device\_sph.hpp&gt;

<a id="l00006" name="l00006"></a> 6\#include &lt;rocky20/sph/sph\_data.h&gt;

<a id="l00007" name="l00007"></a> 7 

<a id="l00008" name="l00008"></a> 8namespace rocky::cuda {

<a id="l00009" name="l00009"></a> 9 

<a id="l00010" name="l00010"></a> 10template &lt;typename Operator, typename Functor&gt;

<a id="l00011" name="l00011"></a> 11__global__ void execute_sph_particles_function(

<a id="l00012" name="l00012"></a> 12 SDeviceModel* device_data,

<a id="l00013" name="l00013"></a> 13 SPHDeviceModel* sph_device,

<a id="l00014" name="l00014"></a> 14 double current_time,

<a id="l00015" name="l00015"></a> 15 Functor sph_particles_f,

<a id="l00016" name="l00016"></a> 16 void* udf_device_data)

<a id="l00017" name="l00017"></a> 17{

<a id="l00018" name="l00018"></a> 18 device::launch_sph_particles_function&lt;CudaBackend, Operator&gt;(

<a id="l00019" name="l00019"></a> 19 device_data, sph_device, current_time, sph_particles_f, udf_device_data);

<a id="l00020" name="l00020"></a> 20}

<a id="l00021" name="l00021"></a> 21 

<a id="l00022" name="l00022"></a> 22template &lt;typename Operator, typename Functor&gt;

<a id="l00023" name="l00023"></a> 23__global__ void execute_sph_interactions_function(

<a id="l00024" name="l00024"></a> 24 SDeviceModel* device_data,

<a id="l00025" name="l00025"></a> 25 SPHDeviceModel* sph_device,

<a id="l00026" name="l00026"></a> 26 double current_time,

<a id="l00027" name="l00027"></a> 27 Functor sph_particle_interaction_f,

<a id="l00028" name="l00028"></a> 28 void* udf_device_data,

<a id="l00029" name="l00029"></a> 29 IterationType it_type)

<a id="l00030" name="l00030"></a> 30{

<a id="l00031" name="l00031"></a> 31 // SPH -&gt; SPH

<a id="l00032" name="l00032"></a> 32 device::launch_sph_interactions_particles&lt;CudaBackend, Operator&gt;(

<a id="l00033" name="l00033"></a> 33 device_data,

<a id="l00034" name="l00034"></a> 34 sph_device,

<a id="l00035" name="l00035"></a> 35 current_time,

<a id="l00036" name="l00036"></a> 36 sph_particle_interaction_f,

<a id="l00037" name="l00037"></a> 37 udf_device_data,

<a id="l00038" name="l00038"></a> 38 it_type);

<a id="l00039" name="l00039"></a> 39}

<a id="l00040" name="l00040"></a> 40 

<a id="l00041" name="l00041"></a> 41template &lt;typename Operator, typename Functor&gt;

<a id="l00042" name="l00042"></a> 42__global__ void execute_sph_interactions_triangle_function(

<a id="l00043" name="l00043"></a> 43 SDeviceModel* device_data,

<a id="l00044" name="l00044"></a> 44 SPHDeviceModel* sph_device,

<a id="l00045" name="l00045"></a> 45 double current_time,

<a id="l00046" name="l00046"></a> 46 Functor sph_triangle_interaction_f,

<a id="l00047" name="l00047"></a> 47 void* udf_device_data)

<a id="l00048" name="l00048"></a> 48{

<a id="l00049" name="l00049"></a> 49 // SPH -&gt; triangles

<a id="l00050" name="l00050"></a> 50 device::launch_sph_interactions_triangles&lt;CudaBackend, Operator&gt;(

<a id="l00051" name="l00051"></a> 51 device_data, sph_device, current_time, sph_triangle_interaction_f, udf_device_data);

<a id="l00052" name="l00052"></a> 52}

<a id="l00053" name="l00053"></a> 53 

<a id="l00054" name="l00054"></a> 54// Dispatchers ------------------------------------------------------------------------------------

<a id="l00055" name="l00055"></a> 55template &lt;typename Operator = device::DefaultOperator&lt;CudaBackend&gt;, typename Functor&gt;

<a id="l00056" name="l00056"></a> 56void dispatch_sph_elements_kernel(

<a id="l00057" name="l00057"></a> 57 [IRockyModel](structIRockyModel.xhtml)&amp; model, Functor&amp; sph_particles_f, void** udf_devices_data)

<a id="l00058" name="l00058"></a> 58{

<a id="l00059" name="l00059"></a> 59 SPHModel* sph_model = &amp;(*model._impl-&gt;sph_model);

<a id="l00060" name="l00060"></a> 60 cuda_host&amp; cu_hos = *model._impl;

<a id="l00061" name="l00061"></a> 61 

<a id="l00062" name="l00062"></a> 62 if (sph_model-&gt;gen_sph.nprt_active &lt;= 0)

<a id="l00063" name="l00063"></a> 63 return;

<a id="l00064" name="l00064"></a> 64 

<a id="l00065" name="l00065"></a> 65 void* udf_device_data = nullptr;

<a id="l00066" name="l00066"></a> 66 

<a id="l00067" name="l00067"></a> 67 // Launching user kernel forces

<a id="l00068" name="l00068"></a> 68 for (int i = 0; i &lt; model.[get\_number\_of\_devices](structIRockyModel.xhtml#a1fd1011aa809fe64647e9cacf1956a11)(); ++i)

<a id="l00069" name="l00069"></a> 69 {

<a id="l00070" name="l00070"></a> 70 CUDA_SET_DEVICE(model.[get\_device\_id](structIRockyModel.xhtml#ae8f8865c809a31988291f9180d7cda2b)(i));

<a id="l00071" name="l00071"></a> 71 

<a id="l00072" name="l00072"></a> 72 SPHDeviceModel* sph_device = model._impl-&gt;sph_model-&gt;get_device_data(i);

<a id="l00073" name="l00073"></a> 73 SDeviceModel* device_data = model.get_device_data(i);

<a id="l00074" name="l00074"></a> 74 if (udf_devices_data != nullptr)

<a id="l00075" name="l00075"></a> 75 {

<a id="l00076" name="l00076"></a> 76 udf_device_data = udf_devices_data[i];

<a id="l00077" name="l00077"></a> 77 }

<a id="l00078" name="l00078"></a> 78 rocky::cuda::execute_sph_particles_function&lt;Operator&gt;&lt;&lt;&lt;CD_NBLOCKS, CD_NTHREADS&gt;&gt;&gt;(

<a id="l00079" name="l00079"></a> 79 device_data,

<a id="l00080" name="l00080"></a> 80 sph_device,

<a id="l00081" name="l00081"></a> 81 cu_hos.iteration.current_time,

<a id="l00082" name="l00082"></a> 82 sph_particles_f,

<a id="l00083" name="l00083"></a> 83 udf_device_data);

<a id="l00084" name="l00084"></a> 84 }

<a id="l00085" name="l00085"></a> 85 

<a id="l00086" name="l00086"></a> 86 // Syncing all GPUs

<a id="l00087" name="l00087"></a> 87 sync_devices(model);

<a id="l00088" name="l00088"></a> 88}

<a id="l00089" name="l00089"></a> 89 

<a id="l00090" name="l00090"></a> 90template &lt;typename Operator = device::DefaultElementsOperator&lt;CudaBackend&gt;, typename Functor&gt;

<a id="l00091" name="l00091"></a> 91void dispatch_sph_interactions_elements_kernel(

<a id="l00092" name="l00092"></a> 92 [IRockyModel](structIRockyModel.xhtml)&amp; model,

<a id="l00093" name="l00093"></a> 93 Functor&amp; sph_particle_interaction_f,

<a id="l00094" name="l00094"></a> 94 void** udf_devices_data,

<a id="l00095" name="l00095"></a> 95 IterationType it_type = itAll)

<a id="l00096" name="l00096"></a> 96{

<a id="l00097" name="l00097"></a> 97 SPHModel* sph_model = &amp;(*model._impl-&gt;sph_model);

<a id="l00098" name="l00098"></a> 98 cuda_host&amp; cu_hos = *model._impl;

<a id="l00099" name="l00099"></a> 99 

<a id="l00100" name="l00100"></a> 100 if (sph_model-&gt;gen_sph.nprt_active &lt;= 0)

<a id="l00101" name="l00101"></a> 101 return;

<a id="l00102" name="l00102"></a> 102 

<a id="l00103" name="l00103"></a> 103 void* udf_device_data = nullptr;

<a id="l00104" name="l00104"></a> 104 

<a id="l00105" name="l00105"></a> 105 // Launching user kernel forces

<a id="l00106" name="l00106"></a> 106 for (int i = 0; i &lt; model.[get\_number\_of\_devices](structIRockyModel.xhtml#a1fd1011aa809fe64647e9cacf1956a11)(); ++i)

<a id="l00107" name="l00107"></a> 107 {

<a id="l00108" name="l00108"></a> 108 CUDA_SET_DEVICE(model.[get\_device\_id](structIRockyModel.xhtml#ae8f8865c809a31988291f9180d7cda2b)(i));

<a id="l00109" name="l00109"></a> 109 

<a id="l00110" name="l00110"></a> 110 SPHDeviceModel* sph_device = model._impl-&gt;sph_model-&gt;get_device_data(i);

<a id="l00111" name="l00111"></a> 111 SDeviceModel* device_data = model.get_device_data(i);

<a id="l00112" name="l00112"></a> 112 if (udf_devices_data != nullptr)

<a id="l00113" name="l00113"></a> 113 {

<a id="l00114" name="l00114"></a> 114 udf_device_data = udf_devices_data[i];

<a id="l00115" name="l00115"></a> 115 }

<a id="l00116" name="l00116"></a> 116 rocky::cuda::execute_sph_interactions_function&lt;Operator&gt;&lt;&lt;&lt;CD_NBLOCKS, CD_NTHREADS&gt;&gt;&gt;(

<a id="l00117" name="l00117"></a> 117 device_data,

<a id="l00118" name="l00118"></a> 118 sph_device,

<a id="l00119" name="l00119"></a> 119 cu_hos.iteration.current_time,

<a id="l00120" name="l00120"></a> 120 sph_particle_interaction_f,

<a id="l00121" name="l00121"></a> 121 udf_device_data,

<a id="l00122" name="l00122"></a> 122 it_type);

<a id="l00123" name="l00123"></a> 123 }

<a id="l00124" name="l00124"></a> 124 

<a id="l00125" name="l00125"></a> 125 // Syncing all GPUs

<a id="l00126" name="l00126"></a> 126 sync_devices(model);

<a id="l00127" name="l00127"></a> 127}

<a id="l00128" name="l00128"></a> 128 

<a id="l00129" name="l00129"></a> 129template &lt;typename Operator = device::DefaultTrianglesOperator&lt;CudaBackend&gt;, typename Functor&gt;

<a id="l00130" name="l00130"></a> 130void dispatch_sph_interactions_triangles_kernel(

<a id="l00131" name="l00131"></a> 131 [IRockyModel](structIRockyModel.xhtml)&amp; model, Functor&amp; sph_triangle_interaction_f, void** udf_devices_data)

<a id="l00132" name="l00132"></a> 132{

<a id="l00133" name="l00133"></a> 133 SPHModel* sph_model = &amp;(*model._impl-&gt;sph_model);

<a id="l00134" name="l00134"></a> 134 cuda_host&amp; cu_hos = *model._impl;

<a id="l00135" name="l00135"></a> 135 

<a id="l00136" name="l00136"></a> 136 if (sph_model-&gt;gen_sph.nprt_active &lt;= 0)

<a id="l00137" name="l00137"></a> 137 return;

<a id="l00138" name="l00138"></a> 138 

<a id="l00139" name="l00139"></a> 139 void* udf_device_data = nullptr;

<a id="l00140" name="l00140"></a> 140 

<a id="l00141" name="l00141"></a> 141 // Launching user kernel forces

<a id="l00142" name="l00142"></a> 142 for (int i = 0; i &lt; model.[get\_number\_of\_devices](structIRockyModel.xhtml#a1fd1011aa809fe64647e9cacf1956a11)(); ++i)

<a id="l00143" name="l00143"></a> 143 {

<a id="l00144" name="l00144"></a> 144 CUDA_SET_DEVICE(model.[get\_device\_id](structIRockyModel.xhtml#ae8f8865c809a31988291f9180d7cda2b)(i));

<a id="l00145" name="l00145"></a> 145 

<a id="l00146" name="l00146"></a> 146 SPHDeviceModel* sph_device = model._impl-&gt;sph_model-&gt;get_device_data(i);

<a id="l00147" name="l00147"></a> 147 SDeviceModel* device_data = model.get_device_data(i);

<a id="l00148" name="l00148"></a> 148 if (udf_devices_data != nullptr)

<a id="l00149" name="l00149"></a> 149 {

<a id="l00150" name="l00150"></a> 150 udf_device_data = udf_devices_data[i];

<a id="l00151" name="l00151"></a> 151 }

<a id="l00152" name="l00152"></a> 152 rocky::cuda::execute_sph_interactions_triangle_function&lt;Operator&gt;

<a id="l00153" name="l00153"></a> 153 &lt;&lt;&lt;CD_NBLOCKS, CD_NTHREADS&gt;&gt;&gt;(

<a id="l00154" name="l00154"></a> 154 device_data,

<a id="l00155" name="l00155"></a> 155 sph_device,

<a id="l00156" name="l00156"></a> 156 cu_hos.iteration.current_time,

<a id="l00157" name="l00157"></a> 157 sph_triangle_interaction_f,

<a id="l00158" name="l00158"></a> 158 udf_device_data);

<a id="l00159" name="l00159"></a> 159 }

<a id="l00160" name="l00160"></a> 160 

<a id="l00161" name="l00161"></a> 161 // Syncing all GPUs

<a id="l00162" name="l00162"></a> 162 sync_devices(model);

<a id="l00163" name="l00163"></a> 163}

<a id="l00164" name="l00164"></a> 164 

<a id="l00165" name="l00165"></a> 165} // namespace rocky::cuda

[IRockyModel](structIRockyModel.xhtml)

**Definition** rocky_api.h:60



[IRockyModel::get\_number\_of\_devices](structIRockyModel.xhtml#a1fd1011aa809fe64647e9cacf1956a11)

int get_number_of_devices() const

**Definition** rocky_api.cpp:159



[IRockyModel::get\_device\_id](structIRockyModel.xhtml#ae8f8865c809a31988291f9180d7cda2b)

int get_device_id(int device_index) const

**Definition** rocky_api.cpp:164