MP-Gadget  5.0.1.dev1-76bc7d4726-dirty
checkpoint.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 <math.h>
6 #include <errno.h>
7 #include <sys/stat.h>
8 #include <sys/types.h>
9 
10 #include "petaio.h"
11 #include "checkpoint.h"
12 #include "walltime.h"
13 #include "fof.h"
14 
15 #include "utils.h"
16 
23 void
24 write_checkpoint(int snapnum, int WriteGroupID, int MetalReturnOn, double Time, const Cosmology * CP, const char * OutputDir, const int OutputDebugFields)
25 {
26  walltime_measure("/Misc");
27  /* write snapshot of particles */
28  struct IOTable IOTable = {0};
29  register_io_blocks(&IOTable, WriteGroupID, MetalReturnOn);
30  if(OutputDebugFields)
32 
33  char * fname = petaio_get_snapshot_fname(snapnum, OutputDir);
34  petaio_save_snapshot(fname, &IOTable, 1, Time, CP);
35  myfree(fname);
36 
38  walltime_measure("/Snapshot/Write");
39 
40  int ThisTask;
41  MPI_Comm_rank(MPI_COMM_WORLD, &ThisTask);
42  if(ThisTask == 0) {
43  char * buf = fastpm_strdup_printf("%s/Snapshots.txt", OutputDir);
44  FILE * fd = fopen(buf, "a");
45  fprintf(fd, "%03d %g\n", snapnum, Time);
46  fclose(fd);
47  myfree(buf);
48  }
49 }
50 
51 void
52 dump_snapshot(const char * dump, const double Time, const Cosmology * CP, const char * OutputDir)
53 {
54  struct IOTable IOTable = {0};
57  char * fname = fastpm_strdup_printf("%s/%s", OutputDir, dump);
58  petaio_save_snapshot(fname, &IOTable, 1, Time, CP);
60 }
61 
62 int
63 find_last_snapnum(const char * OutputDir)
64 {
65  /* FIXME: this is very fragile; should be fine */
66  int snapnumber = -1;
67  int ThisTask;
68  MPI_Comm_rank(MPI_COMM_WORLD, &ThisTask);
69  if(ThisTask == 0) {
70  char * buf = fastpm_strdup_printf("%s/Snapshots.txt", OutputDir);
71  FILE * fd = fopen(buf, "r");
72  if(fd == NULL) {
73  snapnumber = -1;
74  } else {
75  double time;
76  char ch;
77  int line = 0;
78  while (!feof(fd)) {
79  int n = fscanf(fd, "%d %lg%c", &snapnumber, &time, &ch);
80 // message(1, "n = %d\n", n);
81  if (n == 3 && ch == '\n') {
82  line ++;
83  continue;
84  }
85  if (n == -1 && feof(fd)) {
86  continue;
87  }
88  endrun(1, "Failed to parse %s:%d for the last snap shot number.\n", buf, line);
89  }
90  fclose(fd);
91  }
92  myfree(buf);
93  }
94 
95  MPI_Bcast(&snapnumber, 1, MPI_INT, 0, MPI_COMM_WORLD);
96  return snapnumber;
97 }
void write_checkpoint(int snapnum, int WriteGroupID, int MetalReturnOn, double Time, const Cosmology *CP, const char *OutputDir, const int OutputDebugFields)
Definition: checkpoint.c:24
void dump_snapshot(const char *dump, const double Time, const Cosmology *CP, const char *OutputDir)
Definition: checkpoint.c:52
int find_last_snapnum(const char *OutputDir)
Definition: checkpoint.c:63
void endrun(int where, const char *fmt,...)
Definition: endrun.c:147
#define myfree(x)
Definition: mymalloc.h:19
void destroy_io_blocks(struct IOTable *IOTable)
Definition: petaio.c:1034
void register_io_blocks(struct IOTable *IOTable, int WriteGroupID, int MetalReturnOn)
Definition: petaio.c:909
void petaio_save_snapshot(const char *fname, struct IOTable *IOTable, int verbose, const double atime, const Cosmology *CP)
Definition: petaio.c:151
void register_debug_io_blocks(struct IOTable *IOTable)
Definition: petaio.c:1014
char * petaio_get_snapshot_fname(int num, const char *OutputDir)
Definition: petaio.c:207
static Cosmology * CP
Definition: power.c:27
char * fastpm_strdup_printf(const char *fmt,...)
Definition: string.c:41
Definition: petaio.h:60
int ThisTask
Definition: test_exchange.c:23
#define walltime_measure(name)
Definition: walltime.h:8