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

조회 수: 12 (최근 30일)
lingyan
lingyan 2013년 7월 26일
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.

채택된 답변

Friedrich
Friedrich 2013년 7월 26일
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)

추가 답변 (1개)

Matt J
Matt J 2013년 7월 26일
편집: Matt J 2013년 7월 26일
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

카테고리

Help CenterFile Exchange에서 Parallel for-Loops (parfor)에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by