MP-Gadget  5.0.1.dev1-76bc7d4726-dirty
main.c
Go to the documentation of this file.
1 #include <mpi.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <sys/types.h>
6 #include <sys/resource.h>
7 #include <unistd.h>
8 #include <math.h>
9 #include <gsl/gsl_math.h>
10 #include <gsl/gsl_errno.h>
11 #include <omp.h>
12 
13 #include <libgadget/slotsmanager.h>
14 #include <libgadget/partmanager.h>
15 
16 #include <libgadget/run.h>
17 #include <libgadget/checkpoint.h>
18 #include <libgadget/config.h>
19 
20 #include <libgadget/utils.h>
21 
22 #include "params.h"
23 
24 void gsl_handler (const char * reason, const char * file, int line, int gsl_errno)
25 {
26  endrun(2001,"GSL_ERROR in file: %s, line %d, errno:%d, error: %s\n",file, line, gsl_errno, reason);
27 }
28 
39 int main(int argc, char **argv)
40 {
41  int NTask;
42  int thread_provided;
43  MPI_Init_thread(&argc, &argv, MPI_THREAD_FUNNELED, &thread_provided);
44  MPI_Comm_size(MPI_COMM_WORLD, &NTask);
45  if(thread_provided != MPI_THREAD_FUNNELED)
46  message(1, "MPI_Init_thread returned %d != MPI_THREAD_FUNNELED\n", thread_provided);
47 
48  if(argc < 2)
49  {
50  message(0, "Parameters are missing.\n");
51  message(0, "Call with <ParameterFile> [<RestartFlag>] [<RestartSnapNum>]\n\n");
52  message(0, " RestartFlag Action\n");
53  message(0, " 1 Restart from last snapshot (LastSnapNum.txt) and continue simulation\n");
54  message(0, " 2 Restart from specified snapshot (-1 for Initial Condition) and continue simulation\n");
55  message(0, " 3 Run FOF if enabled\n");
56  message(0, " 4 Generate a power spectrum and exit\n");
57  message(0, " 99 Run Tests. \n\n");
58  MPI_Finalize();
59  return 1;
60  }
61 
62  message(0, "This is MP-Gadget, version %s.\n", GADGET_VERSION);
63  message(0, "Running on %d MPI Ranks.\n", NTask);
64  message(0, " %d OpenMP Threads.\n", omp_get_max_threads());
65  message(0, "Code was compiled with settings:\n"
66  "%s\n", GADGET_COMPILER_SETTINGS);
67  message(0, "Size of particle structure %td [bytes]\n",sizeof(struct particle_data));
68  message(0, "Size of blackhole structure %td [bytes]\n",sizeof(struct bh_particle_data));
69  message(0, "Size of sph particle structure %td [bytes]\n",sizeof(struct sph_particle_data));
70  message(0, "Size of star particle structure %td [bytes]\n",sizeof(struct star_particle_data));
71 
72  /* Avoid dumping core, except for the master process. For large jobs writing core
73  * with all of main memory can be very bad. */
74  int ThisTask;
75  MPI_Comm_rank(MPI_COMM_WORLD, &ThisTask);
76  if(ThisTask != 0) {
77  struct rlimit rlim = {0};
78  setrlimit(RLIMIT_CORE, &rlim);
79  }
80  tamalloc_init();
81 
82  int ShowBacktrace;
83  double MaxMemSizePerNode;
84  read_parameter_file(argv[1], &ShowBacktrace, &MaxMemSizePerNode); /* ... read in parameters for this run */
85 
86  int RestartFlag, RestartSnapNum;
87 
88  if(argc >= 3)
89  RestartFlag = atoi(argv[2]);
90  else
91  RestartFlag = 2;
92 
93  if(argc >= 4)
94  RestartSnapNum = atoi(argv[3]);
95  else
96  RestartSnapNum = -1;
97 
98  if(RestartFlag == 0) {
99  message(0, "Restart flag of 0 is deprecated. Use 2.\n");
100  RestartFlag = 2;
101  }
102  if(RestartFlag == 1) {
103  RestartSnapNum = find_last_snapshot();
104  }
105  if(RestartFlag == 3 && RestartSnapNum < 0) {
106  endrun(0, "Need to give the snapshot number if FOF is selected for output\n");
107  }
108 
109  /*Set up GSL so it gives a proper MPI termination*/
110  gsl_set_error_handler(gsl_handler);
111 
112  /*Initialize the memory manager*/
113  mymalloc_init(MaxMemSizePerNode);
114 
115  /* Make sure memory has finished initialising on all ranks before doing more.
116  * This may improve stability */
117  MPI_Barrier(MPI_COMM_WORLD);
118 
120 
121  struct header_data head = {0};
122  inttime_t ti_init = begrun(RestartSnapNum, &head);
123 
124  switch(RestartFlag) {
125  case 3:
126  runfof(RestartSnapNum, ti_init, &head);
127  break;
128  case 4:
129  runpower(&head);
130  break;
131  case 99:
132  runtests(RestartSnapNum, ti_init, &head);
133  break;
134  default:
135  run(RestartSnapNum, ti_init, &head); /* main simulation loop */
136  break;
137  }
138  MPI_Finalize(); /* clean up & finalize MPI */
139 
140  return 0;
141 }
const char * GADGET_VERSION
Definition: config.c:7
const char * GADGET_COMPILER_SETTINGS
Definition: config.c:1
void message(int where, const char *fmt,...)
Definition: endrun.c:175
void init_endrun(int backtrace)
Definition: endrun.c:132
static int ShowBacktrace
Definition: endrun.c:100
void endrun(int where, const char *fmt,...)
Definition: endrun.c:147
void gsl_handler(const char *reason, const char *file, int line, int gsl_errno)
Definition: main.c:24
int main(int argc, char **argv)
Definition: main.c:39
void read_parameter_file(char *fname, int *ShowBacktrace, double *MaxMemSizePerNode)
Definition: params.c:359
void tamalloc_init(void)
Definition: mymalloc.c:29
void mymalloc_init(double MaxMemSizePerNode)
Definition: mymalloc.c:48
void runfof(const int RestartSnapNum, const inttime_t Ti_Current, const struct header_data *header)
Definition: run.c:652
int find_last_snapshot(void)
Definition: run.c:182
void run(const int RestartSnapNum, const inttime_t ti_init, const struct header_data *header)
Definition: run.c:284
void runtests(const int RestartSnapNum, const inttime_t Ti_Current, const struct header_data *header)
Definition: run.c:646
inttime_t begrun(const int RestartSnapNum, struct header_data *head)
Definition: run.c:194
void runpower(const struct header_data *header)
Definition: run.c:691
int ThisTask
Definition: test_exchange.c:23
int NTask
Definition: test_exchange.c:23
int32_t inttime_t
Definition: types.h:8