Ask Your Question
3

How can SharedArray and pmap be applied in Julia?

asked 2023-07-14 19:57:22 +0000

huitzilopochtli gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2023-07-14 20:19:01 +0000

ladyg gravatar image

The SharedArray type and pmap function can be used together in Julia to parallelize computations that involve large arrays.

A SharedArray is a type of array that can be accessed and modified by multiple processes running in parallel. This allows for efficient sharing of large data sets among multiple workers without the need for copying the data.

The pmap function is a parallel map function that applies a given function to a collection of arguments in parallel across multiple processes. When combined with SharedArrays, pmap can be used to parallelize operations on large arrays in a distributed manner.

Here's an example of how to apply SharedArray and pmap in Julia:

using Distributed
addprocs(4) # Add 4 worker processes

@everywhere using SharedArrays

# Create a shared array and fill it with random numbers
shared_array = SharedArray{Float64}(1000, 1000)
for i in 1:length(shared_array)
    shared_array[i] = rand()
end

function process_slice(slice)
    # Process a single slice of the shared array
    # ...
end

# Parallelize processing of the shared array across multiple processes
results = pmap(process_slice, [shared_array[:, i] for i in 1:size(shared_array, 2)])

# Combine the results from each worker process
final_result = reduce(vcat, results)

In this example, we first add four worker processes using the addprocs function. We then load the SharedArrays module on all processes using the @everywhere macro.

Next, we create a 1000x1000 SharedArray and fill it with random numbers. We define a function process_slice that processes a single slice of the shared array.

We then use the pmap function to apply process_slice in parallel to each column slice of the shared array. The resulting array of results from each worker process is combined using the reduce function.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-07-14 19:57:22 +0000

Seen: 8 times

Last updated: Jul 14 '23