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

1 次查看(过去 30 天)
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 个评论
Matt J
Matt J 2023-4-15
编辑:Matt J 2023-4-15
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.

请先登录,再进行评论。

回答(1 个)

Vatsal
Vatsal 2023-12-12
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!

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by