engine.h 9.89 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 "clocks.h"
38
#include "collectgroup.h"
39
#include "cooling_struct.h"
40
#include "gravity_properties.h"
Matthieu Schaller's avatar
Matthieu Schaller committed
41
#include "parser.h"
42
#include "partition.h"
43
#include "potential.h"
Matthieu Schaller's avatar
Matthieu Schaller committed
44
#include "runner.h"
45
#include "scheduler.h"
Tom Theuns's avatar
Tom Theuns committed
46
#include "sourceterms_struct.h"
Matthieu Schaller's avatar
Matthieu Schaller committed
47
48
49
#include "space.h"
#include "task.h"
#include "units.h"
Pedro Gonnet's avatar
Pedro Gonnet committed
50

51
52
53
/**
 * @brief The different policies the #engine can follow.
 */
54
55
enum engine_policy {
  engine_policy_none = 0,
56
57
58
59
  engine_policy_rand = (1 << 0),
  engine_policy_steal = (1 << 1),
  engine_policy_keep = (1 << 2),
  engine_policy_block = (1 << 3),
60
61
62
63
64
65
66
67
  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),
68
69
70
71
  engine_policy_reconstruct_mpoles = (1 << 12),
  engine_policy_cooling = (1 << 13),
  engine_policy_sourceterms = (1 << 14),
  engine_policy_stars = (1 << 15)
72
};
73
#define engine_maxpolicy 15
74
extern const char *engine_policy_names[];
Pedro Gonnet's avatar
Pedro Gonnet committed
75

76
77
78
79
80
81
82
83
84
85
86
87
88
/**
 * @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),
  engine_step_prop_snapshot = (1 << 4)
};

/* Some constants */
89
#define engine_maxproxies 64
90
#define engine_tasksreweight 1
91
#define engine_parts_size_grow 1.05
92
#define engine_redistribute_alloc_margin 1.2
93
94
#define engine_default_energy_file_name "energy"
#define engine_default_timesteps_file_name "timesteps"
95
#define engine_max_parts_per_ghost 1000
Pedro Gonnet's avatar
Pedro Gonnet committed
96

97
98
99
/**
 * @brief The rank of the engine as a global variable (for messages).
 */
100
101
extern int engine_rank;

Pedro Gonnet's avatar
Pedro Gonnet committed
102
103
104
/* Data structure for the engine. */
struct engine {

105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
  /* 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;

120
121
122
  /* Common threadpool for all the engine's tasks. */
  struct threadpool threadpool;

123
  /* The minimum and maximum allowed dt */
124
  double dt_min, dt_max;
125

126
  /* Time of the simulation beginning */
127
  double timeBegin;
128
129

  /* Time of the simulation end */
130
  double timeEnd;
131
132

  /* The previous system time. */
133
  double timeOld;
134
  integertime_t ti_old;
135
136

  /* The current system time. */
137
  double time;
138
  integertime_t ti_current;
139

140
141
142
  /* The highest active bin at this time */
  timebin_t max_active_bin;

143
  /* Time step */
144
  double timeStep;
145

146
147
  /* Time base */
  double timeBase;
148
  double timeBase_inv;
149

150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
  /* 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 */
169
  integertime_t ti_end_min;
170

171
  /* Maximal overall ti_end for the next time-step */
Matthieu Schaller's avatar
Matthieu Schaller committed
172
173
  integertime_t ti_end_max;

174
  /* Maximal overall ti_beg for the next time-step */
Matthieu Schaller's avatar
Matthieu Schaller committed
175
176
  integertime_t ti_beg_max;

177
  /* Number of particles updated in the previous step */
178
  size_t updates, g_updates, s_updates;
179

180
  /* Properties of the previous step */
181
  int step_props;
182

183
  /* Total numbers of particles in the system. */
184
  long long total_nr_parts, total_nr_gparts;
185

186
  /* The internal system of units */
187
  const struct unit_system *internal_units;
188

189
  /* Snapshot information */
190
191
  double timeFirstSnapshot;
  double deltaTimeSnapshot;
192
  integertime_t ti_nextSnapshot;
193
  char snapshotBaseName[PARSER_MAX_LINE_SIZE];
194
  int snapshotCompression;
195
  struct unit_system *snapshotUnits;
196

197
  /* Statistics information */
198
  FILE *file_stats;
199
200
  double timeLastStatistics;
  double deltaTimeStatistics;
Pedro Gonnet's avatar
Pedro Gonnet committed
201

Matthieu Schaller's avatar
Matthieu Schaller committed
202
203
204
  /* Timesteps information */
  FILE *file_timesteps;

205
  /* The current step number. */
206
  int step;
207
208
209
210
211

  /* The number of particles updated in the previous step. */
  int count_step;

  /* Data for the threads' barrier. */
212
213
  swift_barrier_t wait_barrier;
  swift_barrier_t run_barrier;
214
215
216
217
218
219
220
221

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

222
#ifdef SWIFT_DEBUG_TASKS
223
224
  /* Tic/toc at the start/end of a step. */
  ticks tic_step, toc_step;
225
#endif
226

227
#ifdef WITH_MPI
228
229
  /* CPU time of the last step. */
  double cputime_last_step;
230

231
232
  /* Step of last repartition. */
  int last_repartition;
233
234
#endif

235
236
  /* Wallclock time of the last time-step */
  float wallclock_time;
237
238

  /* Force the engine to rebuild? */
239
  int forcerebuild;
240
241
242

  /* Force the engine to repartition ? */
  int forcerepart;
243
  struct repartition *reparttype;
244

245
246
247
  /* Need to dump some statistics ? */
  int save_stats;

248
249
  /* Need to dump a snapshot ? */
  int dump_snapshot;
250
251
252
253
254
255

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

  /* Linked list for cell-task association. */
  struct link *links;
256
  int nr_links, size_links;
257

258
259
260
261
  /* Average number of tasks per cell. Used to estimate the sizes
   * of the various task arrays. */
  int tasks_per_cell;

262
263
  /* Are we talkative ? */
  int verbose;
264
265

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

268
269
270
  /* Properties of the hydro scheme */
  const struct hydro_props *hydro_properties;

271
272
273
  /* Properties of the self-gravity scheme */
  const struct gravity_props *gravity_properties;

274
  /* Properties of external gravitational potential */
Matthieu Schaller's avatar
Matthieu Schaller committed
275
  const struct external_potential *external_potential;
276

277
  /* Properties of the cooling scheme */
Tom Theuns's avatar
Tom Theuns committed
278
  const struct cooling_function_data *cooling_func;
279

Tom Theuns's avatar
Tom Theuns committed
280
  /* Properties of source terms */
Tom Theuns's avatar
Tom Theuns committed
281
  struct sourceterms *sourceterms;
Tom Theuns's avatar
Tom Theuns committed
282

283
284
  /* The (parsed) parameter file */
  const struct swift_params *parameter_file;
285
286
287
288

  /* 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;
289
};
Pedro Gonnet's avatar
Pedro Gonnet committed
290
291

/* Function prototypes. */
Pedro Gonnet's avatar
Pedro Gonnet committed
292
void engine_barrier(struct engine *e);
293
void engine_compute_next_snapshot_time(struct engine *e);
294
295
void engine_unskip(struct engine *e);
void engine_drift_all(struct engine *e);
296
void engine_drift_top_multipoles(struct engine *e);
297
void engine_reconstruct_multipoles(struct engine *e);
298
void engine_print_stats(struct engine *e);
299
void engine_dump_snapshot(struct engine *e);
300
301
void engine_init(struct engine *e, struct space *s,
                 const struct swift_params *params, int nr_nodes, int nodeID,
Matthieu Schaller's avatar
Matthieu Schaller committed
302
303
                 int nr_threads, long long Ngas, long long Ndm, int with_aff,
                 int policy, int verbose, struct repartition *reparttype,
304
                 const struct unit_system *internal_units,
305
                 const struct phys_const *physical_constants,
306
                 const struct hydro_props *hydro,
307
                 const struct gravity_props *gravity,
Tom Theuns's avatar
Tom Theuns committed
308
                 const struct external_potential *potential,
309
                 const struct cooling_function_data *cooling_func,
Tom Theuns's avatar
Tom Theuns committed
310
                 struct sourceterms *sourceterms);
311
void engine_launch(struct engine *e);
312
void engine_prepare(struct engine *e);
313
314
void engine_init_particles(struct engine *e, int flag_entropy_ICs,
                           int clean_h_values);
315
316
void engine_step(struct engine *e);
void engine_maketasks(struct engine *e);
317
void engine_split(struct engine *e, struct partition *initial_partition);
318
319
void engine_exchange_strays(struct engine *e, size_t offset_parts,
                            int *ind_part, size_t *Npart, size_t offset_gparts,
320
321
322
                            int *ind_gpart, size_t *Ngpart,
                            size_t offset_sparts, int *ind_spart,
                            size_t *Nspart);
323
void engine_rebuild(struct engine *e, int clean_h_values);
324
void engine_repartition(struct engine *e);
325
void engine_repartition_trigger(struct engine *e);
326
327
void engine_makeproxies(struct engine *e);
void engine_redistribute(struct engine *e);
Matthieu Schaller's avatar
Matthieu Schaller committed
328
void engine_print_policy(struct engine *e);
329
int engine_is_done(struct engine *e);
Matthieu Schaller's avatar
Matthieu Schaller committed
330
void engine_pin();
331
void engine_unpin();
332
void engine_clean(struct engine *e);
333
int engine_estimate_nr_tasks(struct engine *e);
334
335

#endif /* SWIFT_ENGINE_H */