Cholla 3.0.1-dev
Cholla - Massively parallel hydro on GPUs
Loading...
Searching...
No Matches
particles_3D.h
1#ifdef PARTICLES
2
3 #ifndef PARTICLES_H
4 #define PARTICLES_H
5
6 #include <math.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10
11 #include <cstdlib>
12 #include <map>
13 #include <string>
14
15 #include "../global/global.h"
16 #include "../gravity/grav3D.h"
17 #include "../grid/spatial_domain_props.h"
18 #include "../model/model_collection.h"
19
20 #ifdef PARTICLES_GPU
21 #include "../utils/gpu.hpp" // cudaFree
22
23 #define TPB_PARTICLES 1024
24 // #define PRINT_GPU_MEMORY
25 #define PRINT_MAX_MEMORY_USAGE
26 #endif
27
30class Particles3D
31{
32 public:
33 part_int_t n_local;
34
35 part_int_t n_total;
36 part_int_t n_total_initial;
37
38 Real dt;
39 Real t;
40 Real max_dt;
41
42 Real C_cfl;
43
44 bool INITIAL;
45
46 Real particle_mass;
47
48 #ifdef COSMOLOGY
49 Real current_z;
50 Real current_a;
51 #endif
52
53 #ifdef PARTICLES_CPU
54 #ifdef PARTICLE_IDS
55 int_vector_t partIDs;
56 #endif
57 #ifndef SINGLE_PARTICLE_MASS
58 real_vector_t mass;
59 #endif
60 #ifdef PARTICLE_AGE
61 real_vector_t age;
62 #endif
63 real_vector_t pos_x;
64 real_vector_t pos_y;
65 real_vector_t pos_z;
66 real_vector_t vel_x;
67 real_vector_t vel_y;
68 real_vector_t vel_z;
69 real_vector_t grav_x;
70 real_vector_t grav_y;
71 real_vector_t grav_z;
72 #endif // PARTICLES_CPU
73
74 #ifdef PARTICLES_GPU
75 part_int_t particles_array_size;
76 #ifdef PARTICLE_IDS
77 part_int_t *partIDs_dev;
78 #endif
79 #ifdef PARTICLE_AGE
80 Real *age_dev;
81 #endif
82 Real *mass_dev;
83 Real *pos_x_dev;
84 Real *pos_y_dev;
85 Real *pos_z_dev;
86 Real *vel_x_dev;
87 Real *vel_y_dev;
88 Real *vel_z_dev;
89 Real *grav_x_dev;
90 Real *grav_y_dev;
91 Real *grav_z_dev;
92
93 #endif // PARTICLES_GPU
94
95 #ifdef MPI_CHOLLA
96
97 part_int_t n_transfer_x0;
98 part_int_t n_transfer_x1;
99 part_int_t n_transfer_y0;
100 part_int_t n_transfer_y1;
101 part_int_t n_transfer_z0;
102 part_int_t n_transfer_z1;
103
104 part_int_t n_send_x0;
105 part_int_t n_send_x1;
106 part_int_t n_send_y0;
107 part_int_t n_send_y1;
108 part_int_t n_send_z0;
109 part_int_t n_send_z1;
110
111 part_int_t n_recv_x0;
112 part_int_t n_recv_x1;
113 part_int_t n_recv_y0;
114 part_int_t n_recv_y1;
115 part_int_t n_recv_z0;
116 part_int_t n_recv_z1;
117
118 part_int_t n_in_buffer_x0;
119 part_int_t n_in_buffer_x1;
120 part_int_t n_in_buffer_y0;
121 part_int_t n_in_buffer_y1;
122 part_int_t n_in_buffer_z0;
123 part_int_t n_in_buffer_z1;
124
125 #ifdef PARTICLES_CPU
126 int_vector_t out_indxs_vec_x0;
127 int_vector_t out_indxs_vec_x1;
128 int_vector_t out_indxs_vec_y0;
129 int_vector_t out_indxs_vec_y1;
130 int_vector_t out_indxs_vec_z0;
131 int_vector_t out_indxs_vec_z1;
132 #endif // PARTICLES_CPU
133
134 #endif // MPI_CHOLLA
135
136 bool TRANSFER_DENSITY_BOUNDARIES;
137 bool TRANSFER_PARTICLES_BOUNDARIES;
138
139 struct Grid {
140 int nx_local, ny_local, nz_local;
141 int nx_total, ny_total, nz_total;
142
143 Real xMin, yMin, zMin;
144 Real xMax, yMax, zMax;
145 Real dx, dy, dz;
146
147 Real domainMin_x, domainMax_x;
148 Real domainMin_y, domainMax_y;
149 Real domainMin_z, domainMax_z;
150
151 int boundary_type_x0, boundary_type_x1;
152 int boundary_type_y0, boundary_type_y1;
153 int boundary_type_z0, boundary_type_z1;
154
155 int n_ghost_particles_grid;
156 int n_cells;
157 #ifdef PARTICLES_GPU
158 Real gpu_allocation_factor;
159 part_int_t size_blocks_array;
160 int n_cells_potential;
161 #endif
162
163 Real *density;
164 #ifdef PARTICLES_CPU
165 Real *gravity_x;
166 Real *gravity_y;
167 Real *gravity_z;
168 #ifdef GRAVITY_GPU
169 Real *density_dev;
170 #endif
171 #endif
172
173 #ifdef PARTICLES_GPU
174 Real *density_dev;
175 Real *potential_dev;
176 Real *gravity_x_dev;
177 Real *gravity_y_dev;
178 Real *gravity_z_dev;
179 Real *dti_array_dev;
180 Real *dti_array_host;
181
182 #ifdef MPI_CHOLLA
183 bool *transfer_particles_flags_d;
184 int *transfer_particles_indices_d;
185 int *replace_particles_indices_d;
186 int *transfer_particles_prefix_sum_d;
187 int *transfer_particles_prefix_sum_blocks_d;
188 int *n_transfer_d;
189 int *n_transfer_h;
190
191 int send_buffer_size_x0;
192 int send_buffer_size_x1;
193 int send_buffer_size_y0;
194 int send_buffer_size_y1;
195 int send_buffer_size_z0;
196 int send_buffer_size_z1;
197 Real *send_buffer_x0_d;
198 Real *send_buffer_x1_d;
199 Real *send_buffer_y0_d;
200 Real *send_buffer_y1_d;
201 Real *send_buffer_z0_d;
202 Real *send_buffer_z1_d;
203
204 int recv_buffer_size_x0;
205 int recv_buffer_size_x1;
206 int recv_buffer_size_y0;
207 int recv_buffer_size_y1;
208 int recv_buffer_size_z0;
209 int recv_buffer_size_z1;
210 Real *recv_buffer_x0_d;
211 Real *recv_buffer_x1_d;
212 Real *recv_buffer_y0_d;
213 Real *recv_buffer_y1_d;
214 Real *recv_buffer_z0_d;
215 Real *recv_buffer_z1_d;
216
217 #endif // MPI_CHOLLA
218
219 #endif // PARTICLES_GPU
220
221 } G;
222
228 Particles3D(void);
229
232 void Initialize(Parameters *P, const SpatialDomainProps &spatial_props, Real xbound, Real ybound, Real zbound,
233 Real xdglobal, Real ydglobal, Real zdglobal, const ModelCollection &model_collection);
234
235 void Allocate_Particles_Grid_Field_Real(Real **array_dev, int size);
236 void Free_GPU_Array_Real(Real *array);
237
238 #ifdef PARTICLES_GPU
239
240 void Free_GPU_Array_int(int *array);
241 void Free_GPU_Array_bool(bool *array);
242 template <typename T>
243 void Free_GPU_Array(T *array)
244 {
245 cudaFree(array);
246 } // TODO remove the Free_GPU_Array_<type> functions
247 void Allocate_Memory_GPU();
248 void Allocate_Particles_GPU_Array_Real(Real **array_dev, part_int_t size);
249 void Allocate_Particles_GPU_Array_bool(bool **array_dev, part_int_t size);
250 void Allocate_Particles_GPU_Array_int(int **array_dev, part_int_t size);
251 void Allocate_Particles_GPU_Array_Part_Int(part_int_t **array_dev, part_int_t size);
252 void Copy_Particles_Array_Real_Host_to_Device(Real *array_host, Real *array_dev, part_int_t size);
253 void Copy_Particles_Array_Real_Device_to_Host(Real *array_dev, Real *array_host, part_int_t size);
254 void Copy_Particles_Array_Int_Host_to_Device(part_int_t *array_host, part_int_t *array_dev, part_int_t size);
255 void Copy_Particles_Array_Int_Device_to_Host(part_int_t *array_dev, part_int_t *array_host, part_int_t size);
256 void Set_Particles_Array_Real(Real value, Real *array_dev, part_int_t size);
257 void Free_Memory_GPU();
258 void Initialize_Grid_Values_GPU();
259 void Get_Density_CIC_GPU();
260 void Get_Density_CIC_GPU_function(part_int_t n_local, Real particle_mass, Real xMin, Real xMax, Real yMin, Real yMax,
261 Real zMin, Real zMax, Real dx, Real dy, Real dz, int nx_local, int ny_local,
262 int nz_local, int n_ghost_particles_grid, int n_cells, Real *density_h,
263 Real *density_dev, Real *pos_x_dev, Real *pos_y_dev, Real *pos_z_dev,
264 Real *mass_dev);
265 void Clear_Density_GPU();
266 void Clear_Density_GPU_function(Real *density_dev, int n_cells);
267 void Copy_Potential_To_GPU(Real *potential_host, Real *potential_dev, int n_cells_potential);
268 void Get_Gravity_Field_Particles_GPU(Real *potential_host);
269 void Get_Gravity_Field_Particles_GPU_function(int nx_local, int ny_local, int nz_local, int n_ghost_particles_grid,
270 int n_cells_potential, Real dx, Real dy, Real dz, Real *potential_host,
271 Real *potential_dev, Real *gravity_x_dev, Real *gravity_y_dev,
272 Real *gravity_z_dev);
273 void Get_Gravity_CIC_GPU();
274 void Get_Gravity_CIC_GPU_function(part_int_t n_local, int nx_local, int ny_local, int nz_local,
275 int n_ghost_particles_grid, Real xMin, Real xMax, Real yMin, Real yMax, Real zMin,
276 Real zMax, Real dx, Real dy, Real dz, Real *pos_x_dev, Real *pos_y_dev,
277 Real *pos_z_dev, Real *grav_x_dev, Real *grav_y_dev, Real *grav_z_dev,
278 Real *gravity_x_dev, Real *gravity_y_dev, Real *gravity_z_dev);
279 Real Calc_Particles_dt_GPU_function(int ngrid, part_int_t n_local, Real dx, Real dy, Real dz, Real *vel_x_dev,
280 Real *vel_y_dev, Real *vel_z_dev, Real *dti_array_host, Real *dti_array_dev);
281 void Advance_Particles_KDK_Step1_GPU_function(part_int_t n_local, Real dt, Real *pos_x_dev, Real *pos_y_dev,
282 Real *pos_z_dev, Real *vel_x_dev, Real *vel_y_dev, Real *vel_z_dev,
283 Real *grav_x_dev, Real *grav_y_dev, Real *grav_z_dev);
284 void Advance_Particles_KDK_Step1_Cosmo_GPU_function(part_int_t n_local, Real delta_a, Real *pos_x_dev,
285 Real *pos_y_dev, Real *pos_z_dev, Real *vel_x_dev,
286 Real *vel_y_dev, Real *vel_z_dev, Real *grav_x_dev,
287 Real *grav_y_dev, Real *grav_z_dev, Real current_a, Real H0,
288 Real cosmo_h, Real Omega_M, Real Omega_L, Real Omega_K,
289 Real Omega_R, Real w0, Real wa);
290 void Advance_Particles_KDK_Step2_GPU_function(part_int_t n_local, Real dt, Real *vel_x_dev, Real *vel_y_dev,
291 Real *vel_z_dev, Real *grav_x_dev, Real *grav_y_dev, Real *grav_z_dev);
292 void Advance_Particles_KDK_Step2_Cosmo_GPU_function(part_int_t n_local, Real delta_a, Real *vel_x_dev,
293 Real *vel_y_dev, Real *vel_z_dev, Real *grav_x_dev,
294 Real *grav_y_dev, Real *grav_z_dev, Real current_a, Real H0,
295 Real cosmo_h, Real Omega_M, Real Omega_L, Real Omega_K,
296 Real Omega_R, Real w0, Real wa);
297 part_int_t Compute_Particles_GPU_Array_Size(part_int_t n);
298 int Select_Particles_to_Transfer_GPU(int direction, int side);
299 void Copy_Transfer_Particles_to_Buffer_GPU(int n_transfer, int direction, int side, Real *send_buffer,
300 int buffer_length);
301 void Replace_Tranfered_Particles_GPU(int n_transfer);
302 void Unload_Particles_from_Buffer_GPU(int direction, int side, Real *recv_buffer_h, int n_recv);
303 void Copy_Transfer_Particles_from_Buffer_GPU(int n_recv, Real *recv_buffer_d);
304 void Set_Particles_Open_Boundary_GPU(int dir, int side);
305 #ifdef PRINT_MAX_MEMORY_USAGE
306 void Print_Max_Memory_Usage();
307 #endif
308
309 #endif // PARTICLES_GPU
310
311 void Allocate_Memory();
312
313 void Initialize_Grid_Values();
314
315 // It's unfortunate that we need to pass model_collection to each initializer. But,
316 // short of storing ModelCollection in a global variable, I'm not entirely sure that
317 // there is a whole lot to be done
318
319 void Initialize_Sphere(struct Parameters *P, const ModelCollection &model_collection);
320
321 void Initialize_Stellar_Clusters_Helper_(std::map<std::string, real_vector_t> &real_props,
322 std::map<std::string, int_vector_t> &int_props);
323
324 void Initialize_Isolated_Stellar_Cluster(struct Parameters *P, const ModelCollection &model_collection);
325
326 void Initialize_Disk_Stellar_Clusters(struct Parameters *P, const ModelCollection &model_collection);
327
328 void Initialize_Zeldovich_Pancake(struct Parameters *P, const ModelCollection &model_collection);
329
330 void Initialize_Adiabatic_Expansion(struct Parameters *P, const ModelCollection &model_collection);
331
332 void Load_Particles_Data(struct Parameters *P);
333
334 void Free_Memory();
335
336 void Reset();
337
338 void Clear_Density();
339
340 void Get_Density_CIC_Serial();
341
342 #ifdef HDF5
343 void Load_Particles_Data_HDF5(hid_t file_id, int nfile, struct Parameters *P);
344 #endif
345
346 #ifdef PARALLEL_OMP
347 void Get_Density_CIC_OMP();
348 #endif
349
350 void Get_Density_CIC();
351
352 #ifdef MPI_CHOLLA
353 void Clear_Particles_For_Transfer(void);
354 void Select_Particles_to_Transfer_All(int *flags);
355 void Add_Particle_To_Buffer(Real *buffer, part_int_t n_in_buffer, int buffer_length, Real pId, Real pMass, Real pAge,
356 Real pPos_x, Real pPos_y, Real pPos_z, Real pVel_x, Real pVel_y, Real pVel_z);
357 void Remove_Transfered_Particles();
358
359 #ifdef PARTICLES_CPU
360 void Clear_Vectors_For_Transfers(void);
361 void Add_Particle_To_Vectors(Real pId, Real pMass, Real pAge, Real pPos_x, Real pPos_y, Real pPos_z, Real pVel_x,
362 Real pVel_y, Real pVel_z, int *flags);
363 void Select_Particles_to_Transfer_All_CPU(int *flags);
364 void Load_Particles_to_Buffer_CPU(int direction, int side, Real *send_buffer, int buffer_length);
365 void Unload_Particles_from_Buffer_CPU(int direction, int side, Real *recv_buffer, part_int_t n_recv,
366 Real *send_buffer_y0, Real *send_buffer_y1, Real *send_buffer_z0,
367 Real *send_buffer_z1, int buffer_length_y0, int buffer_length_y1,
368 int buffer_length_z0, int buffer_length_z1, int *flags);
369 #endif // PARTICLES_CPU
370
371 #ifdef PARTICLES_GPU
372 void Allocate_Memory_GPU_MPI();
373 void ReAllocate_Memory_GPU_MPI();
374 void Load_Particles_to_Buffer_GPU(int direction, int side, Real *send_buffer, int buffer_length);
375 #endif // PARTICLES_GPU
376 #endif
377};
378
379 #endif // PARTICLES_H
380#endif // PARTICLES
Holds a collection of models.
Definition model_collection.h:215
Definition global.h:217
Definition spatial_domain_props.h:14