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

[RFC] Enhancement on support for linear equation solvers #7837

Open
houkensjtu opened this issue Apr 18, 2023 · 0 comments
Open

[RFC] Enhancement on support for linear equation solvers #7837

houkensjtu opened this issue Apr 18, 2023 · 0 comments
Assignees
Labels
feature request Suggest an idea on this project RFC

Comments

@houkensjtu
Copy link
Contributor

houkensjtu commented Apr 18, 2023

Purpose

The purpose of this RFC is to discuss the plan on refactoring the linear solvers available under ti.linalg. Generally speaking, all solvers target at solving

$$ Ax = b $$

where $A$ is a $n \times n$ matrix and $n$ is the number of unknowns.

Current status

Currently, users have four options if they need to solve a set of large-scale linear equations.

  1. Use the SparseMatrix API to build a sparse matrix, and then solve it using ti.linalg.SparseSolver. [example]
  2. Use the SparseMatrix API to build a sparse matrix, and then solve it using ti.linalg.CG. [example]
  3. Use the matrix-free CG solver ti.linalg.taichi_cg_solver to which you pass a LinearOperator to represent $A$. [example]
  4. Implement their own linear solvers in Taichi language.

Note 1: We limit the scope of this topic to "large-scale" problems. Inverse of relative small matrices can be solved using the inverse() method provided in the taichi.math module.

Note 2: The development of the SparseMatrix solvers is well-documented in Issue #2906

A brief summary of the supported solvers:

matrix type precision backend support
SparseMatrix f32 CPU
SparseMatrix f64 CPU
SparseMatrix f32 CUDA
SparseMatrix f64 CUDA
matrix-free f32 CPU
matrix-free f64 CPU

We notice that the naming of the provided solvers are not consistent and intuitive. For example,

  • ti.linalg.taichi_cg_solver and ti.linalg.CG exist simultaneously, and it's unclear which one should be used for a certain circumstance
  • ti.linalg.CG is intended (only) for SparseMatrix, but a user might expect it to be able to handle more data types

Proposed plan

I proposed the following plans to resolve the naming issue, as well as promote a better separation of concerns:

Plan 1. Keep the file organization untouched; use a more intuitive naming to suggest their usage. Namely,

ti.linalg.SparseSolver => no change 
ti.linalg.CG => ti.linalg.SparseCG
ti.linalg.taichi_cg_solver => ti.linalg.MatrixFreeCG

Plan 2. Construct an extensible module structure for each matrix type. Specifically, the solvers will be reorganized into subdirectories suggesting its target matrix type:

linalg/
│
├── sparse/
│   ├── __init__.py
│   ├── direct_solver.py
│   └── cg.py
│
└── matrix_free/
    ├── __init__.py
    └── cg.py

This way, user can use the solvers by the following imports:

from ti.linalg.sparse import DirectSolver, CG
from ti.linalg.matrix_free import CG as MatrixFreeCG

Plan 3. Unify the solver API, and let the solver to determine which version it should use. In this case, ti.linalg.CG and ti.linalg.taichi_cg_solver will be combined into one ti.linalg.CG. From a user's perspective, calling the CG solver will simply be

cg = ti.linalg.CG(A, b, x0, max_iter=50, atol=1e-6)
x = cg.solve()

no matter A is a LinearOperator or a SparseMatrix.

Additional comments

Which plan do you prefer? To be sure, each plan has its own pros and cons, and we welcome all users and developers' opinion on this issue!

@houkensjtu houkensjtu added feature request Suggest an idea on this project RFC labels Apr 18, 2023
@github-project-automation github-project-automation bot moved this to Untriaged in Taichi Lang Apr 18, 2023
@ailzhang ailzhang moved this from Untriaged to Todo in Taichi Lang Apr 21, 2023
neozhaoliang pushed a commit that referenced this issue May 8, 2023
Issue: #7837 

### Brief Summary

As described in Issue #7837 , I first implemented Plan # 1 to enforce a
more consistent naming for the current solvers. All solver code are
untouched, except for the naming:
```
ti.linalg.SparseSolver => no change 
ti.linalg.CG => ti.linalg.SparseCG
ti.linalg.taichi_cg_solver => ti.linalg.MatrixFreeCG
```
The Python test scripts are also updated accordingly. This doesn't mean
Plan # 3 is abandoned though: we can unify `SparseCG` and `MatrixFreeCG`
in the future if that's the route we want.

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
neozhaoliang added a commit that referenced this issue May 8, 2023
Issue: #7837 

### Brief Summary

As a follow-up PR related to issue #7837 and PR #7911, split the
description for linear solver from `sparse_matrix.md`. More information
will be filled into the additional `linear_solver.md` page once PR #7911
is merged, to reflect the latest usage information of the linear
solvers.

---------

Co-authored-by: Zhao Liang <mathzhaoliang@gmail.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
neozhaoliang pushed a commit that referenced this issue May 10, 2023
WIP. Don't merge.

Issue: #7837 
Issue: #7921 

### Brief Summary
Update contents in `linear_solver.md` to reflect the latest changes and
development progress on the linear solver.

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
quadpixels pushed a commit to quadpixels/taichi that referenced this issue May 13, 2023
Issue: taichi-dev#7837 

### Brief Summary

As described in Issue taichi-dev#7837 , I first implemented Plan # 1 to enforce a
more consistent naming for the current solvers. All solver code are
untouched, except for the naming:
```
ti.linalg.SparseSolver => no change 
ti.linalg.CG => ti.linalg.SparseCG
ti.linalg.taichi_cg_solver => ti.linalg.MatrixFreeCG
```
The Python test scripts are also updated accordingly. This doesn't mean
Plan # 3 is abandoned though: we can unify `SparseCG` and `MatrixFreeCG`
in the future if that's the route we want.

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
quadpixels pushed a commit to quadpixels/taichi that referenced this issue May 13, 2023
…7921)

Issue: taichi-dev#7837 

### Brief Summary

As a follow-up PR related to issue taichi-dev#7837 and PR taichi-dev#7911, split the
description for linear solver from `sparse_matrix.md`. More information
will be filled into the additional `linear_solver.md` page once PR taichi-dev#7911
is merged, to reflect the latest usage information of the linear
solvers.

---------

Co-authored-by: Zhao Liang <mathzhaoliang@gmail.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
quadpixels pushed a commit to quadpixels/taichi that referenced this issue May 13, 2023
WIP. Don't merge.

Issue: taichi-dev#7837 
Issue: taichi-dev#7921 

### Brief Summary
Update contents in `linear_solver.md` to reflect the latest changes and
development progress on the linear solver.

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Suggest an idea on this project RFC
Projects
Status: Todo
Development

No branches or pull requests

1 participant