Press "Enter" to skip to content

Month: May 2018

Parallel computing with Julia

The Julia Language

Julia is particularly attractive for its ability to run in parallel, not only on clusters, but also on the multiple cores of your laptop processor. Here is a minimalistic example using pmap().

# For parallel computing with julia -p NumberOfAdditionalCPU

@everywhere begin # this part will be available on all CPUs

function computation(runid_is_useless)
  result = rand()
  return result
end # function computation()

end # @everywhere

### Main part - runs only on main instance of Julia.

Nprocs = nprocs()
print("Number of processors ",Nprocs,".\n")

## Launching computations on Nprocs parallel processors
results = @time pmap(computation,1:Nprocs)

## Post-processing
for i in 1:Nprocs
  print(@sprintf("Result of CPU %i is %f\n",i,results[i]))
end # for i

On my laptop, my processor has 4 cores, and I run this code with julia -p 3 code.jl which gives

Number of processors 4.
0.548238 seconds (214.75 k allocations: 9.788 MB, 5.76% gc time)
Result of CPU 1 is 0.725449
Result of CPU 2 is 0.377958
Result of CPU 3 is 0.443606
Result of CPU 4 is 0.016641

The same code with -p 39 can also be run on the 40 CPUs cluster of my department! For more material on parallel computing with Julia, you may take a look at Julia documentation.

Leave a Comment
Syntax · Style · Tracking & Privacy.