parfeval
을 사용하여 백그라운드에서 함수 실행
이 예제에서는 parfeval
을 사용하여 백그라운드로 함수를 실행하고 결과가 나오는 대로 수집할 수 있는 방법을 보여줍니다. 이 예제에서 for 루프 안에 여러 Future 요청으로 구성된 벡터를 제출하고 개별 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.