You can also add processes after Julia has started using the addprocs function. We are running the following code on Windows with two drives, C: and D:, present. Julia is started in the D:\ directory:
D:\> julia --banner=no -p 2 -L hello2.jl
Hello
From worker 3: Hello
From worker 2: Hello
julia> pwd()
"D:\\"
julia> using Distributed
julia> pmap(i -> (i, myid(), pwd()), 1:nworkers())
2-element Array{Tuple{Int64,Int64,String},1}:
(1, 2, "D:\\")
(2, 3, "D:\\")
julia> cd("C:\\")
julia> pwd()
"C:\\"
julia> addprocs(2)
2-element Array{Int64,1}:
4
5
julia> pmap(i -> (i,myid(),pwd()), 1:nworkers())
4-element Array{Tuple{Int64,Int64,String},1}:
(1, 3, "D:\\")
(2, 2, "D:\\")
(3, 5, "C:\\")
(4, 4, "C:\\")
In particular, we see that each worker has its own working directory, which is initially set to the working directory of the master Julia process when it is started. Also, addprocs does not execute the script that was specified by the -L switch on Julia startup.
Additionally, we can see the simple use of the pmap and myid functions. The first one is a parallelized version of the map function. The second returns the identification number of a process that it is run on.
As we explained earlier, it is not possible to add threads to a running Julia process. The number of threads has to be specified before Julia is started.
Deciding between using multiple processes and multiple threads is not a simple decision. A rule of thumb is to use threads if there is a need for data sharing and frequent communication between tasks running in parallel.