Writing to .mat simultaneously with parfor loop
이전 댓글 표시
I'm running the test for 100 iteration and each iteration will save the the output into the same .mat file. At the moment, I'm running the test without any problem for over 100 iteration using Parfor loop. However, I'm worry about will the .mat file get corrupted when multiple workers are writing the output into the same file? I can only try with the maximum of 4 workers, but my main concern is, will this become a problem when the test is run for writing maybe 10 output simultaneously in the same file? Just need to make sure this is safe to do.
*Writing save inside a parfor loop is not possible. saveData function is use to write the output from the function1 to one .mat.
%Sample code
%main
parfor i = 1 : 100
function1(i1,i2,i3);
...
end
%function1
function function1(i1,i2,i3)
...
saveData(x, y, z);
end
%saveData
function saveData(x,y,z)
...
if ~exist(iMatFile, 'file')
save(iMatFile,GetVariableName(iTtiNum, bFd), '-v7.3');
else
save(iMatFile,GetVariableName(iTtiNum, bFd),'-append');
end
end
채택된 답변
추가 답변 (1개)
Walter Roberson
2022년 8월 10일
1 개 추천
This is a valid concern. save() does not promise to be thread-safe
If I recall correctly, a few weeks ago there was a post from someone who was encountering corruption under these circumstances. I believe that they ended up writing to separate files. Only one file per worker is required, not one file per iteration.
카테고리
도움말 센터 및 File Exchange에서 Parallel for-Loops (parfor)에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!