Restriction of functions run under parfeval

조회 수: 21 (최근 30일)
Bruno Luong
Bruno Luong 2021년 9월 23일
편집: Thomas Falch 2024년 1월 26일
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

채택된 답변

Raymond Norris
Raymond Norris 2021년 9월 24일
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
Raymond Norris
Raymond Norris 2024년 1월 19일
No update other than to say R2023b continues to behave in the same manner.
Thomas Falch
Thomas Falch 2024년 1월 26일
편집: Thomas Falch 2024년 1월 26일
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
'

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Background Processing에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by