engine.h 12.9 KB
Newer Older
Pedro Gonnet's avatar
Pedro Gonnet committed
1
/*******************************************************************************
2
 * This file is part of SWIFT.
3
 * Copyright (c) 2012 Pedro Gonnet (pedro.gonnet@durham.ac.uk)
4
5
6
7
8
 *                    Matthieu Schaller (matthieu.schaller@durham.ac.uk)
 *               2015 Peter W. Draper (p.w.draper@durham.ac.uk)
 *                    Angus Lepper (angus.lepper@ed.ac.uk)
 *               2016 John A. Regan (john.a.regan@durham.ac.uk)
 *                    Tom Theuns (tom.theuns@durham.ac.uk)
9
 *
Pedro Gonnet's avatar
Pedro Gonnet committed
10
11
12
13
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
14
 *
Pedro Gonnet's avatar
Pedro Gonnet committed
15
16
17
18
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
19
 *
Pedro Gonnet's avatar
Pedro Gonnet committed
20
21
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 *
Pedro Gonnet's avatar
Pedro Gonnet committed
23
 ******************************************************************************/
24
25
#ifndef SWIFT_ENGINE_H
#define SWIFT_ENGINE_H
Pedro Gonnet's avatar
Pedro Gonnet committed
26

27
28
29
30
31
32
33
34
/* Config parameters. */
#include "../config.h"

/* MPI headers. */
#ifdef WITH_MPI
#include <mpi.h>
#endif

Pedro Gonnet's avatar
Pedro Gonnet committed
35
/* Includes. */
36
#include "barrier.h"
37
#include "chemistry_struct.h"
38
#include "clocks.h"
39
#include "collectgroup.h"
40
#include "cooling_struct.h"
41
#include "gravity_properties.h"
42
#include "mesh_gravity.h"
Matthieu Schaller's avatar
Matthieu Schaller committed
43
#include "parser.h"
44
#include "partition.h"
45
#include "potential.h"
Matthieu Schaller's avatar
Matthieu Schaller committed
46
#include "runner.h"
47
#include "scheduler.h"
Tom Theuns's avatar
Tom Theuns committed
48
#include "sourceterms_struct.h"
Matthieu Schaller's avatar
Matthieu Schaller committed
49
50
51
#include "space.h"
#include "task.h"
#include "units.h"
Pedro Gonnet's avatar
Pedro Gonnet committed
52

53
54
55
/**
 * @brief The different policies the #engine can follow.
 */
56
57
enum engine_policy {
  engine_policy_none = 0,
58
59
60
61
  engine_policy_rand = (1 << 0),
  engine_policy_steal = (1 << 1),
  engine_policy_keep = (1 << 2),
  engine_policy_block = (1 << 3),
62
63
64
65
66
67
68
69
  engine_policy_cputight = (1 << 4),
  engine_policy_mpi = (1 << 5),
  engine_policy_setaffinity = (1 << 6),
  engine_policy_hydro = (1 << 7),
  engine_policy_self_gravity = (1 << 8),
  engine_policy_external_gravity = (1 << 9),
  engine_policy_cosmology = (1 << 10),
  engine_policy_drift_all = (1 << 11),
70
71
72
  engine_policy_reconstruct_mpoles = (1 << 12),
  engine_policy_cooling = (1 << 13),
  engine_policy_sourceterms = (1 << 14),
73
74
  engine_policy_stars = (1 << 15),
  engine_policy_structure_finding = (1 << 16)
75
};
James Willis's avatar
James Willis committed
76
#define engine_maxpolicy 16
77
extern const char *engine_policy_names[];
Pedro Gonnet's avatar
Pedro Gonnet committed
78

79
80
81
82
83
84
85
86
87
/**
 * @brief The different unusual events that can take place in a time-step.
 */
enum engine_step_properties {
  engine_step_prop_none = 0,
  engine_step_prop_rebuild = (1 << 0),
  engine_step_prop_redistribute = (1 << 1),
  engine_step_prop_repartition = (1 << 2),
  engine_step_prop_statistics = (1 << 3),
88
89
  engine_step_prop_snapshot = (1 << 4),
  engine_step_prop_restarts = (1 << 5)
90
91
92
};

/* Some constants */
93
#define engine_maxproxies 64
94
#define engine_tasksreweight 1
95
#define engine_parts_size_grow 1.05
96
#define engine_redistribute_alloc_margin 1.2
97
98
#define engine_default_energy_file_name "energy"
#define engine_default_timesteps_file_name "timesteps"
99
#define engine_max_parts_per_ghost 1000
Pedro Gonnet's avatar
Pedro Gonnet committed
100

101
102
103
/**
 * @brief The rank of the engine as a global variable (for messages).
 */
104
105
extern int engine_rank;

Pedro Gonnet's avatar
Pedro Gonnet committed
106
107
108
/* Data structure for the engine. */
struct engine {

109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
  /* Number of threads on which to run. */
  int nr_threads;

  /* The space with which the runner is associated. */
  struct space *s;

  /* The runner's threads. */
  struct runner *runners;

  /* The running policy. */
  int policy;

  /* The task scheduler. */
  struct scheduler sched;

124
125
126
  /* Common threadpool for all the engine's tasks. */
  struct threadpool threadpool;

127
  /* The minimum and maximum allowed dt */
128
  double dt_min, dt_max;
129

130
131
132
133
134
135
  /* Maximum time-step allowed by the RMS condition in cosmology runs. */
  double dt_max_RMS_displacement;

  /* Dimensionless factor for the RMS time-step condition. */
  double max_RMS_displacement_factor;

136
  /* Time of the simulation beginning */
137
  double time_begin;
138
139

  /* Time of the simulation end */
140
  double time_end;
141
142

  /* The previous system time. */
143
  double time_old;
144
  integertime_t ti_old;
145
146

  /* The current system time. */
147
  double time;
148
  integertime_t ti_current;
149

150
151
152
  /* The highest active bin at this time */
  timebin_t max_active_bin;

153
154
155
  /* The lowest active bin at this time */
  timebin_t min_active_bin;

156
  /* Time step */
157
  double time_step;
158

159
  /* Time base */
160
161
  double time_base;
  double time_base_inv;
162

163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
  /* Minimal hydro ti_end for the next time-step */
  integertime_t ti_hydro_end_min;

  /* Maximal hydro ti_end for the next time-step */
  integertime_t ti_hydro_end_max;

  /* Maximal hydro ti_beg for the next time-step */
  integertime_t ti_hydro_beg_max;

  /* Minimal gravity ti_end for the next time-step */
  integertime_t ti_gravity_end_min;

  /* Maximal gravity ti_end for the next time-step */
  integertime_t ti_gravity_end_max;

  /* Maximal gravity ti_beg for the next time-step */
  integertime_t ti_gravity_beg_max;

  /* Minimal overall ti_end for the next time-step */
182
  integertime_t ti_end_min;
183

184
  /* Maximal overall ti_end for the next time-step */
Matthieu Schaller's avatar
Matthieu Schaller committed
185
186
  integertime_t ti_end_max;

187
  /* Maximal overall ti_beg for the next time-step */
Matthieu Schaller's avatar
Matthieu Schaller committed
188
189
  integertime_t ti_beg_max;

190
  /* Number of particles updated in the previous step */
191
  long long updates, g_updates, s_updates;
192

193
  /* Number of updates since the last rebuild */
194
195
196
  long long updates_since_rebuild;
  long long g_updates_since_rebuild;
  long long s_updates_since_rebuild;
197

198
  /* Properties of the previous step */
199
  int step_props;
200

201
  /* Total numbers of particles in the system. */
202
  long long total_nr_parts, total_nr_gparts, total_nr_sparts;
203

204
205
206
  /* Total mass in the simulation */
  double total_mass;

207
  /* The internal system of units */
208
  const struct unit_system *internal_units;
Matthieu Schaller's avatar
Matthieu Schaller committed
209

210
  /* Top-level cell locations for VELOCIraptor. */
211
  struct cell_loc *cell_loc;
212

213
  /* Snapshot information */
214
215
216
  double a_first_snapshot;
  double time_first_snapshot;
  double delta_time_snapshot;
Loic Hausammann's avatar
Loic Hausammann committed
217

218
219
  /* Output_List for the snapshots */
  struct output_list *output_list_snapshots;
220
221
222
223

  /* Integer time of the next snapshot */
  integertime_t ti_next_snapshot;

224
225
  char snapshot_base_name[PARSER_MAX_LINE_SIZE];
  int snapshot_compression;
226
  int snapshot_label_delta;
227
228
  struct unit_system *snapshot_units;
  int snapshot_output_count;
229

230
  /* Structure finding information */
231
  int stf_output_freq_format;
232
  double a_first_stf;
233
234
  double timeFirstSTFOutput;
  double deltaTimeSTF;
235
  int deltaStepSTF;
Matthieu Schaller's avatar
Matthieu Schaller committed
236

237
238
  /* Output_List for the structure finding */
  struct output_list *output_list_stf;
Loic Hausammann's avatar
Loic Hausammann committed
239

240
  /* Integer time of the next stf output */
241
  integertime_t ti_nextSTF;
Matthieu Schaller's avatar
Matthieu Schaller committed
242

243
  char stfBaseName[PARSER_MAX_LINE_SIZE];
244

245
  /* Statistics information */
246
247
248
249
  double a_first_statistics;
  double time_first_statistics;
  double delta_time_statistics;

250
251
  /* Output_List for the stats */
  struct output_list *output_list_stats;
Loic Hausammann's avatar
Format    
Loic Hausammann committed
252

253
254
255
256
  /* Integer time of the next statistics dump */
  integertime_t ti_next_stats;

  /* File handle for the statistics */
257
  FILE *file_stats;
Pedro Gonnet's avatar
Pedro Gonnet committed
258

259
  /* File handle for the timesteps information */
Matthieu Schaller's avatar
Matthieu Schaller committed
260
261
  FILE *file_timesteps;

262
  /* The current step number. */
263
  int step;
264
265

  /* Data for the threads' barrier. */
266
267
  swift_barrier_t wait_barrier;
  swift_barrier_t run_barrier;
268
269
270
271
272
273
274
275

  /* ID of the node this engine lives on. */
  int nr_nodes, nodeID;

  /* Proxies for the other nodes in this simulation. */
  struct proxy *proxies;
  int nr_proxies, *proxy_ind;

276
#ifdef SWIFT_DEBUG_TASKS
277
278
  /* Tic/toc at the start/end of a step. */
  ticks tic_step, toc_step;
279
#endif
280

281
#ifdef WITH_MPI
282
283
  /* CPU time of the last step. */
  double cputime_last_step;
284

285
286
  /* Step of last repartition. */
  int last_repartition;
287
288
#endif

289
290
  /* Wallclock time of the last time-step */
  float wallclock_time;
291

292
293
294
  /* Are we in the process of restaring a simulation? */
  int restarting;

295
  /* Force the engine to rebuild? */
296
  int forcerebuild;
297
298
299

  /* Force the engine to repartition ? */
  int forcerepart;
300
  struct repartition *reparttype;
301
302
303
304
305
306

  /* How many steps have we done with the same set of tasks? */
  int tasks_age;

  /* Linked list for cell-task association. */
  struct link *links;
307
  size_t nr_links, size_links;
308

309
310
  /* Average number of tasks per cell. Used to estimate the sizes
   * of the various task arrays. */
311
  size_t tasks_per_cell;
312

313
314
  /* Are we talkative ? */
  int verbose;
315
316

  /* Physical constants definition */
Matthieu Schaller's avatar
Matthieu Schaller committed
317
  const struct phys_const *physical_constants;
318

319
320
321
  /* The cosmological model */
  struct cosmology *cosmology;

322
323
324
  /* Properties of the hydro scheme */
  const struct hydro_props *hydro_properties;

325
  /* Properties of the self-gravity scheme */
326
  struct gravity_props *gravity_properties;
327

328
329
330
  /* The mesh used for long-range gravity forces */
  struct pm_mesh *mesh;

331
  /* Properties of external gravitational potential */
Matthieu Schaller's avatar
Matthieu Schaller committed
332
  const struct external_potential *external_potential;
333

334
  /* Properties of the cooling scheme */
Tom Theuns's avatar
Tom Theuns committed
335
  const struct cooling_function_data *cooling_func;
336

337
  /* Properties of the chemistry model */
338
  const struct chemistry_global_data *chemistry;
339

Tom Theuns's avatar
Tom Theuns committed
340
  /* Properties of source terms */
Tom Theuns's avatar
Tom Theuns committed
341
  struct sourceterms *sourceterms;
Tom Theuns's avatar
Tom Theuns committed
342

343
  /* The (parsed) parameter file */
lhausamm's avatar
lhausamm committed
344
  struct swift_params *parameter_file;
345
346
347
348

  /* Temporary struct to hold a group of deferable properties (in MPI mode
   * these are reduced together, but may not be required just yet). */
  struct collectgroup1 collect_group1;
Peter W. Draper's avatar
Peter W. Draper committed
349

350
351
352
  /* Whether to dump restart files. */
  int restart_dump;

353
354
355
  /* Whether to save previous generation of restart files. */
  int restart_save;

356
357
358
  /* Whether to dump restart files after the last step. */
  int restart_onexit;

Peter W. Draper's avatar
Peter W. Draper committed
359
  /* Name of the restart file. */
360
  const char *restart_file;
361
362
363
364
365
366

  /* Ticks between restart dumps. */
  ticks restart_dt;

  /* Time after which next dump will occur. */
  ticks restart_next;
367
368
369

  /* Maximum number of tasks needed for restarting. */
  int restart_max_tasks;
370
};
Pedro Gonnet's avatar
Pedro Gonnet committed
371
372

/* Function prototypes. */
373
void engine_addlink(struct engine *e, struct link **l, struct task *t);
Pedro Gonnet's avatar
Pedro Gonnet committed
374
void engine_barrier(struct engine *e);
375
void engine_compute_next_snapshot_time(struct engine *e);
376
void engine_compute_next_stf_time(struct engine *e);
377
void engine_compute_next_statistics_time(struct engine *e);
378
void engine_recompute_displacement_constraint(struct engine *e);
379
380
void engine_unskip(struct engine *e);
void engine_drift_all(struct engine *e);
381
void engine_drift_top_multipoles(struct engine *e);
382
void engine_reconstruct_multipoles(struct engine *e);
383
void engine_print_stats(struct engine *e);
384
void engine_dump_snapshot(struct engine *e);
385
void engine_init_output_lists(struct engine *e, struct swift_params *params);
Matthieu Schaller's avatar
Matthieu Schaller committed
386
387
388
void engine_init(struct engine *e, struct space *s, struct swift_params *params,
                 long long Ngas, long long Ngparts, long long Nstars,
                 int policy, int verbose, struct repartition *reparttype,
Matthieu Schaller's avatar
Matthieu Schaller committed
389
390
391
                 const struct unit_system *internal_units,
                 const struct phys_const *physical_constants,
                 struct cosmology *cosmo, const struct hydro_props *hydro,
392
                 struct gravity_props *gravity, struct pm_mesh *mesh,
Matthieu Schaller's avatar
Matthieu Schaller committed
393
394
395
396
                 const struct external_potential *potential,
                 const struct cooling_function_data *cooling_func,
                 const struct chemistry_global_data *chemistry,
                 struct sourceterms *sourceterms);
Matthieu Schaller's avatar
Matthieu Schaller committed
397
398
399
void engine_config(int restart, struct engine *e, struct swift_params *params,
                   int nr_nodes, int nodeID, int nr_threads, int with_aff,
                   int verbose, const char *restart_file);
400
void engine_launch(struct engine *e);
401
void engine_prepare(struct engine *e);
402
403
void engine_init_particles(struct engine *e, int flag_entropy_ICs,
                           int clean_h_values);
404
405
void engine_step(struct engine *e);
void engine_maketasks(struct engine *e);
406
void engine_split(struct engine *e, struct partition *initial_partition);
407
408
void engine_exchange_strays(struct engine *e, size_t offset_parts,
                            int *ind_part, size_t *Npart, size_t offset_gparts,
409
410
411
                            int *ind_gpart, size_t *Ngpart,
                            size_t offset_sparts, int *ind_spart,
                            size_t *Nspart);
412
void engine_rebuild(struct engine *e, int clean_h_values);
413
void engine_repartition(struct engine *e);
414
void engine_repartition_trigger(struct engine *e);
415
416
void engine_makeproxies(struct engine *e);
void engine_redistribute(struct engine *e);
Matthieu Schaller's avatar
Matthieu Schaller committed
417
void engine_print_policy(struct engine *e);
418
int engine_is_done(struct engine *e);
419
420
void engine_pin(void);
void engine_unpin(void);
421
void engine_clean(struct engine *e);
422
int engine_estimate_nr_tasks(struct engine *e);
423

424
#ifdef HAVE_SETAFFINITY
425
cpu_set_t *engine_entry_affinity(void);
426
427
#endif

428
429
430
/* Struct dump/restore support. */
void engine_struct_dump(struct engine *e, FILE *stream);
void engine_struct_restore(struct engine *e, FILE *stream);
431
void engine_dump_restarts(struct engine *e, int drifted_all, int final_step);
432

433
#endif /* SWIFT_ENGINE_H */