title: Matlab Parfor loop not working
body: <p>I'm fitting a models to several datasets with <code>fminsearch</code>, and I'm trying to do them in parallel. My code is running up to the start of the <code>parfor</code> loop, but <strong>the parfor loop seems to take forever</strong> to start! (The first line in the parfor is not executed). There are no errors, matlab just remains "busy".</p>
  
  <p>I'm running on local cluster with 4 cores, started with <code>matlabpool 4</code>, which appears to start up fine. I'm running Matlab R2014b 64bit on Ubuntu 14.04.3, eight core i7-3770K @ 3.50GHz, 24GiB RAM (most is unused of course). </p>
  
  <h2>EDIT</h2>
  
  <p>Here is code that reproduces the problem!</p>
  
  <p>file <code>matlab_parfor_test_2</code></p>
  
  <pre><code>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
  </code></pre>
  
  <p>file <code>matlab_parfor_test_1</code></p>
  
  <pre><code>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
  </code></pre>
  
  <p>The time taken to get from 'A' to the first 'B' (ie. time taken to "enter" the <code>parfor</code>) on my machine is</p>
  
  <pre><code>returning 400 functions: 20 sec
            500 functions: 32 sec
            600 functions: 45 sec
            700 functions: 64 sec
  </code></pre>
  
  <p>This is very odd, because in <code>test_1</code> I discard all but 2 of those functions! Why should the discarded functions cause slowing? </p>
  
  <p>I thought perhaps matlab is not actually deleting the unwanted functions in <code>f</code>. So I tried replacing <code>f=f(1:2)</code> with</p>
  
  <pre><code>f={f{1}, f{2}}; 
  </code></pre>
  
  <p>but this also did not help.</p>
  
  <p>If I replace the <code>parfor</code> with <code>for</code>, then of course it takes under 1ms to execute.</p>
  
  <p>Any ideas??</p>
  
  <h2>OLD VERSION OF QUESTION</h2>
  
  <pre><code>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
  </code></pre>
  
  <p>the model functions are created in another file,</p>
  
  <pre><code>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;
  </code></pre>
  
  <p>Running <code>fit_all</code>, I expected to see a list of <code>model 1</code>, <code>data 1</code>, <code>data 2</code>, <code>model 2</code> etc.. The output I'm getting is </p>
  
  <pre><code>model 1
  </code></pre>
  
  <p>then the thing just stops: no prompt, matlab says "busy", UI and OS are responsive as usual. System monitor shows only 1 core is active. It never makes it into the <code>parfor</code>.
  If I press <code>ctrl+C</code> at this point, after a 3-minute delay I get</p>
  
  <pre><code>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&gt;iMakeRemoteParfor (line 1060)
  P = distcomp.remoteparfor(pool, W, @make_channel, parfor_C);
  In parallel_function (line 444)
          [P, W] = iMakeRemoteParfor(pool, W, parfor_C);
  </code></pre>
  
  <p>If I comment out the line indicated, it works -- so the problem seems to be when I access the model functions... Similarly, it works fine if I change models to</p>
  
  <pre><code> models={@sum,@sum}
  </code></pre>
  
  <p>i.e. it's just when I'm using function handles from another file...</p>