Clear Filters
Clear Filters

How to export data out of a parfor loop to be combined later on with a client's existing matrix

1 view (last 30 days)
So I have a long algorithm where the only output is (if certain conditions are true), adding a value to the 4-D matrix "objm". The problem is modifying objm at each worker is not possible in its current configuration. This is the simplification of what I'm trying to do. I figured the best way was to make a pool prior , save the data in a temporary matrix, and after each iteration of the original for-loop (ii), send the data to the pool to be added to objm. I'm not sure if I explained this well, please let me know if you have any questions.
ni = 15;
nj = 25;
nk = 1e6;
objm = zeros(ni,nj,nk);
% Do I need to make a parallel pool here?
parfor ii = 1:ni
for jj = 1:nj
for kk = 1:nk
for ll = 1:4
objm(i,jj,kk,ll) = objm(i,jj,kk,ll) + 1; % This is the only output I need
end
end
end
% Do I need to put an after each here to send data to be combined with
% objm as the data comes in?
end
  1 Comment
Matt J
Matt J on 15 Apr 2023
Edited: Matt J on 15 Apr 2023
You have simplified the description of your task to the point where the need for a parpool at all is not clear. The posted code is equivalent to the single line,
objm = ones(ni,nj,nk,4);
There is no better way to create a 4D array of ones than this, if that is indeed the task.

Sign in to comment.

Answers (1)

Vatsal
Vatsal on 12 Dec 2023
Hi,
I understand that you are attempting to parallelize your algorithm and update a 4-D matrix "objm" within a parfor loop. However, directly modifying "objm" within the loop isn't feasible due to potential conflicts that may arise when multiple workers try to modify it simultaneously.
To address this, you can create a temporary variable within the parfor loop to hold the results of each iteration, and then assign these results to “objm” at the end of each iteration. Here is how you can modify your code:
ni = 15;
nj = 25;
nk = 1e6;
objm = zeros(ni,nj,nk,4);
parfor ii = 1:ni
temp = zeros(nj,nk,4);
for jj = 1:nj
for kk = 1:nk
for ll = 1:4
temp(jj,kk,ll) = temp(jj,kk,ll) + 1;
end
end
end
objm(ii,:,:,:) = temp;
end
I hope this helps!

Categories

Find more on Parallel Computing Fundamentals in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!