Parallel computing example 1: Use DistributedArrays.jl for distributed computing

An example of using DistributedArrays.jl for distributed computing, for reference by novices. For how to build a distributed cluster, please refer to the previous blog.

# Example 1: Distributed computing with DistributedArrays.jl
# More instructions: https://juliaparallel.github.io/DistributedArrays.jl/stable/
# ----------

# Set the number of workers.
n = 2

# Use the Distributed package.
using Distributed

# Add workers.
addprocs(n - nprocs() + 1)
println("Opened ", nworkers()," worker(es) of PID ", workers())

# Use the DistributedArrays package everywhere.
@everywhere using DistributedArrays 

# Define a datatype everywhere.
@everywhere mutable struct MyType
    x
    y
    z
end

# Generate an array with arbitary element datatype, for example, MyType.
A = [MyType(0, 0, 0) for i in 1:4, j in 1:4]

# Distribute the array among workers.
DA = distribute(A, procs = [2, 3])

# Perform the computing among workers. Use @sync to synchronize all tasks.
@sync for pid in workers()
    # Send the task to the remote worker whose PID = pid. The task is wrapped in a "begin" block.
    @spawnat pid begin
        # You can read any element of DA directly on any worker.
        for a in DA
            println(a.x)
        end

        # You can modify the element of DA only when the element is stored on the current worker.
        for a in DA
            a.x = myid()
        end

        # To modify the element, it is highly recommended to use localpart() to access the local part of DA on the remote worker.
        for a in localpart(DA)
            a.y = myid()
        end

        # Or use localindices().
        r1, r2 = localindices(DA)
        for i in r1, j in r2
            DA[i, j].z = myid()
        end
    end
end

# You can read DA on any worker.
display(DA)

Guess you like

Origin blog.csdn.net/iamzhtr/article/details/108565622