Skip to content

Commit 281b068

Browse files
committed
Document BLAS/LAPACK backends
1 parent fa3f147 commit 281b068

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

doc/specs/stdlib_linalg.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,62 @@ title: linalg
66

77
[TOC]
88

9+
The `stdlib` linear algebra library provides high-level APIs for dealing with common linear algebra operations.
10+
11+
## BLAS and LAPACK
12+
13+
### Status
14+
15+
Experimental
16+
17+
### Description
18+
19+
`BLAS` and `LAPACK` backends provide efficient low level implementations of many linear algebra algorithms, and are employed for non-trivial operators.
20+
A Modern Fortran version of the [Reference-LAPACK 3.10.1](http://github.com/reference-LAPACK) implementation is provided as a backend.
21+
Modern Fortran modules with full explicit typing features are provided after an automated conversion of the legacy codes:
22+
- [stdlib_linalg_blas(module)], [stdlib_linalg_lapack(module)] provide kind-agnostic interfaces to all functions.
23+
- Both libraries are available for 32- (`sp`), 64- (`sp`) and 128-bit (`qp`) `real` and `complex` numbers (the latter if available in the current build)
24+
- Free format, lower-case style
25+
- `implicit none(type, external)` applied to all procedures and modules
26+
- `intent` added and all `pure` procedures where possible
27+
- All procedure names are prefixed with `stdlib_`, while their generic interface is the same as the BLAS/LAPACK default, with the header character dropped. For example, `stdlib_dgemv`, `stdlib_sgemv`, etc. provide implementations for matrix-vector multiply, while the generic interface is named `gemv`
28+
- F77-style `parameter`s removed, and all numeric constants have been generalized with KIND-dependent Fortran intrinsics.
29+
- preprocessor-based OpenMP directives retained.
30+
The single-source module structure hopefully allows for cross-procedural inlining which is otherwise impossible without link-time optimization.
31+
32+
When available, highly optimized libraries that take advantage of specialized processor instructions should preferred.
33+
Examples of such libraries are: OpenBLAS, MKL (TM), Accelerate, and ATLAS. In order to enable their usage, simply ensure that the following pre-processor macros are defined:
34+
35+
- `STDLIB_EXTERNAL_BLAS` wraps all BLAS procedures (except for the 128-bit ones) to an external library
36+
- `STDLIB_EXTERNAL_LAPACK` wraps all LAPACK procedures (except for the 128-bit ones) to an external library
37+
38+
These can be enabled during the build process. For example, with CMake, one can enable these preprocessor directives using `add_compile_definitions(STDLIB_EXTERNAL_BLAS STDLIB_EXTERNAL_LAPACK)`.
39+
The same is possible from the `fpm` branch, where the `cpp` preprocessor is enabled by default. For example, the macros can be added to the project's manifest:
40+
41+
```toml
42+
43+
[dependencies]
44+
stdlib="*"
45+
46+
[preprocess]
47+
[preprocess.cpp]
48+
macros = ["STDLIB_EXTERNAL_BLAS", "STDLIB_EXTERNAL_LAPACK"]
49+
```
50+
51+
or directly via compiler flags:
52+
53+
`fpm build --flag "-DSTDLIB_EXTERNAL_BLAS -DSTDLIB_EXTERNAL_LAPACK -framework Accelerate"`.
54+
55+
### Example
56+
57+
```fortran
58+
{!example/linalg/example_blas_gemv.f90!}
59+
```
60+
61+
```fortran
62+
{!example/linalg/example_lapack_getrf.f90!}
63+
```
64+
965
## `diag` - Create a diagonal array or extract the diagonal elements of an array
1066

1167
### Status

example/linalg/example_blas_gemv.f90

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
program example_gemv
2+
use stdlib_linalg, only: eye
3+
use stdlib_linalg_blas, only: sp,gemv
4+
implicit none(type,external)
5+
real(sp) :: A(2, 2), B(2)
6+
B = [1.0,2.0]
7+
A = eye(2)
8+
9+
! Use legacy BLAS interface
10+
call gemv('No transpose',m=size(A,1),n=size(A,2),alpha=1.0,a=A,lda=size(A,1),x=B,incx=1,beta=0.0,y=B,incy=1)
11+
12+
print *, x ! returns 1.0 2.0
13+
14+
end program example_gemv
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
program example_getrf
2+
use stdlib_linalg, only: eye
3+
use stdlib_linalg_lapack, only: dp,ilp,getrf
4+
implicit none(type,external)
5+
real(dp) :: A(3, 3)
6+
integer(ilp) :: ipiv(3),info
7+
8+
A = eye(3)
9+
10+
! LAPACK matrix factorization interface (overwrite result)
11+
call getrf(size(A,1),size(A,2),A,size(A,1),ipiv,info)
12+
print *, info ! info==0: Success!
13+
14+
end program example_getrf

0 commit comments

Comments
 (0)