From ea4b47a327c24af6c35b30f495f2da05bd4aead7 Mon Sep 17 00:00:00 2001
From: Florent Pruvost <florent.pruvost@inria.fr>
Date: Tue, 13 Feb 2024 14:35:34 +0100
Subject: [PATCH] Do not use chameleon_context_self() to get the comm field in
 the chameleon_starpu library, get it with a new function argument instead.

---
 compute/zgetrf.c                                 |  2 +-
 control/descriptor.c                             |  2 +-
 control/descriptor_ipiv.c                        | 13 ++++++++++---
 example/lapack_to_chameleon/step7.h              |  2 ++
 include/chameleon/runtime.h                      | 11 ++++++++---
 runtime/openmp/control/runtime_descriptor.c      |  3 ++-
 runtime/openmp/control/runtime_descriptor_ipiv.c |  8 ++++++--
 runtime/parsec/control/runtime_descriptor.c      |  3 ++-
 runtime/parsec/control/runtime_descriptor_ipiv.c |  8 ++++++--
 runtime/quark/control/runtime_descriptor.c       |  3 ++-
 runtime/quark/control/runtime_descriptor_ipiv.c  |  8 ++++++--
 runtime/starpu/control/runtime_descriptor.c      |  4 ++--
 runtime/starpu/control/runtime_descriptor_ipiv.c | 13 +++++++------
 runtime/starpu/control/runtime_tags.c            |  6 +++---
 runtime/starpu/include/chameleon_starpu.h.in     |  2 +-
 15 files changed, 59 insertions(+), 29 deletions(-)

diff --git a/compute/zgetrf.c b/compute/zgetrf.c
index 0a3682b20..9b002f5a6 100644
--- a/compute/zgetrf.c
+++ b/compute/zgetrf.c
@@ -280,7 +280,7 @@ CHAMELEON_zgetrf( int M, int N, CHAMELEON_Complex64_t *A, int LDA, int *IPIV )
     if ( ( ws->alg == ChamGetrfPPivPerColumn ) ||
          ( ws->alg == ChamGetrfPPiv ) )
     {
-        RUNTIME_ipiv_gather( &descIPIV, IPIV, 0 );
+        RUNTIME_ipiv_gather( sequence, &descIPIV, IPIV, 0 );
     }
     chameleon_sequence_wait( chamctxt, sequence );
 
diff --git a/control/descriptor.c b/control/descriptor.c
index ea6b712d1..3f753f2b9 100644
--- a/control/descriptor.c
+++ b/control/descriptor.c
@@ -333,7 +333,7 @@ int chameleon_desc_init_internal( CHAM_desc_t *desc, const char *name, void *mat
     chameleon_desc_init_tiles( desc, desc->get_rankof_init );
 
     /* Create runtime specific structure like registering data */
-    RUNTIME_desc_create( desc );
+    RUNTIME_desc_create( chamctxt, desc );
 
     return rc;
 }
diff --git a/control/descriptor_ipiv.c b/control/descriptor_ipiv.c
index 784a0ef7b..720230dca 100644
--- a/control/descriptor_ipiv.c
+++ b/control/descriptor_ipiv.c
@@ -73,7 +73,7 @@ int chameleon_ipiv_init( CHAM_ipiv_t *ipiv, const CHAM_desc_t *desc, void *data
     ipiv->mt   = chameleon_ceil( ipiv->m, ipiv->mb );
 
     /* Create runtime specific structure like registering data */
-    RUNTIME_ipiv_create( ipiv );
+    RUNTIME_ipiv_create( chamctxt, ipiv );
 
     return rc;
 }
@@ -240,6 +240,13 @@ int CHAMELEON_Ipiv_Flush( const CHAM_ipiv_t        *ipiv,
  */
 int CHAMELEON_Ipiv_Gather( CHAM_ipiv_t *ipivdesc, int *ipiv, int root )
 {
-    RUNTIME_ipiv_gather( ipivdesc, ipiv, root );
-    return CHAMELEON_SUCCESS;
+    int status;
+    CHAM_context_t *chamctxt = chameleon_context_self();
+    RUNTIME_sequence_t *sequence = NULL;
+    chameleon_sequence_create( chamctxt, &sequence );
+    RUNTIME_ipiv_gather( sequence, ipivdesc, ipiv, root );
+    chameleon_sequence_wait( chamctxt, sequence );
+    status = sequence->status;
+    chameleon_sequence_destroy( chamctxt, sequence );
+    return status;
 }
diff --git a/example/lapack_to_chameleon/step7.h b/example/lapack_to_chameleon/step7.h
index b693e1920..80c1a89f2 100644
--- a/example/lapack_to_chameleon/step7.h
+++ b/example/lapack_to_chameleon/step7.h
@@ -86,6 +86,7 @@ static int Cham_build_plgsy_cpu( void *op_args, cham_uplo_t uplo, int m, int n,
     tempmm = (m == (descA->mt-1)) ? (descA->m - m * descA->mb) : descA->mb;
     tempnn = (n == (descA->nt-1)) ? (descA->n - n * descA->nb) : descA->nb;
 
+    /* fill the tile with the coreblas function plgsy = random SPD matrix generator */
     TCORE_dplgsy( data->bump, tempmm, tempnn, tileA,
                   descA->m, m * descA->mb, n * descA->nb, data->seed );
 
@@ -103,6 +104,7 @@ static int Cham_build_plrnt_cpu( void *op_args, cham_uplo_t uplo, int m, int n,
     tempmm = (m == (descA->mt-1)) ? (descA->m - m * descA->mb) : descA->mb;
     tempnn = (n == (descA->nt-1)) ? (descA->n - n * descA->nb) : descA->nb;
 
+    /* fill the tile with the coreblas function plrnt = random general matrix generator */
     TCORE_dplrnt( tempmm, tempnn, tileA,
                   descA->m, m * descA->mb, n * descA->nb, data->seed );
 
diff --git a/include/chameleon/runtime.h b/include/chameleon/runtime.h
index 7a39e2f2d..94c985cde 100644
--- a/include/chameleon/runtime.h
+++ b/include/chameleon/runtime.h
@@ -409,11 +409,14 @@ RUNTIME_free( void *ptr, size_t size );
  ***
  * @brief Initialize runtime specific data structure to a given descriptor.
  *
+ * @param[in] chamctxt
+ *            The chameleon context.
+ *
  * @param[in,out] desc
  *            The descriptor to initialize.
  */
 void
-RUNTIME_desc_create( CHAM_desc_t *desc );
+RUNTIME_desc_create( CHAM_context_t *chamctxt, CHAM_desc_t *desc );
 
 /**
  * @brief Finalize runtime specific data structure of a given descriptor.
@@ -705,11 +708,13 @@ void RUNTIME_ddisplay_oneprofile (cham_tasktype_t task);
 void RUNTIME_sdisplay_allprofile ();
 void RUNTIME_sdisplay_oneprofile (cham_tasktype_t task);
 
-void RUNTIME_ipiv_create ( CHAM_ipiv_t *ipiv );
+void RUNTIME_ipiv_create ( CHAM_context_t *chamctxt,
+                           CHAM_ipiv_t *ipiv );
 void RUNTIME_ipiv_destroy( CHAM_ipiv_t *ipiv );
 void RUNTIME_ipiv_init   ( const RUNTIME_sequence_t *sequence,
                            CHAM_ipiv_t *ipiv );
-void RUNTIME_ipiv_gather ( CHAM_ipiv_t *desc, int *ipiv, int node );
+void RUNTIME_ipiv_gather ( const RUNTIME_sequence_t *sequence,
+                           CHAM_ipiv_t *desc, int *ipiv, int node );
 
 void *RUNTIME_ipiv_getaddr   ( const CHAM_ipiv_t *ipiv, int m );
 void *RUNTIME_nextpiv_getaddr( const CHAM_ipiv_t *ipiv, int m, int h );
diff --git a/runtime/openmp/control/runtime_descriptor.c b/runtime/openmp/control/runtime_descriptor.c
index 9ba825542..7b8172153 100644
--- a/runtime/openmp/control/runtime_descriptor.c
+++ b/runtime/openmp/control/runtime_descriptor.c
@@ -34,8 +34,9 @@ void RUNTIME_free( void  *ptr,
     return;
 }
 
-void RUNTIME_desc_create( CHAM_desc_t *desc )
+void RUNTIME_desc_create( CHAM_context_t *chamctxt, CHAM_desc_t *desc )
 {
+    (void)chamctxt;
     (void)desc;
     return;
 }
diff --git a/runtime/openmp/control/runtime_descriptor_ipiv.c b/runtime/openmp/control/runtime_descriptor_ipiv.c
index 33b03458f..7b04c0aec 100644
--- a/runtime/openmp/control/runtime_descriptor_ipiv.c
+++ b/runtime/openmp/control/runtime_descriptor_ipiv.c
@@ -17,9 +17,11 @@
  */
 #include "chameleon_openmp.h"
 
-void RUNTIME_ipiv_create( CHAM_ipiv_t *ipiv )
+void RUNTIME_ipiv_create( CHAM_context_t *chamctxt,
+                          CHAM_ipiv_t *ipiv )
 {
     assert( 0 );
+    (void)chamctxt;
     (void)ipiv;
 }
 
@@ -115,9 +117,11 @@ void RUNTIME_ipiv_init( const RUNTIME_sequence_t *sequence,
     (void)ipiv;
 }
 
-void RUNTIME_ipiv_gather( CHAM_ipiv_t *desc, int *ipiv, int node )
+void RUNTIME_ipiv_gather( const RUNTIME_sequence_t *sequence,
+                          CHAM_ipiv_t *desc, int *ipiv, int node )
 {
     assert( 0 );
+    (void)sequence;
     (void)desc;
     (void)ipiv;
     (void)node;
diff --git a/runtime/parsec/control/runtime_descriptor.c b/runtime/parsec/control/runtime_descriptor.c
index a52a37f08..609f4ac90 100644
--- a/runtime/parsec/control/runtime_descriptor.c
+++ b/runtime/parsec/control/runtime_descriptor.c
@@ -198,8 +198,9 @@ chameleon_parsec_key_to_string(parsec_data_collection_t *data_collection, parsec
 /**
  *  Create data descriptor
  */
-void RUNTIME_desc_create( CHAM_desc_t *mdesc )
+void RUNTIME_desc_create( CHAM_context_t *chamctxt, CHAM_desc_t *mdesc )
 {
+    (void)chamctxt;
     parsec_data_collection_t *data_collection;
     chameleon_parsec_desc_t *pdesc;
     int comm_size;
diff --git a/runtime/parsec/control/runtime_descriptor_ipiv.c b/runtime/parsec/control/runtime_descriptor_ipiv.c
index 2cfc2c6a2..d3f9e1997 100644
--- a/runtime/parsec/control/runtime_descriptor_ipiv.c
+++ b/runtime/parsec/control/runtime_descriptor_ipiv.c
@@ -17,9 +17,11 @@
  */
 #include "chameleon_parsec.h"
 
-void RUNTIME_ipiv_create( CHAM_ipiv_t *ipiv )
+void RUNTIME_ipiv_create( CHAM_context_t *chamctxt,
+                          CHAM_ipiv_t *ipiv )
 {
     assert( 0 );
+    (void)chamctxt;
     (void)ipiv;
 }
 
@@ -115,9 +117,11 @@ void RUNTIME_ipiv_init( const RUNTIME_sequence_t *sequence,
     (void)ipiv;
 }
 
-void RUNTIME_ipiv_gather( CHAM_ipiv_t *desc, int *ipiv, int node )
+void RUNTIME_ipiv_gather( const RUNTIME_sequence_t *sequence,
+                          CHAM_ipiv_t *desc, int *ipiv, int node )
 {
     assert( 0 );
+    (void)sequence;
     (void)desc;
     (void)ipiv;
     (void)node;
diff --git a/runtime/quark/control/runtime_descriptor.c b/runtime/quark/control/runtime_descriptor.c
index 2676e02e8..f15668a24 100644
--- a/runtime/quark/control/runtime_descriptor.c
+++ b/runtime/quark/control/runtime_descriptor.c
@@ -35,8 +35,9 @@ void RUNTIME_free( void  *ptr,
     return;
 }
 
-void RUNTIME_desc_create( CHAM_desc_t *desc )
+void RUNTIME_desc_create( CHAM_context_t *chamctxt, CHAM_desc_t *desc )
 {
+    (void)chamctxt;
     (void)desc;
     return;
 }
diff --git a/runtime/quark/control/runtime_descriptor_ipiv.c b/runtime/quark/control/runtime_descriptor_ipiv.c
index dd22e55d8..9e8cdbb00 100644
--- a/runtime/quark/control/runtime_descriptor_ipiv.c
+++ b/runtime/quark/control/runtime_descriptor_ipiv.c
@@ -17,9 +17,11 @@
  */
 #include "chameleon_quark.h"
 
-void RUNTIME_ipiv_create( CHAM_ipiv_t *ipiv )
+void RUNTIME_ipiv_create( CHAM_context_t *chamctxt,
+                          CHAM_ipiv_t *ipiv )
 {
     assert( 0 );
+    (void)chamctxt;
     (void)ipiv;
 }
 
@@ -115,9 +117,11 @@ void RUNTIME_ipiv_init( const RUNTIME_sequence_t *sequence,
     (void)ipiv;
 }
 
-void RUNTIME_ipiv_gather( CHAM_ipiv_t *desc, int *ipiv, int node )
+void RUNTIME_ipiv_gather( const RUNTIME_sequence_t *sequence,
+                          CHAM_ipiv_t *desc, int *ipiv, int node )
 {
     assert( 0 );
+    (void)sequence;
     (void)desc;
     (void)ipiv;
     (void)node;
diff --git a/runtime/starpu/control/runtime_descriptor.c b/runtime/starpu/control/runtime_descriptor.c
index 56210c309..ecfd88d0f 100644
--- a/runtime/starpu/control/runtime_descriptor.c
+++ b/runtime/starpu/control/runtime_descriptor.c
@@ -82,7 +82,7 @@ void RUNTIME_free( void  *ptr,
 /**
  *  Create data descriptor
  */
-void RUNTIME_desc_create( CHAM_desc_t *desc )
+void RUNTIME_desc_create( CHAM_context_t *chamctxt, CHAM_desc_t *desc )
 {
     int64_t lmt = desc->lmt;
     int64_t lnt = desc->lnt;
@@ -149,7 +149,7 @@ void RUNTIME_desc_create( CHAM_desc_t *desc )
      * Book the number of tags required to describe this matrix
      */
     {
-        chameleon_starpu_tag_init();
+        chameleon_starpu_tag_init( chamctxt );
         desc->mpitag = chameleon_starpu_tag_book( nbtiles );
 
         if ( desc->mpitag == -1 ) {
diff --git a/runtime/starpu/control/runtime_descriptor_ipiv.c b/runtime/starpu/control/runtime_descriptor_ipiv.c
index 9806d0dbb..f6cdf950b 100644
--- a/runtime/starpu/control/runtime_descriptor_ipiv.c
+++ b/runtime/starpu/control/runtime_descriptor_ipiv.c
@@ -20,7 +20,8 @@
 /**
  *  Create ws_pivot runtime structures
  */
-void RUNTIME_ipiv_create( CHAM_ipiv_t *ipiv )
+void RUNTIME_ipiv_create( CHAM_context_t *chamctxt,
+                          CHAM_ipiv_t *ipiv )
 {
     assert( ipiv );
     starpu_data_handle_t *handles = calloc( 5 * ipiv->mt, sizeof(starpu_data_handle_t) );
@@ -39,7 +40,7 @@ void RUNTIME_ipiv_create( CHAM_ipiv_t *ipiv )
      * One per handle type
      */
     {
-        chameleon_starpu_tag_init();
+        chameleon_starpu_tag_init( chamctxt );
         ipiv->mpitag_ipiv = chameleon_starpu_tag_book( (int64_t)(ipiv->mt) * 5 );
         if ( ipiv->mpitag_ipiv == -1 ) {
             chameleon_fatal_error("RUNTIME_ipiv_create", "Can't pursue computation since no more tags are available for ipiv structure");
@@ -365,12 +366,12 @@ void RUNTIME_ipiv_init( const RUNTIME_sequence_t *sequence,
     }
 }
 
-void RUNTIME_ipiv_gather( CHAM_ipiv_t *desc, int *ipiv, int node )
+void RUNTIME_ipiv_gather( const RUNTIME_sequence_t *sequence,
+                          CHAM_ipiv_t *desc, int *ipiv, int node )
 {
     int64_t mt   = desc->mt;
     int64_t mb   = desc->mb;
     int64_t tag  = chameleon_starpu_tag_book( (int64_t)(desc->mt) );
-    CHAM_context_t *chamctxt = chameleon_context_self();
     int     rank = CHAMELEON_Comm_rank();
     int     owner = rank;
     int     m;
@@ -390,7 +391,7 @@ void RUNTIME_ipiv_gather( CHAM_ipiv_t *desc, int *ipiv, int node )
                 if (already_received == 0)
                 {
                     MPI_Status status;
-                    starpu_mpi_recv( ipiv_src, owner, tag, chamctxt->comm, &status );
+                    starpu_mpi_recv( ipiv_src, owner, tag, sequence->comm, &status );
                 }
             }
             else if ( rank == owner )
@@ -399,7 +400,7 @@ void RUNTIME_ipiv_gather( CHAM_ipiv_t *desc, int *ipiv, int node )
                 int already_sent = starpu_mpi_cached_send_set( ipiv_src, node );
                 if (already_sent == 0)
                 {
-                    starpu_mpi_send( ipiv_src, node, tag, chamctxt->comm );
+                    starpu_mpi_send( ipiv_src, node, tag, sequence->comm );
                 }
             }
         }
diff --git a/runtime/starpu/control/runtime_tags.c b/runtime/starpu/control/runtime_tags.c
index e8d8c6628..a5c1bc208 100644
--- a/runtime/starpu/control/runtime_tags.c
+++ b/runtime/starpu/control/runtime_tags.c
@@ -52,13 +52,12 @@ static int64_t starpu_tag_ub = 0;
  *
  ******************************************************************************/
 void
-chameleon_starpu_tag_init( void )
+chameleon_starpu_tag_init( CHAM_context_t *chamctxt )
 {
     if (!starpu_tag_ub) {
         int          ok       = 0;
         void        *tag_ub_p = NULL;
 
-        CHAM_context_t *chamctxt = chameleon_context_self();
         starpu_mpi_comm_get_attr( chamctxt->comm, STARPU_MPI_TAG_UB, &tag_ub_p, &ok );
         starpu_tag_ub = (uint64_t)((intptr_t)tag_ub_p);
 
@@ -211,7 +210,8 @@ chameleon_starpu_tag_release( int64_t min )
  *
  ******************************************************************************/
 void
-chameleon_starpu_tag_init( ) {
+chameleon_starpu_tag_init( CHAM_context_t *chamctxt ) {
+    (void)chamctxt;
     return;
 }
 
diff --git a/runtime/starpu/include/chameleon_starpu.h.in b/runtime/starpu/include/chameleon_starpu.h.in
index b102e3441..bbaf1bbdb 100644
--- a/runtime/starpu/include/chameleon_starpu.h.in
+++ b/runtime/starpu/include/chameleon_starpu.h.in
@@ -159,7 +159,7 @@ void *RUNTIME_data_getaddr_withconversion( const RUNTIME_option_t *options,
 /**
  * MPI tag management
  */
-void    chameleon_starpu_tag_init( );
+void    chameleon_starpu_tag_init( CHAM_context_t *chamctxt );
 int64_t chameleon_starpu_tag_book( int64_t nbtags );
 void    chameleon_starpu_tag_release( int64_t min );
 
-- 
GitLab