From 1b4b46f2bc391d1a0faa707b11a6a8b6c8115605 Mon Sep 17 00:00:00 2001 From: Olina Date: Thu, 17 Nov 2022 15:17:47 +0800 Subject: [PATCH] new FAQ --- docs/lang/articles/faqs/faq.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/docs/lang/articles/faqs/faq.md b/docs/lang/articles/faqs/faq.md index b08777de68861c..f29dbd78d213e6 100755 --- a/docs/lang/articles/faqs/faq.md +++ b/docs/lang/articles/faqs/faq.md @@ -307,3 +307,24 @@ for i in x: You can call `ti.sync()`, which is similar to CUDA's `cudaStreamSynchronize()`, in Taichi to synchronize the parallel for loops. `__syncthreads()` is a block-level synchronization barrier, and Taichi provides a synonymous API `ti.simt.block.sync()`, which for now supports CUDA and Vulkan backends only. However, all block-level APIs are still experimental, and you should use this API only when it relates to SIMT operation synchronization and `SharedArray` reads and writes. + +### How can I swap elements between two fields in the Taichi scope? `a,b = b,a` does not work. + +Direct value assignments lead to semantic ambiguity. For example, `a = b` can mean data copy if `a` is pre-defined, or otherwise can serve to define and initialize `a`. + +You can swap two fields in the Taichi scope using a struct for: + +```python +@ti.func +def field_copy(src: ti.template(), dst: ti.template()): + for I in ti.grouped(src): + dst[I] = src[I] + +@ti.kernel +def test(): + # copy b to a + field_copy(b, a) + print(a[0]) + +test() +```