2012-04-13 94 views
2

我目前正在開發一個項目,在這個項目中我必須並行運行多個耗時的MATLAB函數。爲了這個問題的目的,我們將該功能稱爲myfunc在MATLAB中使用parfor循環解決分段錯誤問題

myfunc使用MEX文件並以每3小時一次的隨機分段違例結束。我無法診斷分段錯誤,因爲它來自我自己編寫的專有API。但是,我知道它發生在MEX文件中,而且我也知道它不是確定性地與我可以更改的任何設置相關。

我想解決分割違規問題,並且理想情況下我還想繼續在MATLAB中使用parfor函數。我的想法,現在是到PARFOR環路內使用嘗試捕捉循環如下:

%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. 會不會是OK保持繼續運行多次重複這樣的連儘管MEX文件中存在分段違規問題?或者我應該清除內存/重新啓動matlabpool?我假設這應該不是問題,因爲分割侵犯是C.

  2. 有什麼辦法可以從parfor循環中「打破」嗎?

+0

段錯誤要麼崩潰MATLAB,或停止代碼,並給你一個選擇,以嘗試繼續,您應該僅做TR ❖保存您的數據。你不會得到一個例外。除修復非法內存訪問外,沒有其他辦法可以解決這個問題。 – angainor 2012-09-05 07:43:01

回答

0