From a482cf6e24abca7bf80127fac078e87af70879df Mon Sep 17 00:00:00 2001
From: Chichi Lalescu <clalesc1@jhu.edu>
Date: Wed, 25 Feb 2015 09:18:51 -0500
Subject: [PATCH] fix DEBUG_MSG etc defines

---
 makefile                 |  2 +-
 src/base.hpp             | 11 ++++++++++-
 src/field_descriptor.cpp | 36 ++++++++++++++++++++++++++----------
 3 files changed, 37 insertions(+), 12 deletions(-)

diff --git a/makefile b/makefile
index 4c79519f..680a66ec 100644
--- a/makefile
+++ b/makefile
@@ -1,6 +1,6 @@
 MPICXX  = mpicxx
 LINKER  = mpicxx
-DEFINES = -DNDEBUG
+DEFINES = #-DNDEBUG
 CFLAGS  = -Wall \
 		  -O2 \
 		  #-pg \
diff --git a/src/base.hpp b/src/base.hpp
index d4a63a18..310ce6cc 100644
--- a/src/base.hpp
+++ b/src/base.hpp
@@ -29,11 +29,20 @@ inline void DEBUG_MSG(const char * format, ...)
     std::cerr << debug_message_buffer;
 }
 
+inline void DEBUG_WAIT(MPI_Comm communicator)
+{
+    MPI_Barrier(communicator);
+}
+
+#define DEBUG_WAIT_ALL() MPI_Barrier(MPI_COMM_WORLD)
+
 #define CHECK_POINT() DEBUG_MSG("%s %s", __FILE__, __LINE__)
 
 #else
 
-    #define DEBUG_MSG(x)
+#define DEBUG_MSG(...)
+#define DEBUG_WAIT_ALL()
+#define DEBUG_WAIT(...)
 
 #define CHECK_POINT()
 
diff --git a/src/field_descriptor.cpp b/src/field_descriptor.cpp
index 77d7bc3b..ac9c28bf 100644
--- a/src/field_descriptor.cpp
+++ b/src/field_descriptor.cpp
@@ -52,6 +52,7 @@ field_descriptor::field_descriptor(
             this->sizes[0],
             this->subsizes[0],
             this->starts[0]);
+    DEBUG_WAIT_ALL();
     int local_zero_array[this->nprocs], zero_array[this->nprocs];
     for (int i=0; i<this->nprocs; i++)
         local_zero_array[i] = 0;
@@ -87,9 +88,20 @@ field_descriptor::field_descriptor(
         MPI_Comm_create(this->comm, tgroup, &this->io_comm);
         MPI_Group_free(&tgroup0);
         MPI_Group_free(&tgroup);
+        if (this->subsizes[0] > 0)
+        {
+            MPI_Comm_rank(this->io_comm, &this->io_myrank);
+            MPI_Comm_size(this->io_comm, &this->io_nprocs);
+        }
+        else
+        {
+            this->io_myrank = MPI_PROC_NULL;
+            this->io_nprocs = -1;
+        }
     }
     if (this->subsizes[0] > 0)
     {
+        DEBUG_MSG("creating subarray\n");
         MPI_Type_create_subarray(
                 ndims,
                 this->sizes,
@@ -114,27 +126,31 @@ field_descriptor::field_descriptor(
             MPI_SUM,
             this->comm);
     delete[] local_rank;
-    DEBUG_MSG("exiting field_descriptor::field_descriptor\n");
 }
 
 field_descriptor::~field_descriptor()
 {
-    DEBUG_MSG("entered field_descriptor::~field_descriptor\n");
     delete[] this->sizes;
     delete[] this->subsizes;
     delete[] this->starts;
     delete[] this->rank;
-//    if (this->subsizes[0] > 0)
+    DEBUG_MSG(this->io_comm == MPI_COMM_NULL ? "null\n" : "not null\n");
+    DEBUG_WAIT_ALL();
+    DEBUG_MSG("subsizes[0] = %d \n", this->subsizes[0]);
+    DEBUG_WAIT_ALL();
+    if (this->subsizes[0] > 0)
     {
         DEBUG_MSG("deallocating mpi_array_dtype\n");
+        DEBUG_WAIT(this->io_comm);
         MPI_Type_free(&this->mpi_array_dtype);
-        if (this->nprocs != this->io_nprocs)
-        {
-            DEBUG_MSG("freeing io_comm\n");
-            MPI_Comm_free(&this->io_comm);
-        }
     }
-    DEBUG_MSG("exiting field_descriptor::~field_descriptor\n");
+    DEBUG_WAIT_ALL();
+    if (this->nprocs != this->io_nprocs && this->io_myrank != MPI_PROC_NULL)
+    {
+        DEBUG_MSG("freeing io_comm\n");
+        DEBUG_WAIT(this->io_comm);
+        MPI_Comm_free(&this->io_comm);
+    }
 }
 
 int field_descriptor::read(
@@ -186,7 +202,7 @@ int field_descriptor::write(
         sprintf(ffname, "%s", fname);
 
         MPI_File_open(
-                this->comm,
+                this->io_comm,
                 ffname,
                 MPI_MODE_CREATE | MPI_MODE_WRONLY,
                 info,
-- 
GitLab