From 2c1e236a365ba543fcd38518a914b435b16f1082 Mon Sep 17 00:00:00 2001
From: Mathieu Faverge <mathieu.faverge@inria.fr>
Date: Thu, 12 Mar 2020 15:40:48 +0100
Subject: [PATCH] Add cmake structure to integrate hmat support

---
 CMakeLists.txt                  | 24 ++++++++++++++++++++++++
 cmake_modules/PrintOpts.cmake   |  1 +
 cmake_modules/local_subs.py     |  3 +++
 coreblas/compute/CMakeLists.txt |  8 ++++++++
 coreblas/include/CMakeLists.txt |  9 +++++++++
 include/chameleon/config.h.in   |  3 +++
 6 files changed, 48 insertions(+)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 525c91613..c7b08abfd 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -197,6 +197,9 @@ if (CHAMELEON_ENABLE_CUDA AND NOT CHAMELEON_USE_CUDA)
     message("-- ${BoldGreen}CHAMELEON_USE_CUDA is set to OFF, turn it ON to use CUDA (unsupported by Quark)${ColourReset}")
 endif()
 
+# Enable Hmat-OSS kernels
+option(CHAMELEON_USE_HMAT "Enable hmat-oss kernels" OFF)
+
 option(CHAMELEON_RUNTIME_SYNC "Enable synchronous task submission when available to debug the code without parallelism" OFF)
 if (CHAMELEON_RUNTIME_SYNC)
     message("-- ${BoldGreen}CHAMELEON_RUNTIME_SYNC is set to ON, turn it OFF to avoid synchronisation in the tasks submission${ColourReset}")
@@ -262,6 +265,27 @@ mark_as_advanced(CHAMELEON_COPY_DIAG)
 ################################
 add_subdirectory(hqr)
 
+###############################################################################
+# Build dependency HMAT-OSS library #
+#####################################
+if ( CHAMELEON_USE_HMAT )
+  find_package(HMAT REQUIRED)
+  if(HMAT_FOUND)
+    # hmat-oss target is not correctly defined
+    add_library(HMAT::hmat-oss INTERFACE IMPORTED)
+
+    set_target_properties(HMAT::hmat-oss PROPERTIES
+      INTERFACE_INCLUDE_DIRECTORIES "${HMAT_INCLUDE_DIRS}"
+      INTERFACE_COMPILE_DEFINITIONS "${HMAT_DEFINITIONS}"
+      INTERFACE_LINK_DIRECTORIES "${HMAT_LIBRARY_DIRS}"
+      INTERFACE_LINK_LIBRARIES "hmat-oss"
+      )
+    message( STATUS "HMAT: Found" )
+  else()
+    message( ERROR "HMAT: Not found" )
+  endif()
+endif()
+
 ###############################################################################
 # Look for dependencies #
 #########################
diff --git a/cmake_modules/PrintOpts.cmake b/cmake_modules/PrintOpts.cmake
index 8aac5f8b4..c439626b1 100644
--- a/cmake_modules/PrintOpts.cmake
+++ b/cmake_modules/PrintOpts.cmake
@@ -82,6 +82,7 @@ set(dep_message "${dep_message}"
 "       Kernels specific\n"
 "       BLAS ................: ${BLAS_VENDOR_FOUND}\n"
 "       LAPACK...............: ${LAPACK_VENDOR_FOUND}\n"
+"       HMAT-OSS.............: ${CHAMELEON_USE_HMAT}\n"
 "\n"
 "       Simulation mode .....: ${CHAMELEON_SIMULATION}\n"
 "\n"
diff --git a/cmake_modules/local_subs.py b/cmake_modules/local_subs.py
index c26498a1f..10a56bd0e 100644
--- a/cmake_modules/local_subs.py
+++ b/cmake_modules/local_subs.py
@@ -52,6 +52,7 @@ subs = {
         ('ChamPattern',          'ChamRealFloat',        'ChamRealDouble',       'ChamRealFloat',       r'\bChamRealDouble'    ),
         ('int',                  'float',                'double',               'complex32',             'complex64'          ),
         ('Int',                  'Float',                'Double',               'Complex32',             'Complex64'          ),
+        ('Int',                  'HMAT_SIMPLE_PRECISION','HMAT_DOUBLE_PRECISION','HMAT_SIMPLE_COMPLEX',   'HMAT_DOUBLE_COMPLEX'),
 
         # ----- Additional BLAS
         ('',                     'sTile',                'dTile',                'cTile',                'zTile'               ),
@@ -102,5 +103,7 @@ subs = {
       # ('',                     'stesting',             'dtesting',             'ctesting',             'ztesting'            ),
       # ('',                     'SAUXILIARY',           'DAUXILIARY',           'CAUXILIARY',           'ZAUXILIARY'          ),
       # ('',                     'sbuild',               'dbuild',               'cbuild',               'zbuild'              ),
+        # Hmat-OSS kernels
+        ('hmat_p',               'hmat_s',               'hmat_d',               'hmat_c',               'hmat_z'              ),
     ]
 }
diff --git a/coreblas/compute/CMakeLists.txt b/coreblas/compute/CMakeLists.txt
index 3e5cf7238..26c025f61 100644
--- a/coreblas/compute/CMakeLists.txt
+++ b/coreblas/compute/CMakeLists.txt
@@ -105,6 +105,10 @@ set(ZSRC
     core_zunmqr.c
     core_ztile.c
     )
+if( CHAMELEON_USE_HMAT )
+  list( APPEND ZSRC
+    hmat_z.c )
+endif()
 
 precisions_rules_py(COREBLAS_SRCS_GENERATED "${ZSRC}"
                     PRECISIONS "${CHAMELEON_PRECISION}")
@@ -133,6 +137,10 @@ target_include_directories(coreblas PUBLIC
   $<INSTALL_INTERFACE:include>)
 set_property(TARGET coreblas PROPERTY INSTALL_NAME_DIR "${CMAKE_INSTALL_PREFIX}/lib")
 
+if( CHAMELEON_USE_HMAT )
+  target_link_libraries(coreblas PUBLIC HMAT::hmat-oss )
+endif()
+
 target_link_libraries(coreblas PRIVATE MORSE::LAPACKE)
 target_link_libraries(coreblas PRIVATE MORSE::CBLAS)
 target_link_libraries(coreblas PUBLIC MORSE::M)
diff --git a/coreblas/include/CMakeLists.txt b/coreblas/include/CMakeLists.txt
index 56b43bcfe..30f7ad411 100644
--- a/coreblas/include/CMakeLists.txt
+++ b/coreblas/include/CMakeLists.txt
@@ -34,6 +34,11 @@ set(ZHDR
   coreblas/coreblas_zc.h
   coreblas/coreblas_ztile.h
 )
+if( CHAMELEON_USE_HMAT )
+  list( APPEND ZHDR
+    coreblas/hmat_z.h )
+endif()
+
 precisions_rules_py(
   COREBLAS_HDRS_GENERATED "${ZHDR}"
   TARGETDIR  coreblas
@@ -49,6 +54,10 @@ set(COREBLAS_HDRS
     coreblas/lapacke_mangling.h
     coreblas/random.h
     )
+if( CHAMELEON_USE_HMAT )
+  list( APPEND COREBLAS_HDRS
+    coreblas/hmat.h )
+endif()
 
 # Add generated headers
 # ---------------------
diff --git a/include/chameleon/config.h.in b/include/chameleon/config.h.in
index 58b066488..15073d1c4 100644
--- a/include/chameleon/config.h.in
+++ b/include/chameleon/config.h.in
@@ -51,6 +51,9 @@
 #cmakedefine CHAMELEON_USE_CUBLAS
 #cmakedefine CHAMELEON_USE_CUBLAS_V2
 
+/* Hmat-oss */
+#cmakedefine CHAMELEON_USE_HMAT
+
 /* Simulation */
 #cmakedefine CHAMELEON_SIMULATION
 #cmakedefine CHAMELEON_SIMULATION_EXTENDED
-- 
GitLab