MATLAB Answers

Whats the best command to write to file inside parfor loop?

111 views (last 30 days)
Chris on 22 Mar 2012
Commented: Peng Li on 2 Jun 2020
I want to write results to a file within a parfor loop and ideally append the results. What is the best command that can handle multiple writes potentially at the same time?

Accepted Answer

Edric Ellis
Edric Ellis on 23 Mar 2012
Inside PARFOR, you can still access the current task, and get its ID - that will be unique. My "Worker Object Wrapper" has an example how you could use that to open a file on each worker with a persistent file handle so that each worker can write its own file when executing a PARFOR loop.
Peng Li
Peng Li on 2 Jun 2020
You may take advantage of spmd block to merge these temporal files as well. The variable A (a composite) stores the information. For example:
tblLab = readtable(A, 'ReadVariableNames', 0);
tbl = vertcat(tblLab{:});
writetable(tbl, 'OneFatFile.txt');
you can delete temporal files as well within the spmd block, if you want.

Sign in to comment.

More Answers (4)

Jason Ross
Jason Ross on 22 Mar 2012
Multiple writes to the same file are a quick route to a corrupt file. You need to come up with a plan to assemble the file where only one write is done at a time. For example, you could write a collection of small files that are independently named and then have code that concatenates the data into the one result file.
The tempname function can return you a unique name, and then you can combine it with other information, such as hostname, lab index, time opened, etc to build up the filename.
When you are dealing with files you also need to make sure to pay attention to the return codes of fopen, fclose, etc. Duplicate filenames, read-only filesystems and full filesystems happen, and you should think about how you will handle these conditions when they occur.
Edric Ellis
Edric Ellis on 21 Jul 2014
While that might work, you're somewhat at the mercy of the operating system as to whether it gives you exclusive write access; plus your results will come out in a non-deterministic order.

Sign in to comment.

Jill Reese
Jill Reese on 22 Mar 2012
If you are able to run your code inside an spmd block instead of via parfor, then you will be able to use the labindex variable to create a unique file name for each worker to write to. That is the best option.

Konrad Malkowski
Konrad Malkowski on 22 Mar 2012
Are you planning on writing data from multiple workers to a single file within PARFOR? If so, then there are no commands that will allow you to do it.

Fernando García-García
Fernando García-García on 6 Dec 2014
Edited: Fernando García-García on 6 Dec 2014
Hello everyone,
Well, I'm planning to do what you said, Konrad. What if I do the following?
parfor i=1:N
% do something very time-comsuming, like hours or so
while checkIfOpen(filename)
pause(1); % i don't mind waiting for just 1 second
fprintf(fileID,...); % write whatever, very quick as it's not much data
function isOpen=checkIfOpen(filename)
for j=1:numel(openIDs)
if ~isempty(idxStrFind) % non empty
if idxStrFind(end)==size(filenameTEMP)-size(filename)+1
% found at the end of the entire path
isOpen = true;
Note 1: I don't mind if the writing is not in deterministic order.
Note 2: I would have never expected that, being such a long processing time for the task (and this time varying randomly from iteration to iteration somewhere in the range of minutes) compared to the very brief write operation (milliseconds)... that there was the enormous coincidence of two workers trying to write to file at the same time, but it did occur! Should have bought lottery, hehehe.
Note 3: Code corrected.
Note 4: I'm not sure how to actually check if this code behaves as expected.
Fernando García-García
Fernando García-García on 8 Dec 2014
Ok Edric, I found a post of yours in StackOverflow confirming that the approach based on global variables wouldn't have worked fine either.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!