From 4e8fb785349dd8222fe1d4ea66e1c9053ee8e17d Mon Sep 17 00:00:00 2001 From: tradke Date: Wed, 30 Jun 2004 14:16:34 +0000 Subject: Use the new routine IOUtil_AssembleFilename() to generate filenames for checkpoint/filereader files. This closes PR CactusPUGHIO 1061: "IOHDF5/DumpGH.c/Checkpoint writes to strings without checking the string lengths". git-svn-id: http://svn.cactuscode.org/arrangements/CactusPUGHIO/IOHDF5/trunk@199 4825ed28-b72c-4eae-9704-e50c059e567d --- src/DumpGH.c | 71 +++++++++++++++++++++++++++++---------------------------- src/RecoverGH.c | 47 +++++++++++++++++--------------------- 2 files changed, 57 insertions(+), 61 deletions(-) diff --git a/src/DumpGH.c b/src/DumpGH.c index 9b66404..471e5c8 100644 --- a/src/DumpGH.c +++ b/src/DumpGH.c @@ -51,13 +51,13 @@ static int Checkpoint (const cGH *GH, int called_from); @routine IOHDF5_InitialDataCheckpoint @date Fri Aug 21 14:46:28 1998 @author Gerd Lanfermann - @desc + @desc This routine is registered at CCTK_CPINITIAL. It checks if initial data should be checkpointed. - @enddesc + @enddesc @calls Checkpoint - + @var GH @vdesc Pointer to CCTK grid hierarchy @vtype const cGH * @@ -86,13 +86,13 @@ void IOHDF5_InitialDataCheckpoint (const cGH *GH) @routine IOHDF5_EvolutionCheckpoint @date Fri Aug 21 14:38:25 1998 @author Gabrielle Allen - @desc + @desc This routine is registered at CCTK_CHECKPOINT. It periodically checks if it's time to checkpoint evolution data. - @enddesc + @enddesc @calls Checkpoint - + @var GH @vdesc Pointer to CCTK grid hierarchy @vtype const cGH * @@ -130,10 +130,10 @@ void IOHDF5_EvolutionCheckpoint (const cGH *GH) @routine IOHDF5_TerminationCheckpoint @date Fri Aug 21 14:40:21 1998 @author Gabrielle Allen - @desc + @desc This routine is registered at CCTK_TERMINATE. It checks if the last iteration should be checkpointed. - @enddesc + @enddesc @calls Checkpoint @@ -149,7 +149,7 @@ void IOHDF5_TerminationCheckpoint (const cGH *GH) DECLARE_CCTK_PARAMETERS - myGH = (const ioHDF5GH *) CCTK_GHExtension (GH, "IOHDF5"); + myGH = CCTK_GHExtension (GH, "IOHDF5"); if (checkpoint && checkpoint_on_terminate && myGH) { if (myGH->last_checkpoint_iteration < GH->cctk_iteration) @@ -182,12 +182,12 @@ void IOHDF5_TerminationCheckpoint (const cGH *GH) @routine Checkpoint @date Fri Aug 21 15:13:13 1998 @author Paul Walker - @desc + @desc The heart of checkpointing. Called by the different wrappers, this routine creates a new checkpoint file and then dumps away using the dump routines from IOHDF5Util. - @enddesc + @enddesc @calls IOHDF5Util_DumpGH @@ -219,11 +219,10 @@ static int Checkpoint (const cGH *GH, int called_from) #ifdef CCTK_MPI CCTK_INT tmp; #endif - /* FIXME: allocate filenames dynamically */ - char cp_filename[1024], cp_tempname[1024]; - const char *timer_descriptions[3] = {"Time to dump parameters: ", - "Time to dump datasets: ", - "Total time to checkpoint:"}; + char *filename, *tempname; + const char *timer_descriptions[] = {"Time to dump parameters: ", + "Time to dump datasets: ", + "Total time to checkpoint:"}; DECLARE_CCTK_PARAMETERS @@ -231,8 +230,8 @@ static int Checkpoint (const cGH *GH, int called_from) file = -1; myproc = CCTK_MyProc (GH); - ioUtilGH = (ioGH *) CCTK_GHExtension (GH, "IO"); - myGH = (ioHDF5GH *) CCTK_GHExtension (GH, "IOHDF5"); + ioUtilGH = CCTK_GHExtension (GH, "IO"); + myGH = CCTK_GHExtension (GH, "IOHDF5"); /* check if IOHDF5 was registered as I/O method */ if (myGH == NULL) @@ -247,30 +246,28 @@ static int Checkpoint (const cGH *GH, int called_from) CCTK_TimerStartI (myGH->timers[CP_TOTAL_TIMER]); } - /* get the checkpoint basename ... */ - IOUtil_PrepareFilename (GH, NULL, cp_filename, called_from, - myproc / ioUtilGH->ioproc_every, - ioUtilGH->unchunked); - - /* ... and append the extension */ - sprintf (cp_tempname, "%s.tmp.h5", cp_filename); - sprintf (cp_filename, "%s.h5", cp_filename); + /* get the filenames for both the temporary and real checkpoint file */ + filename = IOUtil_AssembleFilename (GH, NULL, "", ".h5", called_from, + myproc / ioUtilGH->ioproc_every, + ioUtilGH->unchunked); + tempname = IOUtil_AssembleFilename (GH, NULL, ".tmp", ".h5", 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 checkpoint file '%s'", - cp_tempname); + CCTK_VInfo (CCTK_THORNSTRING, "Creating checkpoint file '%s'", tempname); } - file = H5Fcreate (cp_tempname, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); + file = H5Fcreate (tempname, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); if (file < 0) { CCTK_VWarn (1, __LINE__, __FILE__, CCTK_THORNSTRING, "Can't open checkpoint file '%s'. Checkpointing is skipped.", - cp_tempname); + tempname); retval = -1; } } @@ -300,7 +297,7 @@ static int Checkpoint (const cGH *GH, int called_from) { CCTK_VInfo (CCTK_THORNSTRING, "Closing temporary checkpoint file '%s' " "and renaming it to '%s'", - cp_tempname, cp_filename); + tempname, filename); } if (file >= 0) @@ -315,13 +312,13 @@ static int Checkpoint (const cGH *GH, int called_from) #ifdef _WIN32 /* Windows' rename(2) routine isn't POSIX compatible in that it would unlink an existing file :-( */ - unlink (cp_filename); + unlink (filename); #endif - if (rename (cp_tempname, cp_filename)) + if (rename (tempname, filename)) { CCTK_VWarn (1, __LINE__, __FILE__, CCTK_THORNSTRING, "Could not rename temporary checkpoint file '%s' to '%s'", - cp_tempname, cp_filename); + tempname, filename); retval = -1; } else @@ -334,7 +331,7 @@ static int Checkpoint (const cGH *GH, int called_from) } free (myGH->cp_filename_list[myGH->cp_filename_index]); } - myGH->cp_filename_list[myGH->cp_filename_index] = strdup (cp_filename); + myGH->cp_filename_list[myGH->cp_filename_index] = strdup (filename); myGH->cp_filename_index = (myGH->cp_filename_index+1) % abs (checkpoint_keep); } } @@ -351,5 +348,9 @@ static int Checkpoint (const cGH *GH, int called_from) /* save the iteration number of this checkpoint */ myGH->last_checkpoint_iteration = GH->cctk_iteration; + /* free allocated resources */ + free (tempname); + free (filename); + return (retval); } diff --git a/src/RecoverGH.c b/src/RecoverGH.c index 4732cea..e80f6ab 100644 --- a/src/RecoverGH.c +++ b/src/RecoverGH.c @@ -2,7 +2,7 @@ @file RecoverGH.c @date Fri Jun 19 09:14:22 1998 @author Tom Goodale - @desc + @desc Contains the routines to recover from a HDF5 checkpoint file. Currently can recover from: @@ -10,7 +10,7 @@ (2) Multiple unrecombined files, where the current number of processors and IO processors match those used to write the files. - @enddesc + @enddesc @version $Id$ @@*/ @@ -47,11 +47,11 @@ static int OpenFile (cGH *GH, const char *basefilename, int called_from, @routine IOHDF5_Recover @date Fri Jun 19 09:22:52 1998 @author Tom Goodale - @desc + @desc Recovers a GH from an HDF5 file. This routine is registered with IOUtil as IOHDF5's recovery routine. - @enddesc + @enddesc @var GH @vdesc Pointer to CCTK grid hierarchy @vtype cGH @@ -120,7 +120,7 @@ int IOHDF5_Recover (cGH *GH, const char *basefilename, int called_from) else { /* This is the case for CP_RECOVER_DATA. - CCTK_RECOVER_PARAMETERS must have been called before + CCTK_RECOVER_PARAMETERS must have been called before and set up the file info structure. */ if (! fileinfo.is_HDF5_file) { @@ -187,12 +187,10 @@ int IOHDF5_Recover (cGH *GH, const char *basefilename, int called_from) strdup (fileinfo.filename); } } - + /* free the allocated filename */ - if (fileinfo.filename) - { - free (fileinfo.filename); - } + free (fileinfo.filename); + fileinfo.filename = NULL; /* stop total recovery timer and print timing info */ if (called_from == CP_RECOVER_DATA && myGH->print_timing_info) @@ -258,8 +256,8 @@ int IOHDF5_RecoverParameters (void) @desc Open a HDF5 file given by its basefilename. The basefilename is expanded into a full filename by calling - IOUtil_PrepareFilename. Both chunked and unchunked filenames - are tested. + IOUtil_AssembleFilename(). Both chunked and unchunked + filenames are tested. If a file of that name could be opened it checks whether we can recover from that file. The file information is then broadcasted by the IO processor(s) @@ -298,8 +296,7 @@ static int OpenFile (cGH *GH, const char *basefilename, int called_from, { int nprocs, myproc; hid_t group, version_attr; - /* FIXME: want dynamic allocation of the filename */ - char filename[256]; + char *filename; #ifdef CCTK_MPI MPI_Comm comm; CCTK_INT4 info[4]; @@ -331,9 +328,8 @@ static int OpenFile (cGH *GH, const char *basefilename, int called_from, we need to try both file names. */ /* at first try with unchunked mode */ fileinfo->unchunked = 1; - IOUtil_PrepareFilename (GH, basefilename, filename, called_from, 0, - fileinfo->unchunked); - strcat (filename, ".h5"); + filename = IOUtil_AssembleFilename (GH, basefilename, "", ".h5", called_from, + 0, fileinfo->unchunked); if (myproc == 0) { @@ -357,9 +353,9 @@ static int OpenFile (cGH *GH, const char *basefilename, int called_from, /* now try with chunked mode */ fileinfo->unchunked = 0; - IOUtil_PrepareFilename (GH, basefilename, filename, called_from, 0, - fileinfo->unchunked); - strcat (filename, ".h5"); + free (filename); + filename = IOUtil_AssembleFilename (GH, basefilename, "", ".h5", + called_from, 0,fileinfo->unchunked); if (CCTK_Equals (verbose, "full")) { @@ -411,7 +407,7 @@ static int OpenFile (cGH *GH, const char *basefilename, int called_from, HDF5_ERROR (H5Gclose (group)); /* If we recover from multiple files the number of - * writing processors must match the number of reading + * writing processors must match the number of reading * processors, and the total number of processors must match. */ if ((fileinfo->ioproc_every == nprocs && nprocs > 1) || @@ -473,10 +469,9 @@ static int OpenFile (cGH *GH, const char *basefilename, int called_from, /* Determine the IO processors for each node and the corresponding checkpoint file */ fileinfo->ioproc = myproc - (myproc % fileinfo->ioproc_every); - IOUtil_PrepareFilename (GH, basefilename, filename, called_from, - fileinfo->ioproc / fileinfo->ioproc_every, - fileinfo->unchunked); - strcat (filename, ".h5"); + filename = IOUtil_AssembleFilename (GH, basefilename, "", ".h5",called_from, + fileinfo->ioproc/fileinfo->ioproc_every, + fileinfo->unchunked); /* Open chunked files on other IO processors */ if (myproc == fileinfo->ioproc && myproc != 0) @@ -506,7 +501,7 @@ static int OpenFile (cGH *GH, const char *basefilename, int called_from, } /* set the filename in the info structure */ - fileinfo->filename = strdup (filename); + fileinfo->filename = filename; /* return 0 for success otherwise negative */ return (fileinfo->is_HDF5_file ? 0 : -1); -- cgit v1.2.3