Skip to content

Instantly share code, notes, and snippets.

@rleegates
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rleegates/4f389e31e36ed973d12e to your computer and use it in GitHub Desktop.
Save rleegates/4f389e31e36ed973d12e to your computer and use it in GitHub Desktop.
# parallelTest.jl
# automatic differentiation of a function at n = 100e6 points using dual numbers
# compare parallel to serial implementation
# benchmark timings were observed on a 2.66GHz Quad-Core Xeon after an initial warm up run
# initialize processors
availProcs = 8;
addprocs(availProcs-nprocs());
# require loads DualNumbers on all workers
# using DualNumbers
require("using.jl")
# define functions on all workers:
@everywhere begin
# automatic differentiation using dual numbers
function autoDiff(f,x)
y = f(dual(x,1));
return epsilon(y);
end
f(x) = x.^3;
n = 100000000;
end
# parallel autoDiff
# note that convert(Array, arg1::DArray)
# consumes most of the processing time due to
# the limitations of memory bandwidth
res1 = dzeros(n,1);
@time begin
# parallel for loop without reduction assignment
@parallel for worker = 1:nprocs()-1
# get a SubArray on local processor
lRes = localpart(res1);
# get local indexes (returns tuple of ranges)
lIdx = myindexes(res1);
for jj = 1:length(lRes);
# modify SubArray
lRes[jj] = autoDiff(f,lIdx[1][jj]);
end
end
# copy all data back to worker 1, i.e. DArray -> Array
convert(Array, res1);
end
# elapsed time: 14.656142945 seconds (3251017544 bytes allocated)
# serial autoDiff
res2 = zeros(n,1);
@time for jj = 1:n
res2[jj] = autoDiff(f,jj);
end
# elapsed time: 39.727926118 seconds (9599995484 bytes allocated)
# show some other fun stuff in Julia
# apply a function to preceding argument with |> and -> creates an anonymous function
([1:5],[1:5]) |> a->a[1].^2+a[2].^2 |> sum |> inv
# apply a function to each object in array
map((x) -> x * 2, [1, 2, 3])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment