Say I have two usecases for a function. One logs the result of each iteration and is parallelized using Polyester.@batch
, the other only needs to return the final result and uses Threads.@threads
. Performance is top priority in the second case, but “only” very important in the first case (with the logs).
Also, the choice is known at “compile time”, ie when the package is loaded.
NB: The real code has a bunch of logging events, so the code becomes less readable in that case:
using BenchmarkTools, Distributions, Polyester, .Threads
function example!(dat, dlog)
@batch for i in 1:50
flag = rand(Bernoulli(0.25))
if flag
idx = rand(1:10)
dat[idx] += 1
dlog[i] = idx
end
end
return dat, dlog
end
function example!(dat)
@threads for i in 1:50
flag = rand(Bernoulli(0.25))
if flag
idx = rand(1:10)
dat[idx] += 1
end
end
return dat
end
dat = fill(0, 10); dlog = fill(0, 50);
example!(dat, dlog)
dat = fill(0, 10); dlog = fill(0, 50);
example!(dat)
Is there a way to do this without needing to maintain two separate versions, or adding a bunch of if-statements?
Maybe if a global const is set, @batch
is chosen then dlog
gets updated each time dat
is modified?