Matlab Parfor loop not working

I am fitting models to multiple datasets with fminsearch, and I am trying to do them in parallel. My code runs before the start of the loop parfor, but the parfor loop seems to take forever to get started! (The first line in parfor is not executed). No errors, Matlab is just busy.

I am starting a local cluster with 4 cores starting from matlabpool 4which seems to be starting normally. I run Matlab R2014b 64bit on Ubuntu 14.04.3, eight cores i7-3770K @ 3.50GHz, RAM 24GiB (most of them, of course, are not used).

EDIT

Here is the code that reproduces the problem!

file matlab_parfor_test_2

function f=matlab_parfor_test_2
f={}; 
for i=1:400
  a=@(p)i*p;             % make some functions depending on i
  b=@(p)a(p)+0;          % a function depending on this
  f=[f { @(p)b(p) }];    % create a list of i functions using this
end

file matlab_parfor_test_1

function matlab_parfor_test_1
f=matlab_parfor_test_2(); % create the functions
f=f(1:2);       % discard all but two functions
for i=1:2       % for each function                ('A')
  parfor j=1    % dummy parfor 
    tmp=f{i}; % just read a function from the cell ('B')
  end
end

, "" "" (.. , "enter" parfor) ,

returning 400 functions: 20 sec
          500 functions: 32 sec
          600 functions: 45 sec
          700 functions: 64 sec

, test_1 , 2 ! ?

, , , Matlab f. f=f(1:2)

f={f{1}, f{2}}; 

.

parfor for, , , 1 .

?

function fit_all
  models = createModelFunctions();  % creates cell of function handles
  data   = { [1 2 3], [1 2 3] };    % create 2 data sets
  for i = 1:length(models)
    fprintf('model %g\n',i);
    parfor j = 1:length(data)
      fprintf('data %g\n',j);
      tmp = models{i};  % if I comment this line out, it runs fine!
      % p(j) = fminsearch(@(p)models{j}(p,data{j}), [0 0]);
    end
  end

,

function models = createModelFunctions()
  models{1} = @(p,d) likelihoodfun(0,0,p,d);
  models{2} = @(p,d) likelihoodfun(1,0,p,d); 

function L = likelihoodfun(a,b,p,d)
  L = some maths here;

fit_all, model 1, data 1, data 2, model 2 .. , ,

model 1

: , , "", . 1 . parfor. ctrl+C , 3-

Operation terminated by user during parallel.internal.pool.serialize (line 21)
In distcomp.remoteparfor (line 69)
                serializedInitData = parallel.internal.pool.serialize(varargin);
In parallel_function>iMakeRemoteParfor (line 1060)
P = distcomp.remoteparfor(pool, W, @make_channel, parfor_C);
In parallel_function (line 444)
        [P, W] = iMakeRemoteParfor(pool, W, parfor_C);

, - , ... ,

 models={@sum,@sum}

. , ...

+4
1

, . Windows, Linux. , , , , ? , .

FYI, parpool matlabpool. , lagacy matlabpool ? , , .

, , .

0

Source: https://habr.com/ru/post/1616912/


All Articles