engine.h 13.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"
Loikki's avatar
Loikki committed
41
#include "dump.h"
42
#include "gravity_properties.h"
43
#include "mesh_gravity.h"
Matthieu Schaller's avatar
Matthieu Schaller committed
44
#include "parser.h"
45
#include "partition.h"
46
#include "potential.h"
Matthieu Schaller's avatar
Matthieu Schaller committed
47
#include "runner.h"
48
#include "scheduler.h"
Matthieu Schaller's avatar
Matthieu Schaller committed
49
50
51
#include "space.h"
#include "task.h"
#include "units.h"
52
#include "velociraptor_interface.h"
Pedro Gonnet's avatar
Pedro Gonnet committed
53

54
55
56
/**
 * @brief The different policies the #engine can follow.
 */
57
58
enum engine_policy {
  engine_policy_none = 0,
59
60
61
62
  engine_policy_rand = (1 << 0),
  engine_policy_steal = (1 << 1),
  engine_policy_keep = (1 << 2),
  engine_policy_block = (1 << 3),
63
64
65
66
67
68
69
70
  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),
71
  engine_policy_reconstruct_mpoles = (1 << 12),
72
73
74
75
76
  engine_policy_temperature = (1 << 13),
  engine_policy_cooling = (1 << 14),
  engine_policy_stars = (1 << 15),
  engine_policy_structure_finding = (1 << 16),
  engine_policy_star_formation = (1 << 17),
77
78
  engine_policy_feedback = (1 << 18),
  engine_policy_limiter = (1 << 19)
79
};
80
#define engine_maxpolicy 20
Loic Hausammann's avatar
Loic Hausammann committed
81
extern const char *engine_policy_names[engine_maxpolicy + 1];
Pedro Gonnet's avatar
Pedro Gonnet committed
82

83
84
85
86
87
88
89
90
91
/**
 * @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),
92
  engine_step_prop_snapshot = (1 << 4),
93
94
  engine_step_prop_restarts = (1 << 5),
  engine_step_prop_logger_index = (1 << 6)
95
96
97
};

/* Some constants */
98
#define engine_maxproxies 64
99
#define engine_tasksreweight 1
100
#define engine_parts_size_grow 1.05
101
#define engine_max_proxy_centre_frac 0.2
102
#define engine_redistribute_alloc_margin 1.2
103
104
#define engine_default_energy_file_name "energy"
#define engine_default_timesteps_file_name "timesteps"
105
#define engine_max_parts_per_ghost 1000
Loic Hausammann's avatar
Loic Hausammann committed
106
#define engine_max_sparts_per_ghost 1000
Pedro Gonnet's avatar
Pedro Gonnet committed
107

108
109
110
/**
 * @brief The rank of the engine as a global variable (for messages).
 */
111
112
extern int engine_rank;

Pedro Gonnet's avatar
Pedro Gonnet committed
113
114
115
/* Data structure for the engine. */
struct engine {

116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
  /* 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;

131
132
133
  /* Common threadpool for all the engine's tasks. */
  struct threadpool threadpool;

134
  /* The minimum and maximum allowed dt */
135
  double dt_min, dt_max;
136

137
138
139
140
141
142
  /* 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;

143
  /* Time of the simulation beginning */
144
  double time_begin;
145
146

  /* Time of the simulation end */
147
  double time_end;
148
149

  /* The previous system time. */
150
  double time_old;
151
  integertime_t ti_old;
152
153

  /* The current system time. */
154
  double time;
155
  integertime_t ti_current;
156

157
158
159
  /* The highest active bin at this time */
  timebin_t max_active_bin;

160
161
162
  /* The lowest active bin at this time */
  timebin_t min_active_bin;

163
  /* Time step */
164
  double time_step;
165

166
  /* Time base */
167
168
  double time_base;
  double time_base_inv;
169

170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
  /* 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 */
189
  integertime_t ti_end_min;
190

191
  /* Maximal overall ti_end for the next time-step */
Matthieu Schaller's avatar
Matthieu Schaller committed
192
193
  integertime_t ti_end_max;

194
  /* Maximal overall ti_beg for the next time-step */
Matthieu Schaller's avatar
Matthieu Schaller committed
195
196
  integertime_t ti_beg_max;

197
  /* Number of particles updated in the previous step */
198
  long long updates, g_updates, s_updates;
199

200
  /* Number of updates since the last rebuild */
201
202
203
  long long updates_since_rebuild;
  long long g_updates_since_rebuild;
  long long s_updates_since_rebuild;
204

205
  /* Properties of the previous step */
206
  int step_props;
207

208
  /* Total numbers of particles in the system. */
209
  long long total_nr_parts, total_nr_gparts, total_nr_sparts;
210

211
  /* The total number of inhibted particles in the system. */
212
  long long nr_inhibited_parts, nr_inhibited_gparts, nr_inhibited_sparts;
213

Matthieu Schaller's avatar
Matthieu Schaller committed
214
215
216
217
218
219
#ifdef SWIFT_DEBUG_CHECKS
  /* Total number of particles removed from the system since the last rebuild */
  long long count_inhibited_parts, count_inhibited_gparts,
      count_inhibited_sparts;
#endif

220
221
222
  /* Total mass in the simulation */
  double total_mass;

223
  /* The internal system of units */
224
  const struct unit_system *internal_units;
Matthieu Schaller's avatar
Matthieu Schaller committed
225

226
  /* Top-level cell locations for VELOCIraptor. */
227
  struct cell_loc *cell_loc;
228

229
  /* Snapshot information */
230
231
232
  double a_first_snapshot;
  double time_first_snapshot;
  double delta_time_snapshot;
Loic Hausammann's avatar
Loic Hausammann committed
233

234
235
  /* Output_List for the snapshots */
  struct output_list *output_list_snapshots;
236
237
238
239

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

240
241
  char snapshot_base_name[PARSER_MAX_LINE_SIZE];
  int snapshot_compression;
242
  int snapshot_int_time_label_on;
243
244
  struct unit_system *snapshot_units;
  int snapshot_output_count;
245

246
  /* Structure finding information */
247
  enum io_stf_output_format stf_output_freq_format;
248
249
250
251
  int delta_step_stf;
  double a_first_stf_output;
  double time_first_stf_output;
  double delta_time_stf;
Matthieu Schaller's avatar
Matthieu Schaller committed
252

253
254
  /* Output_List for the structure finding */
  struct output_list *output_list_stf;
Loic Hausammann's avatar
Loic Hausammann committed
255

256
  /* Integer time of the next stf output */
257
  integertime_t ti_next_stf;
Matthieu Schaller's avatar
Matthieu Schaller committed
258

259
  char stfBaseName[PARSER_MAX_LINE_SIZE];
260

261
  /* Statistics information */
262
263
264
265
  double a_first_statistics;
  double time_first_statistics;
  double delta_time_statistics;

266
267
  /* Output_List for the stats */
  struct output_list *output_list_stats;
Loic Hausammann's avatar
Format    
Loic Hausammann committed
268

269
270
271
272
  /* Integer time of the next statistics dump */
  integertime_t ti_next_stats;

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

275
  /* File handle for the timesteps information */
Matthieu Schaller's avatar
Matthieu Schaller committed
276
277
  FILE *file_timesteps;

278
  /* The current step number. */
279
  int step;
280
281

  /* Data for the threads' barrier. */
282
283
  swift_barrier_t wait_barrier;
  swift_barrier_t run_barrier;
284
285
286
287
288
289
290
291

  /* 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;

292
#ifdef SWIFT_DEBUG_TASKS
293
294
  /* Tic/toc at the start/end of a step. */
  ticks tic_step, toc_step;
295
#endif
296

297
#ifdef WITH_MPI
298
299
  /* CPU time of the last step. */
  double cputime_last_step;
300

301
302
  /* Step of last repartition. */
  int last_repartition;
303
304
#endif

305
306
  /* Wallclock time of the last time-step */
  float wallclock_time;
307

308
309
310
  /* Are we in the process of restaring a simulation? */
  int restarting;

311
  /* Force the engine to rebuild? */
312
  int forcerebuild;
313
314
315

  /* Force the engine to repartition ? */
  int forcerepart;
316
  struct repartition *reparttype;
317

lhausamm's avatar
lhausamm committed
318
#ifdef WITH_LOGGER
Loic Hausammann's avatar
Loic Hausammann committed
319
  struct logger *logger;
lhausamm's avatar
lhausamm committed
320
321
#endif

322
323
324
325
326
  /* How many steps have we done with the same set of tasks? */
  int tasks_age;

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

329
330
  /* Average number of tasks per cell. Used to estimate the sizes
   * of the various task arrays. */
331
  size_t tasks_per_cell;
332

333
334
  /* Are we talkative ? */
  int verbose;
335
336

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

339
340
341
  /* The cosmological model */
  struct cosmology *cosmology;

342
343
344
  /* Properties of the hydro scheme */
  const struct hydro_props *hydro_properties;

Loic Hausammann's avatar
Loic Hausammann committed
345
346
347
  /* Properties of the star model */
  const struct stars_props *stars_properties;

348
  /* Properties of the self-gravity scheme */
349
  struct gravity_props *gravity_properties;
350

351
352
353
  /* The mesh used for long-range gravity forces */
  struct pm_mesh *mesh;

354
  /* Properties of external gravitational potential */
Matthieu Schaller's avatar
Matthieu Schaller committed
355
  const struct external_potential *external_potential;
356

357
  /* Properties of the cooling scheme */
Alexei Borissov's avatar
Alexei Borissov committed
358
  struct cooling_function_data *cooling_func;
359

360
  /* Properties of the chemistry model */
361
  const struct chemistry_global_data *chemistry;
362

363
  /* The (parsed) parameter file */
lhausamm's avatar
lhausamm committed
364
  struct swift_params *parameter_file;
365
366
367
368

  /* 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
369

370
371
372
  /* Whether to dump restart files. */
  int restart_dump;

373
374
375
  /* Whether to save previous generation of restart files. */
  int restart_save;

376
377
378
  /* Whether to dump restart files after the last step. */
  int restart_onexit;

Peter W. Draper's avatar
Peter W. Draper committed
379
  /* Name of the restart file. */
380
  const char *restart_file;
381
382
383
384
385
386

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

  /* Time after which next dump will occur. */
  ticks restart_next;
387
388
389

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

392
/* Function prototypes, engine.c. */
393
void engine_addlink(struct engine *e, struct link **l, struct task *t);
Pedro Gonnet's avatar
Pedro Gonnet committed
394
void engine_barrier(struct engine *e);
395
void engine_compute_next_snapshot_time(struct engine *e);
396
void engine_compute_next_stf_time(struct engine *e);
397
void engine_compute_next_statistics_time(struct engine *e);
398
void engine_recompute_displacement_constraint(struct engine *e);
399
void engine_unskip(struct engine *e);
400
void engine_drift_all(struct engine *e, const int drift_mpoles);
401
void engine_drift_top_multipoles(struct engine *e);
402
void engine_reconstruct_multipoles(struct engine *e);
403
void engine_print_stats(struct engine *e);
404
void engine_check_for_dumps(struct engine *e);
405
void engine_dump_snapshot(struct engine *e);
406
void engine_init_output_lists(struct engine *e, struct swift_params *params);
Matthieu Schaller's avatar
Matthieu Schaller committed
407
408
409
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
410
411
412
                 const struct unit_system *internal_units,
                 const struct phys_const *physical_constants,
                 struct cosmology *cosmo, const struct hydro_props *hydro,
Loic Hausammann's avatar
Loic Hausammann committed
413
                 struct gravity_props *gravity, const struct stars_props *stars,
414
                 struct pm_mesh *mesh,
Matthieu Schaller's avatar
Matthieu Schaller committed
415
                 const struct external_potential *potential,
Alexei Borissov's avatar
Alexei Borissov committed
416
                 struct cooling_function_data *cooling_func,
417
                 const struct chemistry_global_data *chemistry);
Matthieu Schaller's avatar
Matthieu Schaller committed
418
419
420
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);
Loikki's avatar
Loikki committed
421
void engine_dump_index(struct engine *e);
422
void engine_launch(struct engine *e);
423
void engine_prepare(struct engine *e);
424
425
void engine_init_particles(struct engine *e, int flag_entropy_ICs,
                           int clean_h_values);
426
void engine_step(struct engine *e);
427
void engine_split(struct engine *e, struct partition *initial_partition);
428
429
430
431
432
void engine_exchange_strays(struct engine *e, const size_t offset_parts,
                            const int *ind_part, size_t *Npart,
                            const size_t offset_gparts, const int *ind_gpart,
                            size_t *Ngpart, const size_t offset_sparts,
                            const int *ind_spart, size_t *Nspart);
433
void engine_rebuild(struct engine *e, int redistributed, int clean_h_values);
434
void engine_repartition(struct engine *e);
435
void engine_repartition_trigger(struct engine *e);
436
437
void engine_makeproxies(struct engine *e);
void engine_redistribute(struct engine *e);
Matthieu Schaller's avatar
Matthieu Schaller committed
438
void engine_print_policy(struct engine *e);
439
int engine_is_done(struct engine *e);
440
441
void engine_pin(void);
void engine_unpin(void);
442
void engine_clean(struct engine *e);
443
int engine_estimate_nr_tasks(struct engine *e);
444

Pedro Gonnet's avatar
Pedro Gonnet committed
445
/* Function prototypes, engine_maketasks.c. */
446
447
void engine_maketasks(struct engine *e);

Pedro Gonnet's avatar
Pedro Gonnet committed
448
449
450
/* Function prototypes, engine_marktasks.c. */
int engine_marktasks(struct engine *e);

451
#ifdef HAVE_SETAFFINITY
452
cpu_set_t *engine_entry_affinity(void);
453
454
#endif

455
456
457
/* Struct dump/restore support. */
void engine_struct_dump(struct engine *e, FILE *stream);
void engine_struct_restore(struct engine *e, FILE *stream);
458
void engine_dump_restarts(struct engine *e, int drifted_all, int final_step);
459

460
#endif /* SWIFT_ENGINE_H */