Main Content

このページの内容は最新ではありません。最新版の英語を参照するには、ここをクリックします。

parfeval を使用したバックグラウンドでの関数の評価

この例では、parfeval を使用してバックグラウンドで関数を評価し、使用可能になった結果を収集する方法を説明します。この例では、複数の Future 要求のベクトルを for ループに投入し、Future 出力が利用可能になったら各出力を取得します。

p = gcp();
% To request multiple evaluations, use a loop.
for idx = 1:10
  f(idx) = parfeval(p,@magic,1,idx); % Square size determined by idx
end
% Collect the results as they become available.
magicResults = cell(1,10);
for idx = 1:10
  % fetchNext blocks until next results are available.
  [completedIdx,value] = fetchNext(f);
  magicResults{completedIdx} = value;
  fprintf('Got result with index: %d.\n', completedIdx);
end
 
Got result with index: 1.
Got result with index: 2.
Got result with index: 3.
Got result with index: 4.
Got result with index: 5.
Got result with index: 6.
Got result with index: 7.
Got result with index: 8.
Got result with index: 9.
Got result with index: 10.

関連するトピック