-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdetach_shmat.m
27 lines (27 loc) · 1.04 KB
/
detach_shmat.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
function detach_shmat(host_or_dev_struct)
% Detach and free host or device memory, all attached variables will set to <missing> (it must be called after executed, otherwise it will cause memory leakage)
% Example:
% a = randn(5);
% b = a + eye(5);
% host = create_shmat(a, b);
% clear('a', 'b'); % remove the existed variable
% dev = attach_shmat(host); % usually executed in parfor, etc.
% disp(data.a); disp(data.b);
% detach_shmat(dev);
% detach_shmat(host);
if ~isstruct(host_or_dev_struct)
error('Parameter host_or_dev_struct must be an instance of struct');
end
fields = fieldnames(host_or_dev_struct);
for i = 1:length(fields)
shmat_obj = host_or_dev_struct.(fields{i});
% if isequal(class(shmat_obj), 'shared_matrix')
% % remove device-side variables from caller (if exists)
% if evalin('caller', sprintf('exist(''%s'', ''var'');', fields{i}))
% % assignin('caller', fields{i}, missing);
% evalin('caller', sprintf('clear(''%s'');', fields{i}));
% end
% end
shmat_obj.detach();
end
end