Timer callback function does not execute parfeval?
34 views (last 30 days)
Show older comments
Hello,
I am having trouble with using parfeval in the callback function of a timer. I am not sure if it is even possible. The goal of my code is to be able to run 2 functions on 2 different workers in my parallel pool. These 2 functions need to be executed on these 2 different workers every so often / periodically. Hence I am trying to use a timer. The code below is a simplified version of my actual code, but it captures the functionality I require.
pool = parpool(2);
timer1 = timer('TimerFcn', @myCallbackFunction, 'ExecutionMode', 'fixedRate', 'Period', 1.0);
start(timer1);
pause(5);
stop(timer1);
delete(timer1);
delete(pool);
function parallel1()
disp('done worker 1');
end
function parallel2()
disp('done worker 2');
end
function myCallbackFunction()
parfeval(pool, @parallel1);
parfeval(pool, @parallel2);
end
When running this code I obtain the following message in the command window:
% Error while evaluating TimerFcn for timer 'timer-1'
% Too many input arguments.
I have tried multiple variations of the code, changing the way in which I ensure that "pool" is available to the myCallbackFunction for the timer:
timer1 = timer('TimerFcn', {@myCallbackFunction, pool}, 'ExecutionMode', 'fixedRate', 'Period', 1.0);
...
function myCallbackFunction(~, ~, pool)
parfeval(pool, @parallel1);
parfeval(pool, @parallel2);
end
Also tried:
timer1 = timer('TimerFcn', @(~,~) myCallbackFunction(pool), 'ExecutionMode', 'fixedRate', 'Period', 1.0);
...
function myCallbackFunction(pool)
parfeval(pool, @parallel1);
parfeval(pool, @parallel2);
end
For all of these variations I receive the same error message of too many input arguments or this error:
%Error while evaluating TimerFcn for timer 'timer-1'
%Unrecognized function or variable 'pool'.
If someone could please help me figure out whether I am doing something wrong in terms of syntax, wrong use of parfeval or whether my approach to periodically computing two functions in parallel is incorrect.
Thank you!
0 Comments
Answers (1)
LeoAiE
on 23 Jul 2023
I think the issue here is that the timer function is not able to recognize the pool variable in the myCallbackFunction. The variable pool is in the workspace but it is not being passed into myCallbackFunction when the timer calls it. The solution to this is to define pool as a global variable.
global pool;
pool = parpool(2);
timer1 = timer('TimerFcn', @myCallbackFunction, 'ExecutionMode', 'fixedRate', 'Period', 1.0);
start(timer1);
pause(5);
stop(timer1);
delete(timer1);
delete(pool);
function parallel1()
disp('done worker 1');
end
function parallel2()
disp('done worker 2');
end
function myCallbackFunction(~,~)
global pool;
parfeval(pool, 1, @parallel1);
parfeval(pool, 1, @parallel2);
end
In this code, global pool is declared to make pool a global variable. This means that it is available in all functions. This is necessary since the timer function and myCallbackFunction need access to pool.
Furthermore, you must specify the number of output arguments when using parfeval, which is set to 1 in this case as parfeval(pool, 1, @parallel1) and parfeval(pool, 1, @parallel2).
This code should now be able to execute the functions parallel1 and parallel2 on two separate workers of the parallel pool every second for 5 seconds. Be aware that using global variables can lead to unintended side effects and should be avoided when possible, so this solution should only be used if there's no better alternative.
Note: The timer's callback function may not run at the exact instant specified by the 'Period' property. The actual precision depends on the resolution of your system's timer and the other tasks that MATLAB is executing.
5 Comments
Walter Roberson
on 26 Jul 2023
parfeval(pool, 1, @parallel1);
The syntax for parfeval is
F = parfeval(FCN, NUMOUT, IN1, IN2, ...)
F = parfeval(P, FCN, NUMOUT, IN1, IN2, ...)
MATLAB is detecting that your first parameter, pool is a pool, so it is trying to use the second syntax, which expects the second parameter to be a function handle -- but your second parameter is a numeric value.
Function handle comes first, then the output count.
See Also
Categories
Find more on Parallel Computing Fundamentals 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!