FPRINTF making mistakes when inside a PARFOR loop
10 views (last 30 days)
Show older comments
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 Comments
Mohammad Sami
on 26 Jan 2020
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
on 31 Jan 2020
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?
Answers (1)
See Also
Categories
Find more on Loops and Conditional Statements 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!