fetchNext idx always returning 1

2 次查看(过去 30 天)
The documentation says that fetchNext "returns the linear index of that future in array F as idx". Here is my code:
Script experiment.m:
parpool('threads');
f(1:3)=parallel.FevalFuture;
for i=1:3
f(i)=parfeval(@worker, 1, i);
end
for i=1:3
[idx, x]=fetchNext(f(i));
disp(""+idx+" "+x);
end
Function worker.m:
function x = worker(x)
x=x+1;
end
Output:
1 2
1 3
1 4
Why is idx always 1? What am I doing wrong?

采纳的回答

Edric Ellis
Edric Ellis 2020-7-6
The first output of fetchNext tells you which of the futures you passed in as an input has just completed. In your case, you are calling fetchNext(f(i)) - so you are always calling fetchNext with a single future, therefore the index is always 1.
You should pass all the futures into fetchNext, like this:
for i=1:3
[idx, x]=fetchNext(f);
disp(""+idx+" "+x);
end

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by