aboutsummaryrefslogtreecommitdiff
path: root/src/CheckpointRecovery.c
blob: c7df1e34b50737335f2e1d3d35dbfb8773786ebf (plain)
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
 /*@@
   @file      CheckpointRecovery.c
   @date      Jun 04 1999
   @author    Thomas Radke
   @desc 
   Utility routines for checkpointing/recovery and the filereader
   The actual work is done by the IO thorns.
 @@*/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "cctk.h"
#include "cctk_parameters.h"
#include "ioGH.h"
#include "StoreHandledData.h"

/* Local data holding info on Recover Functions*/
static cHandledData *RecoverFunctions = NULL;
static int num_functions = 0;

/************************************************************************
 *
 *  Registration functions for Restoring from IO Files
 *
 ************************************************************************/

 /*@@
   @routine    IOUtil_RegisterRecover
   @date       Monday 21 June 1999
   @author     Gabrielle Allen
   @desc 
               Registers a new recovery method
   @enddesc 
   @calls      Util_GetHandle
               Util_NewHandle
   @history 
   @endhistory
 
   @var        name
   @vdesc      The name of the function for recovery
   @vtype      const char *
   @vio        in
   @vcomment 
   @endvar 

   @returntype int
   @returndesc
               -2  = memory allocation failed
               -1  = method with this name already registered
               i>0 = handle for function
   @endreturndesc

   @version    $Header$
@@*/

int IOUtil_RegisterRecover(const char *name, int (*func)(cGH *, const char *, int))
{

  int handle;

  /* Check that the method hasn't already been registered */
  handle = Util_GetHandle(RecoverFunctions, name, NULL);

  if(handle < 0)
  {
    /* New function */

    /* Get a handle for it. */
    handle = Util_NewHandle(&RecoverFunctions, name, func);

    /* Remember how many methods there are */
    num_functions++;
  }
  else
  {
    /* function already exists. */
    handle = -1;
  }
    
  return handle;
}

 /*@@
   @routine    IOUtil_PrepareFilename
   @date       Fri Aug 21 14:54:38 1998
   @author     Gerd Lanfermann
   @desc 
     This routine prepares the filenames for the checkpoint/recovery
     and filereader files, paying attention to the different types:
     * it returns the full filename (directory+filename)

     * it prepends "Ln_" level indicators to the filename
       and "low_"/"med_" for convergence levels > 1

     * for checkpoint files it prepends the iteration number as "it_%d"

     * for chunked files it prepends the file number as "file_%d"

   @enddesc 
   @calls     
   @calledby   FlexIO_DumpGH HDF5IO_DumpGH
   @var        GH
   @vdesc      Pointer to CCTK grid hierarchy
   @vtype      cGH
   @vio        in
   @endvar
   @var        basename
   @vdesc      basename of the file(s) to recover from
   @vtype      const char *
   @vio        in
   @endvar
   @var        fname
   @vdesc      the resulting filename
   @vtype      char *
   @vio        out
   @endvar
   @var        called_from
   @vdesc      indicates the caller function:
                 * either Filereader (FILEREADER_DATA) 
                 * or IOUtil_RecoverGH() (CP_RECOVER_DATA)
   @vtype      int
   @vio        in
   @endvar
   @var        file_ioproc
   @vdesc      the IO processor number (for chunked files)
   @vtype      int
   @vio        in
   @endvar
   @var        file_unchunked
   @vdesc      flag to indicate whether file mode is unchunked or not
   @vtype      int
   @vio        in
   @endvar
   @history 
   @hdate Nov 4 1998 @hauthor Gabrielle Allen
   @hdesc A file_* in the name indicates it needs recombining 
   @hdate Apr 14 1999 @hauthor Thomas Radke
   @hdesc Removed code for expanding "basedir" and "nameofparfile"
   @hdate May 06 1999 @hauthor Thomas Radke
   @hdesc Added parameter unchunked to be independent of current chunking mode
          for recovery
   @endhistory 

@@*/
 
void IOUtil_PrepareFilename (cGH *GH, const char *basename, char *fname,
                           int called_from, int file_ioproc, int file_unchunked)
{
  DECLARE_CCTK_PARAMETERS

  /* get the right parameters */
  switch (called_from) {
    case CP_INITIAL_DATA: 
      sprintf (fname, "%s/%s", checkpoint_dir, checkpoint_ID_file); break;
    case CP_EVOLUTION_DATA:
      sprintf (fname, "%s/%s", checkpoint_dir, checkpoint_file); break;
    case CP_RECOVER_DATA:
      sprintf (fname, "%s/%s", recovery_dir, recover_file); break;
    case FILEREADER_DATA:
      strcpy (fname, basename); break;

    default:
      CCTK_WARN (2, "Unknown calling mode in IO_PrepareFilename().");
      break;
  }

  /* add refinement factor and convergence level (med/low) inbetween: */
  /* FIXME Gab ... asymmetric levfac */
  if (GH->cctk_levfac[0] > 1)
    sprintf (fname, "%sL%d_", fname, GH->cctk_levfac[0]);
  if (GH->cctk_convlevel > 1)
    strcat (fname, GH->cctk_convlevel == 2 ? "med_" : "low_");

  /* If checkpoint filename, merge in the iteration number
     and for chunked files also the file number */
  if (called_from == CP_INITIAL_DATA || called_from == CP_EVOLUTION_DATA)
    sprintf (fname, "%s.it_%d", fname, (int) GH->cctk_iteration);

  /* If not one unchunked file give a file number */
  if (! file_unchunked)
    sprintf (fname, "%s.file_%d", fname, file_ioproc);

}


 /*@@
   @routine    IOUtil_RecoverFromFile
   @date       Jun 14 1999
   @author     Thomas Radke
   @desc 
     Recover from a given file.
     This routine loops through all XXX_RecoverGH routines
     registered by IO thorns.
   @enddesc 
   @var        GH
   @vdesc      Pointer to CCTK grid hierarchy
   @vtype      cGH
   @vio        in
   @endvar
   @var        basename
   @vdesc      basename of the file(s) to recover from
   @vtype      const char *
   @vio        in
   @endvar
   @var        called_from
   @vdesc      indicates the caller function:
                 * either Filereader (FILEREADER_DATA) 
                 * or IOUtil_RecoverGH() (CP_RECOVER_DATA)
   @vtype      int
   @vio        in
   @endvar

   @history 
   @endhistory 
 @@*/
int IOUtil_RecoverFromFile (cGH *GH, const char *basename, int called_from)
{
  int handle;
  int retval;
  int (*fnRecoverGH)(cGH *GH, const char *basename, int called_from);

  retval = -1;

  for (handle = 0;handle<num_functions;handle++)
  {

    fnRecoverGH = (int (*)(cGH *, const char *, int))((Util_GetHandledData(RecoverFunctions, handle)));
    if (fnRecoverGH)
    {
      if (fnRecoverGH(GH,basename,called_from) >=0)
      {
	retval = handle;
	break;
      }
    }
    else
    {
      CCTK_WARN (1, "Could not recover");
      break;
    }
  }

  return retval;

}


 /*@@
   @routine    IOUtil_RecoverGH
   @date       Jun 14 1999
   @author     Thomas Radke
   @desc 
     The rfr-registered recovery routine.
     Just calls IOUtil_RecoverFromFile() with called_from == CP_RECOVER_DATA.
   @enddesc 
   @var        GH
   @vdesc      Pointer to CCTK grid hierarchy
   @vtype      cGH
   @vio        in
   @endvar
   @history 
   @endhistory 
 @@*/
void IOUtil_RecoverGH (cGH *GH)
{
  IOUtil_RecoverFromFile (GH, NULL, CP_RECOVER_DATA);
}