In this chapter, we will looking at how to use ArrayFire in multi-threaded programs. We shall go over the details in the following order.
- Move an Array to thread
- Read Array from Multiple threads
- Write to Array from Multiple threads
- Write to single Array using Channel
In this section, we are going to create an Array on main thread and move it to a child thread, modify it and then print it from the child thread.
{{#include ../../src/core/array.rs:move_array_to_thread}}
Now, let's expand the earlier example to do a bunch of arithmetic operations in parallel on multiple threads using the same Array objects.
{{#include ../../src/core/array.rs:read_from_multiple_threads}}
Given below is the definition of the enum Op
we used in the example for illustration simplicity.
{{#include ../../src/core/array.rs:multiple_threads_enum_def}}
Let us further expand the earlier example by accumulating the results of the arithmetic operations into a single Array object.
The code will differ from earlier section in couple of locations:
- In the main thread, we wrap the accumulating Array in a read-write lock (
std::sync::RwLock
) which is in turn wrapped in an atomically reference counted counter a.k.astd::sync::Arc
. - In the children threads, we use the guarded objects returned by RwLock's write method to access the accumulator Array.
{{#include ../../src/core/array.rs:access_using_rwlock}}
In this section, we shall modify the example to use channel instead of data sharing.
{{#include ../../src/core/array.rs:accum_using_channel}}