Skip to content

Commit 500d73f

Browse files
Merge pull request #20 from jchristopherson/sparse
Add Sparse Matrix Support
2 parents 72c60fc + de7eafa commit 500d73f

475 files changed

Lines changed: 71987 additions & 3999 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/cmake.yml

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,40 +10,39 @@ env:
1010
BUILD_TYPE: Release
1111

1212
jobs:
13-
build:
13+
test:
1414
runs-on: ${{ matrix.os }}
1515
strategy:
1616
fail-fast: false
1717
matrix:
18-
fortran_compiler: [gfortran, ifort]
19-
os: [ubuntu-latest, macos-latest] # issue with windows-latest & CMake with CMake not recognizing defined compilers
18+
os: [ubuntu-latest, macos-latest]
19+
toolchain:
20+
- {compiler: gcc, version: 11}
21+
- {compiler: intel-classic, version: '2021.9'}
22+
include:
23+
- os: ubuntu-latest
24+
toolchain: {compiler: intel, version: '2023.2'}
2025

2126
steps:
22-
23-
- name: Setup IFORT
24-
if: contains( matrix.fortran_compiler, 'ifort' )
25-
uses: modflowpy/install-intelfortran-action@v1
26-
27-
- name: Setup GFORTRAN
28-
if: contains( matrix.fortran_compiler, 'gfortran')
29-
uses: awvwgk/setup-fortran@main
30-
id: setup-fortran
31-
with:
32-
compiler: gcc
33-
version: 12
34-
35-
env:
36-
FC: ${{ steps.setup-fortran.outputs.fc }}
37-
CC: ${{ steps.setup-fortran.outputs.cc }}
38-
39-
- uses: actions/checkout@v3
40-
41-
- name: Configure CMake
42-
run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DCMAKE_C_COMPILER=${{ env.CC }} -DCMAKE_Fortran_COMPILER=${{ env.FC }} -DBUILD_TESTING=TRUE -DBUILD_C_API=TRUE
27+
- uses: awvwgk/setup-fortran@v1
28+
id: setup-fortran
29+
with:
30+
compiler: ${{ matrix.toolchain.compiler }}
31+
version: ${{ matrix.toolchain.version }}
32+
33+
- run: ${{ env.FC }} --version
34+
env:
35+
FC: ${{ steps.setup-fortran.outputs.fc }}
36+
CC: ${{ steps.setup-fortran.outputs.cc }}
37+
38+
- uses: actions/checkout@v3
4339

44-
- name: Build with CMake
45-
run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}}
40+
- name: Configure CMake
41+
run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DCMAKE_Fortran_COMPILER=${{ env.FC }} -DBUILD_TESTING=TRUE
42+
43+
- name: Build with CMake
44+
run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}}
4645

47-
- name: Test with CMake
48-
working-directory: ${{github.workspace}}/build
49-
run: ctest -C ${{env.BUILD_TYPE}}
46+
- name: Test with CMake
47+
working-directory: ${{github.workspace}}/build
48+
run: ctest -C ${{env.BUILD_TYPE}}

CMakeLists.txt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ cmake_minimum_required(VERSION 3.24)
33
project(
44
linalg
55
LANGUAGES Fortran C
6-
VERSION 1.7.4
6+
VERSION 1.8.0
77
)
88

99
# Get helper macros and functions
@@ -37,9 +37,6 @@ target_link_libraries(
3737
)
3838
link_library(${PROJECT_NAME} ${ferror_LIBRARY} ${ferror_INCLUDE_DIR})
3939

40-
# Installation
41-
add_subdirectory(install)
42-
4340
# Testing
4441
option(BUILD_TESTING "Build tests")
4542
include(CTest)

README.md

Lines changed: 76 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,23 @@ A linear algebra library that provides a user-friendly interface to several BLAS
55
![Build Status](https://github.com/jchristopherson/linalg/actions/workflows/cmake.yml/badge.svg)
66
[![Actions Status](https://github.com/jchristopherson/linalg/workflows/fpm/badge.svg)](https://github.com/jchristopherson/linalg/actions)
77

8-
## Example 1
8+
## Documentation
9+
The documentation can be found [here](https://jchristopherson.github.io/linalg/).
10+
11+
## Building LINALG
12+
[CMake](https://cmake.org/)This library can be built using CMake. For instructions see [Running CMake](https://cmake.org/runningcmake/).
13+
14+
[FPM](https://github.com/fortran-lang/fpm) can also be used to build this library using the provided fpm.toml.
15+
```txt
16+
fpm build
17+
```
18+
The LINALG library can be used within your FPM project by adding the following to your fpm.toml file.
19+
```toml
20+
[dependencies]
21+
linalg = { git = "https://github.com/jchristopherson/linalg" }
22+
```
23+
24+
## Standard Solution Example
925
This example solves a normally defined system of 3 equations of 3 unknowns.
1026

1127
```fortran
@@ -56,8 +72,8 @@ LU Solution: X =
5672
0.0000
5773
```
5874

59-
## Example 2
60-
This example solves an overdefined system of 3 equations of 2 uknowns.
75+
## Overdetermined System Example
76+
This example solves an overdetermined system of 3 equations of 2 uknowns.
6177

6278
```fortran
6379
program example
@@ -100,7 +116,7 @@ Least Squares Solution: X =
100116
-0.57895
101117
```
102118

103-
## Example 3
119+
## Eigen Analysis Example
104120
This example computes the eigenvalues and eigenvectors of a mechanical system consisting of several masses connected by springs.
105121

106122
```fortran
@@ -180,20 +196,64 @@ Mode 3: (923.5669 Hz)
180196
-0.184
181197
0.179
182198
```
183-
## Documentation
184-
The documentation can be found [here](https://jchristopherson.github.io/linalg/).
185199

186-
## Building LINALG
187-
[CMake](https://cmake.org/)This library can be built using CMake. For instructions see [Running CMake](https://cmake.org/runningcmake/).
200+
## Sparse Matrix Example
201+
The following example solves a sparse system of equations using a direct solver. The solution is compared to the solution of the same system of equations but in dense format for comparison.
202+
```fortran
203+
program example
204+
use iso_fortran_env
205+
use linalg
206+
implicit none
188207
189-
[FPM](https://github.com/fortran-lang/fpm) can also be used to build this library using the provided fpm.toml.
190-
```txt
191-
fpm build
208+
! Local Variables
209+
integer(int32) :: ipiv(4)
210+
real(real64) :: dense(4, 4), b(4), x(4), bc(4)
211+
type(csr_matrix) :: sparse
212+
213+
! Build the matrices as dense matrices
214+
dense = reshape([ &
215+
5.0d0, 0.0d0, 0.0d0, 0.0d0, &
216+
0.0d0, 8.0d0, 0.0d0, 6.0d0, &
217+
0.0d0, 0.0d0, 3.0d0, 0.0d0, &
218+
0.0d0, 0.0d0, 0.0d0, 5.0d0], [4, 4])
219+
b = [2.0d0, -1.5d0, 8.0d0, 1.0d0]
220+
221+
! Convert to sparse (CSR format)
222+
! Note, the assignment operator is overloaded to allow conversion.
223+
sparse = dense
224+
225+
! Compute the solution to the sparse equations
226+
call sparse_direct_solve(sparse, b, x) ! Results stored in x
227+
228+
! Print the solution
229+
print "(A)", "Sparse Solution:"
230+
print *, x
231+
232+
! Perform a sanity check on the solution
233+
! Note, matmul is overloaded to allow multiplication with sparse matrices
234+
bc = matmul(sparse, x)
235+
print "(A)", "Computed RHS:"
236+
print *, bc
237+
print "(A)", "Original RHS:"
238+
print *, b
239+
240+
! For comparison, solve the dense system via LU decomposition
241+
call lu_factor(dense, ipiv)
242+
call solve_lu(dense, ipiv, b) ! Results stored in b
243+
print "(A)", "Dense Solution:"
244+
print *, b
245+
end program
192246
```
193-
The LINALG library can be used within your FPM project by adding the following to your fpm.toml file.
194-
```toml
195-
[dependencies]
196-
linalg = { git = "https://github.com/jchristopherson/linalg" }
247+
The above program produces the following output.
248+
```text
249+
Sparse Solution:
250+
0.40000000000000002 -0.18750000000000000 2.6666666666666665 0.42500000000000004
251+
Computed RHS:
252+
2.0000000000000000 -1.5000000000000000 8.0000000000000000 1.0000000000000000
253+
Original RHS:
254+
2.0000000000000000 -1.5000000000000000 8.0000000000000000 1.0000000000000000
255+
Dense Solution:
256+
0.40000000000000002 -0.18750000000000000 2.6666666666666665 0.42499999999999999
197257
```
198258

199259
## External Libraries
@@ -202,5 +262,6 @@ Here is a list of external code libraries utilized by this library.
202262
- [LAPACK](http://www.netlib.org/lapack/)
203263
- [QRUpdate](https://sourceforge.net/projects/qrupdate/)
204264
- [FERROR](https://github.com/jchristopherson/ferror)
265+
- [SPARSKIT](https://www-users.cse.umn.edu/~saad/software/SPARSKIT/)
205266

206267
The dependencies do not necessarily have to be installed to be used. The build will initially look for installed items, but if not found, will then download and build the latest version as part of the build process.

dependencies/ferror/CMakeLists.txt

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,18 @@
1-
# Get the macros and functions we'll need
2-
include("${PROJECT_SOURCE_DIR}/cmake/helper.cmake")
31
include(FetchContent)
42

53
# Fetch the proper content
64
FetchContent_Declare(
75
ferror
86
GIT_REPOSITORY "https://github.com/jchristopherson/ferror"
97
)
10-
118
FetchContent_MakeAvailable(ferror)
129

13-
if (WIN32)
14-
if (BUILD_SHARED_LIBS)
15-
add_custom_command(
16-
TARGET ${PROJECT_NAME} POST_BUILD
17-
COMMAND ${CMAKE_COMMAND} -E copy_if_different
18-
$<TARGET_FILE:ferror>
19-
$<TARGET_FILE_DIR:${PROJECT_NAME}
20-
)
21-
endif()
22-
endif()
23-
2410
set(ferror_INCLUDE_DIR ${ferror_BINARY_DIR}/include)
25-
set(ferror_INCLUDE_DIR ${ferror_INCLUDE_DIR} PARENT_SCOPE)
2611
configure_file(
2712
"${ferror_SOURCE_DIR}/include/ferror.h"
2813
"${ferror_INCLUDE_DIR}/ferror.h"
2914
COPYONLY
3015
)
31-
32-
# Make a parent-scope variable for the library
3316
set(ferror_LIBRARY ferror)
17+
set(ferror_INCLUDE_DIR ${ferror_INCLUDE_DIR} PARENT_SCOPE)
3418
set(ferror_LIBRARY ${ferror_LIBRARY} PARENT_SCOPE)

docs/Doxyfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ PROJECT_NAME = linalg
4848
# could be handy for archiving the generated documentation or if some version
4949
# control system is used.
5050

51-
PROJECT_NUMBER = 1.7.4
51+
PROJECT_NUMBER = 1.8.0
5252

5353
# Using the PROJECT_BRIEF tag one can provide an optional one line description
5454
# for a project that appears at the top of each page and should give viewer a

0 commit comments

Comments
 (0)