I want to run some code in parallel, however I found a strange behavior that I would like to understand.
Here is a small working example:
using Random;
using Statistics;
using Distributed;
using SharedArrays;
nprocs()
addprocs(3);
nreps = 5;
result = SharedArray{Float64, 1}(nreps);
@distributed for i = 1:nreps
data = randn(20);
result[i] = Statistics.mean(data);
end;
This silly code simply computes the mean of a random sample from N(0, 1) of size 20, nreps times.
What bothers me is that the code breaks in case the second last line is simply the following:
result[i] = mean(data);
Now, array ‘result’ was never changed in the for loop (it’s still full of 0s). Why is this? How can I simply call a function by its name, without explicitly referring to its package? And why isn’t function ‘randn’ suffering from the same problem?
This is a problem to me because the for loop that I really want to implement has many calls to a variety of functions (e.g., var, sd, tdistcdf), and keeping track of the package of each function seems a waste of typing effort and space. I do understand this behavior when there are multiple packages offering a function with the same name (this also happens to me a lot in R, which is my main programming language), but that is definitely not the explanation here.
Many thanks in advance,
bigoten