You can try Julia. It's pretty close to Python, and has a lot of MATLAB constructs. The translation here is:
F = @parallel (vcat) for i in 1:10
my_function(randn(3))
end
This makes the random numbers in parallel too, and just concatenates the results in the end during the reduction. That uses multiprocessing (so you need to do addprocs(N)
to add processes before using, and this is also works on multiple nodes on an HPC as shown in this blog post).
You can also use pmap
instead:
F = pmap((i)->my_function(randn(3)),1:10)
If you want thread parallelism, you can use Threads.@threads
(though make sure you make the algorithm thread-safe). Before opening Julia, set the environment variable JULIA_NUM_THREADS, then it's:
Ftmp = [Float64[] for i in Threads.nthreads()]
Threads.@threads for i in 1:10
push!(Ftmp[Threads.threadid()],my_function(randn(3)))
end
F = vcat(Ftmp...)
Here I make a separate array for each thread, so that way they don't clash when adding to the array, then just concatenate the arrays afterwards. Threading is pretty new so right now there's just the straight use of threads, but I'm sure threaded reductions and maps will be added just like it was for multiprocessing.