フィルターのクリア

parfor with progress report

1 回表示 (過去 30 日間)
Bananach
Bananach 2016 年 4 月 27 日

I would like my parfor loop to count how many of the indices have been handled and display this information during the computations. However, the first code below does not work as MATLAB forces $c$ to be temporary and the second does not work because MATLAB seems to create independent copies of the counting object for each worker.

First try:

c=0;
A=zeros(10,1);
parfor i=1:10
    A(i)=foo(i);
    c=c+1;
    c/10
end

--> Error when trying to run, because c is uninitialized.

Second try:

classdef progress <handle
    properties
        N;
        c;
    end
    methods 
        function obj=progress(N)
            obj.N=N;
            obj.c=0;
        end
        function count(obj)
           obj.c=obj.c+1;
           fprintf('%f',obj.c/obj.N);
        end
    end
    methods(Static)
      function test()
          c=progress(10);
          parfor( i=1:10,3)
             c.count();
          end
      end
  end
end

--> Output:

>> progress.test()
0.1000000.2000000.300000
0.1000000.2000000.3000000.400000
0.1000000.2000000.300000

回答 (0 件)

カテゴリ

Help Center および File ExchangeParallel for-Loops (parfor) についてさらに検索

製品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by