From 5f6baead6a3b93f043c7a4d7de7ea46cdc83f54f Mon Sep 17 00:00:00 2001
From: Mathieu Faverge <mathieu.faverge@inria.fr>
Date: Mon, 11 Oct 2021 20:20:05 +0200
Subject: [PATCH] gcc: add a chameleon_asprintf function and fix many include
 issues

---
 compute/pzlansy.c                            |  3 --
 control/async.c                              |  3 +-
 control/chameleon_f77.c                      |  1 -
 control/chameleon_mf77.c                     |  1 -
 control/chameleon_zf77.c                     |  1 -
 control/common.h                             | 30 ++++++++++++++++----
 control/control.c                            |  4 ---
 control/descriptor.c                         |  3 +-
 control/descriptor.h                         |  4 +++
 control/descriptor_rec.c                     |  3 +-
 control/workspace.c                          |  2 --
 coreblas/compute/core_zcesca.c               |  3 ++
 runtime/parsec/control/runtime_descriptor.c  |  4 +--
 runtime/starpu/codelets/codelet_zplssq.c     |  1 -
 runtime/starpu/control/runtime_async.c       | 11 ++++---
 runtime/starpu/control/runtime_context.c     |  1 -
 runtime/starpu/control/runtime_control.c     |  2 +-
 runtime/starpu/control/runtime_descriptor.c  |  2 --
 runtime/starpu/control/runtime_options.c     |  2 --
 runtime/starpu/control/runtime_profiling.c   |  2 +-
 runtime/starpu/include/chameleon_starpu.h.in |  2 +-
 testing/testing_zcheck.c                     |  2 +-
 testing/testing_zgels.c                      |  2 +-
 testing/testing_zgels_hqr.c                  |  2 +-
 24 files changed, 49 insertions(+), 42 deletions(-)

diff --git a/compute/pzlansy.c b/compute/pzlansy.c
index e87d6a19f..2c6265ee5 100644
--- a/compute/pzlansy.c
+++ b/compute/pzlansy.c
@@ -23,9 +23,6 @@
  */
 //ALLOC_WS :  A->mb
 //WS_ADD :  A->mb
-
-#include <stdlib.h>
-#include <math.h>
 #include "control/common.h"
 
 #define A( m, n )        A,     (m), (n)
diff --git a/control/async.c b/control/async.c
index 0e7001d29..5eea9670b 100644
--- a/control/async.c
+++ b/control/async.c
@@ -24,7 +24,6 @@
  * @brief Group routines exposed to users to handle asynchronous tasks execution
  *
  */
-#include <stdlib.h>
 #include "control/common.h"
 #include "chameleon/runtime.h"
 
@@ -344,4 +343,4 @@ int CHAMELEON_Request_Set( RUNTIME_request_t *request, int param, int value)
 
     status = chameleon_request_set(chamctxt, request, param, value);
     return status;
-}
\ No newline at end of file
+}
diff --git a/control/chameleon_f77.c b/control/chameleon_f77.c
index 6751e4882..a247a75b9 100644
--- a/control/chameleon_f77.c
+++ b/control/chameleon_f77.c
@@ -20,7 +20,6 @@
  * @date 2020-03-12
  *
  */
-#include <stdlib.h>
 #include "control/common.h"
 #include "chameleon.h"
 #include "chameleon_f77.h"
diff --git a/control/chameleon_mf77.c b/control/chameleon_mf77.c
index 76ddb5a5a..2a43c778e 100644
--- a/control/chameleon_mf77.c
+++ b/control/chameleon_mf77.c
@@ -19,7 +19,6 @@
  * @date 2020-03-03
  *
  */
-#include <stdlib.h>
 #include "control/common.h"
 
 #define CHAMELEON_ZCGESV           CHAMELEON_FNAME(zcgesv,   ZCGESV)
diff --git a/control/chameleon_zf77.c b/control/chameleon_zf77.c
index f5f4a37ca..611740889 100644
--- a/control/chameleon_zf77.c
+++ b/control/chameleon_zf77.c
@@ -24,7 +24,6 @@
  * @precisions normal z -> c d s
  *
  */
-#include <stdlib.h>
 #include "control/common.h"
 #include "control/chameleon_f77.h"
 
diff --git a/control/common.h b/control/common.h
index 44b628b40..ed2daacc0 100644
--- a/control/common.h
+++ b/control/common.h
@@ -25,6 +25,15 @@
 #ifndef _chameleon_common_h_
 #define _chameleon_common_h_
 
+#define _GNU_SOURCE 1
+#include <stdlib.h>
+#include <stdio.h>
+#include <stdarg.h>
+
+/**
+ *  Chameleon header files
+ */
+#include "chameleon.h"
 
 #if defined( _WIN32 ) || defined( _WIN64 )
 #include <io.h>
@@ -63,11 +72,6 @@
 #define LAPACK_NAME(a, b) lapackef77_##a
 #endif
 
-/**
- *  Chameleon header files
- */
-#include "chameleon.h"
-
 #include "control/global.h"
 #include "control/auxiliary.h"
 #include "control/context.h"
@@ -118,6 +122,22 @@ void chameleon_pslag2d(CHAM_context_t *chamctxt);
 void chameleon_pclag2z(CHAM_context_t *chamctxt);
 */
 
+#if defined(__GNUC__)
+static inline void chameleon_asprintf( char **strp, const char *fmt, ... ) __attribute__((format(printf,2,3)));
+#endif
+static inline void chameleon_asprintf( char **strp, const char *fmt, ... )
+{
+    va_list ap;
+    int rc;
+
+    va_start( ap, fmt );
+    rc = asprintf( strp, fmt, ap );
+    va_end(ap);
+
+    assert( rc != -1 );
+    (void)rc;
+}
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/control/control.c b/control/control.c
index 5fc134b38..52f738517 100644
--- a/control/control.c
+++ b/control/control.c
@@ -26,10 +26,6 @@
  * @brief Group routines exposed to users to control CHAMELEON state
  *
  */
-
-#include <stdio.h>
-#include <stdlib.h>
-#include "control/auxiliary.h"
 #include "control/common.h"
 #include "chameleon/runtime.h"
 
diff --git a/control/descriptor.c b/control/descriptor.c
index a24164cd6..0f3e9def7 100644
--- a/control/descriptor.c
+++ b/control/descriptor.c
@@ -105,7 +105,7 @@ void chameleon_desc_init_tiles( CHAM_desc_t *desc )
             tile->mat = (rank == desc->myrank) ? desc->get_blkaddr( desc, ii, jj ) : NULL;
             tile->ld  = desc->get_blkldd( desc, ii );
 #if defined(CHAMELEON_KERNELS_TRACE)
-            asprintf( &(tile->name), "%s(%d,%d)", desc->name, ii, jj);
+            chameleon_asprintf( &(tile->name), "%s(%d,%d)", desc->name, ii, jj );
 #endif
         }
     }
@@ -128,6 +128,7 @@ int chameleon_getrankof_2d_diag( const CHAM_desc_t *A, int m, int n )
 {
     int mm = m + A->i / A->mb;
     assert( m == n );
+    (void)n;
     return (mm % A->p) * A->q + (mm % A->q);
 }
 
diff --git a/control/descriptor.h b/control/descriptor.h
index fc239a569..c788bcb43 100644
--- a/control/descriptor.h
+++ b/control/descriptor.h
@@ -70,6 +70,9 @@ static inline int chameleon_desc_init( CHAM_desc_t *desc, void *mat,
     assert( i == 0 );
     assert( j == 0 );
     assert( mb * nb == bsiz );
+    (void)bsiz;
+    (void)i;
+    (void)j;
     return chameleon_desc_init_internal( desc, NULL, mat, dtyp, mb, nb, lm, ln, m, n, p, q,
                                          get_blkaddr, get_blkldd, get_rankof );
 }
@@ -138,6 +141,7 @@ inline static void *chameleon_getaddr_cm(const CHAM_desc_t *A, int m, int n)
 inline static void *chameleon_getaddr_diag( const CHAM_desc_t *A, int m, int n )
 {
     assert( m == n );
+    (void)n;
     return chameleon_getaddr_ccrb( A, m, 0 );
 }
 
diff --git a/control/descriptor_rec.c b/control/descriptor_rec.c
index 1bc506b65..d595ec08b 100644
--- a/control/descriptor_rec.c
+++ b/control/descriptor_rec.c
@@ -76,7 +76,8 @@ chameleon_recdesc_create( const char *name, CHAM_desc_t **descptr, void *mat, ch
             tile = desc->get_blktile( desc, m, n );
             tempmm = m == desc->mt-1 ? desc->m - m * desc->mb : desc->mb;
             tempnn = n == desc->nt-1 ? desc->n - n * desc->nb : desc->nb;
-            asprintf( &subname, "%s[%d,%d]", name, m, n );
+
+            chameleon_asprintf( &subname, "%s[%d,%d]", name, m, n );
 
             chameleon_recdesc_create( subname, &tiledesc, tile->mat,
                                       desc->dtyp, mb, nb,
diff --git a/control/workspace.c b/control/workspace.c
index 8375e8ab7..28581d42b 100644
--- a/control/workspace.c
+++ b/control/workspace.c
@@ -23,8 +23,6 @@
  * @brief Group routines exposed to users about specific workspaces management
  *
  */
-
-#include <stdlib.h>
 #include "control/common.h"
 #include "control/auxiliary.h"
 #include "control/workspace.h"
diff --git a/coreblas/compute/core_zcesca.c b/coreblas/compute/core_zcesca.c
index 989e48b82..3f81f4270 100644
--- a/coreblas/compute/core_zcesca.c
+++ b/coreblas/compute/core_zcesca.c
@@ -136,6 +136,9 @@ int CORE_zcesca( int center, int scale,
     CHAMELEON_Complex64_t gi, gj, g, rc, sqrc;
     double di, dj;
 
+    gj = 0.;
+    dj = 0.;
+
     /* Check input arguments */
     if ( (center != 0) && (center != 1) ) {
         coreblas_error(1, "Illegal value of center");
diff --git a/runtime/parsec/control/runtime_descriptor.c b/runtime/parsec/control/runtime_descriptor.c
index b78181685..ef92ded5c 100644
--- a/runtime/parsec/control/runtime_descriptor.c
+++ b/runtime/parsec/control/runtime_descriptor.c
@@ -229,11 +229,9 @@ void RUNTIME_desc_create( CHAM_desc_t *mdesc )
     data_collection->vpid_of_key = chameleon_parsec_vpid_of_key;
 #if defined(PARSEC_PROF_TRACE)
     {
-        int rc;
         data_collection->key_to_string = chameleon_parsec_key_to_string;
         data_collection->key           = NULL;
-        rc = asprintf(&(data_collection->key_dim), "(%d, %d)", mdesc->lmt, mdesc->lnt);
-        (void)rc;
+        chameleon_asprintf(&(data_collection->key_dim), "(%d, %d)", mdesc->lmt, mdesc->lnt);
     }
 #endif
     data_collection->memory_registration_status = MEMORY_STATUS_UNREGISTERED;
diff --git a/runtime/starpu/codelets/codelet_zplssq.c b/runtime/starpu/codelets/codelet_zplssq.c
index 22179ff00..187ceb3e6 100644
--- a/runtime/starpu/codelets/codelet_zplssq.c
+++ b/runtime/starpu/codelets/codelet_zplssq.c
@@ -21,7 +21,6 @@
  * @precisions normal z -> c d s
  *
  */
-#include <math.h>
 #include "chameleon_starpu.h"
 #include "runtime_codelet_z.h"
 
diff --git a/runtime/starpu/control/runtime_async.c b/runtime/starpu/control/runtime_async.c
index 6d7260d63..cc229cd06 100644
--- a/runtime/starpu/control/runtime_async.c
+++ b/runtime/starpu/control/runtime_async.c
@@ -19,7 +19,6 @@
  * @date 2020-04-22
  *
  */
-#include <stdlib.h>
 #include "chameleon_starpu.h"
 
 /**
@@ -36,7 +35,7 @@ int RUNTIME_sequence_create( CHAM_context_t  *chamctxt,
 /**
  *  Destroy a sequence
  */
-int RUNTIME_sequence_destroy( CHAM_context_t  *chamctxt,
+int RUNTIME_sequence_destroy( CHAM_context_t     *chamctxt,
                               RUNTIME_sequence_t *sequence )
 {
     (void)chamctxt;
@@ -47,7 +46,7 @@ int RUNTIME_sequence_destroy( CHAM_context_t  *chamctxt,
 /**
  *  Wait for the completion of a sequence
  */
-int RUNTIME_sequence_wait( CHAM_context_t  *chamctxt,
+int RUNTIME_sequence_wait( CHAM_context_t     *chamctxt,
                            RUNTIME_sequence_t *sequence )
 {
     (void)chamctxt;
@@ -73,7 +72,7 @@ int RUNTIME_sequence_wait( CHAM_context_t  *chamctxt,
 /**
  *  Terminate a sequence
  */
-void RUNTIME_sequence_flush( CHAM_context_t  *chamctxt,
+void RUNTIME_sequence_flush( CHAM_context_t     *chamctxt,
                              RUNTIME_sequence_t *sequence,
                              RUNTIME_request_t  *request,
                              int status )
@@ -88,7 +87,7 @@ void RUNTIME_sequence_flush( CHAM_context_t  *chamctxt,
 /**
  *  Create a request
  */
-int RUNTIME_request_create( CHAM_context_t  *chamctxt,
+int RUNTIME_request_create( CHAM_context_t    *chamctxt,
                             RUNTIME_request_t *request )
 {
     (void)chamctxt;
@@ -140,4 +139,4 @@ int RUNTIME_request_set( CHAM_context_t  *chamctxt,
     }
 
     return CHAMELEON_SUCCESS;
-}
\ No newline at end of file
+}
diff --git a/runtime/starpu/control/runtime_context.c b/runtime/starpu/control/runtime_context.c
index 177605ca9..176b9cbbb 100644
--- a/runtime/starpu/control/runtime_context.c
+++ b/runtime/starpu/control/runtime_context.c
@@ -19,7 +19,6 @@
  * @date 2020-12-01
  *
  */
-#include <stdlib.h>
 #include "chameleon_starpu.h"
 
 /**
diff --git a/runtime/starpu/control/runtime_control.c b/runtime/starpu/control/runtime_control.c
index 7561f2e4f..611634699 100644
--- a/runtime/starpu/control/runtime_control.c
+++ b/runtime/starpu/control/runtime_control.c
@@ -21,9 +21,9 @@
  * @date 2020-07-01
  *
  */
+#include "chameleon_starpu.h"
 #include <stdio.h>
 #include <stdlib.h>
-#include "chameleon_starpu.h"
 #if defined(STARPU_USE_FXT)
 #include <starpu_fxt.h>
 #endif
diff --git a/runtime/starpu/control/runtime_descriptor.c b/runtime/starpu/control/runtime_descriptor.c
index 8d20b3afa..4ab987d2b 100644
--- a/runtime/starpu/control/runtime_descriptor.c
+++ b/runtime/starpu/control/runtime_descriptor.c
@@ -22,8 +22,6 @@
  * @date 2020-12-01
  *
  */
-#include <stdlib.h>
-#include <unistd.h>
 #include "chameleon_starpu.h"
 
 /**
diff --git a/runtime/starpu/control/runtime_options.c b/runtime/starpu/control/runtime_options.c
index 553fed070..6687984b2 100644
--- a/runtime/starpu/control/runtime_options.c
+++ b/runtime/starpu/control/runtime_options.c
@@ -19,8 +19,6 @@
  * @date 2020-01-07
  *
  */
-#include <stdio.h>
-#include <stdlib.h>
 #include "chameleon_starpu.h"
 
 void RUNTIME_options_init( RUNTIME_option_t *option, CHAM_context_t *chamctxt,
diff --git a/runtime/starpu/control/runtime_profiling.c b/runtime/starpu/control/runtime_profiling.c
index 4881cadee..fb8fc039f 100644
--- a/runtime/starpu/control/runtime_profiling.c
+++ b/runtime/starpu/control/runtime_profiling.c
@@ -20,8 +20,8 @@
  * @date 2020-03-03
  *
  */
-#include <math.h>
 #include "chameleon_starpu.h"
+#include <math.h>
 #if defined(STARPU_USE_FXT)
 #include <starpu_fxt.h>
 #endif
diff --git a/runtime/starpu/include/chameleon_starpu.h.in b/runtime/starpu/include/chameleon_starpu.h.in
index c2ac71d22..e9fc23d52 100644
--- a/runtime/starpu/include/chameleon_starpu.h.in
+++ b/runtime/starpu/include/chameleon_starpu.h.in
@@ -23,7 +23,7 @@
 #ifndef _chameleon_starpu_h_
 #define _chameleon_starpu_h_
 
-#include "chameleon/config.h"
+#include "control/common.h"
 
 /* StarPU options */
 #cmakedefine HAVE_STARPU_IDLE_PREFETCH
diff --git a/testing/testing_zcheck.c b/testing/testing_zcheck.c
index 2bc8ceb11..35b044b4a 100644
--- a/testing/testing_zcheck.c
+++ b/testing/testing_zcheck.c
@@ -18,6 +18,7 @@
  * @precisions normal z -> c d s
  *
  */
+#include "../control/common.h"
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
@@ -33,7 +34,6 @@
 #if defined(CHAMELEON_USE_MPI)
 #include <mpi.h>
 #endif
-#include "../control/common.h"
 #include "testings.h"
 #include "testing_zcheck.h"
 #include <chameleon/flops.h>
diff --git a/testing/testing_zgels.c b/testing/testing_zgels.c
index df61a7ad3..9e63a8020 100644
--- a/testing/testing_zgels.c
+++ b/testing/testing_zgels.c
@@ -17,11 +17,11 @@
  * @precisions normal z -> c d s
  *
  */
+#include "../control/common.h"
 #include <chameleon.h>
 #include "testings.h"
 #include "testing_zcheck.h"
 #include <chameleon/flops.h>
-#include "../control/common.h"
 
 static cham_fixdbl_t
 flops_zgels( cham_trans_t trans, int M, int N, int NRHS )
diff --git a/testing/testing_zgels_hqr.c b/testing/testing_zgels_hqr.c
index e897ac7ae..f0227671a 100644
--- a/testing/testing_zgels_hqr.c
+++ b/testing/testing_zgels_hqr.c
@@ -17,11 +17,11 @@
  * @precisions normal z -> c d s
  *
  */
+#include "../control/common.h"
 #include <chameleon.h>
 #include "testings.h"
 #include "testing_zcheck.h"
 #include <chameleon/flops.h>
-#include "../control/common.h"
 
 static cham_fixdbl_t
 flops_zgels_hqr( cham_trans_t trans, int M, int N, int NRHS )
-- 
GitLab