Seven, shared array
The penultimate section of this chapter introduces one of Julia's features: SharedArray.
Julia's shared array is a shared multi-dimensional array provided by the SharedArrays package. It can be cross-process. At this time, the SharedArrays package must be explicitly loaded to each process that needs it, or it can be cross-coroutine. At this time, just load the SharedArrays package on the main process. Can @everywhere
be easily added to the package SharedArrays all processes:
julia> @everywhere using SharedArrays
But it seems that there is no way to @spawnat
load it to the specified process with such commands, so be honest and practical @everywhere
.
Looking back @distributed
, it can also do coroutine-level parallelism on the main process, so we can use shared arrays to implement it at the coroutine level @distributed
. Make a demonstration:
julia> using SharedArrays
julia> a = SharedArray{Float64}(1,10)
1×10 SharedArray{Float64,2}:
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
julia> @distributed for i = 1:10
a[i]=i
end
Task (done) @0x0000000006671710
julia> a
1×10 SharedArray{Float64,2}:
1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
It can be seen that multiple coroutines operate the same array in parallel. Let's look at an example of cross-process operation:
julia> @everywhere using SharedArrays
julia> a = SharedArray{Float64}(1,10)
1×10 SharedArray{Float64,2}:
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
julia> @fetchfrom 3 (a[6]=2;a[1]=a[6]+1)
3.0
It proves that the shared array can be operated as a normal array in each process, there is no difference.
When declaring a shared array, you can specify two key-value parameters init
and pids
initialize them and map them to the specified process.
Tips: Key-value parameters are also called keyword parameters. Different from general parameters, keywords and values must be explicitly stated. For example: Matlab drawing parameters'
'FontSize','值'
cannot be abbreviated as'值'
. The key-value parameters of the Julia shared arrayinit
must be writteninit=函数
in thepids
same way. The advantage of key-value parameters is that they can be arranged out of order, but they are generally required to be arranged behind ordinary parameters.
for example:
julia> S = SharedArray{Int,2}((3,4),init = A->(for i=1:3;A[i,1]=100;end),pids=[2,4])
3×4 SharedArray{Int64,2}:
100 0 0 0
100 0 0 0
100 0 0 0
Pay attention to the wording of vectors when passing parameters. For example,
pids=[2,4]
if you writepids=[2 4]
achievements, you will get an error because the keyword does not accept multidimensional arrays. In Julia, `[2 4] will be considered as multi-line writing. For details, please refer to the chapter on multi-dimensional arrays.
The efficiency of shared arrays is the same in any process, so parallel operation of large-scale data has extremely high efficiency.
Finally, I specifically point out two important contents:
- The auxiliary initialization function demonstrated in the original work
SharedArrays.localindices()
reported an error in Julia 1.1.0 for unknown reasons. - All the above methods are tested on a single host, that is, the so-called single-machine distributed platform or virtual cluster, which has not been tested on a distributed platform composed of multiple hosts (here, "cluster" refers to a distributed multi-host) test. Julia provides
--machinefile
commands for clusters and a unified style manager for virtual clusters and clustersClusterManager
, includingDistributed.LocalManager
andDistributed.SSHManager
. However, it is a pity that it was mentioned in the original work. The next section will sort out the relevant content in the official documents and may supplement your own test results.