From e4e8423f02d703224c9da1a2cfaa947e2e9c214e Mon Sep 17 00:00:00 2001
From: Raphael Boucherie <raphael.boucherie@inria.fr>
Date: Tue, 2 May 2017 17:45:14 +0200
Subject: [PATCH] added new file pzgeqrfhqr

---
 CMakeLists.txt                 |   6 +-
 cmake_modules/FindLIBHQR.cmake |  35 +++++
 compute/CMakeLists.txt         |   1 +
 compute/pzgeqrfhqr.c           | 240 +++++++++++++++++++++++++++++++++
 4 files changed, 281 insertions(+), 1 deletion(-)
 create mode 100644 cmake_modules/FindLIBHQR.cmake
 create mode 100644 compute/pzgeqrfhqr.c

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 139ba4e79..2570e67cc 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -250,6 +250,10 @@ endif()
 #########################
 set(CHAMELEON_DEP "")
 
+find_package(LIBHQR REQUIRED)
+list(INSERT CHAMELEON_DEP 0 ${LIBHQR_LIBRARIES})
+include_directories(${LIBHQR_INCLUDE_DIRS})
+
 # Check for Thread library
 # ------------------------
 set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
@@ -274,7 +278,7 @@ if(UNIX OR WIN32)
         message(FATAL_ERROR "Could NOT find libm on your system."
             " Are you sure to a have a C compiler installed?")
     endif()
-endif()
+  endif()
 
 # Try to find librt (libposix4 - POSIX.1b Realtime Extensions library)
 # on Unix systems except Apple ones because it does not exist on it
diff --git a/cmake_modules/FindLIBHQR.cmake b/cmake_modules/FindLIBHQR.cmake
new file mode 100644
index 000000000..912d71d87
--- /dev/null
+++ b/cmake_modules/FindLIBHQR.cmake
@@ -0,0 +1,35 @@
+# - Try to find LibHQR
+# Once done this will define
+#  LIBHQR_FOUND - System has LibHQR
+#  LIBHQR_INCLUDE_DIRS - The LibHQR include directories
+#  LIBHQR_LIBRARIES - The libraries needed to use LibHQR
+#  LIBHQR_DEFINITIONS - Compiler switches required for using LIBHQR
+
+find_package(PkgConfig)
+pkg_check_modules(PC_LIBHQR QUIET libhqr)
+set(LIBHQR_DEFINITIONS ${PC_LIBHQR_CFLAGS_OTHER})
+
+find_path(
+  LIBHQR_INCLUDE_DIR
+  libhqr.h
+  HINTS ${PC_LIBHQR_INCLUDEDIR}
+  ${PC_LIBHQR_INCLUDE_DIRS}
+  )
+
+find_library(
+  LIBHQR_LIBRARY
+  NAMES hqr
+  HINTS ${PC_LIBHQR_LIBDIR} ${PC_LIBHQR_LIBRARY_DIRS}
+  )
+
+include(FindPackageHandleStandardArgs)
+# handle the QUIETLY and REQUIRED arguments
+# and set LIBHQR_FOUND to TRUE
+# if all listed variables are TRUE
+find_package_handle_standard_args(
+  LIBHQR  DEFAULT_MSG LIBHQR_LIBRARY LIBHQR_INCLUDE_DIR)
+
+mark_as_advanced(LIBHQR_INCLUDE_DIR LIBHQR_LIBRARY )
+
+set(LIBHQR_LIBRARIES ${LIBHQR_LIBRARY} )
+set(LIBHQR_INCLUDE_DIRS ${LIBHQR_INCLUDE_DIR} )
diff --git a/compute/CMakeLists.txt b/compute/CMakeLists.txt
index 9dcc70224..763902b18 100644
--- a/compute/CMakeLists.txt
+++ b/compute/CMakeLists.txt
@@ -105,6 +105,7 @@ set(ZSRC
     pzgelqfrh.c
     pzgeqrf.c
     pzgeqrfrh.c
+    pzgeqrfhqr.c
     pzgetrf_incpiv.c
     pzgetrf_nopiv.c
     pzlacpy.c
diff --git a/compute/pzgeqrfhqr.c b/compute/pzgeqrfhqr.c
new file mode 100644
index 000000000..713213654
--- /dev/null
+++ b/compute/pzgeqrfhqr.c
@@ -0,0 +1,240 @@
+/**
+ *
+ * @copyright (c) 2009-2014 The University of Tennessee and The University of
+ *                          Tennessee Research Foundation.  All rights reserved.
+ * @copyright (c) 2012-2017 Bordeaux INP, CNRS (LaBRI UMR 5800), Inria,
+ *                          Univ. Bordeaux. All rights reserved.
+ *
+ **/
+
+/**
+ *
+ * @file pzgeqrfhqr.c
+ *
+ *  MORSE auxiliary routines
+ *  MORSE is a software package provided by Univ. of Tennessee,
+ *  Univ. of California Berkeley and Univ. of Colorado Denver
+ *
+ * @version 2.5.0
+ * @author Jakub Kurzak
+ * @author Hatem Ltaief
+ * @author Dulceneia Becker
+ * @author Mathieu Faverge
+ * @author Emmanuel Agullo
+ * @author Cedric Castagnede
+ * @author Raphael Boucherie
+ * @date 2010-11-15
+ * @precisions normal z -> s d c
+ *
+ **/
+#include "control/common.h"
+#include "libhqr.h"
+
+#define A(m,n) A,  (m),  (n)
+#define T(m,n) T,  (m),  (n)
+#define T2(m,n) T,  (m), ((n)+A->nt)
+#if defined(CHAMELEON_COPY_DIAG)
+#define DIAG(m,n) DIAG, (m), (n)
+#else
+#define DIAG(m,n) A,  (m),  (n)
+#endif
+
+/***************************************************************************//**
+                                                                              *  Parallel tile QR factorization (reduction Householder) - dynamic scheduling
+                                                                              **/
+void morse_pzgeqrfhqr(MORSE_desc_t *A, MORSE_desc_t *T, const libhqr_tree_t *qrtree,
+                      MORSE_sequence_t *sequence, MORSE_request_t *request)
+{
+    MORSE_context_t *morse;
+    MORSE_option_t options;
+    size_t ws_worker = 0;
+    size_t ws_host = 0;
+    MORSE_desc_t *DIAG = NULL;
+
+    int k, m, n, i, j, p;
+    int K, M, RD;
+    int ldap, ldam, ldaMRD;
+    int tempkmin, tempkn, tempMm, tempnn, tempmm, tempMRDm;
+    int ib;
+    int *tiles;
+
+    morse = morse_context_self();
+    if (sequence->status != MORSE_SUCCESS)
+        return;
+    RUNTIME_options_init(&options, morse, sequence, request);
+
+    ib = MORSE_IB;
+
+    /*
+     * zgeqrt = A->nb * (ib+1)
+     * zunmqr = A->nb * ib
+     * ztsqrt = A->nb * (ib+1)
+     * zttqrt = A->nb * (ib+1)
+     * ztsmqr = A->nb * ib
+     * zttmqr = A->nb * ib
+     */
+    ws_worker = A->nb * (ib+1);
+
+    /* Allocation of temporary (scratch) working space */
+#if defined(CHAMELEON_USE_CUDA)
+    /* Worker space
+     *
+     * zunmqr = A->nb * ib
+     * ztsmqr = 2 * A->nb * ib
+     */
+    ws_worker = chameleon_max( ws_worker, ib * A->nb * 2 );
+#endif
+
+    /* Initialisation of tiles */
+
+    tiles = (int*)malloc((qrtree->mt)*sizeof(int));
+    memset( tiles, 0, (qrtree->mt)*sizeof(int) );
+
+#if defined(CHAMELEON_USE_MAGMA)
+    /* Worker space
+     *
+     * zgeqrt = max( A->nb * (ib+1), ib * (ib + A->nb) )
+     * ztsqrt = max( A->nb * (ib+1), ib * (ib + A->nb) )
+     */
+    ws_worker = chameleon_max( ws_worker, ib * (ib + A->nb) );
+
+    /* Host space
+     *
+     * zgeqrt = ib * (A->nb+3*ib) + A->nb )
+     * ztsqrt = 2 * ib * (A->nb+ib) + A->nb
+     */
+    ws_host = chameleon_max( ws_host, ib * (A->mb + 3 * ib) + A->mb );
+    ws_host = chameleon_max( ws_host,  2 * ib * (A->nb + ib) + A->nb );
+#endif
+
+    ws_worker *= sizeof(MORSE_Complex64_t);
+    ws_host   *= sizeof(MORSE_Complex64_t);
+
+    RUNTIME_options_ws_alloc( &options, ws_worker, ws_host );
+
+#if defined(CHAMELEON_COPY_DIAG)
+    {
+        /* necessary to avoid dependencies between tasks regarding the diag tile */
+        int nblk = ( A->mt + BS -1 ) / BS;
+        DIAG = (MORSE_desc_t*)malloc(sizeof(MORSE_desc_t));
+        morse_zdesc_alloc_diag(*DIAG, A->mb, A->nb, nblk * A->mb, A->nb, 0, 0, nblk * A->mb, A->nb, A->p, A->q);
+    }
+#endif
+
+    K = chameleon_min(A->mt, A->nt);
+
+    /* The number of the factorization */
+    for (k = 0; k < K; k++) {
+        RUNTIME_iteration_push(morse, k);
+        tempkn = k == A->nt-1 ? A->n-k*A->nb : A->nb;
+
+        /* The number of geqrt to apply */
+        for (i = 0; i < qrtree->getnbgeqrf(qrtree, k); i++) {
+            m = qrtree->getm(qrtree, k, i);
+            tempmm = m == A->mt-1 ? A->m-m*A->mb : A->mb;
+            tempkmin = chameleon_min(tempmm, tempkn);
+            ldam = BLKLDD(A, m);
+
+            MORSE_TASK_zgeqrt(
+                &options,
+                tempmm, tempkn, ib, T->nb,
+                A(m, k), ldam,
+                T(m, k), T->mb);
+            if ( k < (A->nt-1) ) {
+#if defined(CHAMELEON_COPY_DIAG)
+                MORSE_TASK_zlacpy(
+                    &options,
+                    MorseLower, tempmm, A->nb, A->nb,
+                    A(m, k), ldam,
+                    DIAG(m, k), ldam );
+#if defined(CHAMELEON_USE_CUDA)
+                MORSE_TASK_zlaset(
+                    &options,
+                    MorseUpper, tempmm, A->nb,
+                    0., 1.,
+                    DIAG(m, k), ldam );
+#endif
+#endif
+            }
+            for (n = k+1; n < A->nt; n++) {
+                tempnn = n == A->nt-1 ? A->n-n*A->nb : A->nb;
+                MORSE_TASK_zunmqr(
+                    &options,
+                    MorseLeft, MorseConjTrans,
+                    tempmm, tempnn, tempkmin, ib, T->nb,
+                    DIAG(m, k), ldam,
+                    T(m, k), T->mb,
+                    A(m, n), ldam);
+            }
+        }
+
+        /* Setting the order of the tiles */
+        libhqr_treewalk(qrtree, k, tiles);
+
+        for (j = k; j < A->mt-1; j++) {
+            m = tiles[j];
+            p = qrtree->currpiv(qrtree, k, m);
+
+            tempmm == A->mt-1 ? A->m-m*A->mb : A->mb;
+            ldam = BLKLDD(A, m);
+            ldap = BLKLDD(A, p);
+
+            /* Tiles killed is a TS */
+            if(qrtree->gettype(qrtree, k, m) == 0){
+                MORSE_TASK_ztsqrt(
+                    &options,
+                    tempmm, tempkn, ib, T->nb,
+                    A(p, k), ldap,
+                    A(m, k), ldam,
+                    T(m, k), T->mb);
+
+                for (n = k+1; n < A->nt; n++) {
+                    tempnn = n == A->nt-1 ? A->n-n*A->nb : A->nb;
+                    MORSE_TASK_ztsmqr(
+                        &options,
+                        MorseLeft, MorseConjTrans,
+                        A->nb, tempnn, tempmm, tempnn, A->nb, ib, T->nb,
+                        A(p, n), ldap,
+                        A(m, n), ldam,
+                        A(m, k), ldam,
+                        T(m, k), T->mb);
+                }
+            }
+
+            /* Tiles killed is a TT */
+            else {
+                MORSE_TASK_zttqrt(
+                    &options,
+                    tempmm, tempkn, ib, T->nb,
+                    A(p, k), ldap,
+                    A(m, k), ldam,
+                    T(m, k), T->mb);
+
+                for (n = k+1; n < A->nt; n++) {
+                    tempnn = n == A->nt-1 ? A->n-n*A->nb : A->nb;
+                    MORSE_TASK_zttmqr(
+                        &options,
+                        MorseLeft, MorseConjTrans,
+                        A->mb, tempnn, tempmm, tempnn, A->nb, ib, T->nb,
+                        A(p, n), ldap,
+                        A(m, n), ldam,
+                        A(m, k), ldam,
+                        T(m, k), T->mb);
+                }
+            }
+        }
+        RUNTIME_iteration_pop(morse);
+    }
+
+    free(tiles);
+    RUNTIME_options_ws_free(&options);
+    RUNTIME_options_finalize(&options, morse);
+    MORSE_TASK_dataflush_all();
+
+#if defined(CHAMELEON_COPY_DIAG)
+    MORSE_Sequence_Wait(sequence);
+    morse_desc_mat_free(DIAG);
+    free(DIAG);
+#endif
+    (void)DIAG;
+}
-- 
GitLab