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

1 次查看(过去 30 天)
Currently, the parfor command takes an iteration step of one, and it beats my intention of speeding up the loop calculation.

采纳的回答

Walter Roberson
Walter Roberson 2017-8-18
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;

更多回答(0 个)

类别

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