Ken*_*eld 8 oop parallel-processing matlab mex copy-constructor
我正在尝试使用此处概述的方法将C++类包装在matlab mex包装器中.基本上,我有一个初始化mex文件,它返回一个C++对象句柄:
handle = myclass_init()
Run Code Online (Sandbox Code Playgroud)
然后我可以将它传递给另一个myclass_amethod使用句柄调用类方法的mex文件(例如),然后最终myclass_delete释放C++对象:
retval = myclass_amethod(handle, parameter)
myclass_delete(handle)
Run Code Online (Sandbox Code Playgroud)
为了便于使用,我把它包装在一个MATLAB类中:
classdef myclass < handle
properties(SetAccess=protected)
cpp_handle_
end
methods
% class constructor
function obj = myclass()
obj.cpp_handle_ = myclass_init();
end
% class destructor
function delete(obj)
myclass_delete(obj.cpp_handle_);
end
% class method
function amethod(parameter)
myclass_amethod(obj.cpp_handle_, parameter);
end
end
end
Run Code Online (Sandbox Code Playgroud)
这在非并行代码中工作正常.但是,只要我从一个内部调用它parfor:
cls = myclass();
parfor i = 1:10
cls.amethod(i)
end
Run Code Online (Sandbox Code Playgroud)
我得到一个段错误,因为类的副本是在parfor循环中(在每个worker中)制作的,但由于每个worker都是一个单独的进程,因此不会复制C++对象实例,从而导致指针无效.
我最初尝试检测每个类方法何时在parfor循环中运行,并且在这些情况下也重新分配C++对象.但是,由于无法检查是否已为当前工作程序分配了对象,因此会导致多次重新分配,然后只有一次删除(当工作人员退出时)导致内存泄漏(请参阅附录中的附录)问题详情).
matlab.mixin.Copyable在C++中,处理它的方法是复制构造函数(这样只有在复制包装器MATLAB类时才重新分配C++对象).快速搜索会显示matlab.mixin.Copyable类类型,它似乎提供了所需的功能(即MATLAB句柄类的深层副本).因此,我尝试了以下方法:
classdef myclass < matlab.mixin.Copyable
properties(SetAccess=protected)
cpp_handle_
end
methods
function obj = myclass(val)
if nargin < 1
% regular constructor
obj.cpp_handle_ = rand(1);
disp(['Initialized myclass with handle: ' num2str(obj.cpp_handle_)]);
else
% copy constructor
obj.cpp_handle_ = rand(1);
disp(['Copy initialized myclass with handle: ' num2str(obj.cpp_handle_)]);
end
end
% destructor
function delete(obj)
disp(['Deleted myclass with handle: ' num2str(obj.cpp_handle_)]);
end
% class method
function amethod(obj)
disp(['Class method called with handle: ' num2str(obj.cpp_handle_)]);
end
end
end
Run Code Online (Sandbox Code Playgroud)
像上面那样测试这个类,即:
cls = myclass();
parfor i = 1:10
cls.amethod(i)
end
Run Code Online (Sandbox Code Playgroud)
结果输出:
Initialized myclass with handle: 0.65548
Class method called with handle: 0.65548
Class method called with handle: 0.65548
Class method called with handle: 0.65548
Class method called with handle: 0.65548
Class method called with handle: 0.65548
Class method called with handle: 0.65548
Class method called with handle: 0.65548
Class method called with handle: 0.65548
Class method called with handle: 0.65548
Class method called with handle: 0.65548
Deleted myclass with handle: 0.65548
Deleted myclass with handle: 0.65548
Deleted myclass with handle: 0.65548
Run Code Online (Sandbox Code Playgroud)
换句话说,似乎在为parfor生成worker时不会调用复制构造函数.有没有人对我做错了什么有任何指示,或者在复制MATLAB包装类时是否有某种方法可以实现重新初始化C++对象句柄的行为?
仅供参考,这是我在工作中使用时重新分配的替代方法:
classdef myclass < handle
properties(SetAccess=protected)
cpp_handle_
end
methods
function obj = myclass(val)
obj.cpp_handle_ = rand(1);
disp(['Initialized myclass with handle: ' num2str(obj.cpp_handle_)]);
end
% destructor
function delete(obj)
disp(['Deleted myclass with handle: ' num2str(obj.cpp_handle_)]);
end
% class method
function amethod(obj)
obj.check_handle()
disp(['Class method called with handle: ' num2str(obj.cpp_handle_)]);
end
% reinitialize cpp handle if in a worker:
function check_handle(obj)
try
t = getCurrentTask();
% if 'getCurrentTask()' returns a task object, it means we
% are running in a worker, so reinitialize the class
if ~isempty(t)
obj.cpp_handle_ = rand(1);
disp(['cpp_handle_ reinitialized to ' num2str(obj.cpp_handle_)]);
end
catch e
% in case of getCurrentTask() being undefined, this
% probably simply means the PCT is not installed, so
% continue without throwing an error
if ~strcmp(e.identifier, 'MATLAB:UndefinedFunction')
rethrow(e);
end
end
end
end
end
Run Code Online (Sandbox Code Playgroud)
和输出:
Initialized myclass with handle: 0.034446
cpp_handle_ reinitialized to 0.55625
Class method called with handle: 0.55625
cpp_handle_ reinitialized to 0.0048098
Class method called with handle: 0.0048098
cpp_handle_ reinitialized to 0.58711
Class method called with handle: 0.58711
cpp_handle_ reinitialized to 0.81725
Class method called with handle: 0.81725
cpp_handle_ reinitialized to 0.43991
cpp_handle_ reinitialized to 0.79006
cpp_handle_ reinitialized to 0.0015995
Class method called with handle: 0.0015995
cpp_handle_ reinitialized to 0.0042699
cpp_handle_ reinitialized to 0.51094
Class method called with handle: 0.51094
Class method called with handle: 0.0042699
Class method called with handle: 0.43991
cpp_handle_ reinitialized to 0.45428
Deleted myclass with handle: 0.0042699
Class method called with handle: 0.79006
Deleted myclass with handle: 0.43991
Deleted myclass with handle: 0.79006
Class method called with handle: 0.45428
Run Code Online (Sandbox Code Playgroud)
从上面可以看出,在工作者中运行时确实现在发生了重新分配.但是,无论C++类重新分配了多少时间,析构函数只会为每个worker调用一次,从而导致内存泄漏.
loadobj以下作品:
classdef myclass < handle
properties(SetAccess=protected, Transient=true)
cpp_handle_
end
methods(Static=true)
function obj = loadobj(a)
a.cpp_handle_ = rand(1);
disp(['Load initialized encoder with handle: ' num2str(a.cpp_handle_)]);
obj = a;
end
end
methods
function obj = myclass(val)
obj.cpp_handle_ = rand(1);
disp(['Initialized myclass with handle: ' num2str(obj.cpp_handle_)]);
end
% destructor
function delete(obj)
disp(['Deleted myclass with handle: ' num2str(obj.cpp_handle_)]);
end
% class method
function amethod(obj)
disp(['Class method called with handle: ' num2str(obj.cpp_handle_)]);
end
end
end
Run Code Online (Sandbox Code Playgroud)