diff options
author | tradke <tradke@ebee0441-1374-4afa-a3b5-247f3ba15b9a> | 1999-07-22 17:58:44 +0000 |
---|---|---|
committer | tradke <tradke@ebee0441-1374-4afa-a3b5-247f3ba15b9a> | 1999-07-22 17:58:44 +0000 |
commit | f2adb19bfb1515ef99c702be8970b954951857f8 (patch) | |
tree | 95b6e8bde994db91926fc6975979af55c73ca560 /src/Write3D.c | |
parent | 509db8d8789f68e6d179d20f0ed28de5cdf3b538 (diff) |
Always register IO methods in setup so that one can call them via
CCTK_OutputVarAsByMethod.
Store names of output files in a database instead of a fixed array,
use the alias name as search index.
Fix some compiler warnings.
git-svn-id: http://svn.cactuscode.org/arrangements/CactusPUGHIO/IOFlexIO/trunk@19 ebee0441-1374-4afa-a3b5-247f3ba15b9a
Diffstat (limited to 'src/Write3D.c')
-rw-r--r-- | src/Write3D.c | 225 |
1 files changed, 113 insertions, 112 deletions
diff --git a/src/Write3D.c b/src/Write3D.c index 4681371..cdf9704 100644 --- a/src/Write3D.c +++ b/src/Write3D.c @@ -50,12 +50,19 @@ static char *rcsid = "$Id$"; #include "ioFlexGH.h" +/* info structure describing IEEEIO files + For each opened file there is one entry in the file database. */ +typedef struct { + IOFile iofile; + char *filename; +} IEEEfile_3D_t; + + /* local function prototypes */ -void IOFlexIO_Write3D_filename (cGH *GH, char *filename, const char *name); -void IOFlexIO_Write3D_openFile (cGH *GH, const char *filename, IOFile *iofile, int first); -void IOFlexIO_Write3D_closeFile (cGH *GH, const char *filename, IOFile *iofile); -/* external function prototypes */ -void IOFlexIO_DumpVar (cGH *GH, int index, int timelevel, IOFile iof); +IEEEfile_3D_t *IOFlexIO_Get3Dfile (cGH *GH, const char *alias, int *isNewFile); +void IOFlexIO_Write3D_openFile (cGH *GH, IEEEfile_3D_t *IEEEfile_3D, + int isNewFile); +void IOFlexIO_Write3D_closeFile (cGH *GH, IEEEfile_3D_t *IEEEfile_3D); /*@@ @@ -124,34 +131,24 @@ void IOFlexIO_DumpVar (cGH *GH, int index, int timelevel, IOFile iof); @vio in @vcomment @endvar - @var first - @vdesc flag to indicate whether file should be opened in "w" or "a" mode - @vtype int (boolean) - @vio in - @vcomment - @endvar @@*/ -void IOFlexIO_Write3D (cGH *GH, int index, const char *alias, int first) +void IOFlexIO_Write3D (cGH *GH, int index, const char *alias) { DECLARE_CCTK_PARAMETERS int myproc, nprocs; int timelevel; ioGH *ioUtilGH; flexioGH *myGH; - IOFile *iofile; - char *filename; + int isNewFile; + IEEEfile_3D_t *IEEEfile_3D; /* Get the handle for IOUtil and IOFlexIO extensions */ ioUtilGH = (ioGH *) GH->extensions [CCTK_GHExtensionHandle ("IO")]; myGH = (flexioGH *) GH->extensions [CCTK_GHExtensionHandle ("IOFlexIO")]; - /* Get filename and reference to file handle */ - filename = myGH->IEEEfname_3D [index]; - iofile = &myGH->IEEEfile_3D [index]; - if (output_verbose) { printf ("-------------------------------------------------------\n"); printf ("3D I/O on Grid Function %s\n", alias); @@ -166,29 +163,31 @@ void IOFlexIO_Write3D (cGH *GH, int index, const char *alias, int first) nprocs = CCTK_nProcs (GH); - /* build the filename for output */ - /* This code is semi-repeated in DumpGH.c */ - /* The output directory is also created. */ - if (first || onefileperslice) - IOFlexIO_Write3D_filename (GH, filename, alias); + /* Get the filename and descriptor for output */ + /* The output directory is also created if needed. */ + if (myproc == ioUtilGH->ioproc) + IEEEfile_3D = IOFlexIO_Get3Dfile (GH, alias, &isNewFile); + else + IEEEfile_3D = NULL; +#if 0 /* Close the file if it is already open (if IEEEfile_3D is set). * This shouldn't be needed, since we do it below, * but better safe than sorry. * Also I have some vague recollection of needing it in some * very strange case ... So leave it here for now (PW 11.5.98) */ - if (*iofile && onefileperslice) { + if (IEEEfile_3D->iofile >= 0 && onefileperslice) { if (output_verbose) printf ("Closing IEEEfile from previous iteration.\n"); - CACTUS_IEEEIO_ERROR (IOclose (*iofile)); - *iofile = NULL; /* prevent attempts to close it twice! */ + CACTUS_IEEEIO_ERROR (IOclose (IEEEfile_3D->iofile)); + IEEEfile_3D->iofile = -1; /* prevent attempts to close it twice! */ } - +#endif /* open the output file (only if we are an output processor) */ - if (myproc == ioUtilGH->ioproc) - IOFlexIO_Write3D_openFile (GH, filename, iofile, first); + if (IEEEfile_3D) + IOFlexIO_Write3D_openFile (GH, IEEEfile_3D, isNewFile); /* get the current timelevel */ timelevel = CCTK_NumTimeLevelsFromVarI (index) - 1; @@ -196,22 +195,23 @@ void IOFlexIO_Write3D (cGH *GH, int index, const char *alias, int first) timelevel--; /* output the data */ - IOFlexIO_DumpVar (GH, index, timelevel, *iofile); + IOFlexIO_DumpVar (GH, index, timelevel, + IEEEfile_3D ? IEEEfile_3D->iofile : (IOFile) -1); /* output the scalars and parameters */ /* Don't do this if the output is being byte compared, as the * thorn lists might be different. */ - if (myproc == ioUtilGH->ioproc) { + if (IEEEfile_3D) { /* output parameters necessary for filereader datafiles */ - if (first) - IOFlexIO_IEEEIOStructDump (GH, *iofile); + if (isNewFile) + IOFlexIO_IEEEIOStructDump (GH, IEEEfile_3D->iofile); #if 0 if (ioUtilGH->parameters) - IOFlexIO_IEEEIOparamDump (*iofile); + IOFlexIO_IEEEIOparamDump (IEEEfile_3D->iofile); #endif /* close the file */ - IOFlexIO_Write3D_closeFile (GH, filename, iofile); + IOFlexIO_Write3D_closeFile (GH, IEEEfile_3D); } if (output_verbose) @@ -234,37 +234,29 @@ void IOFlexIO_Write3D (cGH *GH, int index, const char *alias, int first) @@*/ -void IOFlexIO_Write3D_closeFile (cGH *GH, const char *filename, IOFile *iofile) +void IOFlexIO_Write3D_closeFile (cGH *GH, IEEEfile_3D_t *IEEEfile_3D) { DECLARE_CCTK_PARAMETERS - ioGH *ioUtilGH; flexioGH *myGH; - ioUtilGH = (ioGH *) GH->extensions [CCTK_GHExtensionHandle ("IO")]; - myGH = (flexioGH *) GH->extensions [CCTK_GHExtensionHandle ("IOFlexIO")]; - - if (*iofile == NULL) + if (IEEEfile_3D == NULL) return; - if (onefileperslice) { - if (output_verbose) - printf ("Closing IEEEfile from this iteration\n"); - CACTUS_IEEEIO_ERROR (IOclose (*iofile)); - *iofile = NULL; - return; - } - + myGH = (flexioGH *) GH->extensions [CCTK_GHExtensionHandle ("IOFlexIO")]; + if (myGH->reuse_fh) { if (output_verbose) - printf ("Pausing file %s\n", filename); - IEEEbufferOff (*iofile); - CACTUS_IEEEIO_ERROR (IOpause (*iofile)); + printf ("Pausing file %s\n", IEEEfile_3D->filename); + IEEEbufferOff (IEEEfile_3D->iofile); + CACTUS_IEEEIO_ERROR (IOpause (IEEEfile_3D->iofile)); } else { if (output_verbose) - printf ("Closing file %s\n", filename); - CACTUS_IEEEIO_ERROR (IOclose (*iofile)); - *iofile = NULL; + printf ("Closing file %s\n", IEEEfile_3D->filename); + CACTUS_IEEEIO_ERROR (IOclose (IEEEfile_3D->iofile)); } + + if (onefileperslice) + free (IEEEfile_3D); } @@ -282,74 +274,71 @@ void IOFlexIO_Write3D_closeFile (cGH *GH, const char *filename, IOFile *iofile) @endhistory @@*/ -void IOFlexIO_Write3D_openFile (cGH *GH, const char *filename, IOFile *iofile, int first) +void IOFlexIO_Write3D_openFile (cGH *GH, IEEEfile_3D_t *IEEEfile_3D, + int isNewFile) { DECLARE_CCTK_PARAMETERS - ioGH *ioUtilGH; flexioGH *myGH; - ioUtilGH = (ioGH *) GH->extensions [CCTK_GHExtensionHandle ("IO")]; myGH = (flexioGH *) GH->extensions [CCTK_GHExtensionHandle ("IOFlexIO")]; /* If we are the first time through, or writing one file per slice, we need to open a file in write mode. Or, if we are reusing file handles we will need to resume the file */ - if (first || onefileperslice) { - + if (isNewFile) { + /* First time through or one file per slice; open anew */ if (output_verbose) - printf ("Opening file %s\n", filename); - - *iofile = IEEEopen (filename, "w"); - if (! IOisValid (*iofile)) { - char *msg = (char *) malloc (strlen (filename) + 80); + printf ("Opening file '%s'\n", IEEEfile_3D->filename); + + IEEEfile_3D->iofile = IEEEopen (IEEEfile_3D->filename, "w"); + if (! IOisValid (IEEEfile_3D->iofile)) { + char *msg = (char *) malloc (strlen (IEEEfile_3D->filename) + 80); - sprintf (msg, "The file %s could not be opened for writing.", filename); + sprintf (msg, "The file '%s' could not be opened for writing.", + IEEEfile_3D->filename); CCTK_WARN (1, msg); free (msg); } - } - - else if (myGH->reuse_fh) { - + + } else if (myGH->reuse_fh) { + /* resume the file (reopen descriptor) This allows descriptor reuse without requiring expensive destruction and reallocation of the associated datastructures */ - if (*iofile) { - - if (output_verbose) - printf ("Resuming file %s\n", filename); + if (output_verbose) + printf ("Resuming file '%s'\n", IEEEfile_3D->filename); + + CACTUS_IEEEIO_ERROR (IOresume (IEEEfile_3D->iofile)); - CACTUS_IEEEIO_ERROR (IOresume (*iofile)); - } } else { /* File is closed, not onefileperslice and not first time. Therefore we must be conserving file handles and failed to use the pause()/resume methods(), So append. */ if (output_verbose) - printf ("Re-opening file %s in append mode\n", filename); - - *iofile = IEEEopen (filename, "a"); - if (! IOisValid (*iofile)) { - char *msg = (char *) malloc (strlen (filename) + 80); + printf ("Re-opening file '%s' in append mode\n", IEEEfile_3D->filename); + + IEEEfile_3D->iofile = IEEEopen (IEEEfile_3D->filename, "a"); + if (! IOisValid (IEEEfile_3D->iofile)) { + char *msg = (char *) malloc (strlen (IEEEfile_3D->filename) + 80); sprintf (msg, "The file %s could not be opened for appending.\n", - filename); + IEEEfile_3D->filename); CCTK_WARN (1, msg); free (msg); } } - + /* Turn on buffer cache (with default size chosen by IEEEIO lib) */ - IEEEbufferOn (*iofile, 0); + IEEEbufferOn (IEEEfile_3D->iofile, 0); } /*@@ - @routine IOFlexIO_Write3D_filename + @routine IOFlexIO_Get3Dfile @author Paul Walker @date Feb 1997 @desc @@ -362,16 +351,27 @@ void IOFlexIO_Write3D_openFile (cGH *GH, const char *filename, IOFile *iofile, i @endhistory @@*/ -void IOFlexIO_Write3D_filename (cGH *GH, char *filename, const char *name) +IEEEfile_3D_t *IOFlexIO_Get3Dfile (cGH *GH, const char *alias, int *isNewFile) { DECLARE_CCTK_PARAMETERS ioGH *ioUtilGH; /* handle for IOUtil extensions */ pGH *pughGH; /* handle for PUGH extensions */ + flexioGH *myGH; /* handle for IOFlexIO extensions */ int nprocs; int myproc; char extra [256]; /* Extra stuff in fname based on mode */ - char extradir [256]; /* Extra stuff for an output dir */ - char createdir [256]; /* Text for system call to create output directory */ + char extradir [256]; /* Extra stuff for an output dir */ + char createdir [256]; /* Text for system call to create output directory */ + IEEEfile_3D_t *IEEEfile_3D; + + myGH = (flexioGH *) GH->extensions [CCTK_GHExtensionHandle ("IOFlexIO")]; + + IEEEfile_3D = (IEEEfile_3D_t *) GetNamedData (myGH->fileList_3D, alias); + if (IEEEfile_3D != NULL) { + /* set flag to indicate that file should be opened in append mode */ + *isNewFile = 0; + return (IEEEfile_3D); + } ioUtilGH = (ioGH *) GH->extensions [CCTK_GHExtensionHandle ("IO")]; pughGH = (pGH *) GH->extensions [CCTK_GHExtensionHandle ("PUGH")]; @@ -388,9 +388,17 @@ void IOFlexIO_Write3D_filename (cGH *GH, char *filename, const char *name) sprintf (extra, "%s_chunked", extra); #endif - if (onefileperslice) + if (onefileperslice) { + char *tmp = extra; + sprintf (extra, "%s.time_%7.3f", extra, GH->cctk_time); + /* And be sure to replace any spaces in the filename with an _ */ + do + if (*tmp == ' ') + *tmp = '_'; + while (*++tmp); + } /* OUTPUT ONE FILE FOR EACH N PROCESSORS * ------------------------------------- @@ -409,7 +417,7 @@ void IOFlexIO_Write3D_filename (cGH *GH, char *filename, const char *name) /* If necessary create the output directory */ if (myproc == 0) { - sprintf (createdir, "mkdir -p %s/%s_3d", ioUtilGH->outpfx_3D, name); + sprintf (createdir, "mkdir -p %s/%s_3d", ioUtilGH->outpfx_3D, alias); if (output_verbose) { printf ("Creating output directory with command\n %s\n", createdir); @@ -426,7 +434,7 @@ void IOFlexIO_Write3D_filename (cGH *GH, char *filename, const char *name) #endif /* extradir is the relative output directory */ - sprintf (extradir, "%s_3d/", name); + sprintf (extradir, "%s_3d/", alias); } @@ -434,29 +442,22 @@ void IOFlexIO_Write3D_filename (cGH *GH, char *filename, const char *name) /* CREATE THE COMPLETE OUTPUT FILENAME ----------------------------------- */ - sprintf (filename, "%s/%s%s_3d%s.ieee", ioUtilGH->outpfx_3D, - extradir, name, extra); + IEEEfile_3D = (IEEEfile_3D_t *) malloc (sizeof (IEEEfile_3D_t)); - /* And be sure to replace any spaces in the filename with an _ */ - do - if (*filename == ' ') - *filename = '_'; - while (*++filename); - -} + IEEEfile_3D->filename = (char *) malloc (strlen (ioUtilGH->outpfx_3D) + + strlen (extradir) + + strlen (alias) + + strlen (extra) + + 10); + sprintf (IEEEfile_3D->filename, "%s/%s%s_3d%s.ieee", ioUtilGH->outpfx_3D, + extradir, alias, extra); + /* no need to store file info if used only once */ + if (! onefileperslice) + StoreNamedData (&myGH->fileList_3D, alias, IEEEfile_3D); -#if 0 -void FMODIFIER FORTRAN_NAME(io_write3d_, - IO_WRITE3D, - io_write3d) - (Double *x) -{ - pGF *GF; - - GF = locateGFbyDataPtr(x); - IO_Write3D(GF->parentGH,GF); + /* set flag to indicate that file should be opened in create mode */ + *isNewFile = 1; + return (IEEEfile_3D); } - -#endif |