How can I modify the code for using 'parfor'?

1 Ansicht (letzte 30 Tage)
SO
SO am 15 Mär. 2015
Beantwortet: Edric Ellis am 16 Mär. 2015
I want to use parfor to speed up the calculation of my program. But some errors occur due to the indexing of variables.
parfor i = 1:size(az_file,1)
row = 1;
col = 1;
for ph1 = 80:-2.5:-80
for alng1 = -180:5:180
if alng1 == 180
gims_nequick_Az(row,col) = gims_nequick_Az(row,1);
else
ph2 =ph1;
alng2 =alng1;
xMODIP = xMODIP_grid(row,col);
index = az_file(i,3) + az_file(i,4)*xMODIP + az_file(i,5)*xMODIP^2;
[final_tec] = NeQuick2_main(year, month, time, R12orNOT, index, ph1, alng1, h1, ph2, alng2, h2, xMODIP, ccirdata_all);
gims_nequick_Az(row,col) = final_tec;
col = col+1;
end
end
row = row+1;
col = 1;
end
row = 1;
all_gims_nequick_Az{i} = gims_nequick_Az;
end
There are warnings caused by indexing of variables 'gims_nequick_Az', 'az_file', xMODIP_grid.
Saying ' The variable 'X' in a parfor cannot be classified.
and ' 'X' is indexed but not sliced, .... result in communication overhead.'
How can I modify the code in order to use parfor?
Thanks.

Antworten (1)

Edric Ellis
Edric Ellis am 16 Mär. 2015
The messages about variables being "index but not sliced" are warnings, and are simply letting you know that you might be transferring more data to the workers than is strictly necessary.
The problem with gims_nequick_Az not being classified needs fixing. In this case, the problem arises because the parfor analysis cannot prove that you aren't doing something order-dependent. A simplified version of the problem might be:
parfor idx = 1:10
for jdx = 1:10
x(jdx) = rand();
end
out{idx} = x;
end
Here, we can see clearly that x is completely overwritten each time around the parfor loop - but the parfor machinery cannot see that, and complains. The fix is simple - pre-allocate x at the start of the loop so that parfor can see that the old value of x is not being used:
parfor idx = 1:10
x = zeros(1,10); % added this line
for jdx = 1:10
x(jdx) = rand();
end
out{idx} = x;
end
So, in your case, you need to pre-allocate gims_nequick_Az each time around your parfor loop.

Kategorien

Mehr zu Parallel for-Loops (parfor) finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by