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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by