FPRINTF making mistakes when inside a PARFOR loop

조회 수: 17 (최근 30일)
Alex van Tuyll
Alex van Tuyll 2020년 1월 22일
답변: Alex van Tuyll 2020년 2월 3일
Hi there,
I am running a model that writes MATLAB code to a file every time it is run, and then runs that code. This model is run 5 times, separately, so I decided to replace the for-loop for this with a parfor-loop. To avoid mistakes/overlaps with writing to the file, I have made the model write to 5 different files. In summary, the pseudocode is this:
parfor i = 1:5
fprintf("model"+i+".m", "lots_of_code_gets_written_in_here"); % write the code for the model
if i == 1
model1(arg1,arg2); % run the model
elseif i == 2
model2(arg1,arg2); % run the model
elseif i == 3
model3(arg1,arg2); % run the model
elseif i == 4
model4(arg1,arg2); % run the model
elseif i == 5
model5(arg1,arg2); % run the model
end
end
This solution used to work perfectly. The parfor-loop would write code to 5 different files and run these 5 files separately and in parallel. Unfortunately, somehow it no longer works. What happens now is that when writing the code to the file, there are mistakes in the code - basic syntax mistakes like leaving out brackets, incomplete lines, etc. As a result, this written code no longer runs and I get an error.
I replaced the parfor-loop with a for-loop again, and everything works fine. There are no errors in the code written by MATLAB. But I would still like to use the parfor-loop because of the increased speed.
Any idea why having a parfor-loop might cause these errors and what I can do about this?
Would greatly appreciate any tips!
Best,
Alex
EDIT: this problem seems to be to do with the fact that these files are being written to a shared hard drive. At least, when I move my files back there, the problem comes back. Does this seem plausible and is there nonetheless a solution?
  댓글 수: 4
Mohammad Sami
Mohammad Sami 2020년 1월 26일
If your code generation part is not taking a lot of time, you can consider moving out to a for loop. This will run first and create your code files. then you can run it in the parfor loop.
Andrew Janke
Andrew Janke 2020년 1월 31일
When you say "when I move my files back there, the problem comes back" does that mean the problem occurs even with a regular for loop (and not just in a parfor) when you are writing to files on the network drive?
Your pseudocode does look reasonable. Can you post actual code that will reproduce the issue, maybe from a cut-down version of your program?
What is the model built in? Is it just another piece of Matlab code?

댓글을 달려면 로그인하십시오.

답변 (1개)

Alex van Tuyll
Alex van Tuyll 2020년 2월 3일
Thanks for your replies!
I ended up using Mohammed's solution and that works.
However, yes, Andrew, the problem still occurs when writing files on the network drive, but only with parfor.
The whole model is purely MATLAB.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by