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
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
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!
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!