How to clean up nested resources in a parpool worker when using parallel.pool.Constant?
I have a handle class that is initialized in parallel pool workers through the usage of parallel.pool.Constant and a function handle. That handle class itself initializes another handle class that allocates some GPU memory in a mex file. By logging to a file I can see what methods of the nested class are executed. The issue is that the deleter of the nested class is never called when the parallel.pool.Constant object is deleted on the main thread (either explicitly by a call to delete(), or implicitly by returning from the function without returning the object). This is a problem because the gpu memory remains unreclaimed.
If I access the Value attribute of the parallel.pool.Constant instance from the main thread I can see that the gpu memory is allocated, then de-allocated when the parallel.pool.Constant object is deleted, as is expected.
Access to the inner handle is private so I can’t even do parallel.pool.Constant (@initFunc, @(obj) delete(obj.inner_handle))
The parallel.pool.Constant object is created as follows:
function obj = initOuterHandleClass()
obj = OuterHandleClass(some_args);
end
C = parallel.pool.Constant (@initOuterHandleClass, @(obj) delete(obj));
How can I ensure the correct destruction of the nested resources?
Edit:
I should add that:
1) the work is executed using a parfeval with the parallel.pool.Constant object being an argument of the function that gets executed.
2) The gpu memory is released when the paralel pool is shutdown.I have a handle class that is initialized in parallel pool workers through the usage of parallel.pool.Constant and a function handle. That handle class itself initializes another handle class that allocates some GPU memory in a mex file. By logging to a file I can see what methods of the nested class are executed. The issue is that the deleter of the nested class is never called when the parallel.pool.Constant object is deleted on the main thread (either explicitly by a call to delete(), or implicitly by returning from the function without returning the object). This is a problem because the gpu memory remains unreclaimed.
If I access the Value attribute of the parallel.pool.Constant instance from the main thread I can see that the gpu memory is allocated, then de-allocated when the parallel.pool.Constant object is deleted, as is expected.
Access to the inner handle is private so I can’t even do parallel.pool.Constant (@initFunc, @(obj) delete(obj.inner_handle))
The parallel.pool.Constant object is created as follows:
function obj = initOuterHandleClass()
obj = OuterHandleClass(some_args);
end
C = parallel.pool.Constant (@initOuterHandleClass, @(obj) delete(obj));
How can I ensure the correct destruction of the nested resources?
Edit:
I should add that:
1) the work is executed using a parfeval with the parallel.pool.Constant object being an argument of the function that gets executed.
2) The gpu memory is released when the paralel pool is shutdown. I have a handle class that is initialized in parallel pool workers through the usage of parallel.pool.Constant and a function handle. That handle class itself initializes another handle class that allocates some GPU memory in a mex file. By logging to a file I can see what methods of the nested class are executed. The issue is that the deleter of the nested class is never called when the parallel.pool.Constant object is deleted on the main thread (either explicitly by a call to delete(), or implicitly by returning from the function without returning the object). This is a problem because the gpu memory remains unreclaimed.
If I access the Value attribute of the parallel.pool.Constant instance from the main thread I can see that the gpu memory is allocated, then de-allocated when the parallel.pool.Constant object is deleted, as is expected.
Access to the inner handle is private so I can’t even do parallel.pool.Constant (@initFunc, @(obj) delete(obj.inner_handle))
The parallel.pool.Constant object is created as follows:
function obj = initOuterHandleClass()
obj = OuterHandleClass(some_args);
end
C = parallel.pool.Constant (@initOuterHandleClass, @(obj) delete(obj));
How can I ensure the correct destruction of the nested resources?
Edit:
I should add that:
1) the work is executed using a parfeval with the parallel.pool.Constant object being an argument of the function that gets executed.
2) The gpu memory is released when the paralel pool is shutdown. parallel computing MATLAB Answers — New Questions