Skip to content

Commit

Permalink
[Doc] Update external.md (taichi-dev#6424)
Browse files Browse the repository at this point in the history
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Olinaaaloompa <[email protected]>
Co-authored-by: Ailing  <[email protected]>
  • Loading branch information
4 people authored Nov 16, 2022
1 parent d53a6ca commit 5485ac3
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions docs/lang/articles/basic/external.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,30 @@ copy(x, y) # error!
copy(x, y.clone()) # correct
copy(x, y.contiguous()) # correct
```

## FAQ

### Can I use `@ti.kernel` to accelerate a NumPy function?

Unlike other Python acceleration frameworks, such as Numba, Taichi does not compile NumPy functions. Calling NumPy functions inside the Taichi scope is not supported, as the following example shows:

```python
import numpy as np

@ti.kernel
def invalid_sum(arr: ti.types.ndarray()):
total = np.sum(arr) # Not supported!
...
```


If you want to use a NumPy function, which lacks a counterpart in Taichi, you can call the function in the Python scope as usual and pass the processed array to Taichi kernels via `ti.types.ndarray()`. For example:

```python
indices = np.argsort(arr) # arr is a Numpy.ndarray

@ti.kernel
def valid_example(arr: ti.types.ndarray(), indices: ti.types.ndarray()):
min_element = arr[indices[0]]
...
```

0 comments on commit 5485ac3

Please sign in to comment.