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

[Lang] Add ti.Vector.ndarray and ti.Matrix.ndarray #2808

Merged
merged 13 commits into from
Aug 30, 2021

Conversation

strongoier
Copy link
Contributor

@strongoier strongoier commented Aug 26, 2021

Related issue = #2771

This PR adds ti.Vector.ndarray and ti.Matrix.ndarray, which support Python-scope and Taichi-scope access. When passing them into kernels, we need to use ti.any_arr() annotation with element_shape and optional layout (default value: ti.Layout.AOS) to enforce type consistency.

@ti.kernel
def func(a: ti.any_arr(element_shape=(2, 2), layout=ti.Layout.SOA)):
    for i in range(5):
        for j, k in ti.ndrange(2, 2):
            a[i][j, k] = j * j + k * k

m = ti.Matrix.ndarray(2, 2, ti.i32, 5, layout=ti.Layout.SOA)
func(m)

@strongoier
Copy link
Contributor Author

/format

@strongoier strongoier requested review from k-ye and ailzhang August 26, 2021 07:59
@strongoier
Copy link
Contributor Author

/format

"""Class for accessing VectorNdarray/MatrixNdarray in Python scope.
Args:
arr (Union[VectorNdarray, MatrixNdarray]): See above.
indices_first (Tuple[Int]): Indices of first-level access.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are first level and second level?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First level refers to coordinates in the field, while second level refers to indices inside the vector/matrix. Maybe I should make it clearer?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using first/second is to tell users this is what they write in the first/second pair of brackets.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Explanations are added.

@@ -57,8 +58,25 @@ def __init__(self, element_shape=(), layout=Layout.AOS):
self.layout = layout

def extract(self, x):
shape = tuple(x.shape)
from taichi.lang.matrix import MatrixNdarray, VectorNdarray
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move to top? (or will it cause circular import?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will cause circular import :-(

Copy link
Contributor

@ailzhang ailzhang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

assert v[4][9] == 9


# number of compiled functions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this comment not longer needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's used to separate three different parts of tests of this file.

python/taichi/lang/matrix.py Outdated Show resolved Hide resolved
python/taichi/lang/matrix.py Show resolved Hide resolved
Copy link
Contributor

@ailzhang ailzhang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@strongoier strongoier merged commit 0b5e408 into taichi-dev:master Aug 30, 2021
@strongoier strongoier deleted the ndarray-vec branch September 2, 2021 02:51
@Leonz5288 Leonz5288 mentioned this pull request Sep 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants