question about Parfor loop

조회 수: 2 (최근 30일)
Athavan Nadarajah
Athavan Nadarajah 2015년 8월 26일
답변: Walter Roberson 2015년 8월 26일
I am writing to ask if you could help me with the parfor loop. My present codes in Matlab are nested “for loops” that consume a large amount of processing time, so I was hoping to convert the codes to allow for parallel computing. Here is the simplified version of my code (which is not presently compatible with parallel computing).
A = zeros(4, 11);
B = zeros(4, 11);
parfor i = 1:4
for j = 1:11
A(i, j+1) =A(i,j)+ B(i,j);
end
end
where A & B are vector data sets (in my actual code)
This code can be hypothetically run on four different computers simultaneously for each “i” value, by computing only the inner “for loop” on each individual computer. So I wonder if we could modify the above code in order to utilize the independent actual processing units (cores) in a single computer. Further, we believe our code requires that A(i, j+1) be dependent on A(i,j) .
Thank you for your time and assistance!

답변 (1개)

Walter Roberson
Walter Roberson 2015년 8월 26일
A = zeros(4, 11);
B = zeros(4, 11);
parfor i = 1:4
Ai = A(i,:);
Bi = B(i,:);
for j = 1:11
Ai(j+1) = Ai(j) + Bi(j);
end
A(i,:) = Ai;
end
In the particular case where you promise that A(i,:) = 0, then
parfor i = 1 : 4
Ai = cumsum(B(i,:));
A(i,:) = Ai;
end
If you do not promise that A(i,1) = 0 then
parfor i = 1 : 4
Ai = cumsum([A(i,1); B(i,:)]);
A(i,:) = Ai(2:end);
end

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by