aboutsummaryrefslogtreecommitdiff
path: root/CarpetAttic/CarpetIOFlexIOCheckpoint/src/checkpointrestart.cc
blob: 5836b194c75bb71dadadcc6dadb60e8e6d6a5bcb (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
#include <assert.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>

#include <algorithm>
#include <fstream>
#include <sstream>
#include <vector>



#include "cctk.h"
#include "cctk_Parameters.h"
#include "cctk_Version.h"

#include "AMRwriter.hh"
#include "AmrGridReader.hh"
#ifdef HDF4
#  include "HDFIO.hh"
#endif
#ifdef HDF5
#  include "H5IO.hh"
#endif
#include "IEEEIO.hh"
#include "IO.hh"

// Hack to stop FlexIO type clash
#undef BYTE
#undef CHAR

#include "CactusBase/IOUtil/src/ioGH.h"
#include "CactusBase/IOUtil/src/ioutil_CheckpointRecovery.h"
#include "CactusBase/IOUtil/src/ioutil_Utils.h"

#include "bbox.hh"
#include "data.hh"
#include "gdata.hh"
#include "ggf.hh"
#include "vect.hh"

//#include "StoreNamedData.h"
#include "carpet.hh"

#include "ioflexio.hh"

extern "C" {
  static const char* rcsid = "$Header: /home/eschnett/C/carpet/Carpet/CarpetAttic/CarpetIOFlexIOCheckpoint/src/checkpointrestart.cc,v 1.5 2003/07/14 15:41:08 schnetter Exp $";
  CCTK_FILEVERSION(Carpet_CarpetIOFlexIO_checkpointrestart_cc);
}

namespace CarpetCheckpointRestart {

  using namespace std;
  using namespace Carpet;
  using namespace CarpetIOFlexIO;

  static int Checkpoint (const cGH* const cgh, int called_from);



  void CarpetChReEvolutionCheckpoint( const cGH* const cgh){
    
    DECLARE_CCTK_PARAMETERS

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

       	if (checkpoint_next)
	{
	  CCTK_ParameterSet ("checkpoint_next", CCTK_THORNSTRING, "no");
	}
      }
    

  }

/********************************************************************
 ********************    Internal Routines   ************************
 ********************************************************************/


  static int DumpParams (const cGH* const cgh, int all, IObase* writer){

    char *parameters;
    
    parameters = IOUtil_GetAllParameters(cgh,all);

    if(parameters)
    {
      writer->writeAttribute("global_parameters",IObase::Char,
			     strlen(parameters)+1,parameters);
      free(parameters);
    }
    return 0;
  }

  static int DumpGHExtensions (const cGH* const cgh, IObase* writer){

    CCTK_INT4 itmp;
    CCTK_REAL dtmp;
    const char *version;
    ioGH *ioUtilGH;


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

    itmp = CCTK_MainLoopIndex ();
    writer->writeAttribute("main loop index",FLEXIO_INT4,1,&itmp);

    itmp = cgh->cctk_iteration;
    writer->writeAttribute("GH$iteration",FLEXIO_INT4, 1, &itmp);

    itmp = ioUtilGH->ioproc_every;
    writer->writeAttribute("GH$ioproc_every",FLEXIO_INT4,1,&itmp);

    itmp = CCTK_nProcs (cgh);
    writer->writeAttribute("GH$nprocs",FLEXIO_INT4, 1, &itmp);

    dtmp = cgh->cctk_time;
    writer->writeAttribute("GH$time", FLEXIO_REAL, 1, &dtmp);

    version = CCTK_FullVersion ();
    writer->writeAttribute("Cactus version", FLEXIO_CHAR,
                                  strlen (version) + 1, version);



    return 0;
  }


  static int Checkpoint (const cGH* const cgh, int called_from)
  {
    char filename[1024];
    int myproc, first_vindex, gindex, retval;
    char *fullname;
    const char *timer_descriptions[3] = {"Time to dump parameters: ",
					 "Time to dump datasets:   ",
					 "Total time to checkpoint:"};
    const ioGH *ioUtilGH;

    //    const int varindex = CCTK_VarIndex("ADMBASE:gxx");
    int varindex = 0;
    int group = 0;

    cGroup  gdata;
    IObase* writer = 0;
    AMRwriter* amrwriter = 0;
    ioRequest *request;

    DECLARE_CCTK_PARAMETERS
     
    myproc = CCTK_MyProc (cgh);
    ioUtilGH = (const ioGH *) CCTK_GHExtension (cgh, "IO");
    IOUtil_PrepareFilename (cgh, NULL, filename, called_from,
            myproc / ioUtilGH->ioproc_every, ioUtilGH->unchunked);
    
    sprintf(filename, "%s.ieee",filename);

    fprintf(stderr,"%s\n",filename);

    if (CCTK_MyProc(cgh)==0)
      {
	if (CCTK_Equals ("verbose", "full"))
	  {
	    CCTK_VInfo (CCTK_THORNSTRING, "Creating file '%s'", filename);
	  }

	writer = new IEEEIO(filename, IObase::Create);

	if (! (writer->isValid()) )
	  {
	    CCTK_VWarn (1, __LINE__, __FILE__, CCTK_THORNSTRING,
			"Can't open checkpoint file '%s'. Checkpointing is skipped",
			filename);
	    return (-1);
	  }
	amrwriter = new AMRwriter(*writer);


	// dump parameters 
	DumpParams (cgh, 1, writer);
	// dump GH extentions
	DumpGHExtensions(cgh,writer);

      }

    
    // now dump the grid varibles, sorted by groups

    if (CCTK_Equals ("verbose", "full"))
      {
	CCTK_INFO ("Dumping Grid Variables ...");
      }
    for (group = CCTK_NumGroups () - 1; group >= 0; group--)
      {
	/* only dump groups which have storage assigned */
	if (CCTK_QueryGroupStorageI (cgh, group) <= 0)
	  {
	    continue;
	  }

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

	int first_vindex = CCTK_FirstVarIndexI (group);

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

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

	/* loop over all variables in this group */
	for (request->vindex = first_vindex;
	     request->vindex < first_vindex + gdata.numvars;
	     request->vindex++)
	  {
	    /* loop over all 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);
		  }
		// write the var
		retval += WriteVarAs(cgh,writer,amrwriter,request->vindex);
	      }
	  } /* end of loop over all variables */

      } /* end of loop over all groups */


    // Close the file
    if (CCTK_MyProc(cgh)==0) {
      delete amrwriter;
      amrwriter = 0;
      delete writer;
      writer = 0;
    }


    return 0;
      }


} // namespace CarpetCheckpointRestart