From b4ebd64d2d2e316662409381d0db707ed2eaa42c Mon Sep 17 00:00:00 2001
From: Norihiro Watanabe <norihiro.watanabe@ufz.de>
Date: Wed, 2 Nov 2016 14:13:32 +0100
Subject: [PATCH] [CMake] add OGS_USE_MKL option

---
 CMakeLists.txt                    |  8 ++++
 MathLib/CMakeLists.txt            |  4 ++
 scripts/cmake/Find.cmake          |  4 ++
 scripts/cmake/cmake/FindMKL.cmake | 69 +++++++++++++++++++++++++++++++
 4 files changed, 85 insertions(+)
 create mode 100644 scripts/cmake/cmake/FindMKL.cmake

diff --git a/CMakeLists.txt b/CMakeLists.txt
index cf2982e9df3..96e2739d160 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -84,6 +84,9 @@ option(OGS_USE_EIGEN "Use Eigen linear solver" ON)
 option(OGS_EIGEN_DYNAMIC_SHAPE_MATRICES "Use dynamically allocated shape matrices" ON)
 option(EIGEN_NO_DEBUG "Disables Eigen's assertions" OFF)
 
+# MKL
+option(OGS_USE_MKL "Use Intel MKL" OFF)
+
 # Logging
 option(OGS_DISABLE_LOGGING "Disables all logog messages." OFF)
 
@@ -135,6 +138,11 @@ if(OGS_USE_LIS)
     set(OGS_USE_EIGEN ON)
 endif()
 
+if(OGS_USE_MKL)
+    add_definitions(-DUSE_MKL)
+    include_directories(SYSTEM ${MKL_INCLUDE_DIR})
+endif()
+
 if(OGS_USE_PETSC)
     add_definitions(-DUSE_PETSC)
     set(OGS_USE_MPI ON CACHE BOOL "Use MPI" FORCE)
diff --git a/MathLib/CMakeLists.txt b/MathLib/CMakeLists.txt
index b0b5e5a54dc..a0d7bff638f 100644
--- a/MathLib/CMakeLists.txt
+++ b/MathLib/CMakeLists.txt
@@ -43,6 +43,10 @@ if (OGS_USE_LIS)
     target_link_libraries(MathLib ${LIS_LIBRARIES})
 endif()
 
+if (OGS_USE_MKL)
+    target_link_libraries(MathLib ${MKL_LIBRARIES})
+endif()
+
 if (OGS_USE_PETSC)
     target_link_libraries(MathLib ${PETSC_LIBRARIES})
 endif()
diff --git a/scripts/cmake/Find.cmake b/scripts/cmake/Find.cmake
index 342de3e5c7f..15f49b08386 100644
--- a/scripts/cmake/Find.cmake
+++ b/scripts/cmake/Find.cmake
@@ -115,6 +115,10 @@ if(OGS_USE_LIS)
     find_package( LIS REQUIRED )
 endif()
 
+if(OGS_USE_MKL)
+    find_package( MKL REQUIRED )
+endif()
+
 if(OGS_USE_PETSC)
     message(STATUS "Configuring for PETSc")
 
diff --git a/scripts/cmake/cmake/FindMKL.cmake b/scripts/cmake/cmake/FindMKL.cmake
new file mode 100644
index 00000000000..4274808b5af
--- /dev/null
+++ b/scripts/cmake/cmake/FindMKL.cmake
@@ -0,0 +1,69 @@
+# Find Intel Math Karnel Library (MKL)
+#
+# Options
+# - MKL_DIR      MKL root directory
+# - MKL_OPENMP   use OpenMP threading
+#
+# Results
+# - MKL_INCLUDE_DIR
+# - MKL_LIBRARIES
+
+# Lookg for MKL root dir
+#SET(MKL_DIR $ENV{MKL_DIR} CACHE PATH "MKL root directory")
+if (NOT MKL_DIR)
+    find_path(MKL_DIR
+        include/mkl.h
+        PATHS
+        $ENV{MKL_DIR}
+        /opt/intel/mkl/
+        )
+endif()
+MESSAGE("MKL_DIR : ${MKL_DIR}")
+
+# Find MKL include dir
+FIND_PATH(MKL_INCLUDE_DIR NAMES mkl.h
+    PATHS
+    ${MKL_DIR}/include
+)
+
+# Set the directory path storing MKL libraries
+if (NOT MKL_LIB_DIR)
+    if(APPLE)
+        set(MKL_LIB_DIR ${MKL_DIR}/lib)
+    else()
+        if (${CMAKE_HOST_SYSTEM_PROCESSOR} STREQUAL "x86_64")
+            set(MKL_LIB_DIR ${MKL_DIR}/lib/intel64)
+        else()
+            set(MKL_LIB_DIR ${MKL_DIR}/lib/ia32)
+        endif()
+    endif()
+endif()
+
+# Find MKL libs
+find_library(MKL_LIB_CORE mkl_core PATHS ${MKL_LIB_DIR})
+
+if (${CMAKE_HOST_SYSTEM_PROCESSOR} STREQUAL "x86_64")
+    set(MKL_INTEL_LIB_NAME mkl_intel_lp64)
+else()
+    set(MKL_INTEL_LIB_NAME mkl_intel)
+endif()
+
+find_library(MKL_LIB_INTEL ${MKL_INTEL_LIB_NAME} PATHS ${MKL_LIB_DIR})
+
+if(OPENMP_FOUND)
+    set(MKL_THREAD_LIB_NAME "mkl_gnu_thread")
+else()
+    set(MKL_THREAD_LIB_NAME "mkl_sequential")
+endif()
+find_library(MKL_LIB_THREAD ${MKL_THREAD_LIB_NAME} PATHS ${MKL_LIB_DIR})
+
+
+SET (MKL_LIBRARIES 
+    "${MKL_LIB_INTEL}" 
+    "${MKL_LIB_THREAD}"
+    "${MKL_LIB_CORE}")
+
+include(FindPackageHandleStandardArgs)
+find_package_handle_standard_args(MKL DEFAULT_MSG MKL_INCLUDE_DIR MKL_LIBRARIES)
+mark_as_advanced(MKL_INCLUDE_DIR MKL_LIBRARIES)
+
-- 
GitLab