From 2d9ffc918f3531c76b4584a07f6f8516a85538b1 Mon Sep 17 00:00:00 2001
From: Mathieu Faverge <mathieu.faverge@inria.fr>
Date: Thu, 23 Feb 2023 15:20:45 +0100
Subject: [PATCH] compute: Add a print function for a descriptor

---
 compute/CMakeLists.txt          |   1 +
 compute/zprint.c                | 227 ++++++++++++++++++++++++++++++++
 include/chameleon.h             |   2 +-
 include/chameleon/chameleon_z.h |   3 +
 4 files changed, 232 insertions(+), 1 deletion(-)
 create mode 100644 compute/zprint.c

diff --git a/compute/CMakeLists.txt b/compute/CMakeLists.txt
index 0106e546c..bed4df6f1 100644
--- a/compute/CMakeLists.txt
+++ b/compute/CMakeLists.txt
@@ -253,6 +253,7 @@ set(ZSRC
     ##################
     zgenm2.c
     pzgenm2.c
+    zprint.c
 )
 
 precisions_rules_py(CHAMELEON_SRCS_GENERATED "${ZSRC}"
diff --git a/compute/zprint.c b/compute/zprint.c
new file mode 100644
index 000000000..3dbd715e2
--- /dev/null
+++ b/compute/zprint.c
@@ -0,0 +1,227 @@
+/**
+ *
+ * @file zprint.c
+ *
+ * @copyright 2012-2023 Bordeaux INP, CNRS (LaBRI UMR 5800), Inria,
+ *                      Univ. Bordeaux. All rights reserved.
+ *
+ ***
+ *
+ * @brief Chameleon zprint wrappers
+ *
+ * @version 1.2.0
+ * @author Mathieu Faverge
+ * @author Matthieu Kuhn
+ * @date 2022-02-22
+ * @precisions normal z -> s d c
+ *
+ */
+#include "control/common.h"
+#if !defined(CHAMELEON_SIMULATION)
+#include <coreblas/coreblas_z.h>
+#endif
+
+/*
+ * Static variable to know how to handle the data within the kernel
+ * This assumes that only one runtime is enabled at a time.
+ */
+static RUNTIME_id_t zprint_runtime_id = RUNTIME_SCHED_STARPU;
+
+struct zprint_args_s {
+    FILE       *file;
+    const char *header;
+};
+
+static inline int
+zprint( const CHAM_desc_t *descA,
+        cham_uplo_t uplo, int m, int n,
+        CHAM_tile_t *tileA, void *op_args )
+{
+    CHAMELEON_Complex64_t *A;
+    struct zprint_args_s  *options = (struct zprint_args_s *)op_args;
+
+    int tempmm = m == descA->mt-1 ? descA->m-m*descA->mb : descA->mb;
+    int tempnn = n == descA->nt-1 ? descA->n-n*descA->nb : descA->nb;
+    int minmn = chameleon_min( tempmm, tempnn );
+    int lda, i;
+
+    if ( zprint_runtime_id == RUNTIME_SCHED_PARSEC ) {
+        A   = (CHAMELEON_Complex64_t*)tileA;
+        lda = descA->get_blkldd( descA, m );
+    }
+    else {
+        A   = CHAM_tile_get_ptr( tileA );
+        lda = tileA->ld;
+    }
+
+#if !defined(CHAMELEON_SIMULATION)
+    CORE_zprint( options->file, options->header, uplo,
+                 tempmm, tempnn, m, n, A, lda );
+#endif
+
+    return 0;
+}
+
+/**
+ ********************************************************************************
+ *
+ * @ingroup CHAMELEON_Complex64_t
+ *
+ * @brief Print a matrix tile by tile.
+ *
+ *******************************************************************************
+ *
+ * @param[in] M
+ *          The number of rows of the matrix A. M >= 0.
+ *
+ * @param[in] N
+ *          The number of columns of the matrix A. N >= 0.
+ *
+ * @param[in] A
+ *          The M-by-N matrix A.
+ *
+ * @param[in] LDA
+ *          The leading dimension of the array A. LDA >= max(1,M).
+ *
+ *******************************************************************************
+ *
+ * @retval the two-norm estimate.
+ *
+ *******************************************************************************
+ *
+ * @sa CHAMELEON_zlatms_Tile
+ * @sa CHAMELEON_zlatms_Tile_Async
+ * @sa CHAMELEON_clange
+ * @sa CHAMELEON_dlange
+ * @sa CHAMELEON_slange
+ *
+ */
+int CHAMELEON_zprint( FILE *file, const char *header,
+                      cham_uplo_t uplo, int M, int N,
+                      CHAMELEON_Complex64_t *A, int LDA )
+{
+    int NB;
+    int status;
+    CHAM_context_t *chamctxt;
+    RUNTIME_sequence_t *sequence = NULL;
+    RUNTIME_request_t request = RUNTIME_REQUEST_INITIALIZER;
+    CHAM_desc_t descAl, descAt;
+    struct zprint_args_s options = {
+        .file   = file,
+        .header = header,
+    };
+
+    chamctxt = chameleon_context_self();
+    if (chamctxt == NULL) {
+        chameleon_fatal_error("CHAMELEON_zlatms", "CHAMELEON not initialized");
+        return CHAMELEON_ERR_NOT_INITIALIZED;
+    }
+    /* Check input arguments */
+    if (M < 0) {
+        chameleon_error("CHAMELEON_zlatms", "illegal value of M");
+        return -1;
+    }
+    if (N < 0) {
+        chameleon_error("CHAMELEON_zlatms", "illegal value of N");
+        return -2;
+    }
+    if (LDA < chameleon_max(1, M)) {
+        chameleon_error("CHAMELEON_zlatms", "illegal value of LDA");
+        return -4;
+    }
+
+    /* Quick return */
+    if (chameleon_min(N, M) == 0) {
+        return (double)0.0;
+    }
+
+    /* Tune NB depending on M, N & NRHS; Set NBNB */
+    status = chameleon_tune(CHAMELEON_FUNC_ZGEMM, M, N, 0);
+    if (status != CHAMELEON_SUCCESS) {
+        chameleon_error("CHAMELEON_zlatms", "chameleon_tune() failed");
+        return status;
+    }
+
+    /* Set NT */
+    NB = CHAMELEON_NB;
+
+    chameleon_sequence_create( chamctxt, &sequence );
+
+    /* Submit the matrix conversion */
+    chameleon_zlap2tile( chamctxt, &descAl, &descAt, ChamDescInput, uplo,
+                         A, NB, NB, LDA, N, M, N, sequence, &request );
+
+    /* Call the tile interface */
+    zprint_runtime_id = chamctxt->scheduler;
+    chameleon_pmap( uplo, &descAt, zprint, &options, sequence, &request );
+
+    /* Submit the matrix conversion back */
+    chameleon_ztile2lap( chamctxt, &descAl, &descAt,
+                         ChamDescInput, uplo, sequence, &request );
+
+    chameleon_sequence_wait( chamctxt, sequence );
+
+    /* Cleanup the temporary data */
+    chameleon_ztile2lap_cleanup( chamctxt, &descAl, &descAt );
+
+    chameleon_sequence_destroy( chamctxt, sequence );
+    return status;
+}
+
+/**
+ ********************************************************************************
+ *
+ * @ingroup CHAMELEON_Complex64_t_Tile
+ *
+ * @brief Tile equivalent of CHAMELEON_zprint().
+ *
+ *  Operates on matrices stored by tiles.
+ *  All matrices are passed through descriptors.
+ *  All dimensions are taken from the descriptors.
+ *
+ *******************************************************************************
+ *
+ * @param[in] A
+ *          On entry, the input matrix A.
+ *
+ *******************************************************************************
+ *
+ * @retval CHAMELEON_SUCCESS successful exit
+ *
+ *******************************************************************************
+ *
+ * @sa CHAMELEON_zlatms
+ * @sa CHAMELEON_zlatms_Tile_Async
+ * @sa CHAMELEON_clange_Tile
+ * @sa CHAMELEON_dlange_Tile
+ * @sa CHAMELEON_slange_Tile
+ *
+ */
+int CHAMELEON_zprint_Tile( FILE *file, const char *header,
+                           cham_uplo_t uplo, CHAM_desc_t *A )
+{
+    CHAM_context_t *chamctxt;
+    RUNTIME_sequence_t *sequence = NULL;
+    RUNTIME_request_t request = RUNTIME_REQUEST_INITIALIZER;
+    struct zprint_args_s options = {
+        .file   = file,
+        .header = header,
+    };
+    int status;
+
+    chamctxt = chameleon_context_self();
+    if (chamctxt == NULL) {
+        chameleon_fatal_error("CHAMELEON_zlatms_Tile", "CHAMELEON not initialized");
+        return CHAMELEON_ERR_NOT_INITIALIZED;
+    }
+    chameleon_sequence_create( chamctxt, &sequence );
+
+    zprint_runtime_id = chamctxt->scheduler;
+    chameleon_pmap( uplo, A, zprint, &options, sequence, &request );
+    CHAMELEON_Desc_Flush( A, sequence );
+
+    chameleon_sequence_wait( chamctxt, sequence );
+    status = sequence->status;
+    chameleon_sequence_destroy( chamctxt, sequence );
+    return status;
+}
diff --git a/include/chameleon.h b/include/chameleon.h
index 4f75ba830..c0a9ea9a9 100644
--- a/include/chameleon.h
+++ b/include/chameleon.h
@@ -27,6 +27,7 @@
  * CHAMELEON types and constants
  */
 #include "chameleon/config.h"
+#include <stdio.h>
 #include "chameleon/constants.h"
 #include "chameleon/types.h"
 #include "chameleon/struct.h"
@@ -37,7 +38,6 @@
  */
 #include "chameleon/runtime.h"
 
-
 /* ****************************************************************************
  * CHAMELEON Simulation mode
  */
diff --git a/include/chameleon/chameleon_z.h b/include/chameleon/chameleon_z.h
index c36e6289d..e587d015c 100644
--- a/include/chameleon/chameleon_z.h
+++ b/include/chameleon/chameleon_z.h
@@ -382,6 +382,9 @@ int CHAMELEON_zgram( cham_uplo_t uplo, int N, CHAMELEON_Complex64_t *A, int LDA
 int CHAMELEON_zgram_Tile( cham_uplo_t uplo, CHAM_desc_t *A );
 int CHAMELEON_zgram_Tile_Async( cham_uplo_t uplo, CHAM_desc_t *A, void *user_ws, RUNTIME_sequence_t *sequence, RUNTIME_request_t *request );
 
+int CHAMELEON_zprint( FILE *file, const char *header, cham_uplo_t uplo, int M, int N, CHAMELEON_Complex64_t *A, int LDA );
+int CHAMELEON_zprint_Tile( FILE *file, const char *header, cham_uplo_t uplo, CHAM_desc_t *descA );
+
 END_C_DECLS
 
 #endif /* _chameleon_z_h_ */
-- 
GitLab