From 53d1bd7d1a4fcacc8fc27a7dff7c5aef60b26da7 Mon Sep 17 00:00:00 2001 From: tradke Date: Fri, 4 Jun 2004 12:19:47 +0000 Subject: Don't hyperslab/output zero-sized grid variables. Finally closes PR CactusPUGH/1243: "Segmentation fault in PUGHSlab". git-svn-id: http://svn.cactuscode.org/arrangements/CactusBase/IOASCII/trunk@177 94b1c47f-dcfd-45ef-a468-0854c0e9e350 --- src/Write1D.c | 37 ++++++++++++++++++++++++------------- src/Write2D.c | 24 +++++++++++++++--------- src/Write3D.c | 31 ++++++++++++++++++++----------- 3 files changed, 59 insertions(+), 33 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); diff --git a/src/Write2D.c b/src/Write2D.c index 66e25f3..4c7db4d 100644 --- a/src/Write2D.c +++ b/src/Write2D.c @@ -219,23 +219,31 @@ int IOASCII_Write2D (const cGH *GH, int vindex, const char *alias) sprintf (format[2], "\t\t%%%s", out_format); /* processor 0 opens the files on the first trip through */ + fileset = NULL; myproc = CCTK_MyProc (GH); if (myproc == 0) { fileset = OpenFile (GH, fullname, alias, gdata.dim, have_coords ? coord_lower : NULL, maxdir); } - else - { - fileset = NULL; - } /* get the extents of the variable */ CCTK_GroupgshVI (GH, gdata.dim, extent_int, vindex); + /* get the total number of grid points to check for zero-sized variables */ + for (dir = 0, hsize[0] = 1; dir < gdata.dim; dir++) + { + hsize[0] *= extent_int[dir]; + } + /* now do the actual I/O looping over all directions */ for (dir = 0; dir < maxdir; dir++) { + if (hsize[0] <= 0) + { + continue; + } + /* get the directions to span the hyperslab */ if (dir == 0) { @@ -288,6 +296,7 @@ int IOASCII_Write2D (const cGH *GH, int vindex, const char *alias) continue; } + hdata[0] = hdata[1] = hdata[2]; if (myproc == 0) { /* allocate hyperslab buffers */ @@ -309,7 +318,7 @@ int IOASCII_Write2D (const cGH *GH, int vindex, const char *alias) Hyperslab_FreeMapping (mapping); /* and dump the data to file */ - if (myproc == 0 && fileset) + if (fileset) { if (num_returned_hslabs == num_requested_hslabs) { @@ -338,10 +347,7 @@ int IOASCII_Write2D (const cGH *GH, int vindex, const char *alias) /* clean up */ free (hdata[0]); - if (hdata[1]) - { - free (hdata[1]); - } + free (hdata[1]); } /* end of outputting the data by processor 0 */ diff --git a/src/Write3D.c b/src/Write3D.c index b904a39..56fb233 100644 --- a/src/Write3D.c +++ b/src/Write3D.c @@ -209,6 +209,17 @@ int IOASCII_Write3D (const cGH *GH, int vindex, const char *alias) /* Open the file on processor 0 */ file = myproc == 0 ? OpenFile (GH, fullname, alias) : NULL; + /* get the total number of grid points to check for zero-sized variables */ + for (i = 0, total_hsize = 1; i < gdata.dim; i++) + { + total_hsize *= extent_int[i]; + } + if (total_hsize <= 0) + { + free (fullname); + return (0); + } + /* set the extent vector (copy from 'int' to 'CCTK_INT' */ CCTK_GroupgshVI (GH, 3, extent_int, vindex); for (i = 0; i < 3; i++) @@ -227,10 +238,10 @@ int IOASCII_Write3D (const cGH *GH, int vindex, const char *alias) { CCTK_VWarn (1, __LINE__, __FILE__, CCTK_THORNSTRING, "IOASCII_Write3D: Failed to define hyperslab mapping for " - "variable '%s'", fullname); + "variable '%s'", fullname); free (fullname); - return (-1); - } + return (-1); + } total_hsize = hsize[0] * hsize[1] * hsize[2]; if (total_hsize <= 0) { @@ -240,8 +251,9 @@ int IOASCII_Write3D (const cGH *GH, int vindex, const char *alias) Hyperslab_FreeMapping (mapping); free (fullname); return (-1); - } - + } + + hdata[0] = hdata[1] = hdata[2] = hdata[3]; if (myproc == 0) { /* allocate hyperslab buffers */ @@ -250,8 +262,8 @@ int IOASCII_Write3D (const cGH *GH, int vindex, const char *alias) NULL; hdata[2] = (CCTK_REAL *) hdata[1] + 1*total_hsize; hdata[3] = (CCTK_REAL *) hdata[1] + 2*total_hsize; - } - + } + /* get the hyperslabs */ num_returned_hslabs = Hyperslab_GetList (GH, mapping, num_requested_hslabs, NULL, vindices, NULL, NULL, hdata, @@ -289,10 +301,7 @@ int IOASCII_Write3D (const cGH *GH, int vindex, const char *alias) /* clean up */ free (hdata[0]); - if (hdata[1]) - { - free (hdata[1]); - } + free (hdata[1]); } /* end of outputting the data by processor 0 */ free (fullname); -- cgit v1.2.3