engine.h 7.68 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
36
/* Some standard headers. */
#include <pthread.h>
37
#include <stdio.h>
Pedro Gonnet's avatar
Pedro Gonnet committed
38
39

/* Includes. */
40
#include "clocks.h"
41
#include "cooling_struct.h"
Matthieu Schaller's avatar
Matthieu Schaller committed
42
#include "parser.h"
43
#include "partition.h"
44
#include "potential.h"
Matthieu Schaller's avatar
Matthieu Schaller committed
45
#include "runner.h"
46
#include "scheduler.h"
Tom Theuns's avatar
Tom Theuns committed
47
#include "sourceterms_struct.h"
Matthieu Schaller's avatar
Matthieu Schaller committed
48
49
50
#include "space.h"
#include "task.h"
#include "units.h"
Pedro Gonnet's avatar
Pedro Gonnet committed
51

Pedro Gonnet's avatar
Pedro Gonnet committed
52
/* Some constants. */
53
54
enum engine_policy {
  engine_policy_none = 0,
55
56
57
58
  engine_policy_rand = (1 << 0),
  engine_policy_steal = (1 << 1),
  engine_policy_keep = (1 << 2),
  engine_policy_block = (1 << 3),
59
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),
  engine_policy_cooling = (1 << 12),
68
69
  engine_policy_sourceterms = (1 << 13),
  engine_policy_stars = (1 << 14)
70
};
Pedro Gonnet's avatar
Pedro Gonnet committed
71

72
extern const char *engine_policy_names[];
Pedro Gonnet's avatar
Pedro Gonnet committed
73

74
#define engine_queue_scale 1.2
75
#define engine_maxtaskspercell 96
76
77
#define engine_maxproxies 64
#define engine_tasksreweight 10
78
#define engine_parts_size_grow 1.05
79
#define engine_redistribute_alloc_margin 1.2
80
81
#define engine_default_energy_file_name "energy"
#define engine_default_timesteps_file_name "timesteps"
Pedro Gonnet's avatar
Pedro Gonnet committed
82

83
84
85
/* The rank of the engine as a global variable (for messages). */
extern int engine_rank;

Pedro Gonnet's avatar
Pedro Gonnet committed
86
87
88
/* Data structure for the engine. */
struct engine {

89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
  /* 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;

104
105
106
  /* Common threadpool for all the engine's tasks. */
  struct threadpool threadpool;

107
  /* The minimum and maximum allowed dt */
108
  double dt_min, dt_max;
109

110
  /* Time of the simulation beginning */
111
  double timeBegin;
112
113

  /* Time of the simulation end */
114
  double timeEnd;
115
116

  /* The previous system time. */
117
  double timeOld;
118
  integertime_t ti_old;
119
120

  /* The current system time. */
121
  double time;
122
  integertime_t ti_current;
123
124

  /* Time step */
125
  double timeStep;
126

127
128
  /* Time base */
  double timeBase;
129
  double timeBase_inv;
130

131
  /* Minimal ti_end for the next time-step */
132
  integertime_t ti_end_min;
133

Matthieu Schaller's avatar
Matthieu Schaller committed
134
135
136
137
138
139
  /* Maximal ti_end for the next time-step */
  integertime_t ti_end_max;

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

140
  /* Number of particles updated */
141
  size_t updates, g_updates, s_updates;
142

143
144
145
  /* The internal system of units */
  const struct UnitSystem *internalUnits;

146
  /* Snapshot information */
147
148
  double timeFirstSnapshot;
  double deltaTimeSnapshot;
149
  integertime_t ti_nextSnapshot;
150
  char snapshotBaseName[200];
151
  int snapshotCompression;
152
  struct UnitSystem *snapshotUnits;
153

154
  /* Statistics information */
155
  FILE *file_stats;
156
157
  double timeLastStatistics;
  double deltaTimeStatistics;
Pedro Gonnet's avatar
Pedro Gonnet committed
158

Matthieu Schaller's avatar
Matthieu Schaller committed
159
160
161
  /* Timesteps information */
  FILE *file_timesteps;

162
  /* The current step number. */
163
  int step;
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179

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

  /* Data for the threads' barrier. */
  pthread_mutex_t barrier_mutex;
  pthread_cond_t barrier_cond;
  volatile int barrier_running, barrier_launch, barrier_launchcount;

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

180
181
  /* Tic/toc at the start/end of a step. */
  ticks tic_step, toc_step;
182

183
184
  /* Wallclock time of the last time-step */
  float wallclock_time;
185
186

  /* Force the engine to rebuild? */
187
  int forcerebuild;
188
189
190
191

  /* Force the engine to repartition ? */
  int forcerepart;
  enum repartition_type reparttype;
192

193
194
  /* Need to dump a snapshot ? */
  int dump_snapshot;
Matthieu Schaller's avatar
Matthieu Schaller committed
195

196
197
198
199
200
  /* How many steps have we done with the same set of tasks? */
  int tasks_age;

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

203
204
  /* Are we talkative ? */
  int verbose;
205
206

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

209
210
211
  /* Properties of the hydro scheme */
  const struct hydro_props *hydro_properties;

212
  /* Properties of external gravitational potential */
Matthieu Schaller's avatar
Matthieu Schaller committed
213
  const struct external_potential *external_potential;
214

215
  /* Properties of the cooling scheme */
Tom Theuns's avatar
Tom Theuns committed
216
  const struct cooling_function_data *cooling_func;
217

Tom Theuns's avatar
Tom Theuns committed
218
  /* Properties of source terms */
Tom Theuns's avatar
Tom Theuns committed
219
  struct sourceterms *sourceterms;
Tom Theuns's avatar
Tom Theuns committed
220

221
222
  /* The (parsed) parameter file */
  const struct swift_params *parameter_file;
223
};
Pedro Gonnet's avatar
Pedro Gonnet committed
224
225

/* Function prototypes. */
226
void engine_barrier(struct engine *e, int tid);
227
void engine_compute_next_snapshot_time(struct engine *e);
228
229
void engine_unskip(struct engine *e);
void engine_drift_all(struct engine *e);
230
void engine_dump_snapshot(struct engine *e);
231
232
void engine_init(struct engine *e, struct space *s,
                 const struct swift_params *params, int nr_nodes, int nodeID,
233
                 int nr_threads, int with_aff, int policy, int verbose,
234
                 enum repartition_type reparttype,
235
                 const struct UnitSystem *internal_units,
236
                 const struct phys_const *physical_constants,
237
                 const struct hydro_props *hydro,
Tom Theuns's avatar
Tom Theuns committed
238
                 const struct external_potential *potential,
Tom Theuns's avatar
Tom Theuns committed
239
                 const struct cooling_function_data *cooling,
Tom Theuns's avatar
Tom Theuns committed
240
                 struct sourceterms *sourceterms);
241
void engine_launch(struct engine *e, int nr_runners);
242
void engine_prepare(struct engine *e);
243
void engine_init_particles(struct engine *e, int flag_entropy_ICs);
244
245
void engine_step(struct engine *e);
void engine_maketasks(struct engine *e);
246
void engine_split(struct engine *e, struct partition *initial_partition);
247
248
void engine_exchange_strays(struct engine *e, size_t offset_parts,
                            int *ind_part, size_t *Npart, size_t offset_gparts,
249
250
251
                            int *ind_gpart, size_t *Ngpart,
                            size_t offset_sparts, int *ind_spart,
                            size_t *Nspart);
252
253
254
255
void engine_rebuild(struct engine *e);
void engine_repartition(struct engine *e);
void engine_makeproxies(struct engine *e);
void engine_redistribute(struct engine *e);
Matthieu Schaller's avatar
Matthieu Schaller committed
256
void engine_print_policy(struct engine *e);
257
int engine_is_done(struct engine *e);
Matthieu Schaller's avatar
Matthieu Schaller committed
258
void engine_pin();
259
void engine_unpin();
260
void engine_clean(struct engine *e);
261
262

#endif /* SWIFT_ENGINE_H */