From 83ed8667eed0d56c3bb20a86b826ed11d6aad03c Mon Sep 17 00:00:00 2001
From: Florent Pruvost <florent.pruvost@inria.fr>
Date: Wed, 11 Feb 2015 13:56:23 +0000
Subject: [PATCH] avoid to call MPI_Finalize if MPI has been initialized by
 user

---
 control/control.c      | 6 ++++--
 include/morse_struct.h | 3 ++-
 2 files changed, 6 insertions(+), 3 deletions(-)

diff --git a/control/control.c b/control/control.c
index 7810c2763..592a844fa 100644
--- a/control/control.c
+++ b/control/control.c
@@ -110,7 +110,7 @@ int MORSE_InitPar(int nworkers, int ncudas, int nthreads_per_worker)
         return MORSE_ERR_NOT_FOUND;
     }
     nworkers = morse->world_size;
-    
+
     /* Get the size of each NUMA node */
     morse->group_size = morse_get_numthreads_numa();
     while ( ((morse->world_size)%(morse->group_size)) != 0 ) 
@@ -121,6 +121,7 @@ int MORSE_InitPar(int nworkers, int ncudas, int nthreads_per_worker)
     {
       int flag = 0, provided = 0;
       MPI_Initialized( &flag );
+      morse->mpi_outer_init = flag;
       if ( !flag ) {
           MPI_Init_thread( NULL, NULL, MPI_THREAD_MULTIPLE, &provided );
       }
@@ -160,7 +161,8 @@ int MORSE_Finalize(void)
     morse_context_destroy();
 #if defined(CHAMELEON_USE_MPI)
     MPI_Barrier(MPI_COMM_WORLD);
-    MPI_Finalize();
+    if (!morse->mpi_outer_init)
+        MPI_Finalize();
 #endif
 
     return MORSE_SUCCESS;
diff --git a/include/morse_struct.h b/include/morse_struct.h
index c84b7f88d..75c686a04 100644
--- a/include/morse_struct.h
+++ b/include/morse_struct.h
@@ -139,7 +139,8 @@ typedef struct morse_context_s {
     int                nbnbsize;           // tile size in elements (possibly padded)
     int                ibnbsize;           // T or L tile size in elements (---''---)
     int                rhblock;            // block size for tree-based (reduction) Householder
-    void              *schedopt;
+    void              *schedopt;           // structure for runtimes
+    int                mpi_outer_init;     // MPI has been initialized outside our functions
 } MORSE_context_t;
 
 
-- 
GitLab