在MATLAB中使用parfor循环解决分段错误

时间:2012-04-13 01:42:32

标签: matlab segmentation-fault mex

我目前正在开展一个项目,我必须并行运行多次重复耗时的MATLAB函数。出于这个问题的目的,我们将函数称为myfunc

myfunc使用MEX文件,每3小时最终会发生一次随机分段违规。我无法诊断分段错误,因为它源于我自己不编码的专有API。但是,我知道它发生在MEX文件中,我也知道它与我可以更改的任何设置没有确定性相关。

我想解决分段违规问题,理想情况下我还想继续在MATLAB中使用parfor函数。我现在的想法是在parfor循环中使用try catch循环,如下所示:

    %create an output cell to store nreps of output from 'myfunc'
    output = cell(1,nreps) 

    %create a vector to keep track of how many runs finish successfully without the error
    successfulrun = zeros(1,nreps);

    % run myfunc in parallel
    parfor i = 1:nreps
       try
        output{i}
        successfulrun(i) = true
       end
    end

    %rerun experiments that did not end up successfully
    while sum(successulruns) < nreps

      %count number of experiments to rerun and initialize variables to hold those results
      reps_to_rerun = find(successfulruns == 0);
      nreps_to_rerun = sum(reps_to_rerun);
      newoutput = cell(1,nreps_to_rerun);
      newsuccessfulrun = zeros(1,nreps_to_rerun)

      %rerun experiments
      parfor i = 1:nreps_to_rerun
         try
          newoutput{i};  
          newsuccessfulrun = true;  
         end  
      end

     %transfer contents to larger loop
     for i = 1:nreps_to_rerun

        rerun_index =  reps_to_rerun(i);
        successfulrun(rerun_index) = newsuccessfulrun(i)

        if newsuccessfulrun(i)
            output{i} = newoutput{i};
        end 
    end
end

我的问题是:

  1. 即使MEX文件中存在分段违规,仍然可以继续运行更多这样的重复吗?或者我应该清除内存/重启matlabpool?我假设这不应该是问题,因为分段违规是在C.

  2. 有没有办法“打破”parfor循环?