Restriction of functions run under parfeval

I just see parfeval is available for R2021b allowing to run parallel function (in background) even without the parallel toolbox, which is great feature.
However when I play with it, the screen output displaying/plotting seems do nothing as illustrate in this script.
My question is is there a document that list the functions that are innactive when runing in such parallel thread?
% script bar.m to be run under R2021b
% run function foo in normal way
res=foo(10)
% run function foo in background way
f = parfeval(backgroundPool,@foo,1,10);
fprintf('Wait for background function foo to finish...')
while true
if strcmp(f.State,'finished')
break
end
pause(0.1);
end
fprintf('\n');
res=fetchOutputs(f)
function res = foo(n)
res = 0;
for i=1:n
res = res + i;
fprintf('%d\n', i); % this statement does nothing when foo is runing in background
pause(1);
end
end
>> bar
1
2
3
4
5
6
7
8
9
10
res =
55
Wait for background function foo to finish...
res =
55

 Respuesta aceptada

Raymond Norris
Raymond Norris el 24 de Sept. de 2021
fprintf isn't inactive/not working, as much backgroundPool doesn't capture the "diary" of parfeval. This has the same behaviour as threaded pools
f = parfeval(parpool('threads'),@foo,1,10);
And it has a similar behaviour as process pools
f = parfeval(parpool('local'),@foo,1,10);
with one exception. With a process pool, in the while-loop you could add the call
f.Diary
It won't do exactly what you want -- it'll print the entire diary each time. For example
ans =
1
2
3
4
5
6
7
8
9
ans =
1
2
3
4
5
6
7
8
9
10
But to your point, if instead of calling
f = parfeval(backgroundPool,@foo,1,10);
you called
parfor i = 1:1
res = foo(10);
end
you would see the fprintf statements. I'll check with Development, but it would appear as thought screen I/O would be the gating functions.

6 comentarios

Bruno Luong
Bruno Luong el 25 de Sept. de 2021
Thanks, parfor does the trick.
So parfeval does not capture tha dairy and parfror does? What is the rational reason of the different behavior?
parfor is blocking (you can't run any other code while it's running), so it makes sense to capture the diary and display it. parfeval is non-blocking, which allows you to spawn the task (which could take hours to run) while running other code. In your example, you've created a barrier with the while-loop, but take the following scenario
f = parfeval(backgroundPool,@foo,1,10);
for sidx = 1:50
[U, S, V] = svd(magic(sidx))
end
While your spawned task is running, I'm also displaying the output the values U, S, & V (50 times). Would you want your diary interspersed with the for-loop (assuming that could even happen -- I'm just illustrating a bunch of code running with a display while foo is running)?
Another task that won't be "captured" (parfor as well) is plotting. Try any one of these
f = parfeval(gcp,@plot,0,rand(100));
f = parfeval(backgroundPool,@plot,0,rand(100));
parfor idx = 1:1, plot(rand(100)), end
The plotting is happening on a remote process, not forwarded back to the client. Rather, look at DataQueue (https://www.mathworks.com/help/parallel-computing/parallel.pool.dataqueue.html), it shows good examples of sending data back to the client to plot for both parfor and parfeval.
Bruno Luong
Bruno Luong el 25 de Sept. de 2021
Editada: Bruno Luong el 27 de Sept. de 2021
"Would you want your diary interspersed with the for-loop (assuming that could even happen -- I'm just illustrating a bunch of code running with a display while foo is running)?"
Honestly if I was the designer, I would prefer to have a freedom to use or not printf in the background function (non blocking function). I would not expect the manager to interfer and blocks screen output. I can deal to make the screen messy or not, that's easier than do not have possibility to achieve something.
Alexander Graf
Alexander Graf el 19 de En. de 2024
Is there already an update on the parfeval fct that allows to decide if the printf-commands are executed or blocked?
Raymond Norris
Raymond Norris el 19 de En. de 2024
No update other than to say R2023b continues to behave in the same manner.
Thomas Falch
Thomas Falch el 26 de En. de 2024
Editada: Thomas Falch el 26 de En. de 2024
Just one clarification, the Diary property also works for thread pools and the background pool:
f = parfeval(backgroundPool, @() disp("Text"), 0);
f.Diary
ans =
'Text
'

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Productos

Etiquetas

Preguntada:

el 23 de Sept. de 2021

Editada:

el 26 de En. de 2024

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by