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

[Doc] Update dac #6875

Merged
merged 10 commits into from
Dec 20, 2022
40 changes: 24 additions & 16 deletions docs/lang/articles/advanced/data_oriented_class.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@
sidebar_position: 3
---

# Data-oriented Class
# Data-Oriented Class

To define a Taichi kernel as a Python class member function:

1. Decorate the class with a `@ti.data_oriented` decorator.
2. Define `ti.kernel`s and `ti.func`s in your data-oriented Python class.
2. Define `ti.kernel`s and `ti.func`s in your Data-Oriented Python class.

:::note
The first argument of the function should be the class instance ("`self`"), unless you are defining a `@staticmethod`.
:::

A brief example:
A brief example. Notice the use of `@ti.data_oriented` and `@ti.kernel` in lines 1 and 6, respectively:

```python {1}
```python {1,6}
@ti.data_oriented
class TiArray:
def __init__(self, n):
Expand All @@ -30,7 +30,7 @@ a = TiArray(32)
a.inc()
```

Definitions of Taichi fields can be made not only in _init_ functions, but also at any place of a Python-scope function in a data-oriented class. For example,
Definitions of Taichi fields can be made not only in _init_ functions, but also at any place of a Python-scope function in a Data-Oriented class.

```python {21,25}
import taichi as ti
Expand All @@ -41,6 +41,8 @@ ti.init()
class MyClass:
@ti.kernel
def inc(self, temp: ti.template()):

#increment all elements in array by 1
for I in ti.grouped(temp):
temp[I] += 1

Expand All @@ -51,15 +53,21 @@ class MyClass:
self.temp = ti.field(dtype = ti.i32, shape=n)


a = MyClass()
<<<<<<< HEAD
a = MyClass() # creating an instance of Data-Oriented Class

=======
a = MyClass() # creating an instance of Data-Oriented Class
>>>>>>> b62e12c10a27a2dd924c4e909976cdfe98a109ce
# a.call_inc() cannot be called, because a.temp has not been allocated at this point
a.allocate_temp(4)
a.call_inc()
a.call_inc()
print(a.temp) # [2 2 2 2]
a.allocate_temp(8)
a.call_inc()
print(a.temp) # [1 1 1 1 1 1 1 1]
a.allocate_temp(4) # [0 0 0 0]
a.call_inc() # [1 1 1 1]
a.call_inc() # [2 2 2 2]
neozhaoliang marked this conversation as resolved.
Show resolved Hide resolved
print(a.temp) # will print [2 2 2 2]

a.allocate_temp(8) # [0 0 0 0 0 0 0 0 0]
a.call_inc() # [1 1 1 1 1 1 1 1]
print(a.temp) # will print [1 1 1 1 1 1 1 1]
```

Another memory recycling example:
Expand Down Expand Up @@ -95,9 +103,9 @@ print(a.y) # [ 5. 13. 21. 29.]
```


## Inheritance of data-oriented classes
## Inheritance of Data-Oriented classes

The data-oriented property is automatically carried along with the Python class inheriting. This means that you can call a Taichi Kernel if any of its ancestor classes is decorated with `@ti.data_oriented`.
The Data-Oriented property is automatically carried along with the Python class inheritence. This implies that you can call a Taichi Kernel if any of its ancestor classes is decorated with `@ti.data_oriented`, which is shown in the example below:

An example:
```python
Expand Down Expand Up @@ -152,7 +160,7 @@ c = BaseClass()

## Python built-in decorators

Common decorators that are pre-built in Python, `@staticmethod`[^1] and `@classmethod`[^2], can decorate a Taichi kernel in data-oriented classes.
Common decorators that are pre-built in Python, `@staticmethod`[^1] and `@classmethod`[^2], can decorate a Taichi kernel in Data-Oriented classes.

[^1]: [Python built-in functions - staticmethod](https://docs.python.org/3/library/functions.html#staticmethod)
[^2]: [Python built-in functions - classmethod](https://docs.python.org/3/library/functions.html#classmethod)
Expand Down