In parfor, how to create a temp folder for every worker

12 views (last 30 days)
Hallo all,
Summarized is: I use parfor to running a simulink model, after running of the model come out some temp files. But because multiple workers attempt to write to the same file, that can sometimes an error results. So i remove these files every time after running of this model. But by next time this model is called, some files will compiled and run again (e.g. some C files), so that the running time of this model is very long. (I don't know well about simulink, so maybe i described not exactly)
My idea is to create a temp folder for each worker, so that there is no conflict between workers. And all files for the model will only once installed at the beginning it's called. So will time saved.
(update1) i wrote code like this
tmpdir = tempname
mkdir(tmpdir)
cd(tmpdir)
sim('XRSimWWPen01');
so there is a different folder for each worker and each loop. What i want to do is, that i create only four folder(four core) for four workers.

Accepted Answer

Friedrich
Friedrich on 26 Jul 2013
Hi,
I would use the process ID for that:
matlabpool open 4
parfor i=1:20
w=getCurrentWorker;
mydir = fullfile(tempdir,['myfolder',num2str(w.ProcessId)]);
disp(mydir)
end
(Note: tempdir is a MATLAB function not the variable from your example)

More Answers (1)

Matt J
Matt J on 26 Jul 2013
Edited: Matt J on 26 Jul 2013
so there is a different folder for each worker and each loop.
You can do this using SPMD, but I don't think you should do it through PARFOR, since operations within a parfor loop are expected to be worker-independent. You can't count on parfor loop iterations always being assigned to the same worker or done in the same order each time the loop is called.
So maybe you can do something like this, instead:
spmd
mkdir(['myfolder' num2str(labindex)])
end
spmd
back=cd;
cd(['myfolder' num2str(labindex)]);
%do stuff...
cd(back)
end

Categories

Find more on Parallel for-Loops (parfor) in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!