Working with the constraints of shared arrays

Elements in a SharedArray must be bits type. What does that mean? The formal definition of bits type can be summarized as follows:

  • The type is immutable.
  • The type contains only primitive types or other bits types.

The following OrderItem type is a bits type because all fields are primitive types:

struct OrderItem
order_id::Int
item_id::Int
price::Float64
quantity::Int
end

The following Customer type is not a bits type because it contains a reference to String, which is neither a primitive type nor a bits type:

struct Customer
name::String
age::Int
end

Let's try to create SharedArray for a bits type. The following code confirms that it works properly:

If we try to create SharedArray with a non-bits type such as a mutable struct type, an error will result:

In summary, Julia's shared array is a great way to distribute data to multiple parallel processes for high-performance computing. The programming interface is also very easy to use.

In the next section, we will look into a pattern that improves performance by exploiting the space-time trade-off.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset