From 56b997eee06516166ffec0814d64b51d0a4ff86e Mon Sep 17 00:00:00 2001
From: Hal Finkel <hfinkel@anl.gov>
Date: Tue, 21 Nov 2017 21:11:12 -0600
Subject: [PATCH] Work around MPI bug where MPI_Get_count does not return zero
 for zero count

---
 GenericIO.cxx | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/GenericIO.cxx b/GenericIO.cxx
index 0d31bae..eb1fb18 100644
--- a/GenericIO.cxx
+++ b/GenericIO.cxx
@@ -122,8 +122,10 @@ void GenericFileIO_MPI::write(const void *buf, size_t count, off_t offset,
     if (MPI_File_write_at(FH, offset, (void *) buf, count, MPI_BYTE, &status) != MPI_SUCCESS)
       throw runtime_error("Unable to write " + D + " to file: " + FileName);
 
-    int scount;
-    (void) MPI_Get_count(&status, MPI_BYTE, &scount);
+    int scount = 0;
+    // On some systems, MPI_Get_count will not return zero even when count is zero.
+    if (count > 0)
+      (void) MPI_Get_count(&status, MPI_BYTE, &scount);
 
     count -= scount;
     buf = ((char *) buf) + scount;
@@ -140,8 +142,10 @@ void GenericFileIO_MPICollective::read(void *buf, size_t count, off_t offset,
     if (MPI_File_read_at_all(FH, offset, buf, count, MPI_BYTE, &status) != MPI_SUCCESS)
       throw runtime_error("Unable to read " + D + " from file: " + FileName);
 
-    int scount;
-    (void) MPI_Get_count(&status, MPI_BYTE, &scount);
+    int scount = 0;
+    // On some systems, MPI_Get_count will not return zero even when count is zero.
+    if (count > 0)
+      (void) MPI_Get_count(&status, MPI_BYTE, &scount);
 
     count -= scount;
     buf = ((char *) buf) + scount;
-- 
GitLab