From f45a9c13a5450c8ddd351df3763b8ff2159324da Mon Sep 17 00:00:00 2001
From: Florent Pruvost <florent.pruvost@inria.fr>
Date: Thu, 27 Apr 2017 18:20:42 +0200
Subject: [PATCH] add an example about fortran linking

---
 example/link_chameleon/CMakeLists.txt     | 11 +++--
 example/link_chameleon/README.org         | 35 +++++++++++++++
 example/link_chameleon/link_chameleon.f90 | 53 +++++++++++++++++++++++
 3 files changed, 95 insertions(+), 4 deletions(-)
 create mode 100644 example/link_chameleon/README.org
 create mode 100644 example/link_chameleon/link_chameleon.f90

diff --git a/example/link_chameleon/CMakeLists.txt b/example/link_chameleon/CMakeLists.txt
index e13eab1a9..957ce2206 100644
--- a/example/link_chameleon/CMakeLists.txt
+++ b/example/link_chameleon/CMakeLists.txt
@@ -17,10 +17,11 @@ elseif (MORSE_CHAMELEON_USE_STARPU)
 endif()
 
 if (MORSE_DISTRIB_DIR)
-    set( MORSE_CMAKE_MODULE_DIR "${MORSE_DISTRIB_DIR}/cmake_modules/morse" CACHE PATH
-         "Directory where to find MORSE CMake modules (cmake_modules/morse)")
+    set( MORSE_CMAKE_MODULE_DIR "${MORSE_DISTRIB_DIR}/morse_cmake/modules" CACHE PATH
+         "Directory where to find MORSE CMake modules (morse_cmake/modules)")
     list(APPEND CMAKE_MODULE_PATH "${MORSE_CMAKE_MODULE_DIR}")
     list(APPEND CMAKE_MODULE_PATH "${MORSE_CMAKE_MODULE_DIR}/find")
+    message(STATUS "MORSE_CMAKE_MODULE_DIR : ${MORSE_CMAKE_MODULE_DIR}/morse_cmake/modules")
     include(MorseInit)
 
     # Detect CHAMELEON
@@ -39,8 +40,10 @@ if (MORSE_DISTRIB_DIR)
     endif()
 
     # link_chameleon exe
-    add_executable(link_chameleon link_chameleon.c)
-    target_link_libraries(link_chameleon ${CHAMELEON_LIBRARIES_DEP})
+    add_executable(link_chameleon_c link_chameleon.c)
+    target_link_libraries(link_chameleon_c ${CHAMELEON_LIBRARIES_DEP})
+    add_executable(link_chameleon_f link_chameleon.f90)
+    target_link_libraries(link_chameleon_f ${CHAMELEON_LIBRARIES_DEP})
 else()
     message(STATUS "MORSE_DISTRIB_DIR is not set")
     message(STATUS "Please indicate where is located your MORSE distribution directory."
diff --git a/example/link_chameleon/README.org b/example/link_chameleon/README.org
new file mode 100644
index 000000000..080ad32aa
--- /dev/null
+++ b/example/link_chameleon/README.org
@@ -0,0 +1,35 @@
+#+TITLE: How to link with Chameleon
+#+LANGUAGE:  en
+#+OPTIONS: H:3 num:t \n:nil @:t ::t |:t _:nil ^:nil -:t f:t *:t <:t
+#+OPTIONS: TeX:t LaTeX:t skip:nil d:nil pri:nil tags:not-in-toc html-style:nil
+
+Install Chameleon and its dependencies
+#+begin_src
+# load an environment with dependencies available
+# for example
+spack install chameleon@master
+spack load openblas
+spack load hwloc
+spack load starpu
+spack load chameleon
+# alternativaly use pkg-config and/or update your CPATH and LD_LIBRARY_PATH
+# for example
+export PKG_CONFIG_PATH=/where/is/installed/chameleon/lib/pkgconfig:$PKG_CONFIG_PATH
+#+end_src
+
+Example to link with chameleon in CMake project, see the
+~CMakeLists.txt~ file
+#+begin_src
+mkdir build
+cd build
+cmake .. -DMORSE_DISTRIB_DIR=$PWD/../../cmake_modules -DMORSE_CHAMELEON_USE_STARPU=ON
+make VERBOSE=1
+./link_chameleon_c
+./link_chameleon_f
+#+end_src
+
+Example using directly the pkg-config file
+#+begin_src
+gcc      `pkg-config --cflags chameleon` -o link_chameleon_c link_chameleon.c   `pkg-config --libs --static chameleon`
+gfortran `pkg-config --cflags chameleon` -o link_chameleon_f link_chameleon.f90 `pkg-config --libs --static chameleon`
+#+end_src
diff --git a/example/link_chameleon/link_chameleon.f90 b/example/link_chameleon/link_chameleon.f90
new file mode 100644
index 000000000..4c689d6a8
--- /dev/null
+++ b/example/link_chameleon/link_chameleon.f90
@@ -0,0 +1,53 @@
+
+program fortran_example
+    implicit none
+    include 'morse_fortran.h'
+    integer, parameter:: dp=kind(0.d0) ! double precision
+    integer, parameter :: NCPU=2, NGPU=0
+    integer, parameter :: N=500, NRHS=1
+    double precision, dimension(N*N)    :: A, Acpy
+    double precision, dimension(N*NRHS) :: B, X
+    double precision :: anorm, bnorm, xnorm, res, eps=1.11022d-16
+    integer :: info
+    integer :: UPLO=MorseUpper
+    logical :: hres
+
+
+    ! Initialize MORSE with main parameters
+    call MORSE_Init(NCPU, NGPU, info)
+
+    ! generate A matrix with random values such that it is spd
+    call MORSE_dplgsy( dfloat(N), MorseUpperLower, N, A, N, 51, info )
+    Acpy = A
+
+    ! generate RHS
+    call MORSE_dplrnt( N, NRHS, B, N, 5673, info )
+    X = B
+
+    call MORSE_dpotrf( UPLO, N, A, N, INFO )
+    call MORSE_dpotrs( UPLO, N, NRHS, A, N, X, N, info)
+
+    ! compute norms to check the result
+    call MORSE_dlange( MorseInfNorm, N, N, Acpy, N, anorm)
+    call MORSE_dlange( MorseInfNorm, N, NRHS, B, N, bnorm)
+    call MORSE_dlange( MorseInfNorm, N, NRHS, X, N, xnorm)
+
+    ! compute A*X-B, store the result in B
+    call MORSE_dgemm( MorseNoTrans, MorseNoTrans, N, NRHS, N, 1.d0, Acpy, N, X, N, -1.d0, B, N, info)
+    call MORSE_dlange( MorseInfNorm, N, NRHS, B, N, res)
+
+    ! if hres = 0 then the test succeed
+    ! else the test failed
+    hres = .TRUE.
+    hres = ( res / N / eps / (anorm * xnorm + bnorm ) > 100.0 )
+    print *, "   ||Ax-b||       ||A||       ||x||       ||b|| ||Ax-b||/N/eps/(||A||||x||+||b||)"
+    if (hres) then
+        print *, res, anorm, xnorm, bnorm, res / N / eps / (anorm * xnorm + bnorm ), "FAILURE"
+    else
+        print *, res, anorm, xnorm, bnorm, res / N / eps / (anorm * xnorm + bnorm), "SUCCESS"
+    endif
+
+    ! Finalize MORSE
+    call MORSE_Finalize(info)
+
+end program fortran_example
-- 
GitLab