Clear Filters
Clear Filters

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

36 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?
  4 Comments
Joseph Conroy
Joseph Conroy about 18 hours ago
I would appreciate your patience with my naive questions, but I am still not sure what is going on here. I do not obtain any errors regarding the addAttachedFiles syntax, but this does not resolve the transparency issue.
I obtain the following error message:
Error using sensor/readdata
Transparency violation error.
See Workspace Transparency in MATLAB Statements.
dataStruct = zeros(128,100); % initialize data array
delete(gcp('nocreate')); % clear out previous pool
parpool(4); poolobj = gcp; % get 4 workers for 4 cores
addAttachedFiles(poolobj, 'CustomSensors') % Attach my library of custom simulation objects
% CustomSensors is a directory containing class definitions, each in their
% own folder. These class definition folders contain .m files defining the
% classes
parfor i = 1:100
gndReader = seismicSensor(depth,size,shape);% instantiate custom sensor object
seisSim = waveGenerator(region, modelName);% instantiate custom generator object
randCoeffs = rand(128,1);
incomingWave = seisSim(randCoeffs);
%'collect' comes from seismicSensor's parent class
gndReader = readdata(gndReader, incomingWave); % Referencing a parent's method causes the issue
% % % % % % % % % % % % % % % % % % % % % % %
data = readout(gndReader);
dataStruct(:,i) = data;
end
The tutorials and documentation I have found do not refer to this particular problem. I understand that each worker requires access to the complete workspace as well as any associated fields and methods, but I do not follow why the call to addAttachedFiles does give each worker access to the fields, methods, and class structures contained within the folders and subfolders of the 'CustomSensors' directory.
Jeff Miller
Jeff Miller about 17 hours ago
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.

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!