1

How to implement @parallel_map macro?

results = @parallel_map for i in 1:10
  i^2
end

So that it won't use Any like code below (I assume it slow down code)?

results = Vector{Any}(undef, 10) # possible slow?
Threads.@threads for i in 1:10
  results[i] = i^2
end
1
  • Just use Vector{Int}(undef, 10) instead. I will grab any memory area so it will contain some random bytes. Since you overwrite them this is fine. This is fastest (with some caveats for complex multiprocesing scenarios). Commented Oct 25 at 23:27

1 Answer 1

1

You can initialize with zeros to avoid Any, and you may use either @threads or Distributed.pmap this way:

using Base.Threads, Distributed

const results = zeros(Int, 10)
@threads for i in 1:10
    results[i] = i^2
end
println("Results:       ", results)
println("PMap Results:  ", pmap(x -> x^2, 1:10))

prints

Results:       [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
PMap Results:  [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, but if the return type is complicated, say the result of optimisation. Won't switching type from array of zeroes to array of optimisation results cause type instability?
If there is a default constructor for your OptimizationResult type, you can use fill(OptimizationResult(), N) instead of zeros(Int, N). When using Distributed it's best to have a default initialization value for the data array input, so that can indicate a lack of one of the threads working correctly if you want to continue other threads (Distributed.pmap has an optional 'on_error' argument to facilitate this). You can of course use Any[] if you want. If the process you parallelize is a long one, calculation run time will far outweigh the Any array lookup time.
You can use undef with concrete types and can avoid CPU usage for unused objects. For types of uknown size undef will yield empty pointers and for types with known size (such as primitives or structs constructed of primitives) it will yield a non-zeroed continues memory area (that will look like random data)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.