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

Feature request: matrix slicing #4257

Closed
xuhao1 opened this issue Feb 10, 2022 · 4 comments
Closed

Feature request: matrix slicing #4257

xuhao1 opened this issue Feb 10, 2022 · 4 comments
Assignees
Labels
feature request Suggest an idea on this project question Question on using Taichi

Comments

@xuhao1
Copy link

xuhao1 commented Feb 10, 2022

Hi there, my program needs to create an array to store some data, it looks like

#Marching...
...
    vertlist = [[0, 0, 0] for i in range(12)]
    for _i in ti.static(range(12)):
        if edges & 2**_i:
            _p0 = ti.static(edges_grid_xyz)[_i][0]
            _p1 = ti.static(edges_grid_xyz)[_i][1]
            p0 = [i + _p0[0]*step, j + _p0[1]*step, k + _p0[2]*step]
            p1 = [i + _p1[0]*step, j + _p1[1]*step, k + _p1[2]*step]
            p = self.vertexInterp(p0, p1, tsdf[p0], tsdf[p1], isolevel)
            vertlist[_i][0] = p[0]
            vertlist[_i][1] = p[1]
            vertlist[_i][2] = p[2]
    
    _i = 0
    while triTable[cubeindex, _i] != -1:
        self.add_triangle(vertlist[triTable[cubeindex, _i]], 
            vertlist[triTable[cubeindex, _i + 1]], vertlist[triTable[cubeindex, _i +2]], triangle_idx)
        triangle_idx += 1
        _i += 3

If a use a normal list here for vertlist, it will raise:

TypeError: list indices must be integers or slices, not Expr

If I change vertlist to

                    vertlist = ti.Matrix([[0, 0, 0] for i in range(12)])

It will raise

                        self.add_triangle(vertlist[triTable[cubeindex, _i], 0],
                                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The 0-th index of a Matrix/Vector must be a compile-time constant integer, got <class 'taichi.lang.expr.Expr'>.
This is because matrix operations will be **unrolled** at compile-time for performance reason.
If you want to *iterate through matrix elements*, use a static range:
  for i in ti.static(range(3)):
    print(i, "-th component is", vec[i])
See https://docs.taichi.graphics/lang/articles/advanced/meta#when-to-use-for-loops-with-tistatic for more details.

I hope to parallelize this piece of code so the vertlist should not be a global accessible field, so how should I make this code works?

@xuhao1 xuhao1 added the question Question on using Taichi label Feb 10, 2022
@strongoier
Copy link
Contributor

Could you try ti.init(dynamic_index=True) and then use vertlist = ti.Matrix([[0, 0, 0] for i in range(12)])? Allowing variable indices of vectors/matrices is still an experimental feature and not officially released yet. You may observe performance regression for now, but it should work properly. We are actively working on it, so feedback is highly welcome!

@xuhao1
Copy link
Author

xuhao1 commented Feb 10, 2022

@strongoier Thanks for your help! By enabling the dynamics index, the error disappears. But another problem arises.
vertlist[i] does give an element of the matrix, but not a column of the matrix, how can I pass a row of the matrix to some function like

vertlist[i,:]

@strongoier
Copy link
Contributor

strongoier commented Feb 10, 2022

Oops, matrix slicing is not supported yet. Could you use a helper function or expression like ti.Vector([vertlist[i, 0], vertlist[i, 1], vertlist[i, 2]]) for now? We will add this feature soon.

@xuhao1
Copy link
Author

xuhao1 commented Feb 10, 2022

@strongoier Thanks! I am writing in ti.Vector([vertlist[i, 0], vertlist[i, 1], vertlist[i, 2]]) style now. Looking forward for this feature.

@bobcao3 bobcao3 changed the title A proper way to create an array in kernel. Feature request: matrix slicing Feb 11, 2022
@bobcao3 bobcao3 added the feature request Suggest an idea on this project label Feb 11, 2022
@k-ye k-ye added this to Taichi Lang Jun 23, 2022
@k-ye k-ye moved this to Untriaged in Taichi Lang Jun 23, 2022
@k-ye k-ye moved this from Untriaged to Backlog in Taichi Lang Jun 23, 2022
Repository owner moved this from Backlog to Done in Taichi Lang Oct 10, 2022
strongoier added a commit that referenced this issue Oct 19, 2022
…6373)

Issue: #4257, #5819

### Brief Summary

Let's refine the matrix slice feature a bit before supporting it with
the new MatrixType.

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 question Question on using Taichi
Projects
Status: Done
Development

No branches or pull requests

5 participants