-
Notifications
You must be signed in to change notification settings - Fork 60
/
swift.c
1965 lines (1743 loc) · 69.3 KB
/
swift.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
* This file is part of SWIFT.
* Copyright (c) 2012 Pedro Gonnet ([email protected]),
* Matthieu Schaller ([email protected])
* 2015 Peter W. Draper ([email protected])
* Angus Lepper ([email protected])
* 2016 John A. Regan ([email protected])
* Tom Theuns ([email protected])
*
* 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.
*
* 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.
*
* 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/>.
*
******************************************************************************/
/* Config parameters. */
#include <config.h>
/* Some standard headers. */
#include <errno.h>
#include <fenv.h>
#include <libgen.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
/* MPI headers. */
#ifdef WITH_MPI
#include <mpi.h>
#endif
/* Local headers. */
#include "argparse.h"
#include "swift.h"
/* Engine policy flags. */
#ifndef ENGINE_POLICY
#define ENGINE_POLICY engine_policy_none
#endif
/* Global profiler. */
struct profiler prof;
/* Usage string. */
static const char *const swift_usage[] = {
"swift [options] [[--] param-file]",
"swift [options] param-file",
"swift_mpi [options] [[--] param-file]",
"swift_mpi [options] param-file",
NULL,
};
/* Function to handle multiple -P arguments. */
struct cmdparams {
const char *param[PARSER_MAX_NO_OF_PARAMS];
int nparam;
};
static int handle_cmdparam(struct argparse *self,
const struct argparse_option *opt) {
struct cmdparams *cmdps = (struct cmdparams *)opt->data;
cmdps->param[cmdps->nparam] = *(char **)opt->value;
cmdps->nparam++;
return 1;
}
/**
* @brief Main routine that loads a few particles and generates some output.
*
*/
int main(int argc, char *argv[]) {
struct clocks_time tic, toc;
struct engine e;
/* Structs used by the engine. Declare now to make sure these are always in
* scope. */
struct chemistry_global_data chemistry;
struct cooling_function_data cooling_func;
struct cosmology cosmo;
struct external_potential potential;
struct forcing_terms forcing_terms;
struct extra_io_properties extra_io_props;
struct star_formation starform;
struct pm_mesh mesh;
struct power_spectrum_data pow_data;
struct gpart *gparts = NULL;
struct gravity_props gravity_properties;
struct hydro_props hydro_properties;
struct stars_props stars_properties;
struct sink_props sink_properties;
struct neutrino_props neutrino_properties;
struct neutrino_response neutrino_response;
struct feedback_props feedback_properties;
struct rt_props rt_properties;
struct entropy_floor_properties entropy_floor;
struct pressure_floor_props pressure_floor_props;
struct black_holes_props black_holes_properties;
struct fof_props fof_properties;
struct lightcone_array_props lightcone_array_properties;
struct part *parts = NULL;
struct phys_const prog_const;
struct space s;
struct spart *sparts = NULL;
struct bpart *bparts = NULL;
struct sink *sinks = NULL;
struct unit_system us;
struct los_props los_properties;
struct ic_info ics_metadata;
int nr_nodes = 1, myrank = 0;
#ifdef WITH_MPI
/* Start by initializing MPI. */
int res = 0, prov = 0;
if ((res = MPI_Init_thread(&argc, &argv, MPI_THREAD_MULTIPLE, &prov)) !=
MPI_SUCCESS)
error("Call to MPI_Init failed with error %i.", res);
if (prov != MPI_THREAD_MULTIPLE)
error(
"MPI does not provide the level of threading"
" required (MPI_THREAD_MULTIPLE).");
if ((res = MPI_Comm_size(MPI_COMM_WORLD, &nr_nodes)) != MPI_SUCCESS)
error("MPI_Comm_size failed with error %i.", res);
if ((res = MPI_Comm_rank(MPI_COMM_WORLD, &myrank)) != MPI_SUCCESS)
error("Call to MPI_Comm_rank failed with error %i.", res);
/* Make sure messages are stamped with the correct rank and step. */
engine_rank = myrank;
engine_current_step = 0;
if ((res = MPI_Comm_set_errhandler(MPI_COMM_WORLD, MPI_ERRORS_RETURN)) !=
MPI_SUCCESS)
error("Call to MPI_Comm_set_errhandler failed with error %i.", res);
if (myrank == 0)
pretime_message("MPI is up and running with %i node(s).\n", nr_nodes);
if (nr_nodes == 1) {
pretime_message("WARNING: you are running with one MPI rank.");
pretime_message(
"WARNING: you should use the non-MPI version of this program.");
}
#endif
/* Welcome to SWIFT, you made the right choice */
if (myrank == 0) greetings(/*fof=*/0);
#ifdef WITH_MPI
/* Sync all output messages starting now to avoid verbose output
* interleaving with greeting. */
fflush(stdout);
MPI_Barrier(MPI_COMM_WORLD);
#endif
int with_aff = 0;
int with_nointerleave = 0;
int with_interleave = 0; /* Deprecated. */
int dry_run = 0;
int dump_tasks = 0;
int dump_cells = 0;
int dump_threadpool = 0;
int nsteps = -2;
int restart = 0;
int with_cosmology = 0;
int with_external_gravity = 0;
int with_temperature = 0;
int with_cooling = 0;
int with_self_gravity = 0;
int with_hydro = 0;
#ifdef MOVING_MESH
int with_grid_hydro = 0;
int with_grid = 0;
#endif
int with_stars = 0;
int with_fof = 0;
int with_lightcone = 0;
int with_star_formation = 0;
int with_feedback = 0;
int with_black_holes = 0;
int with_timestep_limiter = 0;
int with_timestep_sync = 0;
int with_fp_exceptions = 0;
int with_drift_all = 0;
int with_mpole_reconstruction = 0;
int with_structure_finding = 0;
int with_csds = 0;
int with_sinks = 0;
int with_qla = 0;
int with_eagle = 0;
int with_gear = 0;
int with_agora = 0;
int with_line_of_sight = 0;
int with_rt = 0;
int with_power = 0;
int verbose = 0;
int nr_threads = 1;
int nr_pool_threads = -1;
int with_verbose_timers = 0;
char *output_parameters_filename = NULL;
char *cpufreqarg = NULL;
char *param_filename = NULL;
char restart_file[200] = "";
unsigned long long cpufreq = 0;
float dump_tasks_threshold = 0.f;
struct cmdparams cmdps;
cmdps.nparam = 0;
cmdps.param[0] = NULL;
char *buffer = NULL;
/* Parse the command-line parameters. */
struct argparse_option options[] = {
OPT_HELP(),
OPT_GROUP(" Simulation options:\n"),
OPT_BOOLEAN('b', "feedback", &with_feedback, "Run with stars feedback.",
NULL, 0, 0),
OPT_BOOLEAN('c', "cosmology", &with_cosmology,
"Run with cosmological time integration.", NULL, 0, 0),
OPT_BOOLEAN(0, "temperature", &with_temperature,
"Run with temperature calculation.", NULL, 0, 0),
OPT_BOOLEAN('C', "cooling", &with_cooling,
"Run with cooling (also switches on --temperature).", NULL, 0,
0),
OPT_BOOLEAN('D', "drift-all", &with_drift_all,
"Always drift all particles even the ones far from active "
"particles. This emulates Gadget-[23] and GIZMO's default "
"behaviours.",
NULL, 0, 0),
OPT_BOOLEAN('F', "star-formation", &with_star_formation,
"Run with star formation.", NULL, 0, 0),
OPT_BOOLEAN('g', "external-gravity", &with_external_gravity,
"Run with an external gravitational potential.", NULL, 0, 0),
OPT_BOOLEAN('G', "self-gravity", &with_self_gravity,
"Run with self-gravity.", NULL, 0, 0),
OPT_BOOLEAN('M', "multipole-reconstruction", &with_mpole_reconstruction,
"Reconstruct the multipoles every time-step.", NULL, 0, 0),
OPT_BOOLEAN('s', "hydro", &with_hydro, "Run with hydrodynamics.", NULL, 0,
0),
OPT_BOOLEAN('S', "stars", &with_stars, "Run with stars.", NULL, 0, 0),
OPT_BOOLEAN('B', "black-holes", &with_black_holes,
"Run with black holes.", NULL, 0, 0),
OPT_BOOLEAN('k', "sinks", &with_sinks, "Run with sink particles.", NULL,
0, 0),
OPT_BOOLEAN(
'u', "fof", &with_fof,
"Run Friends-of-Friends algorithm to perform black hole seeding.",
NULL, 0, 0),
OPT_BOOLEAN(0, "lightcone", &with_lightcone,
"Generate lightcone outputs.", NULL, 0, 0),
OPT_BOOLEAN('x', "velociraptor", &with_structure_finding,
"Run with structure finding.", NULL, 0, 0),
OPT_BOOLEAN(0, "line-of-sight", &with_line_of_sight,
"Run with line-of-sight outputs.", NULL, 0, 0),
OPT_BOOLEAN(0, "limiter", &with_timestep_limiter,
"Run with time-step limiter.", NULL, 0, 0),
OPT_BOOLEAN(0, "sync", &with_timestep_sync,
"Run with time-step synchronization of particles hit by "
"feedback events.",
NULL, 0, 0),
OPT_BOOLEAN(0, "csds", &with_csds,
"Run with the Continuous Simulation Data Stream (CSDS).",
NULL, 0, 0),
OPT_BOOLEAN('R', "radiation", &with_rt, "Run with radiative transfer.",
NULL, 0, 0),
OPT_BOOLEAN(0, "power", &with_power, "Run with power spectrum outputs.",
NULL, 0, 0),
OPT_GROUP(" Simulation meta-options:\n"),
OPT_BOOLEAN(0, "quick-lyman-alpha", &with_qla,
"Run with all the options needed for the quick Lyman-alpha "
"model. This is equivalent to --hydro --self-gravity --stars "
"--star-formation --cooling.",
NULL, 0, 0),
OPT_BOOLEAN(
0, "eagle", &with_eagle,
"Run with all the options needed for the EAGLE model. This is "
"equivalent to --hydro --limiter --sync --self-gravity --stars "
"--star-formation --cooling --feedback --black-holes --fof.",
NULL, 0, 0),
OPT_BOOLEAN(
0, "gear", &with_gear,
"Run with all the options needed for the GEAR model. This is "
"equivalent to --hydro --limiter --sync --self-gravity --stars "
"--star-formation --cooling --feedback.",
NULL, 0, 0),
OPT_BOOLEAN(
0, "agora", &with_agora,
"Run with all the options needed for the AGORA model. This is "
"equivalent to --hydro --limiter --sync --self-gravity --stars "
"--star-formation --cooling --feedback.",
NULL, 0, 0),
OPT_GROUP(" Control options:\n"),
OPT_BOOLEAN('a', "pin", &with_aff,
"Pin runners using processor affinity.", NULL, 0, 0),
OPT_BOOLEAN(0, "nointerleave", &with_nointerleave,
"Do not interleave memory allocations across NUMA regions.",
NULL, 0, 0),
OPT_BOOLEAN(0, "interleave", &with_interleave,
"Deprecated option, now default", NULL, 0, 0),
OPT_BOOLEAN('d', "dry-run", &dry_run,
"Dry run. Read the parameter file, allocates memory but does "
"not read the particles from ICs. Exits before the start of "
"time integration. Checks the validity of parameters and IC "
"files as well as memory limits.",
NULL, 0, 0),
OPT_BOOLEAN('e', "fpe", &with_fp_exceptions,
"Enable floating-point exceptions (debugging mode).", NULL, 0,
0),
OPT_STRING('f', "cpu-frequency", &cpufreqarg,
"Overwrite the CPU "
"frequency (Hz) to be used for time measurements.",
NULL, 0, 0),
OPT_INTEGER('n', "steps", &nsteps,
"Execute a fixed number of time steps. When unset use the "
"time_end parameter to stop.",
NULL, 0, 0),
OPT_STRING('o', "output-params", &output_parameters_filename,
"Generate a parameter file with the options for selecting the "
"output fields.",
NULL, 0, 0),
OPT_STRING('P', "param", &buffer,
"Set parameter value, overiding the value read from the "
"parameter file. Can be used more than once {sec:par:value}.",
handle_cmdparam, (intptr_t)&cmdps, 0),
OPT_BOOLEAN('r', "restart", &restart, "Continue using restart files.",
NULL, 0, 0),
OPT_INTEGER(
't', "threads", &nr_threads,
"The number of task threads to use on each MPI rank. Defaults to "
"1 if not specified.",
NULL, 0, 0),
OPT_INTEGER(0, "pool-threads", &nr_pool_threads,
"The number of threads to use on each MPI rank for the "
"threadpool operations. "
"Defaults to the numbers of task threads if not specified.",
NULL, 0, 0),
OPT_INTEGER('T', "timers", &with_verbose_timers,
"Print timers every time-step.", NULL, 0, 0),
OPT_INTEGER('v', "verbose", &verbose,
"Run in verbose mode, in MPI mode 2 outputs from all ranks.",
NULL, 0, 0),
OPT_INTEGER('y', "task-dumps", &dump_tasks,
"Time-step frequency at which task graphs are dumped.", NULL,
0, 0),
OPT_INTEGER(0, "cell-dumps", &dump_cells,
"Time-step frequency at which cell graphs are dumped.", NULL,
0, 0),
OPT_INTEGER('Y', "threadpool-dumps", &dump_threadpool,
"Time-step frequency at which threadpool tasks are dumped.",
NULL, 0, 0),
OPT_FLOAT(0, "dump-tasks-threshold", &dump_tasks_threshold,
"Fraction of the total step's time spent in a task to trigger "
"a dump of the task plot on this step",
NULL, 0, 0),
OPT_END(),
};
struct argparse argparse;
argparse_init(&argparse, options, swift_usage, 0);
argparse_describe(&argparse, "\nParameters:",
"\nSee the file examples/parameter_example.yml for an "
"example of parameter file.");
int nargs = argparse_parse(&argparse, argc, (const char **)argv);
/* Deal with meta options */
if (with_qla) {
with_hydro = 1;
with_self_gravity = 1;
with_stars = 1;
with_star_formation = 1;
with_cooling = 1;
}
if (with_eagle) {
with_hydro = 1;
with_timestep_limiter = 1;
with_timestep_sync = 1;
with_self_gravity = 1;
with_stars = 1;
with_star_formation = 1;
with_cooling = 1;
with_feedback = 1;
with_black_holes = 1;
with_fof = 1;
}
if (with_gear) {
with_hydro = 1;
with_timestep_limiter = 1;
with_timestep_sync = 1;
with_self_gravity = 1;
with_stars = 1;
with_star_formation = 1;
with_cooling = 1;
with_feedback = 1;
}
if (with_agora) {
with_hydro = 1;
with_timestep_limiter = 1;
with_timestep_sync = 1;
with_self_gravity = 1;
with_stars = 1;
with_star_formation = 1;
with_cooling = 1;
with_feedback = 1;
}
#ifdef MOVING_MESH
if (with_hydro) {
with_grid = 1;
}
#endif
/* Deal with thread numbers */
if (nr_threads <= 0)
error("Invalid number of threads provided (%d), must be > 0.", nr_threads);
if (nr_pool_threads == -1) nr_pool_threads = nr_threads;
/* Write output parameter file */
if (myrank == 0 && output_parameters_filename != NULL) {
io_write_output_field_parameter(output_parameters_filename, with_cosmology,
with_fof, with_structure_finding);
pretime_message("End of run.");
return 0;
}
/* Need a parameter file. */
if (nargs != 1) {
if (myrank == 0) argparse_usage(&argparse);
pretime_message("\nError: no parameter file was supplied.");
return 1;
}
param_filename = argv[0];
/* Checks of options. */
#if !defined(HAVE_SETAFFINITY) || !defined(HAVE_LIBNUMA)
if (with_aff) {
pretime_message("Error: no NUMA support for thread affinity.");
return 1;
}
#endif
/* Interleave option is not fatal since we want to use it by default. */
if (with_interleave)
pretime_message(
"WARNING: the --interleave option is deprecated and ignored.");
#if !defined(HAVE_LIBNUMA)
if (!with_nointerleave || with_interleave) {
if (verbose)
pretime_message("WARNING: no NUMA support for interleaving memory.\n");
}
#endif
#if !defined(WITH_CSDS)
if (with_csds) {
pretime_message(
"Error: the CSDS is not available, please compile with "
"--enable-csds.");
return 1;
}
#endif
#ifndef HAVE_FE_ENABLE_EXCEPT
if (with_fp_exceptions) {
pretime_message("Error: no support for floating point exceptions.");
return 1;
}
#endif
#ifndef HAVE_VELOCIRAPTOR
if (with_structure_finding) {
pretime_message("Error: VELOCIraptor is not available.");
return 1;
}
#endif
#ifndef SWIFT_DEBUG_TASKS
if (dump_tasks) {
if (myrank == 0) {
pretime_message(
"WARNING: complete task dumps are only created when "
"configured with --enable-task-debugging.");
pretime_message(" Basic task statistics will be output.");
}
}
#endif
if (dump_tasks_threshold > 0.f) {
#ifndef SWIFT_DEBUG_TASKS
if (myrank == 0) {
error(
"Error: Dumping task plot data above a fixed time threshold is only "
"valid when the code is configured with --enable-task-debugging.");
}
#endif
#ifdef WITH_MPI
if (nr_nodes > 1)
error("Cannot dump tasks above a time threshold over MPI (yet).");
#endif
}
#ifndef SWIFT_CELL_GRAPH
if (dump_cells) {
if (myrank == 0) {
error(
"complete cell dumps are only created when "
"configured with --enable-cell-graph.");
}
}
#endif
#ifndef SWIFT_DEBUG_THREADPOOL
if (dump_threadpool) {
pretime_message(
"Error: threadpool dumping is only possible if SWIFT was "
"configured with the --enable-threadpool-debugging option.");
return 1;
}
#endif
#ifdef WITH_MPI
if (with_sinks) {
pretime_message("Error: sink particles are not available yet with MPI.");
return 1;
}
#endif
if (with_sinks && with_star_formation) {
pretime_message(
"Error: The sink particles are not supposed to be run with star "
"formation.");
return 1;
}
/* The CPU frequency is a long long, so we need to parse that ourselves. */
if (cpufreqarg != NULL) {
if (sscanf(cpufreqarg, "%llu", &cpufreq) != 1) {
if (myrank == 0)
pretime_message("Error parsing CPU frequency (%s).", cpufreqarg);
return 1;
}
}
if (!with_self_gravity && !with_hydro && !with_external_gravity) {
if (myrank == 0) {
argparse_usage(&argparse);
pretime_message(
"\nError: At least one of --hydro, --external-gravity"
" or --self-gravity must be chosen.");
}
return 1;
}
if (with_stars && !with_external_gravity && !with_self_gravity) {
if (myrank == 0) {
argparse_usage(&argparse);
pretime_message(
"\nError: Cannot process stars without gravity, --external-gravity "
"or --self-gravity must be chosen.");
}
return 1;
}
if (with_black_holes && !with_self_gravity) {
if (myrank == 0) {
argparse_usage(&argparse);
pretime_message(
"Error: Cannot process black holes without self-gravity, "
"--self-gravity must be chosen.\n");
}
return 1;
}
if (with_fof) {
#ifndef WITH_FOF
error("Running with FOF but compiled without it!");
#endif
}
if (with_fof && !with_self_gravity) {
if (myrank == 0)
pretime_message(
"Error: Cannot perform FOF search without gravity,"
" --external-gravity or --self-gravity must be chosen.");
return 1;
}
if (with_lightcone) {
#ifndef WITH_LIGHTCONE
error("Running with lightcone output but compiled without it!");
#endif
if (!with_cosmology)
error("Error: cannot make lightcones without --cosmology.");
}
if (!with_stars && with_star_formation) {
if (myrank == 0) {
argparse_usage(&argparse);
pretime_message(
"Error: Cannot process star formation without stars, --stars must "
"be chosen.");
}
return 1;
}
if (!with_stars && with_feedback) {
if (myrank == 0) {
argparse_usage(&argparse);
pretime_message(
"Error: Cannot process feedback without stars, --stars must be "
"chosen.");
}
return 1;
}
if (!with_hydro && with_feedback) {
if (myrank == 0) {
argparse_usage(&argparse);
pretime_message(
"Error: Cannot process feedback without gas, --hydro must be "
"chosen.");
}
return 1;
}
if (!with_hydro && with_black_holes) {
if (myrank == 0) {
argparse_usage(&argparse);
pretime_message(
"Error: Cannot process black holes without gas, --hydro must be "
"chosen.");
}
return 1;
}
if (!with_hydro && with_line_of_sight) {
if (myrank == 0) {
argparse_usage(&argparse);
pretime_message(
"Error: Cannot use line-of-sight outputs without gas, --hydro must "
"be chosen.");
}
return 1;
}
#ifdef RT_NONE
if (with_rt) {
error("Running with radiative transfer but compiled without it!");
}
#else
if (!with_rt) {
error("Running without radiative transfer but compiled with it!");
}
if (with_rt && !with_hydro) {
error(
"Error: Cannot use radiative transfer without gas, --hydro must be "
"chosen.");
}
if (with_rt && !with_stars) {
/* In principle we can run without stars, but I don't trust the user to
* remember to add the flag every time they want to run RT. */
error(
"Error: Cannot use radiative transfer without stars, --stars must be "
"chosen.");
}
if (with_rt && !with_feedback) {
error(
"Error: Cannot use radiative transfer without --feedback "
"(even if configured --with-feedback=none).");
}
if (with_rt && with_cooling) {
error("Error: Cannot use radiative transfer and cooling simultaneously.");
}
#endif /* idfef RT_NONE */
#ifdef SINK_NONE
if (with_sinks) {
error("Running with sink particles but compiled without them!");
}
#endif
#ifdef TRACERS_EAGLE
if (!with_cooling && !with_temperature)
error(
"Error: Cannot use EAGLE tracers without --cooling or --temperature.");
#endif
/* Let's pin the main thread, now we know if affinity will be used. */
#if defined(HAVE_SETAFFINITY) && defined(HAVE_LIBNUMA) && defined(_GNU_SOURCE)
if (with_aff &&
((ENGINE_POLICY)&engine_policy_setaffinity) == engine_policy_setaffinity)
engine_pin();
#endif
#if defined(HAVE_LIBNUMA) && defined(_GNU_SOURCE)
/* Set the NUMA memory policy to interleave. */
if (!with_nointerleave) engine_numa_policies(myrank, verbose);
#endif
/* Genesis 1.1: And then, there was time ! */
clocks_set_cpufreq(cpufreq);
/* Are we running with gravity */
const int with_gravity = (with_self_gravity || with_external_gravity);
/* How vocal are we ? */
const int talking = (verbose == 1 && myrank == 0) || (verbose == 2);
if (myrank == 0 && dry_run)
message(
"Executing a dry run. No i/o or time integration will be performed.");
/* Report CPU frequency.*/
cpufreq = clocks_get_cpufreq();
if (myrank == 0) {
message("CPU frequency used for tick conversion: %llu Hz", cpufreq);
}
/* Report host name(s). */
#ifdef WITH_MPI
if (talking) {
message("Rank %d running on: %s", myrank, hostname());
}
#else
message("Running on: %s", hostname());
#endif
/* Do we have debugging checks ? */
#ifdef SWIFT_DEBUG_CHECKS
if (myrank == 0)
message("WARNING: Debugging checks activated. Code will be slower !");
#endif
/* Do we have debugging checks ? */
#ifdef SWIFT_USE_NAIVE_INTERACTIONS
if (myrank == 0)
message(
"WARNING: Naive cell interactions activated. Code will be slower !");
#endif
/* Do we have gravity accuracy checks ? */
#ifdef SWIFT_GRAVITY_FORCE_CHECKS
if (myrank == 0)
message(
"WARNING: Checking 1/%d of all gpart for gravity accuracy. Code will "
"be slower !",
SWIFT_GRAVITY_FORCE_CHECKS);
#endif
/* Do we have hydro accuracy checks ? */
#ifdef SWIFT_HYDRO_DENSITY_CHECKS
if (myrank == 0)
message(
"WARNING: Checking 1/%d of all part for hydro accuracy. Code will be "
"slower !",
SWIFT_HYDRO_DENSITY_CHECKS);
#endif
/* Do we choke on FP-exceptions ? */
if (with_fp_exceptions) {
#ifdef HAVE_FE_ENABLE_EXCEPT
feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW);
#endif
if (myrank == 0)
message("WARNING: Floating point exceptions will be reported.");
}
/* Do we have slow barriers? */
#ifndef HAVE_PTHREAD_BARRIERS
if (myrank == 0)
message("WARNING: Non-optimal thread barriers are being used.");
#endif
/* How large are the parts? */
if (myrank == 0) {
message("sizeof(part) is %4zi bytes.", sizeof(struct part));
message("sizeof(xpart) is %4zi bytes.", sizeof(struct xpart));
message("sizeof(sink) is %4zi bytes.", sizeof(struct sink));
message("sizeof(spart) is %4zi bytes.", sizeof(struct spart));
message("sizeof(bpart) is %4zi bytes.", sizeof(struct bpart));
message("sizeof(gpart) is %4zi bytes.", sizeof(struct gpart));
message("sizeof(multipole) is %4zi bytes.", sizeof(struct multipole));
message("sizeof(grav_tensor) is %4zi bytes.", sizeof(struct grav_tensor));
message("sizeof(task) is %4zi bytes.", sizeof(struct task));
message("sizeof(cell) is %4zi bytes.", sizeof(struct cell));
}
/* Read the parameter file */
struct swift_params *params =
(struct swift_params *)malloc(sizeof(struct swift_params));
/* In scope reference for a potential copy when restarting. */
struct swift_params *refparams = NULL;
if (params == NULL) error("Error allocating memory for the parameter file.");
if (myrank == 0) {
message("Reading runtime parameters from file '%s'", param_filename);
parser_read_file(param_filename, params);
/* Handle any command-line overrides. */
if (cmdps.nparam > 0) {
message(
"Overwriting values read from the YAML file with command-line "
"values.");
for (int k = 0; k < cmdps.nparam; k++)
parser_set_param(params, cmdps.param[k]);
}
}
#ifdef WITH_MPI
/* Broadcast the parameter file */
MPI_Bcast(params, sizeof(struct swift_params), MPI_BYTE, 0, MPI_COMM_WORLD);
#endif
/* Read the provided output selection file, if available. Best to
* do this after broadcasting the parameters as there may be code in this
* function that is repeated on each node based on the parameter file. */
struct output_options *output_options =
(struct output_options *)malloc(sizeof(struct output_options));
output_options_init(params, myrank, output_options);
/* Temporary early aborts for modes not supported over MPI. */
#ifdef WITH_MPI
if (with_mpole_reconstruction && nr_nodes > 1)
error("Cannot reconstruct m-poles every step over MPI (yet).");
#endif
/* Temporary early aborts for modes not supported with hand-vec. */
#if defined(WITH_VECTORIZATION) && defined(GADGET2_SPH) && \
!defined(CHEMISTRY_NONE)
error(
"Cannot run with chemistry and hand-vectorization (yet). "
"Use --disable-hand-vec at configure time.");
#endif
/* Check that we can write the snapshots by testing if the output
* directory exists and is searchable and writable. */
char basename[PARSER_MAX_LINE_SIZE];
parser_get_param_string(params, "Snapshots:basename", basename);
const char *dirp = dirname(basename);
if (access(dirp, W_OK | X_OK) != 0) {
error("Cannot write snapshots in directory %s (%s)", dirp, strerror(errno));
}
/* Check that we can write the structure finding catalogues by testing if the
* output directory exists and is searchable and writable. */
if (with_structure_finding) {
char stfbasename[PARSER_MAX_LINE_SIZE];
parser_get_param_string(params, "StructureFinding:basename", stfbasename);
const char *stfdirp = dirname(stfbasename);
if (access(stfdirp, W_OK | X_OK) != 0) {
error("Cannot write stf catalogues in directory %s (%s)", stfdirp,
strerror(errno));
}
}
/* Check that we can write the line of sight files by testing if the
* output directory exists and is searchable and writable. */
if (with_line_of_sight) {
char losbasename[PARSER_MAX_LINE_SIZE];
parser_get_param_string(params, "LineOfSight:basename", losbasename);
const char *losdirp = dirname(losbasename);
if (access(losdirp, W_OK | X_OK) != 0) {
error("Cannot write line of sight in directory %s (%s)", losdirp,
strerror(errno));
}
}
/* Prepare the domain decomposition scheme */
struct repartition reparttype;
#ifdef WITH_MPI
struct partition initial_partition;
partition_init(&initial_partition, &reparttype, params, nr_nodes);
/* Let's report what we did */
if (myrank == 0) {
#if defined(HAVE_PARMETIS)
if (reparttype.usemetis)
message("Using METIS serial partitioning:");
else
message("Using ParMETIS partitioning:");
#elif defined(HAVE_METIS)
message("Using METIS serial partitioning:");
#else
message("Non-METIS partitioning:");
#endif
message(" initial partitioning: %s",
initial_partition_name[initial_partition.type]);
if (initial_partition.type == INITPART_GRID)
message(" grid set to [ %i %i %i ].", initial_partition.grid[0],
initial_partition.grid[1], initial_partition.grid[2]);
message(" repartitioning: %s", repartition_name[reparttype.type]);
}
#endif
/* Common variables for restart and IC sections. */
int clean_smoothing_length_values = 0;
int flag_entropy_ICs = 0;
/* Work out where we will read and write restart files. */
char restart_dir[PARSER_MAX_LINE_SIZE];
parser_get_opt_param_string(params, "Restarts:subdir", restart_dir,
"restart");
/* The directory must exist. */
if (myrank == 0) {
if (access(restart_dir, W_OK | X_OK) != 0) {
if (restart) {
error("Cannot restart as no restart subdirectory: %s (%s)", restart_dir,
strerror(errno));
} else {
if (mkdir(restart_dir, 0777) != 0)
error("Failed to create restart directory: %s (%s)", restart_dir,
strerror(errno));
}
}
}
/* Basename for any restart files. */
char restart_name[PARSER_MAX_LINE_SIZE];
parser_get_opt_param_string(params, "Restarts:basename", restart_name,
"swift");
/* If restarting, look for the restart files. */
if (restart) {
/* Attempting a restart. */
char **restart_files = NULL;
int restart_nfiles = 0;
if (myrank == 0) {
message("Restarting SWIFT");
/* Locate the restart files. */
restart_files =
restart_locate(restart_dir, restart_name, &restart_nfiles);
if (restart_nfiles == 0)
error("Failed to locate any restart files in %s", restart_dir);
/* We need one file per rank. */
if (restart_nfiles != nr_nodes)
error("Incorrect number of restart files, expected %d found %d",
nr_nodes, restart_nfiles);
if (verbose > 0)
for (int i = 0; i < restart_nfiles; i++)
message("found restart file: %s", restart_files[i]);
}
#ifdef WITH_MPI
/* Distribute the restart files, need one for each rank. */
if (myrank == 0) {
for (int i = 1; i < nr_nodes; i++) {
strcpy(restart_file, restart_files[i]);
MPI_Send(restart_file, 200, MPI_BYTE, i, 0, MPI_COMM_WORLD);
}
/* Keep local file. */
strcpy(restart_file, restart_files[0]);
/* Finished with the list. */
restart_locate_free(restart_nfiles, restart_files);
} else {
MPI_Recv(restart_file, 200, MPI_BYTE, 0, 0, MPI_COMM_WORLD,
MPI_STATUS_IGNORE);
}
if (verbose > 1) message("local restart file = %s", restart_file);
#else
/* Just one restart file. */
strcpy(restart_file, restart_files[0]);
/* Finished with the list. */
restart_locate_free(1, restart_files);
#endif
/* Now read it. */
restart_read(&e, restart_file);
#ifdef WITH_MPI
integertime_t min_ti_current = e.ti_current;
integertime_t max_ti_current = e.ti_current;
/* Verify that everyone agrees on the current time */
MPI_Allreduce(&e.ti_current, &min_ti_current, 1, MPI_LONG_LONG_INT, MPI_MIN,
MPI_COMM_WORLD);
MPI_Allreduce(&e.ti_current, &max_ti_current, 1, MPI_LONG_LONG_INT, MPI_MAX,
MPI_COMM_WORLD);