Skip to content

Commit 77c0227

Browse files
authored
Merge pull request #23 from jfdev001/fix-use-mkl
Fix use mkl
2 parents 5534ed0 + 5a063d4 commit 77c0227

File tree

8 files changed

+53
-73
lines changed

8 files changed

+53
-73
lines changed

CMakeLists.txt

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ project(
2020
# Set paths to Intel oneAPI MKL
2121
if(${ENABLE_SPBLAS})
2222
if (${ENABLE_FIND_INTEL_MKL})
23-
find_package(MKL CONFIG)
24-
get_target_property(MKLINCLUDE MKL::MKL INTERFACE_INCLUDE_DIRECTORIES)
23+
#find_package(MKL CONFIG)
24+
find_package(MKL REQUIRED)
25+
#get_target_property(MKLINCLUDE MKL::MKL INTERFACE_INCLUDE_DIRECTORIES)
2526
endif()
2627

2728
# manually set MKL vars
@@ -36,33 +37,6 @@ if(${ENABLE_SPBLAS})
3637
else()
3738
message(STATUS "Found MKL")
3839
endif()
39-
40-
if(NOT EXISTS "${MKLINCLUDE}/mkl_spblas.mod")
41-
set(NO_MKL_SPBLAS_MOD "
42-
=======================================================================
43-
${MKLINCLUDE} does not contain mkl_spblas.mod
44-
If you installed Intel MKL to a directory
45-
that requires root permissions,
46-
(e.g., /opt/intel/oneapi/mkl/latest/)
47-
```
48-
# alternatively... you could update secure path in sudo visudo
49-
sudo -i
50-
echo 'source /opt/intel/oneapi/setvars.sh > /dev/null' >> ~/.bashrc
51-
source ~/.bashrc
52-
your_mkl_include=/opt/intel/oneapi/mkl/latest/include
53-
ifx -c your_mkl_include/mkl_spblas.f90
54-
```
55-
otherwise if you installed to a directory
56-
that does not require root permissions, it
57-
should be sufficient to call
58-
```
59-
cd /your/mkl/include # replace with your local path
60-
ifx -c mkl_spblas.f90
61-
```
62-
=======================================================================
63-
")
64-
message(FATAL_ERROR "${NO_MKL_SPBLAS_MOD}")
65-
endif()
6640
endif()
6741

6842
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${PROJECT_SOURCE_DIR}/cmake")

README.md

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,12 @@ projects.
77
Can easily use `pFUnit` or `test-drive` for unit testing.
88

99
Make sure you have the [Intel oneAPI HPC Toolkit](https://www.intel.com/content/www/us/en/developer/tools/oneapi/hpc-toolkit-download.html?packages=hpc-toolkit&hpc-toolkit-os=linux&hpc-toolkit-lin=apt) or [Intel Fortran Essentials](https://www.intel.com/content/www/us/en/developer/tools/oneapi/hpc-toolkit-download.html?packages=fortran-essentials&fortran-essentials-os=linux&fortran-essentials-lin=apt).
10-
11-
Make sure also that in your `${MKLROOT}/include` directory there is a file
12-
called `mkl_spblas.mod`. The most up to date version of Intel MKL does not
13-
ship with `mkl_spblas.mod` by default, so you have to compile it separately
14-
via `ifx -c /path/to/mkl_spblas.f90`.
1510

1611
Then, you can configure, build, and run tests for the project.
1712

1813
```shell
1914
# test-drive builds faster than pfunit
20-
./config/intel_ubuntu_test_drive
15+
./config/intel_ubuntu_test_drive_fftpack_spblas
2116
```
2217

2318
Alternatively, if on a system where you need to specify a different MKL library

cmake/helper.cmake

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@
88
function(link_spblas targ)
99
if (MKL_FOUND)
1010
# https://www.intel.com/content/www/us/en/docs/onemkl/developer-guide-macos/2023-0/cmake-config-for-onemkl.html
11-
target_compile_options(
12-
${targ} PUBLIC $<TARGET_PROPERTY:MKL::MKL,INTERFACE_COMPILE_OPTIONS>)
1311
target_include_directories(
1412
${targ} PUBLIC $<TARGET_PROPERTY:MKL::MKL,INTERFACE_INCLUDE_DIRECTORIES>)
1513
target_link_libraries(
1614
${targ} PUBLIC $<LINK_ONLY:MKL::MKL>)
15+
16+
# NOTE: If these are PUBLIC, it means(?) the unit tests inherit these
17+
# options and this leads to an error where `CALL check` is unrecognized...
18+
target_compile_options(
19+
${targ} PRIVATE $<TARGET_PROPERTY:MKL::MKL,INTERFACE_COMPILE_OPTIONS>)
1720
else()
1821
# Add include directories
1922
target_include_directories(
@@ -107,7 +110,7 @@ endfunction()
107110

108111
# Links the supplied library
109112
function(link_library targ lib include_dir)
110-
target_link_libraries(${targ} ${lib})
113+
target_link_libraries(${targ} PUBLIC ${lib})
111114
target_include_directories(${targ} PUBLIC $<BUILD_INTERFACE:${include_dir}>)
112115
endfunction()
113116

config/intel_ubuntu_spblas

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ cmake -S . -B build-spblas\
77
-DCMAKE_Fortran_COMPILER=ifx\
88
-DBUILD_TESTING=ON\
99
-DENABLE_SPBLAS=ON\
10-
-DENABLE_FIND_INTEL_MKL=OFF
10+
-DENABLE_FIND_INTEL_MKL=ON
1111
cmake --build build-spblas
1212
./build-spblas/test/test_spblas
1313
[[ -n $builddocs ]] && cmake --build build-spblas --target doxygen_docs

config/intel_ubuntu_test_drive renamed to config/intel_ubuntu_test_drive_fftpack_spblas

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#!/usr/bin/bash
22
# @brief Baseline configuration assuming standard oneapi installation via apt.
33
builddocs=$1
4+
DEFAULT_FIND_INTEL_MKL="ON"
5+
FIND_INTEL_MKL="${FIND_INTEL_MKL:-$DEFAULT_FIND_INTEL_MKL}"
46
(
57
curdir=$(pwd)
68
[[ $curdir == *"config"* ]] && echo "must be in root project dir" && exit 1
@@ -11,15 +13,15 @@ cmake -S . -B build\
1113
-DENABLE_TEST_DRIVE=ON\
1214
-DENABLE_FFTPACK=ON\
1315
-DENABLE_SPBLAS=ON\
14-
-DENABLE_FIND_INTEL_MKL=OFF\
16+
-DENABLE_FIND_INTEL_MKL=${FIND_INTEL_MKL}\
1517
-DENABLE_PFUNIT=OFF\
1618
-DCMAKE_INSTALL_PREFIX=build/installed
1719
cmake --build build
1820
cmake --build build --target install
1921
cd build
2022
# name from test/CMakeLists.txt build_tests(FIRST_ARG ...)
2123
ctest -R test_drive_square_tests
22-
# test/test_spblas
24+
test/test_spblas
2325
# build docs
2426
[[ -n $builddocs ]] && cmake --build . --target doxygen_docs
2527
)

src/mo_square.f90

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
1-
!> @file mo_square.f90
2-
!! @brief Defines a module of simple mathematical operations.
1+
! Prevents doxygen from documenting the included module
2+
!! @cond
3+
#ifdef INTEL_SPBLAS
4+
INCLUDE "mkl_spblas.f90"
5+
#endif
6+
!! @endcond
7+
8+
!> Defines a module of simple mathematical operations.
39
MODULE mo_square
410

511
#ifdef FFTPACK
612
USE fftpack
713
#endif
814

915
#ifdef INTEL_SPBLAS
10-
USE mkl_spblas
16+
USE mkl_spblas
1117
#endif
1218

1319
IMPLICIT NONE
1420

1521
PRIVATE
1622

1723
PUBLIC :: sum_cube_square
24+
PUBLIC :: square
1825

1926
CONTAINS
2027

@@ -28,7 +35,6 @@ END FUNCTION square
2835

2936
!> @brief Computes the cube of a real number.
3037
PURE REAL FUNCTION cube(x) result(r)
31-
! An undocumented function
3238
REAL, INTENT(in) :: x
3339
r = x**3
3440
END FUNCTION cube

test/test_spblas.f90

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,33 +16,31 @@
1616
!
1717
! In the MKL Cookbook (cookbook_index.htm) many different example problems are
1818
! solved... could use that also as a reference.
19-
include "mkl_spblas.f90" ! TODO: remove this!
19+
!! @cond
20+
include "mkl_spblas.f90"
21+
!! @endcond
2022
PROGRAM TEST_SPBLAS
21-
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_FLOAT, C_INT, C_LONG
22-
USE mkl_spblas, ONLY: sparse_matrix_t, matrix_descr, &
23-
mkl_sparse_s_create_csr, mkl_sparse_s_create_coo, mkl_sparse_s_mv, &
24-
SPARSE_INDEX_BASE_ONE, SPARSE_MATRIX_TYPE_GENERAL, &
25-
SPARSE_OPERATION_NON_TRANSPOSE
23+
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_FLOAT, C_LONG, C_INT, C_DOUBLE
24+
USE MKL_SPBLAS
2625

2726
IMPLICIT NONE
2827

29-
INTEGER(KIND=C_INT), PARAMETER :: rows = 4
30-
INTEGER(KIND=C_INT), PARAMETER :: cols = 6
28+
INTEGER(KIND=C_LONG), PARAMETER :: rows = 4
29+
INTEGER(KIND=C_LONG), PARAMETER :: cols = 6
3130

32-
INTEGER(KIND=C_INT), PARAMETER :: nnz = 8
31+
INTEGER(KIND=C_LONG), PARAMETER :: nnz = 8
3332

34-
!INTEGER(KIND=C_INT) :: ia(rows+1), ja(nnz)
35-
INTEGER(KIND=C_LONG) :: ia(rows+1), ja(nnz)
33+
INTEGER(KIND=C_LONG) :: ia(rows+1), ja(nnz) ! works with include 'mkl_spblas.f90'
3634
INTEGER :: stat
37-
REAL(KIND=C_FLOAT) :: values(nnz), x(6), y(4), y_coo(4)
35+
REAL(KIND=C_DOUBLE) :: values(nnz), x(6), y(4), y_coo(4), alpha, beta
3836

39-
TYPE(sparse_matrix_t) :: a
40-
TYPE(matrix_descr) :: descr
37+
TYPE(SPARSE_MATRIX_T) :: a
38+
TYPE(MATRIX_DESCR) :: descr
4139

42-
TYPE(sparse_matrix_t) :: A_coo
40+
TYPE(SPARSE_MATRIX_T) :: A_coo
4341

44-
INTEGER(KIND=C_INT), ALLOCATABLE :: row_indx(:)
45-
INTEGER(KIND=C_INT), ALLOCATABLE :: col_indx(:)
42+
INTEGER(KIND=C_LONG), ALLOCATABLE :: row_indx(:)
43+
INTEGER(KIND=C_LONG), ALLOCATABLE :: col_indx(:)
4644

4745
! Matrix example taken from:
4846
! https://en.wikipedia.org/wiki/Sparse_matrix#Compressed_sparse_row_(CSR,_CRS_or_Yale_format)
@@ -58,30 +56,32 @@ PROGRAM TEST_SPBLAS
5856
ja = [1,2,2,4,3,4,5,6]
5957
values = [10, 20, 30, 40, 50, 60, 70, 80]
6058

61-
stat = mkl_sparse_s_create_csr(&
59+
stat = MKL_SPARSE_D_CREATE_CSR(&
6260
a,SPARSE_INDEX_BASE_ONE,rows,cols,ia(1:4),ia(2:5),ja,values)
6361

6462
print *, "stat create = ", stat
6563
descr%type = SPARSE_MATRIX_TYPE_GENERAL
6664

6765
!! create coo
68-
!row_indx = [1, 1, 2, 2, 3, 3, 3, 4]
69-
!col_indx = [1, 2, 2, 4, 3, 4, 5, 6]
70-
!stat = mkl_sparse_d_create_coo(&
71-
!A_coo, SPARSE_INDEX_BASE_ONE, rows, cols, nnz, row_indx, col_indx,&
72-
!values)
66+
row_indx = [1, 1, 2, 2, 3, 3, 3, 4]
67+
col_indx = [1, 2, 2, 4, 3, 4, 5, 6]
68+
stat = mkl_sparse_d_create_coo(&
69+
A_coo, SPARSE_INDEX_BASE_ONE, rows, cols, nnz, row_indx, col_indx,&
70+
values)
7371

7472
! spmv:
7573
x = [1,1,1,1,1,1]
74+
beta = 0
75+
alpha = 1
7676
! spmv csr
77-
stat = mkl_sparse_s_mv(SPARSE_OPERATION_NON_TRANSPOSE,1.0,a,descr,x,0.0,y)
77+
stat = MKL_SPARSE_D_MV(SPARSE_OPERATION_NON_TRANSPOSE,alpha,a,descr,x,beta,y)
7878
print *, "stat mv = ", stat
7979
print *, "result csr = ", y
8080
print *, "expected = ", [30., 70., 180., 80.]
8181

8282
!! spmv coo
83-
!stat = mkl_sparse_d_mv(&
84-
!SPARSE_OPERATION_NON_TRANSPOSE, 1.0, A_coo, descr, x, 0.0, y_coo)
85-
!print *, "result coo = ", y_coo
83+
stat = mkl_sparse_d_mv(&
84+
SPARSE_OPERATION_NON_TRANSPOSE, alpha, A_coo, descr, x, beta, y_coo)
85+
print *, "result coo = ", y_coo
8686

8787
END PROGRAM TEST_SPBLAS

test/test_square_with_test_drive.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
! Example module that includes mo_square and performs a dummy unit test
22
MODULE test_square_with_test_drive
33

4-
use mo_square
4+
USE mo_square
55
USE testdrive, ONLY: new_unittest, unittest_type, error_type, check
66
IMPLICIT NONE
77
PRIVATE

0 commit comments

Comments
 (0)