Some demo tests fail with "MPI_Comm_rank called before MPI_INIT"

Issue #1127 new
bavier created an issue

The demo_waveguide_serial and demo_stokes-iterative_serial tests fail on my system with the following error:

Directory: /tmp/guix-build-fenics-dolfin-2019.1.0.post0.drv-0/build/demo/undocumented/waveguide/cpp
"demo_waveguide_serial" start time: Feb 04 05:47 UTC
Output:
----------------------------------------------------------
*** The MPI_Comm_rank() function was called before MPI_INIT was invoked.
*** This is disallowed by the MPI standard.
*** Your MPI job will now abort.

Both tests call dolfin::info() early on, which ends up calling dolfin::MPI::rank() via Logger::write() before MPI has been initialized.

It seems that https://bitbucket.org/fenics-project/dolfin/commits/9cc87357b87e250aa628047e0becf86e04091ec3 attempted to fix something similar, but was perhaps not thorough enough.

The following patch fixes both tests for me, and brings the behavior of dolfin::MPI::rank() in line with dolfin::MPI::size()

-- a/dolfin/common/MPI.cpp                                                                                                                                                                                                                    
+++ b/dolfin/common/MPI.cpp                                                                                                                                                                                                                    
@@ -143,6 +143,7 @@ MPI_Info& dolfin::MPIInfo::operator*()                                                                                                                                                                                     
 unsigned int dolfin::MPI::rank(const MPI_Comm comm)                                                                                                                                                                                           
 {                                                                                                                                                                                                                                             
 #ifdef HAS_MPI                                                                                                                                                                                                                                
+  SubSystemsManager::init_mpi();                                                                                                                                                                                                              
   int rank;                                                                                                                                                                                                                                   
   MPI_Comm_rank(comm, &rank);                                                                                                                                                                                                                 
   return rank;                                                                                                                                                                                                                                
diff --git a/dolfin/common/MPI.h b/dolfin/common/MPI.h                                                                                                                                                                                         
index b93f6df30..854114a3f 100644                                                                                                                                                                                                              
--- a/dolfin/common/MPI.h                                                                                                                                                                                                                      
+++ b/dolfin/common/MPI.h                                                                                                                                                                                                                      
@@ -102,12 +102,13 @@ namespace dolfin                                                                                                                                                                                                         
       /// communicator                                                                                                                                                                                                                        
       void reset(MPI_Comm comm);                                                                                                                                                                                                              

-      /// Return process rank for the communicator                                                                                                                                                                                            
+      /// Return process rank for the communicator. This function will                                                                                                                                                                        
+      /// also initialize MPI if it hasn't already been initialised.                                                                                                                                                                          
       unsigned int rank() const;                                                                                                                                                                                                              

       /// Return size of the group (number of processes) associated                                                                                                                                                                           
-      /// with the communicator. This function will also intialise MPI                                                                                                                                                                        
-      /// if it hasn't already been intialised.                                                                                                                                                                                               
+      /// with the communicator. This function will also initialise MPI                                                                                                                                                                       
+      /// if it hasn't already been initialised.                                                                                                                                                                                              
       unsigned int size() const;                                                                                                                                                                                                              

       /// Set a barrier (synchronization point)

Comments (0)

  1. Log in to comment