aboutsummaryrefslogtreecommitdiff
path: root/src/RecoverGH.c
blob: 272022828f4f2c7aa010f742ceae04e091e2764a (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
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
 /*@@
   @file      RecoverGH.c
   @date      Fri Jun 19 09:14:22 1998
   @author    Tom Goodale
   @desc
              Routines to recover variables from a given IEEEIO data or
              checkpoint file.
   @enddesc
   @version   $Id$
 @@*/


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

#include "cctk.h"
#include "cctk_Parameters.h"
#include "CactusPUGH/PUGH/src/include/pugh.h"
#include "CactusBase/IOUtil/src/ioutil_CheckpointRecovery.h"
#include "ioFlexGH.h"

/* the rcs ID and its dummy function to use it */
static const char *rcsid = "$Id$";
CCTK_FILEVERSION(CactusPUGHIO_IOFlexIO_RecoverGH_c)


/********************************************************************
 ********************    Macro Definitions   ************************
 ********************************************************************/
/* maximum length of an attribute name */
#define MAX_ATTRNAME_LEN 256


/********************************************************************
 ********************    External Routines   ************************
 ********************************************************************/
int IOFlexIO_RecoverParameters (void);


/********************************************************************
 ********************    Internal Routines   ************************
 ********************************************************************/
static int RecoverParameters (fileinfo_t *file);
static int RecoverGHextensions (cGH *GH, fileinfo_t *file);
static int OpenFile (cGH *GH, const char *basefilename, int called_from,
                     fileinfo_t *file);


 /*@@
   @routine    IOFlexIO_Recover
   @date       Fri Jun 19 09:22:52 1998
   @author     Tom Goodale
   @desc
               Recovers a GH from an IEEEIO file.
               This routine is registered with IOUtil as IOFlexIO's recovery
               routine.
   @enddesc

   @calls      OpenFile
               RecoverParameters
               RecoverGHextensions
               IOFlexIOi_RecoverVariables
               IOUtil_PrintTimings

   @var        GH
   @vdesc      Pointer to CCTK grid hierarchy
   @vtype      cGH *
   @vio        in
   @endvar
   @var        basefilename
   @vdesc      the basefilename of the file to recover from
               The file suffix is appended by the routine.
   @vtype      const char *
   @vio        in
   @endvar
   @var        called_from
   @vdesc      flag indicating where this routine was called from
               (either CP_RECOVER_DATA or FILEREADER_DATA)
   @vtype      int
   @vio        in
   @endvar

   @returntype int
   @returndesc
               >0 = success
               -1 = recovery failed
   @endreturndesc
@@*/
int IOFlexIO_Recover (cGH *GH, const char *basefilename, int called_from)
{
  int result;
  flexioGH *myGH;
  static fileinfo_t file;      /* this is static because info is passed from
                                  CP_RECOVERY_PARAMETERS to CP_RECOVERY_DATA */
  const char *timer_description = "Time to recover:";
  DECLARE_CCTK_PARAMETERS


  /* to make the compiler happy */
  myGH = NULL;
  result = 0;

  /* start the total timer */
  if (GH)
  {
    myGH = CCTK_GHExtension (GH, "IOFlexIO");
    if (myGH->print_timing_info)
    {
      CCTK_TimerStartI (myGH->timers[3]);
    }
  }

  /* open the file if it wasn't already opened at CCTK_RECOVER_PARAMETERS */
  /* FIXME Gab ... asymmetric levfac */
  if (called_from == CP_RECOVER_PARAMETERS ||
      called_from == FILEREADER_DATA ||
      (GH && (GH->cctk_levfac[0] > 1 || GH->cctk_convlevel > 0)))
  {
    memset (&file, 0, sizeof (file));
    if (OpenFile (GH, basefilename, called_from, &file) < 0)
    {
      return (-1);
    }
  }
  else
  {
    /* This is the case for CP_RECOVER_DATA.
       CCTK_RECOVER_PARAMETERS must have been called before
       and set up the file info structure. */
    if (! file.is_IEEEIO_file)
    {
      return (-1);
    }
  }

  /* Recover parameters (and return) */
  if (called_from == CP_RECOVER_PARAMETERS)
  {
    return (RecoverParameters (&file));
  }

  /* Recover GH extensions */
  if (called_from == CP_RECOVER_DATA)
  {
    if (CCTK_Equals (verbose, "full"))
    {
      CCTK_INFO ("Recovering GH extensions");
    }
    result = RecoverGHextensions (GH, &file);
  }

  if (! result)
  {
    /* Recover variables */
    if (CCTK_Equals (verbose, "full"))
    {
      CCTK_VInfo (CCTK_THORNSTRING, "Recovering %schunked data with ioproc %d, "
                  "ioproc_every %d", file.unchunked ? "un" : "", file.ioproc,
                  file.ioproc_every);
    }
    result = IOFlexIOi_RecoverVariables (GH, &file);
  }

  /* Close the file and remove it if requested by the user */
  if (CCTK_MyProc (GH) == file.ioproc)
  {
    if (CCTK_Equals (verbose, "full"))
    {
      if (called_from == CP_RECOVER_DATA)
      {
        CCTK_VInfo (CCTK_THORNSTRING, "Closing checkpoint file '%s' after "
                    "recovery", file.filename);
      }
      else
      {
        CCTK_VInfo (CCTK_THORNSTRING, "Closing data file '%s'", file.filename);
      }
    }
    FLEXIO_ERROR (IOclose (file.fid));

    if (called_from == CP_RECOVER_DATA && recover_and_remove)
    {
      if (CCTK_Equals (verbose, "full"))
      {
        CCTK_VInfo (CCTK_THORNSTRING, "Old checkpoint file '%s' will be removed"
                                      " after next IO::checkpoint_keep "
                                      "successful checkpoints", file.filename);
      }
      myGH->cp_filenames[myGH->cp_fileindex] = strdup (file.filename);
      myGH->cp_fileindex = (myGH->cp_fileindex+1) % checkpoint_keep;
    }
  }

  /* stop total recovery timer and print timing info */
  if (called_from == CP_RECOVER_DATA && myGH->print_timing_info)
  {
    CCTK_TimerStopI (myGH->timers[3]);
    IOUtil_PrintTimings ("Timing information for recovery in IOFlexIO:", 1,
                         &myGH->timers[3], &timer_description);
  }

  /* print an info message */
  if (called_from == CP_RECOVER_DATA)
  {
    CCTK_VInfo (CCTK_THORNSTRING,
                "Restarting simulation at iteration %d (physical time %g)",
                GH->cctk_iteration, (double) GH->cctk_time);
  }

  return (result);
}


 /*@@
   @routine    IOFlexIO_RecoverParameters
   @date       Thu Apr 13 2000
   @author     Thomas Radke
   @desc
   @desc
               Recovers the parameters from an IEEEIO checkpoint file.
               This routine is scheduled at CCTK_RECOVER_PARAMETERS.

               Note that it cannot be registered with IOUtil to be scheduled
               from there (as done with the IOFlexIO_Recover routine) because
               the registration mechanism isn't activated yet
               at CCTK_RECOVER_PARAMETERS.
               Instead we call the generic parameter recovery routine
               from IOUtil here, and just pass the necessary callback function
               and its arguments.

               Note also that this routine doesn't get passed any parameters,
               not even a GH, because this doesn't exist yet at the time it is
               being called.
   @enddesc

   @calls      IOUtil_RecoverParameters

   @returntype int
   @returndesc
               return code of @seeroutine IOUtil_RecoverParameters, ie.
               positive for successful parameter recovery, or<BR>
               0 if recovery wasn't requested, or<BR>
               negative if parameter recovery failed
   @endreturndesc
@@*/
int IOFlexIO_RecoverParameters (void)
{
  return (IOUtil_RecoverParameters (IOFlexIO_Recover, ".ieee", "IEEEIO"));
}


/********************************************************************
 ********************    Internal Routines   ************************
 ********************************************************************/
static int OpenFile (cGH *GH, const char *basefilename, int called_from,
                     fileinfo_t *file)
{
  int i, myproc, nprocs, type, retval;
  Long dim;
#ifdef CCTK_MPI
  MPI_Comm comm;
  CCTK_INT4 info[4];
#endif
  DECLARE_CCTK_PARAMETERS


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

  /* Examine basefile to find out whether we are recovering from
   * one or multiple files and whether the data are chunked or not.
   *
   * This is done by processor 0 only since this is always an IO processor
   * and a corresponding file must exist in all cases.
   */

  /* 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 unchunked mode */
  file->unchunked = 1;
  free (file->filename);
  file->filename = IOUtil_AssembleFilename (GH, basefilename, "", ".ieee",
                                            called_from, 0, file->unchunked);

  if (myproc == 0)
  {
    if (CCTK_Equals (verbose, "full"))
    {
      CCTK_VInfo (CCTK_THORNSTRING, "Opening file '%s'", file->filename);
    }

    /* Open file, make sure the file is valid */
    file->fid = IEEEopen (file->filename, "r");
    file->is_IEEEIO_file = IOisValid (file->fid);
    if (! file->is_IEEEIO_file)
    {
      CCTK_VWarn (2, __LINE__, __FILE__, CCTK_THORNSTRING,
                  "No valid IEEEIO file '%s' found", file->filename);

      /* now try with chunked mode */
      file->unchunked = 0;
      free (file->filename);
      file->filename = IOUtil_AssembleFilename (GH, basefilename, "", ".ieee",
                                                called_from, 0,file->unchunked);

      if (CCTK_Equals (verbose, "full"))
      {
        CCTK_VInfo (CCTK_THORNSTRING, "Trying now file '%s'...",file->filename);
      }

      /* Open file, make sure the file is valid */
      file->fid = IEEEopen (file->filename, "r");
      file->is_IEEEIO_file = IOisValid (file->fid);
    }
  }

  /* okay, we have the complete filename. Let's read the file now. */
  if (myproc == 0 && file->is_IEEEIO_file)
  {
    file->is_IEEEIO_file = 0;

    /* determine how the data was written by reading the GH extensions */
    i = IOreadAttributeInfo (file->fid, "GH$ioproc_every", &type, &dim);
    if (i >= 0 && type == FLEXIO_INT4 && dim == 1)
    {
      IOreadAttribute (file->fid, i, &file->ioproc_every);
    }
    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 */
    i = IOreadAttributeInfo (file->fid, "GH$nprocs", &type, &dim);
    if (i >= 0 && type == FLEXIO_INT4 && dim == 1)
    {
      IOreadAttribute (file->fid, i, &file->nprocs);
    }
    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 ... */
    i = IOreadAttributeInfo (file->fid, "unchunked", &type, &dim);
    if (i >= 0 && type == FLEXIO_INT4 && dim == 1)
    {
      IOreadAttribute (file->fid, i, &file->unchunked);
    }
    else
    {
      CCTK_VWarn (1, __LINE__, __FILE__, CCTK_THORNSTRING,
                  "Unable to restore 'unchunked' attribute. Assuming it is %s "
                  "and continuing", file->unchunked ? "true" : "false");
    }

    /* determine whether data was written using the old timelevel naming
       scheme. New files contain a Cactus version string attribute... */
    i = IOreadAttributeInfo (file->fid, "Cactus version", &type, &dim);
    file->has_version = i >= 0 && type == CHAR;
    if (! file->has_version)
    {
      CCTK_WARN (4, "Unable to restore 'Cactus version' attribute. "
                    "Assuming data was written using the the old timelevel "
                    "naming scheme and continuing");
    }

    /* If we recover from multiple files the number of
     * writing processors must match the number of reading
     * processors, and the total number of processors must match.
     */
    if ((file->ioproc_every == nprocs  && nprocs > 1) || file->unchunked)
    {
      if (CCTK_Equals (verbose, "full"))
      {
        CCTK_VInfo (CCTK_THORNSTRING, "Recovering from one %schunked file",
                                      file->unchunked ? "un":"");
      }
      file->ioproc_every = nprocs;
      file->is_IEEEIO_file = 1;
    }
    else
    {
      if (file->nprocs != nprocs)
      {
        CCTK_VWarn (1, __LINE__, __FILE__, CCTK_THORNSTRING,
                    "Must restart on %d processors with chunked files "
                    "or recombine them", file->nprocs);
      }
      else
      {
        if (CCTK_Equals (verbose, "full"))
        {
          CCTK_VInfo (CCTK_THORNSTRING, "Recovering from %d chunked files",
                      nprocs / file->ioproc_every +
                      (nprocs % file->ioproc_every ? 1 : 0));
        }
        file->is_IEEEIO_file = 1;
      }
    }
  }

  if (myproc == 0 && ! file->is_IEEEIO_file)
  {
    CCTK_VWarn (2, __LINE__, __FILE__, CCTK_THORNSTRING,
                "No valid IEEEIO file '%s' found", file->filename);
  }

#ifdef CCTK_MPI
  /* Get the communicator for broadcasting the info structure */
  /* NOTE: When recovering parameters thorn PUGH is not yet initialized
           so that we have to use MPI_COMM_WORLD in this case */
  comm = CCTK_GHExtensionHandle ("PUGH") < 0 ?
           MPI_COMM_WORLD : PUGH_pGH (GH)->PUGH_COMM_WORLD;

  /* Broadcast the file information to all processors */
  info[0] = file->is_IEEEIO_file;
  info[1] = file->unchunked;
  info[2] = file->ioproc_every;
  info[3] = file->has_version;
  CACTUS_MPI_ERROR (MPI_Bcast (info, 4, PUGH_MPI_INT, 0, comm));
  file->is_IEEEIO_file = info[0];
  file->unchunked = info[1];
  file->ioproc_every = info[2];
  file->has_version = info[3];
#endif

  if (file->is_IEEEIO_file)
  {
    /* Determine the IO processors for each node and the corresponding
       checkpoint file */
    file->ioproc = myproc - (myproc % file->ioproc_every);
    free (file->filename);
    file->filename = IOUtil_AssembleFilename (GH, basefilename, "", ".ieee",
                                              called_from,
                                              file->ioproc / file->ioproc_every,
                                              file->unchunked);

    /* Open chunked files on other IO processors */
    if (myproc == file->ioproc && myproc != 0)
    {
      if (CCTK_Equals (verbose, "full"))
      {
        CCTK_VInfo (CCTK_THORNSTRING, "Opening chunked file '%s' on "
                    "processor %d", file->filename, myproc);
      }

      /* Open file, make sure the file is valid */
      file->fid = IEEEopen (file->filename, "r");
      if (! IOisValid (file->fid))
      {
        CCTK_VWarn (1, __LINE__, __FILE__, CCTK_THORNSTRING,
              "Cannot open file '%s' on processor %d", file->filename, myproc);
        file->is_IEEEIO_file = 0;
      }
    }

#ifdef CCTK_MPI
    /* Finally check whether all files have valid recovery files */
    info[0] = file->is_IEEEIO_file;
    CACTUS_MPI_ERROR (MPI_Allreduce (&info[0], &info[1], 1, PUGH_MPI_INT4,
                                     MPI_LAND, comm));
    file->is_IEEEIO_file = info[1];
#endif
  }

  /* Return 0 for success otherwise negative */
  retval = (file->is_IEEEIO_file ? 0 : -1);

  return (retval);
}


/* NOTE: Although we could read the GH extensions from multiple recovery files
         in parallel, this is done only on by processor 0 here.
         Broadcasting the GH extensions is found faster than
         sending it in a loop from each IO processor to all the non IOs
         (don't have subcommunicators yet) */
static int RecoverGHextensions (cGH *GH, fileinfo_t *file)
{
  Long dim;
  int i, type;
  CCTK_REAL realBuffer;
  CCTK_INT4 int4Buffer[2];


  if (CCTK_MyProc (GH) == 0)
  {
    /* rewind to the first dataset where the GH extensions are attached to */
    FLEXIO_ERROR (IOseek (file->fid, 0));

    /* get the iteration number */
    i = IOreadAttributeInfo (file->fid, "GH$iteration", &type, &dim);
    if (i >= 0 && type == FLEXIO_INT4 && dim == 1)
    {
      IOreadAttribute (file->fid, i, &int4Buffer[0]);
    }
    else
    {
      CCTK_WARN (1, "Unable to restore GH->cctk_iteration, defaulting to 0");
      int4Buffer[0] = 0;
    }

    /* get the main loop index */
    i = IOreadAttributeInfo (file->fid, "main loop index", &type, &dim);
    if (i >= 0 && type == FLEXIO_INT4 && dim == 1)
    {
      IOreadAttribute (file->fid, i, &int4Buffer[1]);
    }
    else
    {
      CCTK_WARN (1, "Unable to restore main loop index, defaulting to 0");
      int4Buffer[1] = 0;
    }

    /* get cctk_time */
    i = IOreadAttributeInfo (file->fid, "GH$time", &type, &dim);
    if (i >= 0 && type == FLEXIO_REAL && dim == 1)
    {
      IOreadAttribute (file->fid, i, &realBuffer);
    }
    else
    {
      CCTK_WARN (1, "Unable to restore GH->cctk_time, defaulting to 0.0");
      realBuffer = 0.0;
    }
  }

#ifdef CCTK_MPI
  /* Broadcast the GH extensions to all processors */
  /* NOTE: We have to use MPI_COMM_WORLD here
     because PUGH_COMM_WORLD is not yet set up at parameter recovery time.
     We also assume that PUGH_MPI_INT4 is a compile-time defined datatype. */
  CACTUS_MPI_ERROR (MPI_Bcast (int4Buffer, 2, PUGH_MPI_INT4, 0,MPI_COMM_WORLD));
  CACTUS_MPI_ERROR (MPI_Bcast (&realBuffer, 1, PUGH_MPI_REAL,0,MPI_COMM_WORLD));
#endif

  GH->cctk_time = realBuffer;
  GH->cctk_iteration = (int) int4Buffer[0];
  CCTK_SetMainLoopIndex ((int) int4Buffer[1]);

  return (0);
}


/* NOTE: Although we could read the parameters from multiple recovery files
         in parallel, this is done only on by processor 0 here.
         Broadcasting the complete parameter string is found faster than
         sending it in a loop from each IO processor to all the non IOs
         (don't have subcommunicators yet) */
static int RecoverParameters (fileinfo_t *file)
{
  int i, myproc, atype, retval;
  Long asize;
  char *parameters;
  CCTK_INT4 parameterSize;
  DECLARE_CCTK_PARAMETERS


  /* To make the compiler happy */
  parameterSize = -1;
  parameters = NULL;

  myproc = CCTK_MyProc (NULL);
  if (myproc == 0)
  {
    if (CCTK_Equals (verbose, "full"))
    {
      CCTK_VInfo (CCTK_THORNSTRING, "Recovering parameters from checkpoint "
                                    "file '%s'", file->filename);
    }

    /* get the parameters attribute. */
    i = IOreadAttributeInfo (file->fid, GLOBAL_PARAMETERS, &atype, &asize);
    if (i >= 0 && atype == CHAR && asize > 0)
    {
      parameterSize = (CCTK_INT4) asize;
      parameters = malloc (parameterSize + 1);
      IOreadAttribute (file->fid, i, parameters);
    }
    else
    {
      CCTK_WARN (1, "Can't read global parameters. "
                    "Is this really a Cactus IEEEIO checkpoint file ?");
    }
  }

#ifdef CCTK_MPI
  /* Broadcast the parameter buffer size to all processors */
  /* NOTE: We have to use MPI_COMM_WORLD here
     because PUGH_COMM_WORLD is not yet set up at parameter recovery time.
     We also assume that PUGH_MPI_INT4 is a compile-time defined datatype. */
  CACTUS_MPI_ERROR (MPI_Bcast (&parameterSize, 1, PUGH_MPI_INT4, 0,
                    MPI_COMM_WORLD));
#endif

  if (parameterSize > 0)
  {
#ifdef CCTK_MPI
    if (myproc)
    {
      parameters = malloc (parameterSize + 1);
    }

    CACTUS_MPI_ERROR (MPI_Bcast (parameters, parameterSize + 1, PUGH_MPI_CHAR,
                      0, MPI_COMM_WORLD));
#endif

    IOUtil_SetAllParameters (parameters);

    free (parameters);
  }

  /* return positive value for success otherwise negative */
  retval = (parameterSize > 0 ? 1 : -1);

  return (retval);
}