running a file from inside a parfor loop

17 views (last 30 days)
I'm doing a parameter sweep on a transfer function which is constructed from a lenghty statement that I'm printing to a file from sympy, which calculates the transfer function based on workspace variables. Without parallel computing, I just use
run('Gcl_tf.m')
I've tried to read the file to a string and eval'ing it from the parfor loop, unfortunately eval doesn't seem to like parfor. I could find information on how to attach a file to a parallel pool, but I can't figure out how to execute a file from those attached files.
I could of course just copy the file contents into my script, that works, but the line is so long that MATLAB doesn't display it properly. And in case I update the file, I'd have to copy it again.

Accepted Answer

Thiago Henrique Gomes Lobato
The main problem is that parfor can not tell from which workspace the variables should be used since you have many. You can solve this problem by actually creating a function from your file, something as:
function varargout = Gcl_tf(varargin)
...
% Your function
end
And then, from your parfor code, call the function passing only the variables you need
parfor idx=1:Iterations
...
TransferFunction = Gcl_tf(input);
...
end
  2 Comments
Malte Eggers
Malte Eggers on 29 Mar 2020
If I just copy the contents of the file into the code, it works. The variables I created outside the parfor loop can be used inside it without any problems. I prefer to have an option that acts as if I just had the file contents in that line.
The function option looks like an acceptable alternative, but I prefer not to use it because it makes both code generation from sympy more complicated and also adds more code to my matlab script. I have a lot of variables and I may add or remove some in the future. It will be annoying to keep track of.
Obviously, for this one case it would be faster that way rather than trying to find a way around it, but I'll probably be using something like this more often in the future.
Thiago Henrique Gomes Lobato
Unfortunately you will not be able to have a solution like this in parfor due to the complication in finding the right workspace for each loop. The fact that it works without parfor actually doesn't mean much when you try to do things in parallel. I would strongly advise you to use the function approach even if you plan to make changes in the future since it is so much cleaner from a programming perspective. If you really want to call an extra file the only workaround I can think around is to trick the parfor with something like this:
parfor ...
workspace = runFile(workspace)
end
function workspace = runFile(workspace)
run('Gcl_tf.m')
end
You'll have to define the variable/structure workspace, do the necessary changes in your code and the code will be way more complex, but it will work in the way you want.

Sign in to comment.

More Answers (0)

Categories

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

Products


Release

R2017b

Community Treasure Hunt

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

Start Hunting!