私は現在、時間のかかる 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
私の質問は次のとおりです。
MEX ファイル内にセグメンテーション違反があったとしても、このような反復をさらに実行し続けてよいでしょうか? または、メモリをクリアするか、matlabpool を再起動する必要がありますか? セグメンテーション違反は C にあったため、これは問題にはならないと思います。
parfor ループから「抜け出す」方法はありますか?