aboutsummaryrefslogtreecommitdiff
path: root/src/RecoverGH.c
blob: a8fd02b1f46b1836573a678f439c56bbdf630fc8 (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
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
 /*@@
   @file      RecoverGH.c
   @date      Fri Jun 19 09:14:22 1998
   @author    Tom Goodale
   @desc 
   Contains the routines to do the internal checkpoint recovery.

   Currently can recover from:
     (1) One file containing recombined data
     (2) Multiple unrecombined files, where the current
	     number of processors and outputing processors
		 match those used to write the data.
   @enddesc 
   @history
   @hauthor Gabrielle Allen @hdate 19 Oct 1998      
   @hdesc Changed names ready for thorn_IO
   @endhistory
   @version $Id$
 @@*/

static char *rcsid = "$Id$";

#include <stdio.h>

#include "cctk.h"
#include "cctk_Flesh.h"
#include "cctk_parameters.h"
#include "cctk_GHExtensions.h"
#include "cctk_WarnLevel.h"
#include "cctk_Comm.h"
#include "CactusPUGH/PUGH/src/include/pugh.h"
#include "CactusBase/IOUtil/src/ioGH.h"
#include "ioFlexGH.h"


/* this one comes from RestoreFile.c */
int IOFlexIO_RestoreIEEEIOfile (cGH *GH, IOFile ifp,
                              int IOrecover_ioproc,
                              int IOrecover_ioproc_every,
                              int IOrecover_unchunked);


 /*@@
   @routine    IOFlexIO_RecoverGH
   @date       Fri Jun 19 09:22:52 1998
   @author     Tom Goodale
   @desc 
   Recovers a GH.
   @enddesc 
   @calls IOUtil_PrepareFilename IOFlexIO_RestoreIEEEIOfile
   @calledby   
   @history 
   @hauthor Gabrielle Allen
   @hdate Thu Jul  2 18:17:59 1998
   @hdesc Restore the physical time and iteration count, pass myproc to
          IEEEIOparamRestore
	  Derives the filename from IOUtil_PrepareFilename (in thorn IOUtil)
   @hauthor Gabrielle Allen @hdate Oct 17 1998
   @hdesc Added input of (some) GH structure variables
   @endhistory 

@@*/

int IOFlexIO_RecoverGH (cGH *GH, const char *basename, int called_from)
{
  DECLARE_CCTK_PARAMETERS
  IOFile ifp;
  char fname [1024];
  int proc, nprocs, myproc;
  int index;
  int is_IEEEIO_file;
  Long nels_stored;
  int nt_stored;
  int main_loop_index_stored;
  int cctk_iteration_stored;
  CCTK_REAL cctk_time_stored;
  int file_ioproc, file_ioproc_every;
  int file_nprocs, file_unchunked;
  CCTK_INT4 tmpInt;
  CCTK_REAL tmpDouble;
  char msg [512];
  pGH *pughGH;
  ioGH *ioUtilGH;
#if 0
  cTimer total_time, dataset_time, param_time;
#endif
#ifdef MPI
  CCTK_INT info [3];
#endif


  /* just to make the compiler happy */
  ifp = (IOFile) -1;
  is_IEEEIO_file = 0;
  file_ioproc_every = 0;
  file_unchunked = 0;

  /* Get the handles for PUGH and IOUtil extensions */
  pughGH = (pGH *) GH->extensions [CCTK_GHExtensionHandle ("PUGH")];
  ioUtilGH = (ioGH *) GH->extensions [CCTK_GHExtensionHandle ("IO")];

  /* identify myself */
  nprocs = CCTK_nProcs (GH);
  myproc = CCTK_MyProc (GH);

#if 0
  /* initialize timers */
  CactusResetTimer (&total_time);
  CactusResetTimer (&dataset_time);
  CactusResetTimer (&param_time);

  CactusStartTimer (&total_time);
#endif

  /* Examine base file to find whether recovering from
   * one (recombined) file or from multiple files
   */

  if (myproc == 0) {
    
    /* Determine name of base file
       NOTE: As we don't know whether the file is chunked or not
             we need to try both file names. */
    /* at first try with current chunking mode */
    file_unchunked = ioUtilGH->unchunked;
    IOUtil_PrepareFilename(GH, basename, fname, called_from, 0, file_unchunked);
    if (called_from == CP_RECOVER_DATA)
      strcat (fname, ".chkpt");
    strcat (fname, ".ieee");

    if (verbose)
      printf ("Opening file %s\n", fname);

    /* Open file, make sure the file is valid */
    ifp = IEEEopen (fname, "r");
    if (IOisValid (ifp))
      is_IEEEIO_file = 1;
    else {
      if (verbose)
        printf ("Cannot open file '%s'\n", fname);

      /* now try with the other chunking mode */
      file_unchunked = ! ioUtilGH->unchunked;
      IOUtil_PrepareFilename (GH, basename, fname, called_from,
                          0, file_unchunked);
      if (called_from == CP_RECOVER_DATA)
        strcat (fname, ".chkpt");
      strcat (fname, ".ieee");
      if (verbose)
        printf ("Trying now file '%s'...\n", fname);

      /* Open file, make sure the file is valid */
      ifp = IEEEopen (fname, "r");
      is_IEEEIO_file = IOisValid (ifp);
    }
  }
    
  if (myproc == 0 && is_IEEEIO_file) {
    /* Now determine how the data was written */

    /* Read nioprocs used to write data */
    index = IOreadAttributeInfo (ifp, "GH$ioproc_every", &nt_stored, &nels_stored);
    if (index >= 0 && nt_stored == FLEXIO_INT4 && nels_stored == 1) {
      IOreadAttribute (ifp, index, &tmpInt);
      file_ioproc_every = tmpInt;
    } else {
      CCTK_WARN (1, "Unable to restore GH$ioproc_every. "
                 "Assuming it is nprocs and continuing");
      file_ioproc_every = nprocs;
    }

    /* Read nprocs used to write data */
    index = IOreadAttributeInfo (ifp, "GH$nprocs", &nt_stored, &nels_stored);
    if (index >= 0 && nt_stored == FLEXIO_INT4 && nels_stored == 1) {
      IOreadAttribute (ifp, index, &tmpInt);
      file_nprocs = tmpInt;
    } else {
      CCTK_WARN (1, "Unable to restore GH$nprocs. "
                 "Assuming it is 1 and continuing");
      file_nprocs = 1;
    }
    
    /* Determine whether data is chunked or unchunked
       We could derive this from the filename itself but just to be sure ... */
    index = IOreadAttributeInfo (ifp, "unchunked", &nt_stored, &nels_stored);
    if (index >= 0 && nt_stored == FLEXIO_INT4 && nels_stored == 1) {
      IOreadAttribute (ifp, index, &tmpInt);
      file_unchunked = tmpInt;
    } else {
      sprintf (msg, "Unable to restore 'unchunked' attribute. "
               "Assuming it is %s and continuing",
               file_unchunked ? "true" : "false");
      CCTK_WARN (1, msg);
    }

    /* If we restore from multiple files
     * the number of processors must match.
     */
 
    if (file_ioproc_every == nprocs || file_unchunked) {
      if (verbose)
        printf ("Recovering from one %s file\n",
                file_unchunked ? "unchunked" : "chunked");
      file_ioproc_every = nprocs;
    } else {
      if (file_nprocs != nprocs) {
        sprintf (msg, "Must restart on %d processors with multiple files "
                 "or recombine them", file_nprocs);
        CCTK_WARN (0, msg);
      }
      if (verbose)
        printf ("Recovering from %d chunked files\n",
                nprocs / file_ioproc_every + (nprocs % file_ioproc_every?1:0));
    }
  }
  
#ifdef MPI
  /* Broadcast chunking mode to all processors from processor zero */
  info [0] = is_IEEEIO_file;
  info [1] = file_unchunked;
  info [2] = file_ioproc_every;
  CACTUS_MPI_ERROR (MPI_Bcast (info, 3, PUGH_MPI_INT, 0,
                    pughGH->PUGH_COMM_WORLD));
  is_IEEEIO_file = info [0];
  /* FIXME: Parameter on LHS */
  file_unchunked = info [1];
  file_ioproc_every = info [2];
#endif

  /* return here to IOUtil if no valid file could be found */
  if (! is_IEEEIO_file) {
    if (myproc == 0) {
      sprintf (msg, "No valid IEEEIO file '%s' found !", fname);
      CCTK_WARN (2, msg);
    }
    return (-1);
  }

  /* Determine the IO processors for each node and the corresponding
     checkpoint file */
  file_ioproc = myproc - (myproc % file_ioproc_every);
  IOUtil_PrepareFilename (GH, basename, fname, called_from,
                      file_ioproc/file_ioproc_every, file_unchunked);
  if (called_from == CP_RECOVER_DATA)
    strcat (fname, ".chkpt");
  strcat (fname, ".ieee");

  /* Open chunked files on other IO processors */
  if (myproc != 0 && myproc == file_ioproc) {
      
    if (verbose)
      printf ("Opening chunked file '%s' on processor %d.\n",
              fname, myproc);

    /* Open file, make sure the file is valid */
    ifp = IEEEopen (fname, "r");
    if (! IOisValid (ifp)) {
      sprintf (msg, "Cannot open file '%s' on processor %d",
               fname, myproc);
      CCTK_WARN (0, msg);
    }
  }
 
  /* Restore the data */
  if (verbose && myproc == 0)
    printf ("Recovering %schunked data with ioproc %d, ioproc_every %d.\n",
            file_unchunked ? "un" : "", file_ioproc, file_ioproc_every);

#if 0
  CactusStartTimer (&dataset_time);
#endif
  IOFlexIO_RestoreIEEEIOfile (GH, ifp, file_ioproc, file_ioproc_every,
                            file_unchunked);
#if 0
  CactusStopTimer (&dataset_time);
#endif
  /* Close the file. */
  if (myproc == file_ioproc) {
    if (verbose)
      printf ("Closing file '%s' after recovery.\n", fname);
    IOclose (ifp);
  }
 
  if (called_from == CP_RECOVER_DATA) {
    /* Must read in parameters and scalars on all processors. */
#if 0
    CactusStartTimer (&param_time);
#endif
    for (proc = file_ioproc;
         proc < file_ioproc+file_ioproc_every && proc < nprocs;
         proc++) {
    
      /* Only have the file open by one proc at any time. */
      if (proc == myproc) {
      
        /* Open file, make sure the file is valid */
        ifp = IEEEopen (fname, "r");
        if (! IOisValid (ifp)) {
          sprintf (msg, "Cannot open checkpoint file '%s' on processor %d",
                   fname, myproc);
          CCTK_WARN (0, msg);
        }

        /* Restore the parameters. */
        if (verbose)
          printf ("Recovering parameters on processor %d.\n", myproc);
/*** FIXME ***/
#if	0
        IO_IEEEIOparamRestore (ifp, myproc);
      
        /* Restore the structure variables. */
        if (verbose)
          printf ("Recovering GH variables.\n");
        IO_IEEEIOStructRestore (GH, ifp);
#endif

        /* Restore global variables */
      
        /* Get the main loop index. */
        if (verbose)
          printf ("Recovering main loop index.\n");
        index = IOreadAttributeInfo (ifp, "main loop index", &nt_stored, &nels_stored);
	
        if (index >= 0 && nt_stored == FLEXIO_INT4 && nels_stored == 1) {
          IOreadAttribute (ifp, index, &tmpInt);
          main_loop_index_stored = (int) tmpInt;
          if (verbose)
            printf ("main loop index is %d\n", (int) main_loop_index_stored);
          CCTK_SetMainLoopIndex (main_loop_index_stored);
        } else
          printf ("*Warning* Unable to restore main loop index\n");
      
        /* Get the iteration number. */
        if (verbose)
          printf ("Recovering GH->cctk_iteration.\n");
        index = IOreadAttributeInfo (ifp, "GH$iteration", &nt_stored, &nels_stored);
	
        if (index >= 0 && nt_stored == FLEXIO_INT4 && nels_stored == 1) {
          IOreadAttribute (ifp, index, &tmpInt);
          cctk_iteration_stored = (int) tmpInt;
          if (verbose)
            printf ("cctk_iteration is %d\n", (int) cctk_iteration_stored);
          GH->cctk_iteration = cctk_iteration_stored;
        } else
          printf ("*Warning* Unable to restore GH->cctk_iteration\n");
      
        /* Get cctk_time. */
        if (verbose)
          printf ("Recovering GH->cctk_time.\n");
        index = IOreadAttributeInfo (ifp, "GH$time", &nt_stored, &nels_stored);
	
        if (index >= 0 && nt_stored == FLEXIO_REAL && nels_stored == 1) {
          IOreadAttribute (ifp, index, &tmpDouble);
          cctk_time_stored = (double) tmpDouble;
          if (verbose)
            printf ("GH->cctk_time is %f\n", cctk_time_stored);
          GH->cctk_time = cctk_time_stored;
        } else
          printf ("*Warning* Unable to restore GH->cctk_time\n");
      
        /* Close the file. */
        if (verbose)
          printf ("Closing '%s' after recovery.\n", fname);
        IOclose (ifp);
      }
    
      /* Synchronise all processors */
      CCTK_Barrier (GH);
    }
#if 0
    CactusStopTimer (&param_time);
#endif
  }

  /* print timing output */
  if (verbose && called_from == CP_RECOVER_DATA && myproc == 0) {
#if 0
    printf (
      "----------------------------------------------------------------\n");
/*** FIXME: choose right component of basic[] ***/
    printf ("Time to restore data:            %10.3f sec\n",
            dataset_time.total.basic [0]);
    printf ("Time to restore parameters:      %10.3f sec\n",
            param_time.total.basic [0]);
    printf ("Time to recover from checkpoint: %10.3f sec\n",
            total_time.total.basic [0]);
#endif
  }

  return (0);
}