필터 지우기
필터 지우기

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

조회 수: 57 (최근 30일)
Joseph Conroy
Joseph Conroy 2024년 7월 23일
댓글: Aditya Saikumar 2024년 8월 6일 10:29
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
Jeff Miller
Jeff Miller 2024년 7월 27일 0:10
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 2024년 8월 6일 10:29
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

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Parallel for-Loops (parfor)에 대해 자세히 알아보기

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by