Reputation: 23
I have a code that goes like this which I want to run using parpool: result = zeros(J,K)
for k = 1:K
for j = 1:J
build(:,1) = old1(:,j,k)
build(:,2) = old2(:,j,k)
result(j,k) = call_function(build); %Takes a long time to run
end
end
It takes a long time to run this code and I have to run this multiple times for my simulation so I want to run the outermost loop (k = 1:K) in parallel in MATLAB.
From what I have read, I cannot use parfor since all each function uses the same variables old1 and old2. I could use spmd and distribute my matrices old1 and old2. But I read this creates as many copies of the variable as the workers and I do not want this to happen. I could use drange. But I am not sure how it exactly works. I am finding it difficult to actually use what I have been reading in MATLAB references. Any resource and pointers would be of great help!
Constraints are as follows: Must not create multiple copies of the variables old1, old2. But I can slice it across workers as each iteration doesn't require other iterations. Have to distribute for the outermost loop only. For ease of accessing data outside this block of code.
Thank you.
Upvotes: 2
Views: 173
Reputation: 18
old1 and old2 can be used, I think. Initialize as constants using:
old1 = parallel.pool.Constant(old1);
old2 = parallel.pool.Constant(old2);
Have you seen this post? https://www.mathworks.com/help/distcomp/improve-parfor-performance.html
Upvotes: 0