Skip to content
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

Provide state/error handling for linear algebra #774

Merged
merged 28 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
7ccdb49
add `linalg` state handler
perazz Mar 16, 2024
6d6fb55
cleanup, `fypp`ize numeric types
perazz Mar 16, 2024
e7398a0
more cleanup
perazz Mar 16, 2024
31cb5eb
`complex` format: add brackets i.e. `(0.0,1.0)` instead of `0.0 1.0`
perazz Mar 16, 2024
4ef4fb0
add `linalg_state` tests
perazz Mar 16, 2024
c5c568b
remove multiple `public` statement
perazz Mar 16, 2024
88317d9
Update src/stdlib_linalg_constants.fypp
perazz Mar 25, 2024
9eb9775
Update src/stdlib_linalg_constants.fypp
perazz Mar 25, 2024
9f74fd7
address style changes
perazz Mar 26, 2024
3762517
documentation + example
perazz Mar 26, 2024
cb9a7fc
generalize interface (`rank`-agnostic)
perazz Mar 26, 2024
0782e43
add `xdp` formats
perazz Mar 26, 2024
b9e7e67
intel compiler issue: remove buffer write
perazz Mar 26, 2024
affd5d0
add `xdp` symbol
perazz Mar 26, 2024
322b37e
move kinds to `stdlib_kinds`
perazz Mar 26, 2024
526e816
Merge branch 'master' into linalg_state
perazz Apr 2, 2024
41bf3a3
remove duplicate `stdlib_linalg_constants.fypp`
perazz Apr 2, 2024
a90f25a
more explanation
perazz Apr 2, 2024
fb008b4
Update example_state1.f90
perazz Apr 2, 2024
00db324
add another state example
perazz Apr 6, 2024
9b52091
Update example/linalg/example_state1.f90
perazz Apr 8, 2024
4b3c20a
Update example/linalg/example_state2.f90
perazz Apr 8, 2024
422a3d0
Update example/linalg/example_state2.f90
perazz Apr 8, 2024
c6ab922
Update src/stdlib_linalg_state.fypp
perazz Apr 8, 2024
36b3f93
Update src/stdlib_linalg_state.fypp
perazz Apr 8, 2024
dbc843a
Update src/stdlib_linalg_state.fypp
perazz Apr 8, 2024
33c2de6
example_state1: import `operator(/=)`
perazz Apr 8, 2024
dfd03fc
import `linalg_error_handling`
perazz Apr 8, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/specs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ This is an index/directory of the specifications (specs) for each new module/fea
- [io](./stdlib_io.html) - Input/output helper & convenience
- [kinds](./stdlib_kinds.html) - Kind parameters
- [linalg](./stdlib_linalg.html) - Linear Algebra
- [linalg_state_type](./stdlib_linalg_state_type.html) - Linear Algebra state and error handling
- [logger](./stdlib_logger.html) - Runtime logging system
- [math](./stdlib_math.html) - General purpose mathematical functions
- [optval](./stdlib_optval.html) - Fallback value for optional arguments
Expand Down
64 changes: 64 additions & 0 deletions doc/specs/stdlib_linalg_state_type.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
---
title: linalg_state_type
---

# Linear Algebra -- State and Error Handling Module

[TOC]

## Introduction

The `stdlib_linalg_state` module provides a derived type holding information on the
state of linear algebra operations, and procedures for expert control of linear algebra workflows.
All linear algebra procedures are engineered to support returning an optional `linalg_state_type`
variable to holds such information, as a form of expert API. If the user does not require state
information, but fatal errors are encountered during the execution of linear algebra routines, the
program will undergo a hard stop.
Instead, if the state argument is present, the program will never stop, but will return detailed error
information into the state handler.

## Derived types provided

<!-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -->
### The `linalg_state_type` derived type

The `linalg_state_type` is defined as a derived type containing an integer error flag, and
fixed-size character strings to store an error message and the location of the error state change.
Fixed-size string storage was chosen to facilitate the compiler's memory allocation and ultimately
ensure maximum computational performance.

A similarly named generic interface, `linalg_state_type`, is provided to allow the developer to
create diagnostic messages and raise error flags easily. The call starts with an error flag or
the location of the event, and is followed by an arbitrary list of `integer`, `real`, `complex` or
`character` variables. Numeric variables may be provided as either scalars or rank-1 (array) inputs.

#### Type-bound procedures

The following convenience type-bound procedures are provided:
- `print()` returns an allocatable character string containing state location, message, and error flag;
- `print_message()` returns an allocatable character string containing the state message;
- `ok()` returns a `logical` flag that is `.true.` in case of successful state (`flag==LINALG_SUCCESS`);
- `error()` returns a `logical` flag that is `.true.` in case of error state (`flag/=LINALG_SUCCESS`).

#### Status

Experimental

#### Example

```fortran
{!example/linalg/example_state1.f90!}
```

## Error flags provided

The module provides the following state flags:
- `LINALG_SUCCESS`: Successful execution
- `LINALG_VALUE_ERROR`: Numerical errors (such as infinity, not-a-number, range bounds) are encountered.
- `LINALG_ERROR`: Linear Algebra errors are encountered, such as: non-converging iterations, impossible operations, etc.
- `LINALG_INTERNAL_ERROR`: Provided as a developer safeguard for internal errors that should never occur.

## Comparison operators provided

The module provides overloaded comparison operators for all comparisons of a `linalg_state_type` variable
with an integer error flag: `<`, `<=`, `==`, `>=`, `>`, `/=`.
18 changes: 18 additions & 0 deletions example/linalg/example_state1.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
program example_state1
use stdlib_linalg_state
implicit none
type(linalg_state_type) :: err

! Create a state flag
err = linalg_state_type(LINALG_VALUE_ERROR,'just an example with scalar ',&
'integer=',1,'real=',2.0,'complex=',(3.0,1.0),'and array ',[1,2,3],'inputs')

! Print flag
print *, err%print()

! Check success
print *, 'Check error: ',err%error()
print *, 'Check flag : ',err /= LINALG_SUCCESS


end program example_state1
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ set(fppFiles
stdlib_linalg_outer_product.fypp
stdlib_linalg_kronecker.fypp
stdlib_linalg_cross_product.fypp
stdlib_linalg_state.fypp
stdlib_optval.fypp
stdlib_selection.fypp
stdlib_sorting.fypp
Expand Down
1 change: 0 additions & 1 deletion src/stdlib_linalg_constants.fypp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ module stdlib_linalg_constants
public



! Integer size support for ILP64 builds should be done here
integer, parameter :: ilp = int32
private :: int32, int64
Expand Down
Loading
Loading