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] Customized struct support #2627

Merged
merged 45 commits into from
Aug 30, 2021

Conversation

victoriacity
Copy link
Member

@victoriacity victoriacity commented Aug 2, 2021

Related issue = close #2614

Adds customized struct support to Taichi fields, e.g.:

f = ti.Struct.field({'a':ti.i32, 'b':ti.f32}, shape=(8,))
# or
# f = ti.Struct.field({'a':ti.i32, 'b':ti.f32})
# ti.root.dense(ti.i, 8).place(f)
@ti.kernel
def run():
  for i in f:
    f[i].a = i
    f[i].b = f[i].a

The implementation generally follows the Matrix and MatrixField class with similar API definitions, while members of a Struct can be only accessed using attribute names.

A CompoundType interface has been implemented to instantiate vector, matrices, and structs with pre-defined shapes or members. This allows for a custom struct to have vectors, matrices and nested structs as members.

vec3f = ti.types.vector(3, ti.f32)
ray3f = ti.types.struct(origin=vec3f, raydir=vec3f, raylen=ti.f32)
pixelrays = ray3f.field(shape=(512, 512))

Local structs in both python and Taichi scopes can be initialized with either keyword arguments and dictionaries:

ray = ti.Struct(
    origin=ti.Vector([0.0, 0.0, 0.0]), raydir=ti.Vector([0.0, 0.0, 1.0]), raylen=1.0,
)
# or
ray = ti.Struct({
    "origin": ti.Vector([0.0, 0.0, 0.0]),
    "raydir": ti.Vector([0.0, 0.0, 1.0]), 
    "raylen": 1.0,
})

In addition to directly invoking initializers of Struct and Matrix classes, CompoundType instances can be also called to create matrix or struct instances. Vectors and matrices can be created using GLSL-like broadcast syntax since the shape of the vector or matrix is already known:

vec2f = ti.types.vector(2, ti.f32)
...
ro = vec3(0.0) # ti.Vector([0.0, 0.0, 0.0])
rd = vec3(vec2(0), 1) # ti.Vector([0.0, 0.0, 1.0]), will perform implicit cast
ray = ray3f(origin=ro, raydir=rd, raylen=1.0)

@k-ye k-ye requested a review from strongoier August 3, 2021 12:34
@k-ye
Copy link
Member

k-ye commented Aug 18, 2021

Ping :-) ?

@victoriacity victoriacity marked this pull request as ready for review August 25, 2021 07:21
@victoriacity
Copy link
Member Author

/format

@k-ye k-ye requested a review from ailzhang August 26, 2021 00:50
@victoriacity
Copy link
Member Author

/format

Copy link
Contributor

@strongoier strongoier left a comment

Choose a reason for hiding this comment

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

Excellent work!!

docs/lang/articles/basic/field.md Outdated Show resolved Hide resolved
tests/python/test_torch_io.py Outdated Show resolved Hide resolved
tests/python/test_numpy_io.py Outdated Show resolved Hide resolved
tests/python/test_numpy_io.py Outdated Show resolved Hide resolved
python/taichi/lang/struct.py Outdated Show resolved Hide resolved
python/taichi/lang/struct.py Show resolved Hide resolved
@victoriacity
Copy link
Member Author

/format

Copy link
Contributor

@strongoier strongoier left a comment

Choose a reason for hiding this comment

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

Would you also resolve conflicts with master?

python/taichi/lang/struct.py Outdated Show resolved Hide resolved
python/taichi/lang/struct.py Outdated Show resolved Hide resolved
python/taichi/lang/struct.py Outdated Show resolved Hide resolved
@victoriacity
Copy link
Member Author

/format

Copy link
Contributor

@strongoier strongoier left a comment

Choose a reason for hiding this comment

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

LGTM! Thanks for your effort!

@strongoier strongoier merged commit 555956d into taichi-dev:master Aug 30, 2021
@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.

[Lang] Support customized struct in a SNode container?
4 participants