Skip to content

Commit

Permalink
[Doc] Update dac (taichi-dev#6875)
Browse files Browse the repository at this point in the history
Issue: #

### Brief Summary

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Olinaaaloompa <[email protected]>
  • Loading branch information
3 people authored and quadpixels committed May 13, 2023
1 parent 983d565 commit 9016a1f
Showing 1 changed file with 20 additions and 16 deletions.
36 changes: 20 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 @@ -50,16 +52,18 @@ class MyClass:
def allocate_temp(self, n):
self.temp = ti.field(dtype = ti.i32, shape=n)

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

a = MyClass()
a = MyClass() # creating an instance of Data-Oriented Class
# 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]
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 +99,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 +156,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

0 comments on commit 9016a1f

Please sign in to comment.