exponenta event banner

Анализ функций в фоновом режиме с помощью parfeval

В этом примере показано, как можно использовать parfeval для оценки функции в фоновом режиме и сбора результатов по мере их появления. В этом примере можно отправить вектор из нескольких будущих запросов в цикле for и извлечь отдельные будущие результаты по мере их доступности.

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.

Связанные темы