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

MatrixFreeCG is not working with 1.60 #8189

Open
fangde opened this issue Jun 15, 2023 · 1 comment
Open

MatrixFreeCG is not working with 1.60 #8189

fangde opened this issue Jun 15, 2023 · 1 comment
Assignees

Comments

@fangde
Copy link

fangde commented Jun 15, 2023

MatrixFree is not working
by following the blog (https://mp.weixin.qq.com/s/BrgY5U4WBjqPo1smf4DkbA) of MatrixFreeCG in taichi . we test the matrix free cg solver.
none of api(taichi_cg_solver or MatrixFreeCG) is working.

To Reproduce
we use the sample code in the blog

# matrix freee cg solver

import taichi as ti
arch = ti.cuda# or ti.cuda
ti.init(arch=arch)
n = 4
x = ti.field(dtype=ti.f32, shape=n) # x 为未知数向量
Ax = ti.field(dtype=ti.f32, shape=n) # Ax 为矩阵向量点乘的结果
@ti.kernel
def compute_matrix_vector(v:ti.template(), mv:ti.template()):
    for i in v:
        mv[i] = 2 * v[i]
        
compute_matrix_vector(x, Ax)
A = ti.linalg.LinearOperator(compute_matrix_vector)
b = ti.field(ti.f32, shape=n)
@ti.kernel
def init_b():
    for i in b:
        b[i] = 1.0
init_b()
print(b)

ti.linalg.taichi_cg_solver(A, b, x)

the output is

[Taichi] Starting on arch=cuda
[1. 1. 1. 1.]
---------------------------------------------------------------------------
TaichiIndexError                          Traceback (most recent call last)
Cell In[4], line 22
     19 init_b()
     20 print(b)
---> 22 ti.linalg.taichi_cg_solver(A, b, x)

File [~/anaconda2/envs/taichi16/lib/python3.10/site-packages/taichi/linalg/taichi_cg.py:100](https://vscode-remote+ssh-002dremote-002bdlhome-002elocal.vscode-resource.vscode-cdn.net/home/fliu/taichisparse_test/~/anaconda2/envs/taichi16/lib/python3.10/site-packages/taichi/linalg/taichi_cg.py:100), in taichi_cg_solver(A, b, x, tol, maxiter, quiet)
     97         if not quiet:
     98             print(f">>> Iter = {i+1:4}, Residual = {sqrt(new_rTr):e}")
--> 100 solve()
    101 vector_fields_snode_tree.destroy()
    102 scalar_snode_tree.destroy()

File [~/anaconda2/envs/taichi16/lib/python3.10/site-packages/taichi/linalg/taichi_cg.py:75](https://vscode-remote+ssh-002dremote-002bdlhome-002elocal.vscode-resource.vscode-cdn.net/home/fliu/taichisparse_test/~/anaconda2/envs/taichi16/lib/python3.10/site-packages/taichi/linalg/taichi_cg.py:75), in taichi_cg_solver..solve()
     74 def solve():
---> 75     init()
     76     initial_rTr = reduce(r, r)
     77     if not quiet:

File [~/anaconda2/envs/taichi16/lib/python3.10/site-packages/taichi/lang/kernel_impl.py:976](https://vscode-remote+ssh-002dremote-002bdlhome-002elocal.vscode-resource.vscode-cdn.net/home/fliu/taichisparse_test/~/anaconda2/envs/taichi16/lib/python3.10/site-packages/taichi/lang/kernel_impl.py:976), in _kernel_impl..wrapped(*args, **kwargs)
    974     return primal(*args, **kwargs)
    975 except (TaichiCompilationError, TaichiRuntimeError) as e:
--> 976     raise type(e)("\n" + str(e)) from None

TaichiIndexError: 
File "/home/fliu/anaconda2/envs/taichi16/lib/python3.10/site-packages/taichi/linalg/taichi_cg.py", line 48, in init:
            r[I] = b[I]
            ^^^^
Field with dim 2 accessed with indices of dim 1

we also test the new name api as

ti.linalg.MatrixFreeCG(A,x)

MatrixFree is not named,output as follows

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[7], line 1
----> 1 ti.linalg.MatrixFreeCG(A,x)

AttributeError: module 'taichi.linalg' has no attribute 'MatrixFreeCG'

Additional comments
If possible, please also consider attaching the output of command ti diagnose. This produces the detailed environment information and hopefully helps us diagnose faster.

If you have local commits (e.g. compile fixes before you reproduce the bug), please make sure you first make a PR to fix the build errors and then report the bug.

@github-project-automation github-project-automation bot moved this to Untriaged in Taichi Lang Jun 15, 2023
@houkensjtu houkensjtu self-assigned this Jun 15, 2023
@houkensjtu houkensjtu moved this from Untriaged to Todo in Taichi Lang Jun 15, 2023
@houkensjtu houkensjtu added this to the v1.7.0 milestone Jun 15, 2023
@houkensjtu
Copy link
Contributor

Hi @fangde ! Thank you for trying out our MatrixFreeCG. The issue was two-fold:

  1. MatrixFreeCG does not exist in 1.6.0 and will be released in 1.7.0. That's why you saw the following error message
AttributeError: module 'taichi.linalg' has no attribute 'MatrixFreeCG'
  1. The RuntimeError is caused by a previous (known) bug in our CG solver, fortunately it's solved by PR#8070([bug] Fix MatrixFreeCG so it can handle multiple input sizes. #8070). Again, this bug fix will come with 1.7.0. For the moment, please try to use shape=(n,1) instead of shape=n for one-dimension vectors.

Let me know if you have further questions!

@houkensjtu houkensjtu removed this from the v1.7.0 milestone Jun 15, 2023
@houkensjtu houkensjtu moved this from Todo to Done in Taichi Lang Jun 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

No branches or pull requests

2 participants