Clear Filters
Clear Filters

How do I use objects with inheritance structures in a `parfor` loop in Matlab?

16 views (last 30 days)
I am trying to parallelize a simulation that uses a custom library. Within the 'parfor' loop I am using, I receive the following error:
Error using sensor/readdata
Unrecognized field name "sensorDevice".
The object is an instance of Sensor that inherits its .sensorDevice field from its parent class. The method readdata calls on the inherited field. This works fine in a for loop. The transparency requirements for variable definitions and the corresponding documentation emphasize the ability of all variables to be machine-readable, but I do not follow why the parfor loop cannot trace the pointers to the parent classes' fields and methods. Re-writing the class in a flattened structure is not an option due to the time that would be required to do so.
How do I use methods and fields from a parent class in an instance of a child class in a parfor loop?
  5 Comments
Jeff Miller
Jeff Miller on 27 Jul 2024
I have no helpful suggestion but feel that I should comment on this part of your original post: "I do not follow why the parfor loop cannot trace the pointers to the parent classes' fields and methods."
With some pretty simple class/inheritance hierarchies, I use parfor loops like this all the time without any problems. That is, my parfor calls a child's method, and the child's method references its parent's properties and calls its parent's methods. So at least in some situations MATLAB can trace the pointers to the parents as needed.
If you can create a minimal example of a class structure where the tracing fails, it may be possible to spot just what determines whether tracing succeeds or fails.
Aditya Saikumar
Aditya Saikumar on 6 Aug 2024
Hi Joseph,
As already mentioned in the comments, “addAttachedFiles” does resolve the error you were getting about the field name “sensorDevice” not being recognized. However, transparency violation error is a different one.
Transparency violation is usually caused by calling methods in “parfor” body which access or modify the caller’s workspace. The “readdata” implementation could be doing just that. For example, the following code will cause transparency violation.
class code:
classdef sensor
methods(Access=public)
function obj = readdata(obj, wave)
% ...
evalin('caller', 'x = 35'); % modifies caller workspace
% ...
end
end
end
parfor loop:
parfor i = 1:100
% ...
gndReader = readdata(gndReader, incomingWave);
% ...
end
You can get around this issue by simply wrapping this function call in another function as follows.
function code:
function ret = myReadData(gndReader, incomingWave)
ret = readdata(gndReader, incomingWave);
end
parfor loop:
parfor i = 1:100
% ...
gndReader = myReadData(gndReader, incomingWave);
% ...
end
I hope this helps!
Aditya

Sign in to comment.

Answers (0)

Categories

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

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!