From 8f7daef45d20cfc65028da1dd671493f445dfe56 Mon Sep 17 00:00:00 2001
From: Mathieu Faverge <mathieu.faverge@inria.fr>
Date: Mon, 22 Jan 2018 16:10:00 +0100
Subject: [PATCH] Move zpotrf to the new conversion functions

---
 compute/zpotrf.c    | 32 ++++++++----------
 control/compute_z.h | 81 ++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 94 insertions(+), 19 deletions(-)

diff --git a/compute/zpotrf.c b/compute/zpotrf.c
index 082618489..8468bdb65 100644
--- a/compute/zpotrf.c
+++ b/compute/zpotrf.c
@@ -91,7 +91,7 @@ int MORSE_zpotrf(MORSE_enum uplo, int N,
     MORSE_context_t *morse;
     MORSE_sequence_t *sequence = NULL;
     MORSE_request_t request = MORSE_REQUEST_INITIALIZER;
-    MORSE_desc_t descA;
+    MORSE_desc_t descAt, descAl;
 
     morse = morse_context_self();
     if (morse == NULL) {
@@ -123,29 +123,25 @@ int MORSE_zpotrf(MORSE_enum uplo, int N,
     }
 
     /* Set NT */
-    NB   = MORSE_NB;
+    NB = MORSE_NB;
 
     morse_sequence_create(morse, &sequence);
 
-/*    if ( MORSE_TRANSLATION == MORSE_OUTOFPLACE ) {*/
-        morse_zooplap2tile( descA, A, NB, NB, LDA, N, 0, 0, N, N, sequence, &request,
-                             morse_desc_mat_free(&(descA)) );
-/*    } else {*/
-/*        morse_ziplap2tile( descA, A, NB, NB, LDA, N, 0, 0, N, N,*/
-/*                            sequence, &request);*/
-/*    }*/
+    /* Submit the matrix conversion */
+    morse_zlap2tile( morse, &descAl, &descAt, uplo,
+                     A, NB, NB, LDA, N, N, N, sequence, &request );
 
     /* Call the tile interface */
-    MORSE_zpotrf_Tile_Async(uplo, &descA, sequence, &request);
+    MORSE_zpotrf_Tile_Async(uplo, &descAt, sequence, &request);
 
-/*    if ( MORSE_TRANSLATION == MORSE_OUTOFPLACE ) {*/
-        morse_zooptile2lap(descA, A, NB, NB, LDA, N,  sequence, &request);
-        morse_sequence_wait(morse, sequence);
-        morse_desc_mat_free(&descA);
-/*    } else {*/
-/*        morse_ziptile2lap( descA, A, NB, NB, LDA, N,  sequence, &request);*/
-/*        morse_sequence_wait(morse, sequence);*/
-/*    }*/
+    /* Submit the matrix conversion back */
+    morse_ztile2lap( morse, &descAl, &descAt, uplo,
+                     A, NB, NB, LDA, N, N, N, sequence, &request );
+
+    morse_sequence_wait(morse, sequence);
+
+    /* Cleanup the temporary data */
+    morse_ztile2lap_cleanup( morse, &descAl, &descAt );
 
     status = sequence->status;
     morse_sequence_destroy(morse, sequence);
diff --git a/control/compute_z.h b/control/compute_z.h
index 2ac2d5090..6314c01be 100644
--- a/control/compute_z.h
+++ b/control/compute_z.h
@@ -31,7 +31,7 @@
 /***************************************************************************//**
  *  Macro for matrix conversion / Lapack interface
  **/
-#define morse_zdesc_alloc_diag(descA, mb, nb, lm, ln, i, j, m, n, p, q) \
+#define morse_zdesc_alloc_diag( descA, mb, nb, lm, ln, i, j, m, n, p, q) \
     descA = morse_desc_init_diag(                                       \
         MorseComplexDouble, (mb), (nb), ((mb)*(nb)),                    \
         (m), (n), (i), (j), (m), (n), p, q);                            \
@@ -171,3 +171,82 @@ void morse_pzunglq_param(const libhqr_tree_t *qrtree, MORSE_desc_t *A, MORSE_des
 void morse_pzungqr_param(const libhqr_tree_t *qrtree, MORSE_desc_t *A, MORSE_desc_t *Q,
                          MORSE_desc_t *TS, MORSE_desc_t *TT, MORSE_desc_t *D,
                          MORSE_sequence_t *sequence, MORSE_request_t *request);
+
+
+/**
+ * @brief Internal function to convert the lapack format to tile format in
+ * LAPACK interface calls
+ */
+static inline int
+morse_zlap2tile( MORSE_context_t *morse,
+                 MORSE_desc_t *descAl, MORSE_desc_t *descAt,
+                 MORSE_enum uplo, MORSE_Complex64_t *A, int mb, int nb, int lm, int ln, int m, int n,
+                 MORSE_sequence_t *seq, MORSE_request_t *req )
+{
+    /* Initialize the Lapack descriptor */
+    *descAl = morse_desc_init_user( MorseComplexDouble, mb, nb, (mb)*(nb),
+                                    lm, ln, 0, 0, m, n, 1, 1,
+                                    morse_getaddr_cm, morse_getblkldd_cm, NULL  );
+    descAl->mat = A;
+    descAl->styp = MorseCM;
+
+    /* Initialize the tile descriptor */
+    *descAt = morse_desc_init( MorseComplexDouble, mb, nb, (mb)*(nb),
+                               lm, ln, 0, 0, m, n, 1, 1 );
+
+    RUNTIME_desc_create( descAl );
+    RUNTIME_desc_create( descAt );
+
+    if ( MORSE_TRANSLATION == MORSE_OUTOFPLACE ) {
+        if ( morse_desc_mat_alloc( descAt ) ) {
+            morse_error( "morse_zlap2tile", "morse_desc_mat_alloc() failed");
+
+            RUNTIME_desc_destroy( descAl );
+            RUNTIME_desc_destroy( descAt );
+            return MORSE_ERR_OUT_OF_RESOURCES;
+        }
+        morse_pzlacpy( uplo, descAl, descAt, seq, req );
+    }
+    else {
+        morse_fatal_error( "morse_zlap2tile", "INPLACE translation not supported yet");
+        descAt->mat = A;
+        /* MORSE_zgecfi_Async( lm, ln, A, MorseCM, mb, nb, */
+        /*                     MorseCCRB, mb, nb, seq, req ); */
+    }
+}
+
+/**
+ * @brief Internal function to convert back the tile format to the lapack format
+ * in LAPACK interface calls
+ */
+static inline int
+morse_ztile2lap( MORSE_context_t *morse,
+                 MORSE_desc_t *descAl, MORSE_desc_t *descAt,
+                 MORSE_enum uplo, MORSE_Complex64_t *A, int mb, int nb, int lm, int ln, int m, int n,
+                 MORSE_sequence_t *seq, MORSE_request_t *req )
+{
+    if ( MORSE_TRANSLATION == MORSE_OUTOFPLACE ) {
+        morse_pzlacpy( uplo, descAt, descAl, seq, req );
+    }
+    else {
+        morse_fatal_error( "morse_ztile2lap", "INPLACE translation not supported yet");
+        /* MORSE_zgecfi_Async( lm, ln, A, MorseCCRB, mb, nb, */
+        /*                     MorseCM, mb, nb, seq, req ); */
+    }
+    RUNTIME_desc_flush( descAl, seq );
+    RUNTIME_desc_flush( descAt, seq );
+}
+
+/**
+ * @brief Internal function to cleanup the temporary data from the layout
+ * conversions in LAPACK interface calls
+ */
+static inline int
+morse_ztile2lap_cleanup( MORSE_context_t *morse, MORSE_desc_t *descAl, MORSE_desc_t *descAt )
+{
+    if ( MORSE_TRANSLATION == MORSE_OUTOFPLACE ) {
+        morse_desc_mat_free( descAt );
+    }
+    RUNTIME_desc_destroy( descAl );
+    RUNTIME_desc_destroy( descAt );
+}
-- 
GitLab