Is there a workaround to the 'parfor' command so that it can work for non-consecutive iteration steps?

1 view (last 30 days)
Currently, the parfor command takes an iteration step of one, and it beats my intention of speeding up the loop calculation.

Accepted Answer

Walter Roberson
Walter Roberson on 18 Aug 2017
If you are using the parfor iteration number as a data parameter, then create a vector with all of the desired parameters and use a sequential parfor index to index that vector. For example instead of
total = 0;
parfor K = [1 2 4 8 16]
total = total + B.^K;
end
you would use
K_vals = [1 2 4 8 16];
total = 0;
parfor Kidx = 1 : length(K_vals)
K = K_vals(Kidx);
total = total + B.^K;
end
This will not work if you are using K to index an array you are writing into.
If you are using the parfor iteration number to iterate in a "random" order, then leave out the re-ordering: parfor does not iterate sequentially anyhow. For example, instead of
parfor K = randperm(123)
X(K) = ....
end
just use
parfor K = 1 : 123
X(K) = ...
end
If you are using the parfor iteration number to select a subset of the data, then the work-around is to extract the needed subset of the data and iterate sequentially over the subset, and later write the subset back. For example, instead of
parfor K = [1 2 4 8 16]
X(K) = X(K) * 2;
end
use
K_vals = [1 2 4 8 16];
X_subset = X(K_vals);
parfor K = 1 : length(K_vals)
X_subset(K) = X_subset(K) * 2;
end
X(K_vals) = X_subset;

More Answers (0)

Categories

Find more on Parallel for-Loops (parfor) in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!