From d1abde8f0ef98f199faabae8bb0520141341ac12 Mon Sep 17 00:00:00 2001
From: Mathieu Faverge <mathieu.faverge@inria.fr>
Date: Fri, 4 Oct 2019 20:22:49 +0200
Subject: [PATCH] Add functionnalities to the descriptor interface

---
 control/descriptor.c          | 106 +++++++++++++++++++++++++++++-----
 include/chameleon.h           |  54 +++++++++--------
 include/chameleon/constants.h |  10 ++++
 include/chameleon/struct.h    |  10 +++-
 4 files changed, 138 insertions(+), 42 deletions(-)

diff --git a/control/descriptor.c b/control/descriptor.c
index f72312e88..2f7394277 100644
--- a/control/descriptor.c
+++ b/control/descriptor.c
@@ -139,14 +139,14 @@ int chameleon_getrankof_2d_diag( const CHAM_desc_t *A, int m, int n )
  * @param[in] q
  *          Number of processes columns for the 2D block-cyclic distribution.
  *
- * @param[in] (*get_blkaddr)( const CHAM_desc_t *A, int m, int n)
+ * @param[in] get_blkaddr
  *          A function which return the address of the data corresponding to
  *          the tile A(m,n).
  *
- * @param[in] (*get_blkldd)( const CHAM_desc_t *A, int m )
+ * @param[in] get_blkldd
  *          A function that return the leading dimension of the tile A(m,*).
  *
- * @param[in] (*get_rankof)( const CHAM_desc_t *A, int m, int n)
+ * @param[in] get_rankof
  *          A function that return the MPI rank of the tile A(m,n).
  *
  ******************************************************************************
@@ -158,9 +158,9 @@ int chameleon_desc_init( CHAM_desc_t *desc, void *mat,
                          cham_flttype_t dtyp, int mb, int nb, int bsiz,
                          int lm, int ln, int i, int j,
                          int m,  int n,  int p, int q,
-                         void* (*get_blkaddr)( const CHAM_desc_t*, int, int ),
-                         int   (*get_blkldd) ( const CHAM_desc_t*, int      ),
-                         int   (*get_rankof) ( const CHAM_desc_t*, int, int ) )
+                         blkaddr_fct_t   get_blkaddr,
+                         blkldd_fct_t    get_blkldd,
+                         blkrankof_fct_t get_rankof)
 {
     CHAM_context_t *chamctxt;
     int rc = CHAMELEON_SUCCESS;
@@ -291,7 +291,7 @@ int chameleon_desc_init( CHAM_desc_t *desc, void *mat,
 /**
  *  Internal static descriptor initializer for submatrices
  */
-CHAM_desc_t* chameleon_desc_submatrix(CHAM_desc_t *descA, int i, int j, int m, int n)
+CHAM_desc_t* chameleon_desc_submatrix( CHAM_desc_t *descA, int i, int j, int m, int n )
 {
     CHAM_desc_t *descB = malloc(sizeof(CHAM_desc_t));
     int mb, nb;
@@ -376,6 +376,12 @@ int chameleon_desc_check(const CHAM_desc_t *desc)
     return CHAMELEON_SUCCESS;
 }
 
+CHAM_desc_t *
+CHAMELEON_Desc_SubMatrix( CHAM_desc_t *descA, int i, int j, int m, int n )
+{
+    return chameleon_desc_submatrix( descA, i, j, m, n );
+}
+
 /**
  *****************************************************************************
  *
@@ -502,14 +508,14 @@ int CHAMELEON_Desc_Create( CHAM_desc_t **descptr, void *mat, cham_flttype_t dtyp
  * @param[in] q
  *          Number of processes columns for the 2D block-cyclic distribution.
  *
- * @param[in] (*get_blkaddr)( const CHAM_desc_t *A, int m, int n)
+ * @param[in] get_blkaddr
  *          A function which return the address of the data corresponding to
  *          the tile A(m,n).
  *
- * @param[in] (*get_blkldd)( const CHAM_desc_t *A, int m)
+ * @param[in] get_blkldd
  *          A function that return the leading dimension of the tile A(m,*).
  *
- * @param[in] (*get_rankof)( const CHAM_desc_t *A, int m, int n)
+ * @param[in] get_rankof
  *          A function that return the MPI rank of the tile A(m,n).
  *
  ******************************************************************************
@@ -519,9 +525,9 @@ int CHAMELEON_Desc_Create( CHAM_desc_t **descptr, void *mat, cham_flttype_t dtyp
  */
 int CHAMELEON_Desc_Create_User( CHAM_desc_t **descptr, void *mat, cham_flttype_t dtyp, int mb, int nb, int bsiz,
                                 int lm, int ln, int i, int j, int m, int n, int p, int q,
-                                void* (*get_blkaddr)( const CHAM_desc_t*, int, int ),
-                                int   (*get_blkldd) ( const CHAM_desc_t*, int      ),
-                                int   (*get_rankof) ( const CHAM_desc_t*, int, int ) )
+                                blkaddr_fct_t   get_blkaddr,
+                                blkldd_fct_t    get_blkldd,
+                                blkrankof_fct_t get_rankof )
 {
     CHAM_context_t *chamctxt;
     CHAM_desc_t *desc;
@@ -610,7 +616,7 @@ int CHAMELEON_Desc_Create_User( CHAM_desc_t **descptr, void *mat, cham_flttype_t
  * @param[in] q
  *          Number of processes columns for the 2D block-cyclic distribution.
  *
- * @param[in] (*get_rankof)( const CHAM_desc_t *A, int m, int n)
+ * @param[in] get_rankof
  *          A function that return the MPI rank of the tile A(m,n).
  *
  ******************************************************************************
@@ -620,7 +626,7 @@ int CHAMELEON_Desc_Create_User( CHAM_desc_t **descptr, void *mat, cham_flttype_t
  */
 int CHAMELEON_Desc_Create_OOC_User(CHAM_desc_t **descptr, cham_flttype_t dtyp, int mb, int nb, int bsiz,
                                    int lm, int ln, int i, int j, int m, int n, int p, int q,
-                                   int (*get_rankof)( const CHAM_desc_t*, int, int ))
+                                   blkrankof_fct_t get_rankof )
 {
 #if !defined (CHAMELEON_SCHED_STARPU)
     (void)descptr; (void)dtyp; (void)mb; (void)nb; (void)bsiz;
@@ -704,6 +710,76 @@ int CHAMELEON_Desc_Create_OOC(CHAM_desc_t **descptr, cham_flttype_t dtyp, int mb
                                        chameleon_getaddr_null, NULL, NULL );
 }
 
+/**
+ *****************************************************************************
+ *
+ * @ingroup Descriptor
+ *
+ * @brief Creates a new descriptor with the same properties as the one given as
+ * input.
+ *
+ * @warning This function copies the descriptor structure, but does not copy the
+ * matrix data.
+ *
+ ******************************************************************************
+ *
+ * @param[in] descin
+ *          The descriptor structure to duplicate.
+ *
+ * @param[in] mat
+ *          Memory location for the copy. If mat is NULL, the space to store
+ *          the data is automatically allocated by the call to the function.
+ *
+ ******************************************************************************
+ *
+ * @retval The new matrix descriptor.
+ *
+ */
+CHAM_desc_t *CHAMELEON_Desc_Copy( CHAM_desc_t *descin, void *mat )
+{
+    CHAM_desc_t *descout;
+    CHAMELEON_Desc_Create_User( &descout, mat,
+                                descin->dtyp, descin->mb, descin->nb, descin->bsiz,
+                                descin->lm, descin->ln, descin->i, descin->j, descin->m, descin->n, descin->p, descin->q,
+                                descin->get_blkaddr, descin->get_blkldd, descin->get_rankof );
+    return descout;
+}
+
+/**
+ *****************************************************************************
+ *
+ * @ingroup Descriptor
+ *
+ * @brief Creates a new descriptor with the same properties as the one given as
+ * input and restricted on node 0.
+ *
+ * @warning This function copies the descriptor structure, but does not copy the
+ * matrix data.
+ *
+ ******************************************************************************
+ *
+ * @param[in] descin
+ *          The descriptor structure to duplicate.
+ *
+ * @param[in] mat
+ *          Memory location for the copy. If mat is NULL, the space to store
+ *          the data is automatically allocated by the call to the function.
+ *
+ ******************************************************************************
+ *
+ * @retval The new matrix descriptor.
+ *
+ */
+CHAM_desc_t *CHAMELEON_Desc_CopyOnZero( CHAM_desc_t *descin, void *mat )
+{
+    CHAM_desc_t *descout;
+    CHAMELEON_Desc_Create_User( &descout, mat,
+                                descin->dtyp, descin->mb, descin->nb, descin->bsiz,
+                                descin->lm, descin->ln, descin->i, descin->j, descin->m, descin->n, 1, 1,
+                                descin->get_blkaddr, descin->get_blkldd, descin->get_rankof );
+    return descout;
+}
+
 /**
  *****************************************************************************
  *
diff --git a/include/chameleon.h b/include/chameleon.h
index dfd0c423a..dfaaab485 100644
--- a/include/chameleon.h
+++ b/include/chameleon.h
@@ -100,36 +100,42 @@ int CHAMELEON_GetThreadNbr      (void);
 
 /* Descriptor */
 int CHAMELEON_Element_Size(int type);
-int CHAMELEON_Desc_Create  (CHAM_desc_t **desc, void *mat, cham_flttype_t dtyp,
-                        int mb, int nb, int bsiz, int lm, int ln,
-                        int i, int j, int m, int n, int p, int q);
-int CHAMELEON_Desc_Create_User(CHAM_desc_t **desc, void *mat, cham_flttype_t dtyp, int mb, int nb, int bsiz,
-                           int lm, int ln, int i, int j, int m, int n, int p, int q,
-                           void* (*get_blkaddr)( const CHAM_desc_t*, int, int ),
-                           int (*get_blkldd)( const CHAM_desc_t*, int ),
-                           int (*get_rankof)( const CHAM_desc_t*, int, int ));
-int CHAMELEON_Desc_Create_OOC(CHAM_desc_t **desc, cham_flttype_t dtyp,
-                          int mb, int nb, int bsiz, int lm, int ln,
-                          int i, int j, int m, int n, int p, int q);
-int CHAMELEON_Desc_Create_OOC_User(CHAM_desc_t **desc, cham_flttype_t dtyp,
+
+int CHAMELEON_Desc_Create_User( CHAM_desc_t **desc, void *mat, cham_flttype_t dtyp, int mb, int nb, int bsiz,
+                                int lm, int ln, int i, int j, int m, int n, int p, int q,
+                                blkaddr_fct_t get_blkaddr, blkldd_fct_t get_blkldd, blkrankof_fct_t get_rankof );
+
+int CHAMELEON_Desc_Create( CHAM_desc_t **desc, void *mat, cham_flttype_t dtyp,
+                           int mb, int nb, int bsiz, int lm, int ln,
+                           int i, int j, int m, int n, int p, int q );
+
+int CHAMELEON_Desc_Create_OOC_User( CHAM_desc_t **desc, cham_flttype_t dtyp,
+                                    int mb, int nb, int bsiz, int lm, int ln,
+                                    int i, int j, int m, int n, int p, int q,
+                                    blkrankof_fct_t get_rankof );
+int CHAMELEON_Desc_Create_OOC( CHAM_desc_t **desc, cham_flttype_t dtyp,
                                int mb, int nb, int bsiz, int lm, int ln,
-                               int i, int j, int m, int n, int p, int q,
-                               int (*get_rankof)( const CHAM_desc_t*, int, int ));
-int CHAMELEON_Desc_Destroy (CHAM_desc_t **desc);
-int CHAMELEON_Desc_Acquire (CHAM_desc_t  *desc);
-int CHAMELEON_Desc_Release (CHAM_desc_t  *desc);
-int CHAMELEON_Desc_Flush   (CHAM_desc_t  *desc, RUNTIME_sequence_t *sequence);
-void CHAMELEON_user_tag_size(int, int) ;
+                               int i, int j, int m, int n, int p, int q );
+
+CHAM_desc_t *CHAMELEON_Desc_Copy( CHAM_desc_t *descin, void *mat );
+CHAM_desc_t *CHAMELEON_Desc_CopyOnZero( CHAM_desc_t *descin, void *mat );
+CHAM_desc_t *CHAMELEON_Desc_SubMatrix( CHAM_desc_t *descA, int i, int j, int m, int n );
+
+int CHAMELEON_Desc_Destroy( CHAM_desc_t **desc );
+int CHAMELEON_Desc_Acquire( CHAM_desc_t  *desc );
+int CHAMELEON_Desc_Release( CHAM_desc_t  *desc );
+int CHAMELEON_Desc_Flush  ( CHAM_desc_t  *desc, RUNTIME_sequence_t *sequence );
 
 /* Workspaces */
 int CHAMELEON_Dealloc_Workspace (CHAM_desc_t **desc);
 
 /* Options */
-int CHAMELEON_Enable  (int option);
-int CHAMELEON_Disable (int option);
-int CHAMELEON_Set     (int param, int  value);
-int CHAMELEON_Get     (int param, int *value);
-int CHAMELEON_Set_update_progress_callback(void (*p)(int, int)) ;
+int  CHAMELEON_Enable  (int option);
+int  CHAMELEON_Disable (int option);
+int  CHAMELEON_Set     (int param, int  value);
+int  CHAMELEON_Get     (int param, int *value);
+int  CHAMELEON_Set_update_progress_callback(void (*p)(int, int)) ;
+void CHAMELEON_user_tag_size(int, int);
 
 /* Sequences */
 int CHAMELEON_Sequence_Create  (RUNTIME_sequence_t **sequence);
diff --git a/include/chameleon/constants.h b/include/chameleon/constants.h
index 968a259cc..b58d4bcc6 100644
--- a/include/chameleon/constants.h
+++ b/include/chameleon/constants.h
@@ -111,6 +111,16 @@ typedef enum chameleon_dist_e {
     ChamDistNormal    = 203,
 } cham_dist_t;
 
+/**
+ * @brief Main matrix type
+ */
+typedef enum chameleon_mtxtype_e {
+    ChamGeneral    = 231,
+    ChamSymmetric  = 232,
+    ChamHermitian  = 233,
+    ChamTriangular = 234,
+} cham_mtxtype_t;
+
 /**
  * @brief Eigen and singular values generator format
  */
diff --git a/include/chameleon/struct.h b/include/chameleon/struct.h
index cbd860c6d..74758056e 100644
--- a/include/chameleon/struct.h
+++ b/include/chameleon/struct.h
@@ -49,13 +49,17 @@ BEGIN_C_DECLS
 struct chameleon_desc_s;
 typedef struct chameleon_desc_s CHAM_desc_t;
 
+typedef void* (*blkaddr_fct_t)  ( const CHAM_desc_t*, int, int );
+typedef int   (*blkldd_fct_t)   ( const CHAM_desc_t*, int );
+typedef int   (*blkrankof_fct_t)( const CHAM_desc_t*, int, int );
+
 struct chameleon_desc_s {
     // function to get chameleon tiles address
-    void *(*get_blkaddr)( const CHAM_desc_t*, int, int );
+    blkaddr_fct_t   get_blkaddr;
     // function to get chameleon tiles leading dimension
-    int   (*get_blkldd )( const CHAM_desc_t*, int );
+    blkldd_fct_t    get_blkldd;
     // function to get chameleon tiles MPI rank
-    int   (*get_rankof) ( const CHAM_desc_t*, int, int );
+    blkrankof_fct_t get_rankof;
     void *mat;        // pointer to the beginning of the matrix
     size_t A21;       // pointer to the beginning of the matrix A21
     size_t A12;       // pointer to the beginning of the matrix A12
-- 
GitLab