Parallel execution using parfeval does not modify input handle object as expected
15 views (last 30 days)
Show older comments
I'm trying to implement parallelism - I want to run my function in parallel, and it's supposed to modify its input OOP object asynchronously.
Here is a simplified test implementation that runs, but the input object is NOT modified by the function calls.
if isempty(gcp('nocreate'))
% Start a new parallel pool
parpool();
end
futs= Futures();
nodeCount = NodeCount(1);
for i=1:5
f=parfeval(@Testparffun,0,nodeCount);
futs.append(f)
end
If the parfeval is replaced by the normal function call, it works as expected. With parfeval, the code completes without errors, but nodeCount is not modified .
Here is also the code for Testparffun and NodeCount:
function Testparffun(counter)
counter.increment;
pause(5);
end
And NodeCount:
classdef NodeCount < handle
%NODECOUNT Summary of this class goes here
% Detailed explanation goes here
properties
count
end
methods
function obj = NodeCount(inputArg1)
%NODECOUNT Construct an instance of this class
% Detailed explanation goes here
obj.count =inputArg1 ;
end
function increment(obj)
%METHOD1 Summary of this method goes here
% Detailed explanation goes here
obj.count = obj.count + 1;
end
end
end
The goal of this test would be that each async call increments the value of nodeCount.count by 1, but it does nothing. I understand that I would need to implement some locks to avoid race conditions, etc. but even if the loop makes just one call, it still doesn't affect the object. Any ideas?
0 Comments
Answers (1)
Steven Lord
on 4 May 2024
See this documentation page for an explanation and a description of how I think you can achieve the behavior you're looking for.
0 Comments
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!