Trouble in rewriting the for loop as a parfor loop

조회 수: 1 (최근 30일)
Chenguang Yan
Chenguang Yan 2020년 11월 3일
댓글: Chenguang Yan 2020년 11월 3일
I want to reduce unnecessary calculations in the loop, and this is the sample code for "for" loop which reduce the number of calculations from 200 to 120 :
R_num = 20;
C_numhalf = 10;
row = 1:C_numhalf;
Ray = R_num/C_numhalf*2;
row_flip = flip(row);
a = zeros(R_num,C_numhalf);
for ii = 1:R_num
col = [row(1:ceil(ii/Ray)) flip(row_flip((1:ceil(ii/Ray))))];
for jj = col % 1:C_numhalf
a(ii,jj) = 1;
end
end
disp(a)
The problem is how to rewrite the "for" loop into "parfor" loop, the following code does not work properly and this is the help link:
R_num = 20;
C_numhalf = 10;
row = 1:C_numhalf;
Ray = R_num/C_numhalf*2;
row_flip = flip(row);
a = zeros(R_num,C_numhalf);
parfor ii = 1:R_num
col = [row(1:ceil(ii/Ray)) flip(row_flip((1:ceil(ii/Ray))))];
for jj = col
a(ii,jj) = 1;
end
end
disp(a)
I have poor knowledge of parallel computing and failed to find a solution to the problem. Any suggestions are helpful!
By the way, there is an error in the help link:

채택된 답변

Raymond Norris
Raymond Norris 2020년 11월 3일
This might work, but two clarifying points
  • These aren't the same
for jj = col % 1:C_numhalf
In your example code, col might be the vector [1 10], which when used as the range in a for loop is not the same as
1:10. I'm assuming you want the vector [1 x] and not the vector 1:x.
  • Depending on how you initialize a, what I'm showing may or may not work (unless you really do want to work with the vector 1:x and not [1 x]). If it's with zeros, you don't need to call zeros again in unit_of_work (just because of the way scalar expansion works). If it's any other helper function (e.g. nan), then you'll need to preallocate it with the same helper function (e.g. tempa = nan(1,C_numhalf)). If a is assigned some other way, for example a = myfcn(R_num,C_numhalf), what I'm suggesting most likely won't work. Based on your response, we might be able to write it differently.
function chenguang
R_num = 20;
C_numhalf = 10;
row = 1:C_numhalf;
Ray = R_num/C_numhalf*2;
row_flip = flip(row);
a = zeros(R_num,C_numhalf);
parfor ii = 1:R_num
a(ii,:) = unit_of_work(row,ii,Ray,row_flip,C_numhalf);
end
disp(a)
function tempa = unit_of_work(row,ii,Ray,row_flip,C_numhalf)
col = [row(1:ceil(ii/Ray)) flip(row_flip((1:ceil(ii/Ray))))];
tempa = zeros(1,C_numhalf);
for jj = col
tempa(1,jj) = 1;
end
  댓글 수: 1
Chenguang Yan
Chenguang Yan 2020년 11월 3일
  • I want the vector [1 x] and not the vector 1:x, when ii == 1.
  • I preallocate a with a = sym(zeros(R_num,C_numhalf)) actually. Calculating the element of matrix a involves symbolic computation, which is why I hope to use parfor to speed up the calculation.
Following the method you provided, I placed the inner for loop in the local function in my own code, and the outer parfor loop seems to work normally. Thank you very much for your answers.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by