Last active
May 30, 2018 06:58
-
-
Save multidis/318bb95707a66ed56720 to your computer and use it in GitHub Desktop.
Multicore computations in Julia language.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# start julia specifying how many cores to make available | |
##$ julia -p 4 | |
# julia multicore start command in a shell script | |
##$ julia -p $(nproc) | |
# http://stackoverflow.com/questions/2314750/how-to-assign-the-output-of-a-shell-command-to-a-variable | |
# alternatively add local cores dynamically (e.g. after starting julia single-core) | |
##$ julia | |
addprocs_local(3) | |
# check the number of cores made available | |
nprocs() | |
# total number of cores (with hyperthreading) | |
# http://stackoverflow.com/questions/27931026/obtain-the-number-of-cpu-cores-in-julia | |
CPU_CORES | |
# make functions from a source file available to all processes | |
require("source-name.jl") | |
# altermnatively: (e.g. in interactive work) | |
# @everywhere macro executes a statement on all processes | |
@everywhere id = myid() | |
@everywhere function name(arg) | |
# do something | |
end | |
# @parallel for | |
# use for small iterations, reduce-step at the calling node | |
@parallel for i=1:4 | |
run(`hostname`) | |
end | |
# IMPORTANT: allocate variables before the loop scope | |
# do not mutate on some process as the change is not visible to others | |
# comprehension syntax | |
@parallel [funname(i) for i=1:4] | |
# pmap | |
# use when each call does lots of computations | |
# multiple argument function examples: | |
# http://stackoverflow.com/questions/24637064/julia-using-pmap-correctly | |
@everywhere f(s,count)=(println("process id = $(myid()) s = $s count = $count");repeat(s,count)) | |
pmap((a1,a2)->f(a1,a2),{"a","b","c"},{2,1,3}) | |
# or: | |
pmap((args)->f(args...),{{"a",2},{"b",1},{"c",3}}) | |
# loop parallelization with timing | |
tic() | |
# NOTE: (+) here is reduce-step | |
nheads = @parallel (+) for i=1:200000000 | |
int(randbool()) | |
end | |
s=toc() | |
println("Number of Heads: $nheads in $s seconds") | |
# further reading | |
# http://julia.readthedocs.org/en/latest/manual/parallel-computing/ | |
# http://julialang.org/blog/2013/04/distributed-numerical-optimization/ | |
# http://www.admin-magazine.com/HPC/Articles/Parallel-Julia-Jumping-Right-In | |
# http://www.blog.juliaferraioli.com/2014/02/julia-on-google-compute-engine-parallel.html |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# setting a hard limit on the code runtime | |
timeout 600 julia -p $(nproc) juliacode.jl >>results.log 2>&1 | |
# clean up remaining processes if any | |
killall -9 -v julia >>cleanup.log 2>&1 | |
### does this cover all possible cases? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment