aboutsummaryrefslogtreecommitdiff
path: root/src/DumpGH.c
blob: 522d0208c120f133de649103d22226925f5d60c9 (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
 /*@@
   @file      DumpGH.c
   @date      Wed Jun 10 14:13:35 1998
   @author    Paul Walker
   @desc 
   DumpGH dumps an entire grid hierarchy (except for 1D arrays) to a 
   checkpoint file. This file also contains the different wrappers for
   IOFlexIODumpGH: in case of initial data, termination or regular checkpointing
   @enddesc 
   @version $Id$
 @@*/

static char *rcsid = "$Id$";

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

#include "cctk.h"
#include "cctk_parameters.h"
#include "cctk_ParameterFunctions.h"
#include "CactusBase/IOUtil/src/ioGH.h"
#include "ioFlexGH.h"


 /*@@
   @routine    IOFlexIO_ConditionallyDumpGH
   @date       Fri Aug 21 14:38:25 1998
   @author    Gabrielle Allen
   @desc 
     registered as CCTK_CHECKPOINT, checks if it's time for 
     checkpointing, sets the checkpoint type and calls DumpGH 
   @enddesc 
   @calls    IOFlexIO_DumpGH
   @history 
 
   @endhistory 

@@*/

void IOFlexIO_ConditionallyDumpGH (cGH *GH)
{
  DECLARE_CCTK_PARAMETERS

  if (checkpoint_every > 0 &&
      GH->cctk_iteration % checkpoint_every == 0) {
    if (verbose) {
      printf ("------------------------------------------------------------\n");
      printf ("Dumping periodic checkpoint file at iteration %d\n",
              GH->cctk_iteration);
    }
    IOFlexIO_DumpGH (GH, CP_EVOLUTION_DATA);
  }
  USE_CCTK_PARAMETERS
}


 /*@@
   @routine    IOFlexIO_TerminationDumpGH
   @date       Fri Aug 21 14:40:21 1998
   @author     Gabrielle Allen
   @desc 
     This routine is registered as CCTK_TERMINATE, it checks if a
     termination signal is raised (that is: global var cactus_terminate
     set to TERMINATION_RAISED_BRDCAST by ./steppers/TerminationStepper.c)
     and checkpoints all available grid hierarchies.
   @enddesc 
   @calls     IOFlexIO_DumpGH
   @history 
 
   @endhistory 

@@*/

void IOFlexIO_TerminationDumpGH (cGH *GH)
{
/*** FIXME: no cactus_terminate flag anymore ? ***/
#if	0
  if (cactus_terminate == TERMINATION_RAISED_BRDCAST) {
    IOFlexIO_DumpGH (GH, CP_EVOLUTION_DATA); 
  }
#else
  CCTK_WARN (1, "TerminationDumpGH() not yet implemented !");
#endif
}

 /*@@
   @routine    IOFlexIO_InitialDataDumpGH
   @date       Fri Aug 21 14:46:28 1998
   @author     Gerd Lanfermann
   @desc 
     This routine dumps the initial data of the GHs, registered 
     as CCTK_CPINITIAL; sets cp_type to CP_INITIAL_DATA and calls
     DumpGH; BoxInBox needs special treatment since for now.
   @enddesc 
   @calls     IOFlexIO_DumpGH
   @history 
 
   @endhistory 

@@*/

void IOFlexIO_InitialDataDumpGH (cGH *GH)
{
/*** FIXME ***/
#if	0
  cGH *helperGH;
  static int maxlev = 0;
  int l;

#ifdef THORN_BOXINBOX
  /* This will go once we reorganize the CINitial calling structure! */
  /* or have a function to deregister a rfr routine */
  /* BoxinBox does its own: get maxlevel (once!) and do dumpGH for all GH*/
  /* when this is called by the highest box */
  if (Contains("boxinbox","yes")) {
    if (maxlev==0) while (GHbyLevel(0,maxlev+1,0)) maxlev++;
    if (maxlev==0) return; 
    
    if (GH->level==maxlev) 
      for (l=0;l<=maxlev;l++) {
	printf("Dumping BoxinBox level %d of %d \n",l,maxlev);
	IOFlexIO_DumpGH(GHbyLevel(0,l,0), CP_INITIAL_DATA);
      }
    return;
  }
#endif
#endif
  IOFlexIO_DumpGH (GH, CP_INITIAL_DATA);
}


void IOFlexIO_DumpParams (cGH *GH, IOFile iof)
{
  int first;
  const char *param;


  first = 1;
  while ((param = ParameterWalk (first, NULL)) != NULL) {
    char *impl, *name, *aname, *value;
  
    first = 0;

    if (Util_SplitString (&impl, &name, param, "::") != 0)
      CCTK_VWarn (2, __LINE__, __FILE__, CCTK_THORNSTRING,
                  "Couldn't parse name of parameter '%s'", param);
    else {
      value = ParameterValString (name, CCTK_ImplementationThorn (impl));
      if (value == NULL)
        CCTK_VWarn (2, __LINE__, __FILE__, CCTK_THORNSTRING,
                    "Couldn't get value for parameter '%s'", param);
      else {
        aname = (char *) malloc (sizeof (PARAMETER_PREFIX) +
                                 sizeof (ANAME_DELIMITER) + strlen (param));
        sprintf (aname, "%s%s%s", PARAMETER_PREFIX, ANAME_DELIMITER, param);

        CACTUS_IEEEIO_ERROR (IOwriteAttribute (iof, aname, FLEXIO_CHAR,
                             strlen (value) + 1, value));
        free (aname);
        free (value);
      }

      free (impl);
      free (name);

    }

    free ((void *) param);

  } /* end of loop walking over all parameters */
}


void IOFlexIO_DumpGHExtensions (cGH *GH, IOFile iof)
{
  CCTK_INT4 i_temp;
  CCTK_REAL d_temp;
  ioGH *ioUtilGH;

  /* Get the handle for IOUtil extensions */
  ioUtilGH = (ioGH *) GH->extensions [CCTK_GHExtensionHandle ("IO")];

  i_temp = CCTK_MainLoopIndex ();
  CACTUS_IEEEIO_ERROR (IOwriteAttribute (iof, "main loop index", FLEXIO_INT4,
                       1, &i_temp));

  i_temp = GH->cctk_iteration;
  CACTUS_IEEEIO_ERROR (IOwriteAttribute (iof, "GH$iteration", FLEXIO_INT4,
                       1, &i_temp));

  i_temp = ioUtilGH->ioproc_every;
  CACTUS_IEEEIO_ERROR (IOwriteAttribute (iof, "GH$ioproc_every", FLEXIO_INT4,
                       1, &i_temp));

  i_temp = CCTK_nProcs (GH);
  CACTUS_IEEEIO_ERROR (IOwriteAttribute (iof, "GH$nprocs", FLEXIO_INT4,
                       1, &i_temp));

  d_temp = GH->cctk_time;
  CACTUS_IEEEIO_ERROR (IOwriteAttribute (iof, "GH$time", FLEXIO_REAL,
                       1, &d_temp));
}


 /*@@
   @routine    IOFlexIO_DumpGH
   @date       Fri Aug 21 15:13:13 1998
   @author     Paul Walker
   @desc 
     The heart of checkpointing. Called by the different wrappers, this
     routines get the appropriate filename by IOUtil_PrepareFilename() and 
     then dumps away using the dump routines from IO ...
   @enddesc 
   @calls IO_DumpGF IO_DumpArray IO_DumpScalar
   @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 

@@*/

void IOFlexIO_DumpGH (cGH *GH, int called_from)
{
  DECLARE_CCTK_PARAMETERS
  char dumpfname [1024], tmpfname [1024];
  IOFile iof;
  int index, wrotech;
  int timelevel, current_timelevel;
  int old_downsample_x, old_downsample_y, old_downsample_z;
  int old_out_single;
  ioGH *ioUtilGH;
  flexioGH *myGH;
  static char **dumpfnames = NULL;  /* dump filename ring buffer */
  static int findex = 0;            /* index into ring buffer */


  /* Get the handles for IOUtil and IOFlexIO extensions */
  ioUtilGH = (ioGH *) GH->extensions [CCTK_GHExtensionHandle ("IO")];
  myGH = (flexioGH *) GH->extensions [CCTK_GHExtensionHandle ("IOFlexIO")];

  /* allocate the ring buffer for filenames */
  if (! dumpfnames)
    dumpfnames = (char **) calloc (checkpoint_keep, sizeof (char *));

  /* disable downsampling after saving original downsampling params */
  old_downsample_x = ioUtilGH->downsample_x;
  old_downsample_y = ioUtilGH->downsample_y;
  old_downsample_z = ioUtilGH->downsample_z;
  ioUtilGH->downsample_x = ioUtilGH->downsample_y = ioUtilGH->downsample_z = 1;

  /* disable output in single precision */
  old_out_single = ioUtilGH->out_single;
  ioUtilGH->out_single = 0;

  /* start the total timer */
  if (print_timing_info)
    CCTK_TimerStartI (myGH->checkpointTotalTimer);

  /* sync all groups */
  for (index = 0; index < CCTK_NumGroups (); index++) {
    char *gname = CCTK_GroupName (index);

    CCTK_SyncGroup (GH, gname);
    free (gname);
  }

  /* get the base filename ... */
  IOUtil_PrepareFilename (GH, NULL, dumpfname, called_from,
                          CCTK_MyProc (GH) / ioUtilGH->ioproc_every,
                          ioUtilGH->unchunked);

  /* ... and append the extension */
  sprintf (tmpfname,  "%s.chkpt_tmp.ieee", dumpfname);
  sprintf (dumpfname, "%s.chkpt.ieee",     dumpfname);

  /* Now open the file */
  if (CCTK_MyProc (GH) == ioUtilGH->ioproc) {
    if (verbose)
      printf ("Creating file %s\n", tmpfname);
    iof = IEEEopen (tmpfname, "w");
    if (! IOisValid (iof)) {
      CCTK_VWarn (1, __LINE__, __FILE__, CCTK_THORNSTRING,
                  "Can't open checkpoint file '%s'. Checkpointing is skipped",
                  tmpfname);
      return;
    }
  } else
    iof = (IOFile) NULL;

  /* Great; Now start dumping away! */

  /* start timer for dumping parameters */
  if (print_timing_info) {
    CCTK_TimerResetI (myGH->dumpParamsTimer);
    CCTK_TimerStartI (myGH->dumpParamsTimer);
  }

  if (verbose)
    printf ("Dumping Params ...\n -- ");

  /* first the parameters ... */
  if (iof) {
    if (out3D_parameters)
      IOFlexIO_DumpParams (GH, iof);
    IOFlexIO_DumpGHExtensions (GH, iof);
  }

  /* stop parameter timer and start timer for dumping datasets */
  if (print_timing_info) {
    CCTK_TimerStopI (myGH->dumpParamsTimer);
    CCTK_TimerResetI (myGH->dumpVarsTimer);
    CCTK_TimerStartI (myGH->dumpVarsTimer);
  }

  wrotech = 0;
 
  /* ... now the variables */
  for (index = 0; index < CCTK_NumVars (); index++) {

    /* let only variables pass which have storage assigned */
    if (! CCTK_QueryGroupStorageI (GH, CCTK_GroupIndexFromVarI (index)))
      continue;

    if (verbose) {
      char *varname = CCTK_VarName (index);

      printf (" %s", varname);
      wrotech += strlen (varname) + 1;
      if (wrotech > 65) {
        printf ("\n    ");
        wrotech = 0;
      }
    }

    /* get the current timelevel */
    current_timelevel = CCTK_NumTimeLevelsFromVarI (index) - 1;
    if (current_timelevel > 0)
      current_timelevel--;

    /* now dump all timelevels up to the current */
    for (timelevel = 0; timelevel <= current_timelevel; timelevel++)
      IOFlexIO_DumpVar (GH, index, timelevel, iof);
  } /* end of loop over all variables */

  /* stop timer for dumping datasets */
  if (print_timing_info)
    CCTK_TimerStopI (myGH->dumpVarsTimer);

  CACTUS_IEEEIO_ERROR (IOclose (iof));
  
  if (CCTK_MyProc (GH) == ioUtilGH->ioproc) {
    if (rename (tmpfname, dumpfname))
      CCTK_VWarn (0, __LINE__, __FILE__, CCTK_THORNSTRING,
                  "Could not rename temporary checkpoint file %s to %s",
                  tmpfname, dumpfname);
  }

  /* delete the oldest dumpfile if checkpoint_keep_all isn't set
     and put the new filename into the ring buffer */
  if (dumpfnames [findex]) {
    if (! checkpoint_keep_all)
      remove (dumpfnames [findex]);
    free (dumpfnames [findex]);
  }
  dumpfnames [findex] = strdup (dumpfname);
  findex = (findex + 1) % checkpoint_keep;

  /* restore output precision flag */
  ioUtilGH->out_single = old_out_single;

  /* restore original downsampling params */
  ioUtilGH->downsample_x = old_downsample_x;
  ioUtilGH->downsample_y = old_downsample_y;
  ioUtilGH->downsample_z = old_downsample_z;

  /* stop total checkpoint timer and print timing info */
  if (print_timing_info) {
    t_TimerInfo *info;

    CCTK_TimerStopI (myGH->checkpointTotalTimer);

    printf ("%s timing information for checkpointing:\n", CCTK_THORNSTRING);

/*** FIXME: select timer to get info from at runtime ***/
#ifdef HAVE_TIME_GETTIMEOFDAY
    info = CCTK_TimerCreateInfo ();
    if (info) {

      if (myGH->dumpParamsTimer >= 0) {
        CCTK_TimerGetI (myGH->dumpParamsTimer, info);
        printf ("  Time to dump parameters:  %5.1f sec\n", info->vals[0].val.d);
      }

      if (myGH->dumpVarsTimer >= 0) {
        CCTK_TimerGetI (myGH->dumpVarsTimer, info);
        printf ("  Time to dump datasets:    %5.1f sec\n", info->vals[0].val.d);
      }

      if (myGH->checkpointTotalTimer >= 0) {
        CCTK_TimerGetI (myGH->checkpointTotalTimer, info);
        printf ("  Total time to checkpoint: %5.1f sec\n", info->vals[0].val.d);
      }

      CCTK_TimerDestroyInfo (info);
    } else
      CCTK_WARN (1, "Couldn't create timer info structure ! No timing output "
                    "available.");
#else
    CCTK_WARN (1, "No GETTIMEOFDAY timer available.");
#endif

    printf ("----------------------------------------------------------------"
            "-----------\n");
  }

  USE_CCTK_PARAMETERS

}