Parallel Computing - COM Port Error

3 views (last 30 days)
Student_123
Student_123 on 21 Feb 2019
Edited: Student_123 on 22 Feb 2019
Hello Matlab Community,
I'm a moderate experienced Matlab User, but quite new in using the Parallel Computing Toolbox. I hope you guys could help me.
And sorry for my bad english ;)
Don't be afraid to ask me for more details.
Problem:
I have a Matlab code which uses a "Analog Discovery Box" (function generator and Osziloscope in one) to generate a function for an electric Powersupply which is attached to an electric circuit. The "Analog Discovery Box" also measures some value from the eletric circuit, but thats not important so far. Parallel to the "Analog Discovery Box", the electric Powersupply has to "switch on" by the matlab code. Generating the function (COM"XYZ") and switch on the power supply (COM3) runs parallel in a loop with maybe 20 iteratrions (Parallel Computing Toolbox). Therefore, i am using 2 USB Interfaces to generate the function and switch on the Powersupply.
This code was created by somebody other. So far so good. The code runs without any big issues.
BUT
Everytime, the code generates the function and switch on the Powersupply, it opens up a parallel pool and close it again in every iteration. This takes a lot of time.
The code looks like this:
while (i <= max_iteration)
% some regulation calculations
p = parpool(2);
spmd
% Interface "function generator" assigned to Worker 1
% Doing some stuff
% Interface "switch on power supply" assigned to Worker 2
% Doing some stuff
% evaluate measurment results
% some calculations
end
delete(p)
end
One of my first try was to delete "p = parpool(2); delete(p);" because its not necessary. "spmd" creates parpool automatically and is going to use workes if necessary (if the option is set in preferences). Now, the code runs much faster but it stops after 3, 4 or 7 iterations. Following Error meassage appears:
Error detected on worker 2.
...
...
Open failed: Cannot connect to the COM3 port. Possible reasons are another
application is connected to the port or the port does not exist.
Tried a lot of workarounds, without any solution. I think something is blocking COM3, maybe a windows application? Using the Software "Process Explorer" to search for a Application, which blocks COM3, did not find anything. If i kill the connection with matlab ("instrreset"), the code runs 1 iteration and stops with the same error measage
If i close the parallel pool and run the code again (new parallel pool created). All is fine.
Any ideas?
I would be glad if someone helps me :D

Accepted Answer

Edric Ellis
Edric Ellis on 22 Feb 2019
My guess is that your code inside the spmd block performs some initialization that either should be done only once per MATLAB process, or else perhaps isn't getting cleaned up correctly.
One way of addressing this sort of thing is to use parallel.pool.Constant - this lets you allocate a resource once, and then use it in multiple spmd blocks or parfor loops.
Consider the example of a file handle - in that case, you don't want to keep opening the same file handle multiple times. So, you could do this:
parpool(); % Open the pool
% Create the resource - in this case I'm using spmd
spmd
fh = fopen(fullfile(tempdir, sprintf('log_%d.txt', labindex)), 'wt');
end
% Build the constant
fhConst = parallel.pool.Constant(fh);
% Use the constant
for idx = 1:10
spmd
fprintf(fhConst.Value, 'Iteration %d on lab %d\n', idx, labindex);
end
end
% Clean up
spmd
fclose(fhConst.Value);
end
Perhaps something like this pattern can help.
  5 Comments
Edric Ellis
Edric Ellis on 22 Feb 2019
Edited: Edric Ellis on 22 Feb 2019
Ah, I missed a nuance from your previous code. Perhaps it helps to say:
spmd
if labindex == 1
Powersupply = serial('COM3','BaudRate',9600);
fopen(Powersupply);
else
Powersupply = [];
end
end
fhConst = parallel.pool.Constant(Powersupply);
...
and then ensure that you only try to write to the serial object from labindex == 1.
Student_123
Student_123 on 22 Feb 2019
Edited: Student_123 on 22 Feb 2019
It works !
Thank you very much !!!

Sign in to comment.

More Answers (0)

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!