Writing to .mat simultaneously with parfor loop
Afficher commentaires plus anciens
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
Réponse acceptée
Plus de réponses (1)
Walter Roberson
le 10 Août 2022
1 vote
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.
Catégories
En savoir plus sur Parallel for-Loops (parfor) dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!