-
Notifications
You must be signed in to change notification settings - Fork 200
feat: [linalg] add iterative solvers #994
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 93 commits
Commits
Show all changes
94 commits
Select commit
Hold shift + click to select a range
716b3c5
start iterative solvers
jalvesz 9ed419f
simplify workspace
jalvesz 9106676
add pccg solver and example
jalvesz 297a18d
Merge branch 'fortran-lang:master' into iterative
jalvesz 9eccdd8
Merge branch 'fortran-lang:master' into iterative
jalvesz a93f6b7
Merge branch 'fortran-lang:master' into iterative
jalvesz 16e5cd7
Merge branch 'fortran-lang:master' into iterative
jalvesz e551a5d
complete cg with dirichlet flag, add example, fix di filter
jalvesz 9309c5c
Merge branch 'fortran-lang:master' into iterative
jalvesz 19167d5
add other sparse matrices
jalvesz 9324971
add example for custom solver extending the generic interface
jalvesz 71c2630
Merge branch 'iterative' of https://github.com/jalvesz/stdlib into it…
jalvesz 85a70ba
small simplifications for working data
jalvesz 84f4bc9
make default inner_product point to a default dot_product add enum fo…
jalvesz 3ec23a4
make preconditionner a linop
jalvesz bfafaa5
use facility size
jalvesz 0b01dbd
Add a customizable logger facility, change linop matvec to apply
jalvesz 07b97ce
change internal procedure names for custom example
jalvesz 379cd81
refactor to remove hard dependency on dirichlet BCs
jalvesz 5e15a33
add forward/backward solvers for preconditionning
jalvesz 8c2aa90
fix solve forward/backward
jalvesz e7bb7ce
Merge branch 'fortran-lang:master' into iterative
jalvesz 517291a
fix colum index
jalvesz 2c2196a
Merge branch 'iterative' of https://github.com/jalvesz/stdlib into it…
jalvesz 367987a
add preconditionners
jalvesz 05c076b
fix cmake
jalvesz f027017
add factorizations
jalvesz 7fd2586
backward solve to use Lt marix
jalvesz c596ac0
start unit testing
jalvesz acefaaf
review csr factorization
jalvesz f413cbf
change name generic for kernel
jalvesz 5cb2ad7
Merge branch 'fortran-lang:master' into iterative
jalvesz ea7d35e
shorten factorization procedures names
jalvesz 8068f2d
Merge branch 'fortran-lang:master' into iterative
jalvesz eeddf7c
change precond ldl name
jalvesz b239028
rename to pcg
jalvesz 724289f
Merge branch 'iterative' of https://github.com/jalvesz/stdlib into it…
jalvesz 79dbbbe
Merge branch 'fortran-lang:master' into iterative
jalvesz 463259b
start working on the documentation
jalvesz 6627f4c
clean docstrings
jalvesz fe331f9
add cg and pcg tests
jalvesz 9e5d000
Update example/linalg/example_solve_cg.f90
jalvesz 0f9732e
add _type suffix
jalvesz 177e545
reduce PR scope
jalvesz e68ce8e
rename wksp size constants
jalvesz 1bc601c
rename constant
jalvesz 64f83f8
Merge branch 'fortran-lang:master' into iterative
jalvesz a4d53e1
Merge branch 'fortran-lang:master' into iterative
jalvesz 076a05d
Merge branch 'fortran-lang:master' into iterative
jalvesz 507df75
Merge branch 'fortran-lang:master' into iterative
jalvesz 64cc86e
Merge branch 'fortran-lang:master' into iterative
jalvesz 2c519c2
Merge branch 'fortran-lang:master' into iterative
jalvesz 34b83a0
Merge branch 'fortran-lang:master' into iterative
jalvesz e5fa984
change signature of linop apply to matvec and add operator input argu…
jalvesz b8055f3
Update doc/specs/stdlib_linalg_iterative_solvers.md
jalvesz daabef6
Update test/linalg/test_linalg_solve_iterative.fypp
jalvesz ef196b3
Update test/linalg/test_linalg_solve_iterative.fypp
jalvesz 494a129
Update src/stdlib_linalg_iterative_solvers_cg.fypp
jalvesz a660e67
Update src/stdlib_linalg_iterative_solvers_cg.fypp
jalvesz aaaf94e
Update doc/specs/stdlib_linalg_iterative_solvers.md
jalvesz afe20df
Update example/linalg/example_solve_cg.f90
jalvesz a4b10a4
Update doc/specs/stdlib_linalg_iterative_solvers.md
jalvesz 55bba1b
Update doc/specs/stdlib_linalg_iterative_solvers.md
jalvesz dbea562
Update doc/specs/stdlib_linalg_iterative_solvers.md
jalvesz 555b512
Update doc/specs/stdlib_linalg_iterative_solvers.md
jalvesz 9d64fe6
Update example/linalg/example_solve_cg.f90
jalvesz b9d74da
Update example/linalg/example_solve_cg.f90
jalvesz 5c86e65
Update example/linalg/example_solve_custom.f90
jalvesz 97e9a45
Update example/linalg/example_solve_custom.f90
jalvesz f1a73e1
Update example/linalg/example_solve_custom.f90
jalvesz 59cc51a
Update example/linalg/example_solve_custom.f90
jalvesz b0f70af
Update example/linalg/example_solve_custom.f90
jalvesz 24821e0
Update example/linalg/example_solve_custom.f90
jalvesz 219637a
Update example/linalg/example_solve_custom.f90
jalvesz 1fe49a3
Update example/linalg/example_solve_pcg.f90
jalvesz 403b687
Update example/linalg/example_solve_pcg.f90
jalvesz 5775d10
Update example/linalg/example_solve_pcg.f90
jalvesz 7706e1b
Update example/linalg/example_solve_pcg.f90
jalvesz 05e536d
Update example/linalg/example_solve_pcg.f90
jalvesz f61224b
Update example/linalg/example_solve_pcg.f90
jalvesz 59aa11c
Update src/stdlib_linalg_iterative_solvers_cg.fypp
jalvesz bc53766
small fixes
jalvesz f03506e
add documentation information
jalvesz 6c4fb1d
forgotten attribute in doc
jalvesz 8519e1b
use optval
jalvesz b3b5f43
explicit use of private/public
jalvesz 1a814fb
Update doc/specs/stdlib_linalg_iterative_solvers.md
jalvesz 8afdf4b
rename with stdlib_ prefix
jalvesz a246074
Merge branch 'iterative' of https://github.com/jalvesz/stdlib into it…
jalvesz 731a0d6
Update example/linalg/example_solve_custom.f90
jalvesz 945319b
Update src/stdlib_linalg_iterative_solvers_pcg.fypp
jalvesz f65ad71
replace tol by rtol and atol for convergence test
jalvesz 8ff4866
Merge branch 'iterative' of https://github.com/jalvesz/stdlib into it…
jalvesz 37be20b
change rtol and atol defaults
jalvesz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,251 @@ | ||
| --- | ||
| title: linalg_iterative_solvers | ||
| --- | ||
|
|
||
| # The `stdlib_linalg_iterative_solvers` module | ||
|
|
||
| [TOC] | ||
|
|
||
| ## Introduction | ||
|
|
||
| The `stdlib_linalg_iterative_solvers` module provides base implementations for known iterative solver methods. Each method is exposed with two procedure flavors: | ||
|
|
||
| * A `solve_<method>_kernel` which holds the method's base implementation. The linear system argument is defined through a `linop` derived type which enables extending the method for implicit or unknown (by `stdlib`) matrices or to complex scenarios involving distributed parallelism for which the user shall extend the `inner_product` and/or matrix-vector product to account for parallel syncrhonization. | ||
|
|
||
| * A `solve_<method>` which proposes an off-the-shelf ready to use interface for `dense` and `CSR_<kind>_type` matrices for all `real` kinds. | ||
|
|
||
| <!-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --> | ||
| ### The `linop` derived type | ||
|
|
||
| The `stdlib_linop_<kind>_type` derive type is an auxiliary class enabling to abstract the definition of the linear system and the actual implementation of the solvers. | ||
|
|
||
| #### Type-bound procedures | ||
|
|
||
| The following type-bound procedure pointers enable customization of the solver: | ||
|
|
||
| ##### `matvec` | ||
|
|
||
| Proxy procedure for the matrix-vector product $y = alpha * op(M) * x + beta * y$. | ||
|
|
||
| #### Syntax | ||
|
|
||
| `call ` [[stdlib_iterative_solvers(module):matvec(interface)]] ` (x,y,alpha,beta,op)` | ||
|
|
||
| ###### Class | ||
|
|
||
| Subroutine | ||
|
|
||
| ###### Argument(s) | ||
|
|
||
| `x`: 1-D array of `real(<kind>)`. This argument is `intent(in)`. | ||
|
|
||
| `y`: 1-D array of `real(<kind>)`. This argument is `intent(inout)`. | ||
|
|
||
| `alpha`: scalar of `real(<kind>)`. This argument is `intent(in)`. | ||
|
|
||
| `beta`: scalar of `real(<kind>)`. This argument is `intent(in)`. | ||
|
|
||
| `op`: `character(1)` scalar which can be have any of the following values: `N` (no transpose), `T` (transpose) or `H` (conjugate transpose). This argument is `intent(in)`. | ||
|
|
||
| ##### `inner_product` | ||
|
|
||
| Proxy procedure for the `dot_product`. | ||
|
|
||
| #### Syntax | ||
|
|
||
| `res = ` [[stdlib_iterative_solvers(module):inner_product(interface)]] ` (x,y)` | ||
|
|
||
| ###### Class | ||
|
|
||
| Pure function | ||
|
|
||
| ###### Argument(s) | ||
|
|
||
| `x`: 1-D array of `real(<kind>)`. This argument is `intent(in)`. | ||
|
|
||
| `y`: 1-D array of `real(<kind>)`. This argument is `intent(in)`. | ||
|
|
||
| ###### Output value or Result value | ||
|
|
||
| The output is a scalar of `type` and `kind` same as to that of `x` and `y`. | ||
|
|
||
| <!-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --> | ||
| ### The `solver_workspace` derived type | ||
|
|
||
| The `stdlib_solver_workspace_<kind>_type` derive type is an auxiliary class enabling to hold the data associated to the working arrays needed by the solvers to operate. | ||
|
|
||
| #### Type-bound procedures | ||
|
|
||
| - `callback`: null pointer procedure enabling to pass a callback at each iteration to check on the solvers status. | ||
|
|
||
| ##### Class | ||
|
|
||
| Subroutine | ||
|
|
||
| ##### Argument(s) | ||
|
|
||
| `x`: 1-D array of `real(<kind>)` type with the current state of the solution vector. This argument is `intent(in)` as it should not be modified by the callback. | ||
|
|
||
| `norm_sq`: scalar of `real(<kind>)` type representing the squared norm of the residual at the current iteration. This argument is `intent(in)`. | ||
|
|
||
| `iter`: scalar of `integer` type giving the current iteration counter. This argument is `intent(in)`. | ||
|
|
||
| <!-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --> | ||
| ### `stdlib_solve_cg_kernel` subroutine | ||
|
|
||
| #### Description | ||
|
|
||
| Implements the Conjugate Gradient (CG) method for solving the linear system \( Ax = b \), where \( A \) is a symmetric positive-definite linear operator defined via the `stdlib_linop` type. This is the core implementation, allowing flexibility for custom matrix types or parallel environments. | ||
|
|
||
| #### Syntax | ||
|
|
||
| `call ` [[stdlib_iterative_solvers(module):stdlib_solve_cg_kernel(interface)]] ` (A, b, x, tol, maxiter, workspace)` | ||
|
|
||
| #### Status | ||
|
|
||
| Experimental | ||
|
|
||
| #### Class | ||
|
|
||
| Subroutine | ||
|
|
||
| #### Argument(s) | ||
|
|
||
| `A`: `class(stdlib_linop_<kind>_type)` defining the linear operator. This argument is `intent(in)`. | ||
|
|
||
| `b`: 1-D array of `real(<kind>)` defining the loading conditions of the linear system. This argument is `intent(in)`. | ||
|
|
||
| `x`: 1-D array of `real(<kind>)` which serves as the input initial guess and the output solution. This argument is `intent(inout)`. | ||
|
|
||
| `rtol` and `atol`: scalars of type `real(<kind>)` specifying the convergence test. For convergence, the following criterion is used \( || b - Ax ||^2 <= max(rtol^2 * || b ||^2 , atol^2 ) \). These arguments are `intent(in)`. | ||
|
|
||
| `maxiter`: scalar of type `integer` defining the maximum allowed number of iterations. This argument is `intent(in)`. | ||
|
|
||
| `workspace`: scalar derived type of `type(stdlib_solver_workspace_<kind>_type)` holding the work array for the solver. This argument is `intent(inout)`. | ||
|
|
||
| <!-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --> | ||
| ### `solve_cg` subroutine | ||
|
|
||
| #### Description | ||
|
|
||
| Provides a user-friendly interface to the CG method for solving \( Ax = b \), supporting `dense` and `CSR_<kind>_type` matrices. It handles workspace allocation and optional parameters for customization. | ||
|
|
||
| #### Syntax | ||
|
|
||
| `call ` [[stdlib_iterative_solvers(module):solve_cg(interface)]] ` (A, b, x [, di, rtol, atol, maxiter, restart, workspace])` | ||
|
|
||
| #### Status | ||
|
|
||
| Experimental | ||
|
|
||
| #### Class | ||
|
|
||
| Subroutine | ||
|
|
||
| #### Argument(s) | ||
|
|
||
| `A`: `dense` or `CSR_<kind>_type` matrix defining the linear system. This argument is `intent(in)`. | ||
|
|
||
| `b`: 1-D array of `real(<kind>)` defining the right-hand-side (or loading) of the linear system. This argument is `intent(in)`. | ||
|
|
||
| `x`: 1-D array of `real(<kind>)` which serves as the input initial guess and as the output solution. This argument is `intent(inout)`. | ||
|
|
||
| `di` (optional): 1-D mask array of type `logical(int8)` defining the degrees of freedom subject to dirichlet boundary conditions. The actual boundary conditions values should be stored in the `b` load array. This argument is `intent(in)`. | ||
|
|
||
| `rtol` and `atol` (optional): scalars of type `real(<kind>)` specifying the convergence test. For convergence, the following criterion is used \( || b - Ax ||^2 <= max(rtol^2 * || b ||^2 , atol^2 ) \). Defaults values are `rtol=1.e-4` and `atol=0`. These arguments are `intent(in)`. | ||
|
|
||
| `maxiter` (optional): scalar of type `integer` defining the maximum allowed number of iterations. If no value is given, a default of `N` is set, where `N = size(b)`. This argument is `intent(in)`. | ||
|
|
||
| `workspace` (optional): scalar derived type of `type(stdlib_solver_workspace_<kind>_type)` holding the work array for the solver. If the user passes its own `workspace`, then a pointer is set internally to it. Otherwise, memory will be internally allocated and deallocated before exiting the procedure. This argument is `intent(inout)`. | ||
|
|
||
| #### Example | ||
|
|
||
| ```fortran | ||
| {!example/linalg/example_solve_cg.f90!} | ||
| ``` | ||
|
|
||
| <!-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --> | ||
| ### `stdlib_solve_pcg_kernel` subroutine | ||
|
|
||
| #### Description | ||
|
|
||
| Implements the Preconditioned Conjugate Gradient (PCG) method for solving the linear system \( Ax = b \), where \( A \) is a symmetric positive-definite linear operator defined via the `stdlib_linop` type. This is the core implementation, allowing flexibility for custom matrix types or parallel environments. | ||
|
|
||
| #### Syntax | ||
|
|
||
| `call ` [[stdlib_iterative_solvers(module):stdlib_solve_cg_kernel(interface)]] ` (A, M, b, x, tol, maxiter, workspace)` | ||
|
|
||
| #### Status | ||
|
|
||
| Experimental | ||
|
|
||
| #### Class | ||
|
|
||
| Subroutine | ||
|
|
||
| #### Argument(s) | ||
|
|
||
| `A`: `class(stdlib_linop_<kind>_type)` defining the linear operator. This argument is `intent(in)`. | ||
|
|
||
| `M`: `class(stdlib_linop_<kind>_type)` defining the preconditioner linear operator. This argument is `intent(in)`. | ||
|
|
||
| `b`: 1-D array of `real(<kind>)` defining the loading conditions of the linear system. This argument is `intent(in)`. | ||
|
|
||
| `x`: 1-D array of `real(<kind>)` which serves as the input initial guess and the output solution. This argument is `intent(inout)`. | ||
|
|
||
| `rtol` and `atol` (optional): scalars of type `real(<kind>)` specifying the convergence test. For convergence, the following criterion is used \( || b - Ax ||^2 <= max(rtol^2 * || b ||^2 , atol^2 ) \). These arguments are `intent(in)`. | ||
|
|
||
| `maxiter`: scalar of type `integer` defining the maximum allowed number of iterations. This argument is `intent(in)`. | ||
|
|
||
| `workspace`: scalar derived type of `type(stdlib_solver_workspace_<kind>_type)` holding the work array for the solver. This argument is `intent(inout)`. | ||
|
|
||
| #### Example | ||
|
|
||
| ```fortran | ||
| {!example/linalg/example_solve_custom.f90!} | ||
| ``` | ||
|
|
||
| <!-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --> | ||
| ### `stdlib_solve_pcg` subroutine | ||
|
|
||
| #### Description | ||
|
|
||
| Provides a user-friendly interface to the PCG method for solving \( Ax = b \), supporting `dense` and `CSR_<kind>_type` matrices. It supports optional preconditioners and handles workspace allocation. | ||
|
|
||
| #### Syntax | ||
|
|
||
| `call ` [[stdlib_iterative_solvers(module):stdlib_solve_pcg(interface)]] ` (A, b, x [, di, tol, maxiter, restart, precond, M, workspace])` | ||
|
|
||
| #### Status | ||
|
|
||
| Experimental | ||
|
|
||
| #### Class | ||
|
|
||
| Subroutine | ||
|
|
||
| #### Argument(s) | ||
|
|
||
| `A`: `dense` or `CSR_<kind>_type` matrix defining the linear system. This argument is `intent(in)`. | ||
|
|
||
| `b`: 1-D array of `real(<kind>)` defining the loading conditions of the linear system. This argument is `intent(in)`. | ||
|
|
||
| `x`: 1-D array of `real(<kind>)` which serves as the input initial guess and the output solution. This argument is `intent(inout)`. | ||
|
|
||
| `di` (optional): 1-D mask array of type `logical(int8)` defining the degrees of freedom subject to dirichlet boundary conditions. The actual boundary conditions values should be stored in the `b` load array. This argument is `intent(in)`. | ||
|
|
||
| `rtol` and `atol` (optional): scalars of type `real(<kind>)` specifying the convergence test. For convergence, the following criterion is used \( || b - Ax ||^2 <= max(rtol^2 * || b ||^2 , atol^2 ) \). Defaults values are `rtol=1.e-4` and `atol=0`. These arguments are `intent(in)`. | ||
|
|
||
| `maxiter` (optional): scalar of type `integer` defining the maximum allowed number of iterations. If no value is given, a default of `N` is set, where `N = size(b)`. This argument is `intent(in)`. | ||
|
|
||
| `precond` (optional): scalar of type `integer` enabling to switch among the default preconditioners available with the following enum (`pc_none`, `pc_jacobi`). If no value is given, no preconditionning will be applied. This argument is `intent(in)`. | ||
|
|
||
| `M` (optional): scalar derived type of `class(stdlib_linop_<kind>_type)` defining a custom preconditioner linear operator. If given, `precond` will have no effect, a pointer is set to this custom preconditioner. | ||
|
|
||
| `workspace` (optional): scalar derived type of `type(stdlib_solver_workspace_<kind>_type)` holding the work temporal array for the solver. If the user passes its own `workspace`, then internally a pointer is set to it, otherwise, memory will be internally allocated and deallocated before exiting the procedure. This argument is `intent(inout)`. | ||
|
|
||
| #### Example | ||
|
|
||
| ```fortran | ||
| {!example/linalg/example_solve_pcg.f90!} | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| program example_solve_cg | ||
| use stdlib_kinds, only: int8, dp | ||
| use stdlib_linalg_iterative_solvers, only: stdlib_solve_cg | ||
|
|
||
| real(dp) :: matrix(2,2) | ||
| real(dp) :: x(2), rhs(2) | ||
|
|
||
| matrix = reshape( [4, 1,& | ||
| 1, 3] , [2,2]) | ||
|
|
||
| x = dble( [2,1] ) !> initial guess | ||
| rhs = dble( [1,2] ) !> rhs vector | ||
|
|
||
| call stdlib_solve_cg(matrix, rhs, x, restart=.false.) | ||
| print *, x !> solution: [0.0909, 0.6364] | ||
|
|
||
| end program |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.