You can use n-dimensional methods of a distributed array:
First you need to add some more processes, local or remote:
julia> addprocs(CPU_CORES - 1);
Then you should use DistributedArrays for each of the created processes:
julia> @everywhere using DistributedArrays
Finally, you can use the @DArray macro, for example:
julia> x = @DArray [@show x^2 for x = 1:10]; From worker 2: x ^ 2 = 1 From worker 2: x ^ 2 = 4 From worker 4: x ^ 2 = 64 From worker 2: x ^ 2 = 9 From worker 4: x ^ 2 = 81 From worker 4: x ^ 2 = 100 From worker 3: x ^ 2 = 16 From worker 3: x ^ 2 = 25 From worker 3: x ^ 2 = 36 From worker 3: x ^ 2 = 49
You can see that it does what you expect:
julia> x 10-element DistributedArrays.DArray{Int64,1,Array{Int64,1}}: 1 4 9 16 25 36 49 64 81 100
Remember that it works with an arbitrary number of dimensions:
julia> y = @DArray [@show i + j for i = 1:3, j = 4:6]; From worker 4: i + j = 7 From worker 4: i + j = 8 From worker 4: i + j = 9 From worker 2: i + j = 5 From worker 2: i + j = 6 From worker 2: i + j = 7 From worker 3: i + j = 6 From worker 3: i + j = 7 From worker 3: i + j = 8 julia> y 3x3 DistributedArrays.DArray{Int64,2,Array{Int64,2}}: 5 6 7 6 7 8 7 8 9 julia>
This is the most Julian way to do what you intended IMHO.
We can look at macroexpand output to see what happens:
Note: this result has been slightly edited for readability, T means:
DistributedArrays.Tuple{DistributedArrays.Vararg{DistributedArrays.UnitRange{DistributedArrays.Int}}}
julia> macroexpand(:(@DArray [i^2 for i = 1:10])) :( DistributedArrays.DArray( (
This is basically the same as when entering manually:
julia> n = 10; dims = (n,); julia> DArray(x -> [i^2 for i = (1:n)[x[1]]], dims) 10-element DistributedArrays.DArray{Any,1,Array{Any,1}}: 1 4 9 16 25 36 49 64 81 100 julia>