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.
0 件のコメント
採用された回答
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 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Parallel for-Loops (parfor) についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!