aboutsummaryrefslogtreecommitdiff
path: root/src/DumpGH.c
blob: 3b75071cae8500e94ceff25ff79a18ce85d22afc (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
 /*@@
   @file      DumpGH.c
   @date      Wed Jun 10 14:13:35 1998
   @author    Paul Walker
   @desc
              Checkpoint routines scheduled at CCTK_CPINITIAL, CCTK_CHECKPOINT,
              and CCTK_TERMINATE.
              They check the IO checkpointing parameters and - if it's time
              to do so - call the routine which finally creates a checkpoint.
   @enddesc
   @version   $Id$
 @@*/


#include "cctk.h"
#include "cctk_Version.h"
#include "cctk_Parameters.h"
#include "CactusBase/IOUtil/src/ioGH.h"
#include "CactusBase/IOUtil/src/ioutil_CheckpointRecovery.h"
#include "ioFlexGH.h"

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

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


/********************************************************************
 ********************    External Routines   ************************
 ********************************************************************/
void IOFlexIO_InitialDataCheckpoint (const cGH *GH);
void IOFlexIO_EvolutionCheckpoint (const cGH *GH);
void IOFlexIO_TerminationCheckpoint (const cGH *GH);


/********************************************************************
 ********************    Internal Routines   ************************
 ********************************************************************/
static int Checkpoint (const cGH *GH, int called_from);
 /*@@
   @routine    IOFlexIO_InitialDataCheckpoint
   @date       Fri Aug 21 14:46:28 1998
   @author     Gerd Lanfermann
   @desc
               This routine is registered at CCTK_CPINITIAL.
               It checks if initial data should be checkpointed.
   @enddesc

   @calls      Checkpoint

   @var        GH
   @vdesc      pointer to CCTK grid hierarchy
   @vtype      const cGH *
   @vio        in
   @endvar
@@*/
void IOFlexIO_InitialDataCheckpoint (const cGH *GH)
{
  DECLARE_CCTK_PARAMETERS


  if (checkpoint && checkpoint_ID)
  {
    if (! CCTK_Equals (verbose, "none"))
    {
      CCTK_INFO ("---------------------------------------------------------");
      CCTK_INFO ("Dumping initial data checkpoint");
      CCTK_INFO ("---------------------------------------------------------");
    }
    Checkpoint (GH, CP_INITIAL_DATA);
  }
}


 /*@@
   @routine    IOFlexIO_EvolutionCheckpoint
   @date       Fri Aug 21 14:38:25 1998
   @author     Gabrielle Allen
   @desc
               This routine is registered at CCTK_CHECKPOINT.
               It periodically checks if it's time to checkpoint evolution data.
   @enddesc

   @calls      Checkpoint

   @var        GH
   @vdesc      pointer to CCTK grid hierarchy
   @vtype      const cGH *
   @vio        in
   @endvar
@@*/
void IOFlexIO_EvolutionCheckpoint (const cGH *GH)
{
  DECLARE_CCTK_PARAMETERS


  if (checkpoint &&
      ((checkpoint_every > 0 && GH->cctk_iteration % checkpoint_every == 0) ||
       checkpoint_next))
  {
    if (! CCTK_Equals (verbose, "none"))
    {
      CCTK_INFO ("---------------------------------------------------------");
      CCTK_VInfo (CCTK_THORNSTRING, "Dumping periodic checkpoint at "
                  "iteration %d", GH->cctk_iteration);
      CCTK_INFO ("---------------------------------------------------------");
    }
    Checkpoint (GH, CP_EVOLUTION_DATA);

    /* reset the 'checkpoint_next' parameter */
    if (checkpoint_next)
    {
      CCTK_ParameterSet ("checkpoint_next", CCTK_THORNSTRING, "no");
    }
  }
}


 /*@@
   @routine    IOFlexIO_TerminationCheckpoint
   @date       Fri Aug 21 14:40:21 1998
   @author     Gabrielle Allen
   @desc
               This routine is registered at CCTK_TERMINATE.
               It checks if the last iteration should be checkpointed.
   @enddesc

   @calls      Checkpoint

   @var        GH
   @vdesc      pointer to CCTK grid hierarchy
   @vtype      const cGH *
   @vio        in
   @endvar
@@*/
void IOFlexIO_TerminationCheckpoint (const cGH *GH)
{
  const flexioGH *myGH;
  DECLARE_CCTK_PARAMETERS


  myGH = CCTK_GHExtension (GH, "IOFlexIO");
  if (checkpoint && checkpoint_on_terminate && myGH)
  {
    if (myGH->last_checkpoint_iteration < GH->cctk_iteration)
    {
      if (! CCTK_Equals (verbose, "none"))
      {
        CCTK_INFO ("---------------------------------------------------------");
        CCTK_VInfo (CCTK_THORNSTRING, "Dumping termination checkpoint at "
                    "iteration %d", GH->cctk_iteration);
        CCTK_INFO ("---------------------------------------------------------");
      }
      Checkpoint (GH, CP_EVOLUTION_DATA);
    }
    else if (! CCTK_Equals (verbose, "none"))
    {
      CCTK_INFO ("---------------------------------------------------------");
      CCTK_VInfo (CCTK_THORNSTRING, "Termination checkpoint already dumped "
                  "as last evolution checkpoint at iteration %d",
                  myGH->last_checkpoint_iteration);
      CCTK_INFO ("---------------------------------------------------------");
    }
  }
}


 /*@@
   @routine    IOFlexIOi_DumpParameters
   @date       Thu Jan 20 2000
   @author     Thomas Radke
   @desc
               Gets the parameters of all active implementations and writes them
               as a single string attribute into a group GLOBAL_PARAMETERS.
   @enddesc
   @calls      IOUtil_GetAllParameters

   @var        GH
   @vdesc      pointer to CCTK grid hierarchy
   @vtype      const cGH *
   @vio        in
   @endvar
   @var        all
   @vdesc      flag indicating whether to save all parameters or just the ones
               which have been set before
   @vtype      int
   @vio        in
   @endvar
   @var        file
   @vdesc      the IEEEIO file where to dump the parameters to
   @vtype      IOFile
   @vio        in
   @endvar
@@*/
void IOFlexIOi_DumpParameters (const cGH *GH, int all, IOFile file)
{
  char *parameters;


  parameters = IOUtil_GetAllParameters (GH, all);

  if (parameters)
  {
    FLEXIO_ERROR (IOwriteAttribute (file, GLOBAL_PARAMETERS, CHAR,
                                    strlen (parameters) + 1, parameters));
    free (parameters);
  }
}


 /*@@
   @routine    IOFlexIOi_DumpGHExtensions
   @date       Thu Jan 20 2000
   @author     Thomas Radke
   @desc
               Dumps the important variables from the grid hierarchy, PUGH,
               and IO into a group of an already opened IEEEIO file.
   @enddesc

   @var        GH
   @vdesc      pointer to CCTK grid hierarchy
   @vtype      const cGH *
   @vio        in
   @endvar
   @var        file
   @vdesc      the IEEEIO file where to dump the GH extensions to
   @vtype      IOFile
   @vio        in
   @endvar
@@*/
void IOFlexIOi_DumpGHExtensions (const cGH *GH, IOFile file)
{
  CCTK_INT4 itmp;
  CCTK_REAL dtmp;
  const char *version;
  ioGH *ioUtilGH;


  /* get the handle for IOUtil extensions */
  ioUtilGH = CCTK_GHExtension (GH, "IO");

  itmp = CCTK_MainLoopIndex ();
  FLEXIO_ERROR (IOwriteAttribute (file, "main loop index",FLEXIO_INT4,1,&itmp));

  itmp = GH->cctk_iteration;
  FLEXIO_ERROR (IOwriteAttribute (file, "GH$iteration", FLEXIO_INT4, 1, &itmp));

  itmp = ioUtilGH->ioproc_every;
  FLEXIO_ERROR (IOwriteAttribute (file, "GH$ioproc_every",FLEXIO_INT4,1,&itmp));

  itmp = CCTK_nProcs (GH);
  FLEXIO_ERROR (IOwriteAttribute (file, "GH$nprocs", FLEXIO_INT4, 1, &itmp));

  dtmp = GH->cctk_time;
  FLEXIO_ERROR (IOwriteAttribute (file, "GH$time", FLEXIO_REAL, 1, &dtmp));

  version = CCTK_FullVersion ();
  FLEXIO_ERROR (IOwriteAttribute (file, "Cactus version", CHAR,
                                  strlen (version) + 1, version));
}


/********************************************************************
 ********************    Internal Routines   ************************
 ********************************************************************/
 /*@@
   @routine    Checkpoint
   @date       Fri Aug 21 15:13:13 1998
   @author     Paul Walker
   @desc
               The heart of checkpointing.
               Called by the different wrappers, this routine creates
               a new checkpoint file and then dumps away all grid variables.
   @enddesc

   @calls      IOUtil_AssembleFilename
               IOUtil_PrintTimings
               IOFlexIOi_DumpParameters
               IOFlexIOi_DumpGHExtensions

   @var        GH
   @vdesc      pointer to CCTK grid hierarchy
   @vtype      const cGH *
   @vio        in
   @endvar
   @var        called_from
   @vdesc      flag indicating where this routine was called from
               (either CHECKPOINT_ID, CHECKPOINT, or filereader)
   @vtype      int
   @vio        in
   @endvar

   @history
   @hdate      Oct 4 1998
   @hauthor    Gabrielle Allen
   @hdesc      Removed code which checked and reset unchunked, assume that this
               is done when the variable is first set.
   @hdate      Nov 4 1998
   @hauthor    Gabrielle Allen
   @hdesc      Do a forced synchronization before checkpointing
   @hdate      Apr 16 1999
   @hauthor    Thomas Radke
   @hdesc      Removed forced sync before checkpointing (just do a normal sync)
               Introduced a ring buffer for keeping last <checkpoint_keep> files
   @endhistory

   @returntype int
   @returndesc
               the accumulated return code from @seeroutine IOFlexIO_DumpVar
               (should be 0 for success, or negative if some error occured), or
               -1 if checkpoint file could not be created
   @endreturndesc
@@*/
static int Checkpoint (const cGH *GH, int called_from)
{
  IOFile file;
  int dim, groupvarsize, myproc, first_vindex, gindex, retval;
  cGroup gdata;
  cGroupDynamicData gdynamicdata;
  ioRequest *request;
  flexioGH *myGH;
  const ioGH *ioUtilGH;
  char *filename, *tempname, *fullname;
  static char **cp_filenames = NULL;
  static int cp_fileindex = 0;
  const char *timer_descriptions[] = {"Time to dump parameters: ",
                                      "Time to dump datasets:   ",
                                      "Total time to checkpoint:"};
  DECLARE_CCTK_PARAMETERS


  retval = 0;

  /* allocate the ring buffer for checkpoint filenames */
  if (! cp_filenames)
  {
    cp_filenames = calloc (abs (checkpoint_keep), sizeof (char *));
  }

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

  myproc = CCTK_MyProc (GH);
  ioUtilGH = CCTK_GHExtension (GH, "IO");

  /* get the filenames for both the temporary and final checkpoint file */
  filename = IOUtil_AssembleFilename (GH, NULL, "", ".ieee", called_from,
                                      myproc / ioUtilGH->ioproc_every,
                                      ioUtilGH->unchunked);
  tempname = IOUtil_AssembleFilename (GH, NULL, ".tmp", ".ieee", called_from,
                                      myproc / ioUtilGH->ioproc_every,
                                      ioUtilGH->unchunked);

  /* now open the file */
  if (myproc == ioUtilGH->ioproc)
  {
    if (CCTK_Equals (verbose, "full"))
    {
      CCTK_VInfo (CCTK_THORNSTRING, "Creating file '%s'", tempname);
    }
    file = IEEEopen (tempname, "w");
    if (! IOisValid (file))
    {
      CCTK_VWarn (1, __LINE__, __FILE__, CCTK_THORNSTRING,
                  "Can't open checkpoint file '%s'. Checkpointing is skipped",
                  tempname);
      return (-1);
    }
  }
  else
  {
    file = (IOFile) NULL;
  }

  /* start timer for dumping parameters */
  if (myGH->print_timing_info)
  {
    CCTK_TimerResetI (myGH->timers[0]);
    CCTK_TimerStartI (myGH->timers[0]);
  }

  /* first dump the parameters ... */
  if (file)
  {
    if (CCTK_Equals (verbose, "full"))
    {
      CCTK_INFO ("Dumping Params ...");
    }
    IOFlexIOi_DumpParameters (GH, 1, file);
    IOFlexIOi_DumpGHExtensions (GH, file);
  }

  /* stop parameter timer and start timer for dumping datasets */
  if (myGH->print_timing_info)
  {
    CCTK_TimerStopI (myGH->timers[0]);
    CCTK_TimerResetI (myGH->timers[1]);
    CCTK_TimerStartI (myGH->timers[1]);
  }

  /* ... now the variables, sorted by groups */
  if (CCTK_Equals (verbose, "full"))
  {
    CCTK_INFO ("Dumping Grid Variables ...");
  }
  for (gindex = CCTK_NumGroups () - 1; gindex >= 0; gindex--)
  {
    /* skip empty groups */
    if (CCTK_NumVarsInGroupI (gindex) <= 0)
    {
      continue;
    }

    /* only dump groups which have storage assigned */
    if (CCTK_QueryGroupStorageI (GH, gindex) <= 0)
    {
      continue;
    }

    /* get the number of allocated timelevels */
    CCTK_GroupData (gindex, &gdata);
    gdata.numtimelevels = 0;
    gdata.numtimelevels = CCTK_GroupStorageIncrease (GH, 1, &gindex,
                                                     &gdata.numtimelevels,NULL);

    /* dump all timelevels except the oldest (for multi-level groups) */
    if (gdata.numtimelevels > 1)
    {
      gdata.numtimelevels--;
    }

    /* skip zero-sized groups */
    CCTK_GroupDynamicData (GH, gindex, &gdynamicdata);
    groupvarsize = 1;
    for (dim = 0; dim < gdynamicdata.dim; dim++)
    {
      groupvarsize *= gdynamicdata.gsh[dim];
    }
    if (groupvarsize <= 0)
    {
      continue;
    }

    first_vindex = CCTK_FirstVarIndexI (gindex);

    /* get the default I/O request for this group */
    request = IOUtil_DefaultIORequest (GH, first_vindex, 1);

    /* disable checking for old data objects,
       disable datatype conversion and downsampling,
       request hyperslab output including ghostzones */
    request->check_exist = 0;
    request->hdatatype = gdata.vartype;
    for (request->hdim = 0; request->hdim < request->vdim; request->hdim++)
    {
      request->downsample[request->hdim] = 1;
    }
    request->with_ghostzones = 1;

    /* loop over all variables in this group */
    for (request->vindex = first_vindex;
         request->vindex < first_vindex + gdata.numvars;
         request->vindex++)
    {
      /* loop over all allocated timelevels of this variable */
      for (request->timelevel = 0;
           request->timelevel < gdata.numtimelevels;
           request->timelevel++)
      {
        if (CCTK_Equals (verbose, "full"))
        {
          fullname = CCTK_FullName (request->vindex);
          CCTK_VInfo (CCTK_THORNSTRING, "  %s (timelevel %d)",
                      fullname, request->timelevel);
          free (fullname);
        }

        retval += IOFlexIO_DumpVar (GH, request, file);
      }
    } /* end of loop over all variables */
  } /* end of loop over all groups */

  /* stop timer for dumping datasets */
  if (myGH->print_timing_info)
  {
    CCTK_TimerStopI (myGH->timers[1]);
  }

  /* close the temporary checkpoint file */
  FLEXIO_ERROR (IOclose (file));

  /* free I/O request */
  IOUtil_FreeIORequest (&request);

  if (myproc == ioUtilGH->ioproc)
  {
    if (CCTK_Equals (verbose, "full"))
    {
      CCTK_VInfo (CCTK_THORNSTRING, "Closing and renaming checkpoint file "
                  "into '%s'", filename);
    }

#ifdef _WIN32
    /* Windows' rename(2) routine isn't POSIX compatible in that it would
       unlink an existing file */
    unlink (filename);
#endif
    if (rename (tempname, filename))
    {
      CCTK_VWarn (0, __LINE__, __FILE__, CCTK_THORNSTRING,
                  "Could not rename temporary checkpoint file %s to %s",
                  tempname, filename);
    }
  }

  /* delete the oldest dumpfile if checkpoint_keep_all isn't set
     and put the new filename into the ring buffer */
  if (cp_filenames[cp_fileindex])
  {
    if (checkpoint_keep > 0)
    {
      remove (cp_filenames[cp_fileindex]);
    }
    free (cp_filenames[cp_fileindex]);
  }
  cp_filenames[cp_fileindex] = strdup (filename);
  cp_fileindex = (cp_fileindex + 1) % abs (checkpoint_keep);

  /* stop total checkpoint timer and print timing info */
  if (myGH->print_timing_info)
  {
    CCTK_TimerStopI (myGH->timers[2]);
    IOUtil_PrintTimings ("Timing information for checkpointing in IOFlexIO:", 3,
                         myGH->timers, timer_descriptions);
  }

  /* save the iteration number of this checkpoint */
  myGH->last_checkpoint_iteration = GH->cctk_iteration;

  /* free allocated resources */
  free (tempname);
  free (filename);

  return (retval);
}