Skip to content

Commit 581ee57

Browse files
committed
trying with test root
1 parent 5840eb5 commit 581ee57

File tree

4 files changed

+280
-0
lines changed

4 files changed

+280
-0
lines changed

test_root_spblas.f90

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
! Sparse BLAS matrix-vector product example
2+
!
3+
! The following code demonstrates the creation of
4+
! a CSR matrix, and the sparse matrix-vector product
5+
! using the sparse BLAS functions in Intel MKL.
6+
!
7+
! For linking options for your specific platform see
8+
! the Intel MKL link line advisor located at:
9+
! https://software.intel.com/content/www/us/en/develop/tools/oneapi/components/onemkl/link-line-advisor.html
10+
!
11+
! In the Inspector-Executor Sparse BLAS Routines, the creation of matrices
12+
! which can be used for solving is described. (mkl_devref_fortran_index.htm)
13+
!
14+
! Is are those matrices used for solving? Might be simpler means of this...
15+
! But at least this will be useful later. There is a direct solver interface..
16+
!
17+
! In the MKL Cookbook (cookbook_index.htm) many different example problems are
18+
! solved... could use that also as a reference.
19+
INCLUDE "mkl_spblas.f90"
20+
PROGRAM TEST_SPBLAS
21+
USE mkl_spblas, ONLY: sparse_matrix_t, matrix_descr, &
22+
mkl_sparse_s_create_csr, mkl_sparse_s_create_coo, mkl_sparse_s_mv, &
23+
SPARSE_INDEX_BASE_ONE, SPARSE_MATRIX_TYPE_GENERAL, &
24+
SPARSE_OPERATION_NON_TRANSPOSE
25+
26+
IMPLICIT NONE
27+
28+
INTEGER, PARAMETER :: rows = 4
29+
INTEGER, PARAMETER :: cols = 6
30+
31+
INTEGER, PARAMETER :: nnz = 8
32+
33+
INTEGER :: ia(rows+1), ja(nnz), stat
34+
REAL :: values(nnz), x(6), y(4), y_coo(4)
35+
36+
TYPE(sparse_matrix_t) :: a
37+
TYPE(matrix_descr) :: descr
38+
39+
TYPE(sparse_matrix_t) :: A_coo
40+
41+
INTEGER, ALLOCATABLE :: row_indx(:)
42+
INTEGER, ALLOCATABLE :: col_indx(:)
43+
44+
! Matrix example taken from:
45+
! https://en.wikipedia.org/wiki/Sparse_matrix#Compressed_sparse_row_(CSR,_CRS_or_Yale_format)
46+
!
47+
! | 10 20 0 0 0 0 |
48+
! A = | 0 30 0 40 0 0 |
49+
! | 0 0 50 60 70 0 |
50+
! | 0 0 0 0 0 80 |
51+
52+
! create csr
53+
ia = [1,3,5,8,9]
54+
ja = [1,2,2,4,3,4,5,6]
55+
values = [10, 20, 30, 40, 50, 60, 70, 80]
56+
57+
stat = mkl_sparse_s_create_csr(&
58+
a,SPARSE_INDEX_BASE_ONE,rows,cols,ia(1:4),ia(2:5),ja,values)
59+
60+
print *, "stat create = ", stat
61+
descr%type = SPARSE_MATRIX_TYPE_GENERAL
62+
63+
! create coo
64+
row_indx = [1, 1, 2, 2, 3, 3, 3, 4]
65+
col_indx = [1, 2, 2, 4, 3, 4, 5, 6]
66+
stat = mkl_sparse_s_create_coo(&
67+
A_coo, SPARSE_INDEX_BASE_ONE, rows, cols, nnz, row_indx, col_indx,&
68+
values)
69+
70+
! spmv csr
71+
x = [1,1,1,1,1,1]
72+
stat = mkl_sparse_s_mv(SPARSE_OPERATION_NON_TRANSPOSE,1.0,a,descr,x,0.0,y)
73+
print *, "stat mv = ", stat
74+
print *, "result csr = ", y
75+
print *, "expected = ", [30., 70., 180., 80.]
76+
77+
! spmv coo
78+
stat = mkl_sparse_s_mv(&
79+
SPARSE_OPERATION_NON_TRANSPOSE, 1.0, A_coo, descr, x, 0.0, y_coo)
80+
print *, "result coo = ", y_coo
81+
82+
END PROGRAM TEST_SPBLAS

tmp/CMakeLists.txt

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
cmake_minimum_required(VERSION 3.15)
2+
3+
# Flags for testing
4+
# TODO: add BUILD_TESTING --> BUILD_TESTING
5+
option(PFUNIT "ON - using pFUnit for unit tests, OFF - skip building pfunit tests" OFF)
6+
option(TEST_DRIVE "ON - Using test-drive for unittests, OFF - skip building test-drive tests" OFF)
7+
8+
project(
9+
HelloSPBLAS
10+
VERSION 0.1.0
11+
LANGUAGES Fortran
12+
)
13+
14+
# Set paths to Intel oneAPI MKL
15+
set(MKLROOT $ENV{MKLROOT}) # Assuming MKLROOT is already set in the environment
16+
17+
if (NOT MKLROOT)
18+
message(FATAL_ERROR "MKLROOT environment variable is not set.")
19+
endif()
20+
21+
# These can be changed with -D... flag during build config
22+
set(MKLLIB ${MKLROOT}/lib CACHE PATH "Path to Intel MKL Libraries")
23+
set(MKLINCLUDE ${MKLROOT}/include CACHE PATH "Path to Intel MKL include files")
24+
25+
# Get cmake functions
26+
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${PROJECT_SOURCE_DIR}/cmake")
27+
28+
include(GNUInstallDirs)
29+
30+
message(STATUS "Build extern")
31+
add_subdirectory(extern)
32+
33+
message(STATUS "Build src")
34+
add_subdirectory(src)
35+
36+
# Documentation
37+
find_package(Doxygen)
38+
if(Doxygen_FOUND)
39+
message(STATUS "Building doxygen documentation")
40+
add_subdirectory(doc)
41+
else()
42+
message(STATUS "Doxygen not found, not building docs")
43+
endif()
44+
45+
# Add testing last (from test-drive)
46+
if (${TEST_DRIVE} OR ${PFUNIT})
47+
include(CTest)
48+
enable_testing()
49+
add_subdirectory(test)
50+
endif()

tmp/test/CMakeLists.txt

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#
2+
# Build unit tests and naive MKL executable for SpBLAS
3+
#
4+
5+
# helper for building tests (from fftpack)
6+
variable_watch(PROJECT_INCLUDE_DIR)
7+
macro(build_tests testname)
8+
add_executable(${testname} ${ARGN})
9+
link_library(${testname} ${PROJECT_NAME} ${PROJECT_INCLUDE_DIR})
10+
link_library(${testname} test-drive ${test-drive_INCLUDE_DIR})
11+
add_test(
12+
NAME ${testname}
13+
WORKING_DIRECTORY ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}
14+
COMMAND $<TARGET_FILE:${testname}>
15+
)
16+
endmacro()
17+
18+
# Define the executable target
19+
add_executable(
20+
test_spblas
21+
test_spblas.f90
22+
)
23+
24+
# Add include directories
25+
target_include_directories(
26+
test_spblas
27+
PRIVATE ${MKLINCLUDE}
28+
)
29+
30+
# Specify the MKL libraries for linking
31+
target_link_libraries(
32+
test_spblas
33+
PRIVATE
34+
# must be static
35+
${MKLLIB}/libmkl_blas95_ilp64.a
36+
37+
# must shared
38+
${MKLLIB}/libmkl_intel_ilp64.so
39+
${MKLLIB}/libmkl_sequential.so # or libmkl_intel_thread.so
40+
${MKLLIB}/libmkl_core.so
41+
42+
# standard shared libs
43+
pthread
44+
m
45+
dl
46+
)
47+
48+
# Add pfunit tests
49+
if (${PFUNIT})
50+
add_pfunit_ctest (test_square
51+
TEST_SOURCES test_square.pf
52+
LINK_LIBRARIES ${PROJECT_NAME}
53+
LABELS "myproject"
54+
)
55+
# TODO: more tests with the same label as above (e.g., testlib)
56+
endif()
57+
58+
# Add test-drive tests
59+
if (${TEST_DRIVE})
60+
set(
61+
TEST_DRIVE_SOURCES
62+
test_square_with_test_drive.f90 # files containing tests
63+
test_drive_tester.f90 # must have this driver!
64+
)
65+
build_tests(test_drive_square_tests ${TEST_DRIVE_SOURCES})
66+
endif()

tmp/test_spblas.f90

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
! Sparse BLAS matrix-vector product example
2+
!
3+
! The following code demonstrates the creation of
4+
! a CSR matrix, and the sparse matrix-vector product
5+
! using the sparse BLAS functions in Intel MKL.
6+
!
7+
! For linking options for your specific platform see
8+
! the Intel MKL link line advisor located at:
9+
! https://software.intel.com/content/www/us/en/develop/tools/oneapi/components/onemkl/link-line-advisor.html
10+
!
11+
! In the Inspector-Executor Sparse BLAS Routines, the creation of matrices
12+
! which can be used for solving is described. (mkl_devref_fortran_index.htm)
13+
!
14+
! Is are those matrices used for solving? Might be simpler means of this...
15+
! But at least this will be useful later. There is a direct solver interface..
16+
!
17+
! In the MKL Cookbook (cookbook_index.htm) many different example problems are
18+
! solved... could use that also as a reference.
19+
INCLUDE "mkl_spblas.f90"
20+
PROGRAM TEST_SPBLAS
21+
USE mkl_spblas, ONLY: sparse_matrix_t, matrix_descr, &
22+
mkl_sparse_s_create_csr, mkl_sparse_s_create_coo, mkl_sparse_s_mv, &
23+
SPARSE_INDEX_BASE_ONE, SPARSE_MATRIX_TYPE_GENERAL, &
24+
SPARSE_OPERATION_NON_TRANSPOSE
25+
26+
IMPLICIT NONE
27+
28+
INTEGER, PARAMETER :: rows = 4
29+
INTEGER, PARAMETER :: cols = 6
30+
31+
INTEGER, PARAMETER :: nnz = 8
32+
33+
INTEGER :: ia(rows+1), ja(nnz), stat
34+
REAL :: values(nnz), x(6), y(4), y_coo(4)
35+
36+
TYPE(sparse_matrix_t) :: a
37+
TYPE(matrix_descr) :: descr
38+
39+
TYPE(sparse_matrix_t) :: A_coo
40+
41+
INTEGER, ALLOCATABLE :: row_indx(:)
42+
INTEGER, ALLOCATABLE :: col_indx(:)
43+
44+
! Matrix example taken from:
45+
! https://en.wikipedia.org/wiki/Sparse_matrix#Compressed_sparse_row_(CSR,_CRS_or_Yale_format)
46+
!
47+
! | 10 20 0 0 0 0 |
48+
! A = | 0 30 0 40 0 0 |
49+
! | 0 0 50 60 70 0 |
50+
! | 0 0 0 0 0 80 |
51+
52+
! create csr
53+
ia = [1,3,5,8,9]
54+
ja = [1,2,2,4,3,4,5,6]
55+
values = [10, 20, 30, 40, 50, 60, 70, 80]
56+
57+
stat = mkl_sparse_s_create_csr(&
58+
a,SPARSE_INDEX_BASE_ONE,rows,cols,ia(1:4),ia(2:5),ja,values)
59+
60+
print *, "stat create = ", stat
61+
descr%type = SPARSE_MATRIX_TYPE_GENERAL
62+
63+
! create coo
64+
row_indx = [1, 1, 2, 2, 3, 3, 3, 4]
65+
col_indx = [1, 2, 2, 4, 3, 4, 5, 6]
66+
stat = mkl_sparse_s_create_coo(&
67+
A_coo, SPARSE_INDEX_BASE_ONE, rows, cols, nnz, row_indx, col_indx,&
68+
values)
69+
70+
! spmv csr
71+
x = [1,1,1,1,1,1]
72+
stat = mkl_sparse_s_mv(SPARSE_OPERATION_NON_TRANSPOSE,1.0,a,descr,x,0.0,y)
73+
print *, "stat mv = ", stat
74+
print *, "result csr = ", y
75+
print *, "expected = ", [30., 70., 180., 80.]
76+
77+
! spmv coo
78+
stat = mkl_sparse_s_mv(&
79+
SPARSE_OPERATION_NON_TRANSPOSE, 1.0, A_coo, descr, x, 0.0, y_coo)
80+
print *, "result coo = ", y_coo
81+
82+
END PROGRAM TEST_SPBLAS

0 commit comments

Comments
 (0)