Parallel execution using parfeval does not modify input handle object as expected
15 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponses (1)
Steven Lord
le 4 Mai 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 commentaires
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!