diff options
Diffstat (limited to 'src/Write1D.c')
-rw-r--r-- | src/Write1D.c | 37 |
1 files changed, 24 insertions, 13 deletions
diff --git a/src/Write1D.c b/src/Write1D.c index 12cb33d..8ab3057 100644 --- a/src/Write1D.c +++ b/src/Write1D.c @@ -252,6 +252,7 @@ int IOASCII_Write1D (const cGH *GH, int vindex, const char *alias) /* processor 0 opens the files with the appropriate name */ myproc = CCTK_MyProc (GH); + memset (file, 0, sizeof (file)); if (myproc == 0) { OpenFile (GH, fullname, alias, &gdata, do_dir, @@ -274,10 +275,20 @@ int IOASCII_Write1D (const cGH *GH, int vindex, const char *alias) extent_int[3] = extent_int[2]; } } + /* get the total number of grid points to check for zero-sized variables */ + for (dir = 0, hsize = 1; dir < gdata.dim; dir++) + { + hsize *= extent_int[dir]; + } /* now do the actual I/O looping over all directions */ for (dir = 0; dir < 4; dir++) { + if (hsize <= 0) + { + continue; + } + /* skip empty slices */ if (! do_dir[dir]) { @@ -339,9 +350,10 @@ int IOASCII_Write1D (const cGH *GH, int vindex, const char *alias) continue; } + /* allocate hyperslab buffers on I/O processor */ + hdata[0] = hdata[1] = NULL; if (myproc == 0) { - /* allocate hyperslab buffers */ hdata[0] = malloc (hsize * CCTK_VarTypeSize (gdata.vartype)); hdata[1] = have_coords ? malloc (hsize * sizeof (CCTK_REAL)) : NULL; } @@ -357,7 +369,7 @@ int IOASCII_Write1D (const cGH *GH, int vindex, const char *alias) Hyperslab_FreeMapping (mapping); /* And dump the data to file */ - if (myproc == 0 && file[dir] != NULL) + if (file[dir] != NULL) { if (num_returned_hslabs != num_requested_hslabs) { @@ -393,22 +405,21 @@ int IOASCII_Write1D (const cGH *GH, int vindex, const char *alias) WriteData (gdata.vartype, header, format, hsize, hstride, hdata, file + dir); } - - /* close the output file(s) */ - fclose (file[dir]); - if (num_files == 8) - { - fclose (file[dir + 4]); - } } /* clean up */ - if (myproc == 0) + free (hdata[0]); + free (hdata[1]); + } /* end of loop through all directions */ + + /* close the output file(s) */ + for (dir = 0; dir < 8; dir++) + { + if (file[dir]) { - free (hdata[0]); - free (hdata[1]); + fclose (file[dir]); } - } /* end of loop through all directions */ + } /* free allocated resources */ free (origin); |