/*@@ @file CheckpointRecovery.c @date Jun 04 1999 @author Thomas Radke @desc Utility routines for checkpointing/recovery and the filereader The actual work is done by the IO thorns. @@*/ #include #include #include #include "cctk.h" #include "cctk_parameters.h" #include "ioGH.h" #include "StoreHandledData.h" /* Local data holding info on Recover Functions*/ static cHandledData *RecoverFunctions = NULL; static int num_functions = 0; /************************************************************************ * * Registration functions for Restoring from IO Files * ************************************************************************/ /*@@ @routine IOUtil_RegisterRecover @date Monday 21 June 1999 @author Gabrielle Allen @desc Registers a new recovery method @enddesc @calls Util_GetHandle Util_NewHandle @history @endhistory @var name @vdesc The name of the function for recovery @vtype const char * @vio in @vcomment @endvar @returntype int @returndesc -2 = memory allocation failed -1 = method with this name already registered i>0 = handle for function @endreturndesc @version $Header$ @@*/ int IOUtil_RegisterRecover(const char *name, int (*func)(cGH *, const char *, int)) { int handle; /* Check that the method hasn't already been registered */ handle = Util_GetHandle(RecoverFunctions, name, NULL); if(handle < 0) { /* New function */ /* Get a handle for it. */ handle = Util_NewHandle(&RecoverFunctions, name, func); /* Remember how many methods there are */ num_functions++; } else { /* function already exists. */ handle = -1; } return handle; } /*@@ @routine IOUtil_PrepareFilename @date Fri Aug 21 14:54:38 1998 @author Gerd Lanfermann @desc This routine prepares the filenames for the checkpoint/recovery and filereader files, paying attention to the different types: * it returns the full filename (directory+filename) * it prepends "Ln_" level indicators to the filename and "low_"/"med_" for convergence levels > 1 * for checkpoint files it prepends the iteration number as "it_%d" * for chunked files it prepends the file number as "file_%d" @enddesc @calls @calledby FlexIO_DumpGH HDF5IO_DumpGH @var GH @vdesc Pointer to CCTK grid hierarchy @vtype cGH @vio in @endvar @var basename @vdesc basename of the file(s) to recover from @vtype const char * @vio in @endvar @var fname @vdesc the resulting filename @vtype char * @vio out @endvar @var called_from @vdesc indicates the caller function: * either Filereader (FILEREADER_DATA) * or IOUtil_RecoverGH() (CP_RECOVER_DATA) @vtype int @vio in @endvar @var file_ioproc @vdesc the IO processor number (for chunked files) @vtype int @vio in @endvar @var file_unchunked @vdesc flag to indicate whether file mode is unchunked or not @vtype int @vio in @endvar @history @hdate Nov 4 1998 @hauthor Gabrielle Allen @hdesc A file_* in the name indicates it needs recombining @hdate Apr 14 1999 @hauthor Thomas Radke @hdesc Removed code for expanding "basedir" and "nameofparfile" @hdate May 06 1999 @hauthor Thomas Radke @hdesc Added parameter unchunked to be independent of current chunking mode for recovery @endhistory @@*/ void IOUtil_PrepareFilename (cGH *GH, const char *basename, char *fname, int called_from, int file_ioproc, int file_unchunked) { DECLARE_CCTK_PARAMETERS /* get the right parameters */ switch (called_from) { case CP_INITIAL_DATA: sprintf (fname, "%s/%s", checkpoint_dir, checkpoint_ID_file); break; case CP_EVOLUTION_DATA: sprintf (fname, "%s/%s", checkpoint_dir, checkpoint_file); break; case CP_RECOVER_DATA: sprintf (fname, "%s/%s", recovery_dir, recover_file); break; case FILEREADER_DATA: strcpy (fname, basename); break; default: CCTK_WARN (2, "Unknown calling mode in IO_PrepareFilename()."); break; } /* add refinement factor and convergence level (med/low) inbetween: */ /* FIXME Gab ... asymmetric levfac */ if (GH->cctk_levfac[0] > 1) sprintf (fname, "%sL%d_", fname, GH->cctk_levfac[0]); if (GH->cctk_convlevel > 1) strcat (fname, GH->cctk_convlevel == 2 ? "med_" : "low_"); /* If checkpoint filename, merge in the iteration number and for chunked files also the file number */ if (called_from == CP_INITIAL_DATA || called_from == CP_EVOLUTION_DATA) sprintf (fname, "%s.it_%d", fname, (int) GH->cctk_iteration); /* If not one unchunked file give a file number */ if (! file_unchunked) sprintf (fname, "%s.file_%d", fname, file_ioproc); } /*@@ @routine IOUtil_RecoverFromFile @date Jun 14 1999 @author Thomas Radke @desc Recover from a given file. This routine loops through all XXX_RecoverGH routines registered by IO thorns. @enddesc @var GH @vdesc Pointer to CCTK grid hierarchy @vtype cGH @vio in @endvar @var basename @vdesc basename of the file(s) to recover from @vtype const char * @vio in @endvar @var called_from @vdesc indicates the caller function: * either Filereader (FILEREADER_DATA) * or IOUtil_RecoverGH() (CP_RECOVER_DATA) @vtype int @vio in @endvar @history @endhistory @@*/ int IOUtil_RecoverFromFile (cGH *GH, const char *basename, int called_from) { int handle; int retval; int (*fnRecoverGH)(cGH *GH, const char *basename, int called_from); retval = -1; for (handle = 0;handle=0) { retval = handle; break; } } else { CCTK_WARN (1, "Could not recover"); break; } } return retval; } /*@@ @routine IOUtil_RecoverGH @date Jun 14 1999 @author Thomas Radke @desc The rfr-registered recovery routine. Just calls IOUtil_RecoverFromFile() with called_from == CP_RECOVER_DATA. @enddesc @var GH @vdesc Pointer to CCTK grid hierarchy @vtype cGH @vio in @endvar @history @endhistory @@*/ void IOUtil_RecoverGH (cGH *GH) { IOUtil_RecoverFromFile (GH, NULL, CP_RECOVER_DATA); }