SharedArray

SharedArray(T::Type, dims::NTuple; init=false, pids=Int[])

Construct a SharedArray of a bitstype T and size dims across the processes specified by pids - all of which have to be on the same host.

If pids is left unspecified, the shared array will be mapped across all processes on the current host, including the master. But, localindexes and indexpids will only refer to worker processes. This facilitates work distribution code to use workers for actual computation with the master process acting as a driver.

If an init function of the type initfn(S::SharedArray) is specified, it is called on all the participating workers.

Examples

julia> using Distributed

julia> addprocs(4) # Adding 4 worker processes

julia> @everywhere using SharedArrays

julia> shared_array = SharedArray{Float64}(3, 3)
3×3 SharedArray{Float64,2}:
 0.0  0.0  0.0
 0.0  0.0  0.0
 0.0  0.0  0.0

julia> fill!(shared_array, 1.5)
3×3 SharedArray{Float64,2}:
 1.5  1.5  1.5
 1.5  1.5  1.5
 1.5  1.5  1.5

julia> shared_array[2, 2] = 2.0
2.0

julia> shared_array
3×3 SharedArray{Float64,2}:
 1.5  1.5  1.5
 1.5  2.0  1.5
 1.5  1.5  1.5

In this example, we create a SharedArray of type Float64 with dimensions 3x3. The SharedArray is mapped across all processes (including the master process) on the current host. We initialize the shared array with zeros and then fill it with the value 1.5. Finally, we modify the element at index [2, 2] to 2.0.

Note: The example assumes that the SharedArray function is used in a distributed Julia environment where worker processes are available.

See Also

User Contributed Notes

Add a Note

The format of note supported is markdown, use triple backtick to start and end a code block.

*Required Field
Details

Checking you are not a robot: