From 1ad1746d3411b3eed7fca39c477760a8f180ac21 Mon Sep 17 00:00:00 2001
From: Philippe SWARTVAGHER <philippe.swartvagher@inria.fr>
Date: Wed, 31 Aug 2022 18:43:44 +0200
Subject: [PATCH] Do not attempt to shutdown StarPU if initialization failed

---
 runtime/starpu/control/runtime_control.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)

diff --git a/runtime/starpu/control/runtime_control.c b/runtime/starpu/control/runtime_control.c
index 554d81eff..b77b161e1 100644
--- a/runtime/starpu/control/runtime_control.c
+++ b/runtime/starpu/control/runtime_control.c
@@ -29,6 +29,8 @@
 #include <starpu_fxt.h>
 #endif
 
+static int starpu_initialized = 0;
+
 /**
  *
  */
@@ -56,7 +58,7 @@ static int chameleon_starpu_init( starpu_conf_t *conf )
         if (rc < 0) {
             return CHAMELEON_ERR_NOT_INITIALIZED;
         }
-        starpu_mpi_init(NULL, NULL, !flag);
+        rc = starpu_mpi_init(NULL, NULL, !flag);
 #  endif
     }
 #else
@@ -143,6 +145,12 @@ int RUNTIME_init( CHAM_context_t *chamctxt,
         chamctxt->nthreads_per_worker = nthreads_per_worker;
     }
 
+    if ( hres != CHAMELEON_SUCCESS ) {
+        return hres;
+    }
+
+    starpu_initialized = 1;
+
 #ifdef HAVE_STARPU_MALLOC_ON_NODE_SET_DEFAULT_FLAGS
     starpu_malloc_on_node_set_default_flags(STARPU_MAIN_RAM, STARPU_MALLOC_PINNED | STARPU_MALLOC_COUNT
 #ifdef STARPU_MALLOC_SIMULATION_FOLDED
@@ -165,10 +173,8 @@ int RUNTIME_init( CHAM_context_t *chamctxt,
  */
 void RUNTIME_finalize( CHAM_context_t *chamctxt )
 {
-    (void)chamctxt;
-
-    /* StarPU was already initialized by an external library */
-    if ( chamctxt->schedopt == NULL ) {
+    /* StarPU was already initialized by an external library or was not successfully initialized: */
+    if ( (chamctxt->schedopt == NULL) || !starpu_initialized ) {
         return;
     }
 
-- 
GitLab