这是我的代码(不确定parfeval中的函数句柄)。我得到的错误在第11行。我不理解这个错误
poolobj=parpool('my_cluster',8);
[up, op]=ndgri(1e-3:1e-2:1,1e-3:1e-2:1);
up=reshape(up, [1,size(up,1)*size(up,2)]);
up=reshape(up, [1,size(up,1)*size(up,2)]);
z=rand(5,5e3);
addAttachedFiles('<path to my function/cores_random.m');%%to add the function files to workers on the pool
C1=parallel.pool.Constant(z);%%use parallel.pool.Constant to copy these variables into workers
U2=parallel.pool.Constant(up);
O2=parallel.pool.Constant(op);
for i=1:size(up,2)
f1(i)=parfeval(poolobj,@cores_random,3,U2.value(i),O2.Value(i),C1.Value(1,:)); %%line 11
f2(i)=parfeval(poolobj,@cores_random,3,U2.value(i),O2.Value(i),C1.Value(2,:));
f3(i)=parfeval(poolobj,@cores_random,3,U2.value(i),O2.Value(i),C1.Value(3,:));
f4(i)=parfeval(poolobj,@cores_random,3,U2.value(i),O2.Value(i),C1.Value(4,:));
f5(i)=parfeval(poolobj,@cores_random,3,U2.value(i),O2.Value(i),C1.Value(5,:));
end
for j=1:size(up,2):-1:1
[idx1,u1,o1,ep1]=fetchNext(f1);
[idx2,u2,o2,ep2]=fetchNext(f2);
[idx3,u3,o3,ep3]=fetchNext(f3);
[idx4,u4,o4,ep4]=fetchNext(f4);
[idx5,u5,o5,ep5]=fetchNext(f5);
end我收到一个错误
{Error using paralle.pool.Constant/get.Value The value of a parallel.pool.Constant is only available on the workers.
Error in main_parallel_norm (line 11)
f1(i)=parfeval(poolobj,@cores_random,3,U2.value(i),O2.Value(i),C1.Value(1,:));函数cores_random如下:
[uu,oo,ep]=cores_random(up,op,z)
%%doing some calculations here
%%z is of size 1*1e3
%%up is scalar op is scalar
end发布于 2020-10-05 17:12:49
正如错误消息所述,parallel.pool.Constant的Value属性仅在工作进程上可用。如上所述,您的parfeval请求正在尝试访问客户端上的它。简而言之,您的代码如下所示:
c = parallel.pool.Constant(42);
f = parfeval(pool, @myFcn, 1, c.Value);这会强制客户端在设置parfeval调用时评估c.Value。
您应该做的是将Constant本身传递到parfeval请求中,然后访问workers上的Value字段。这样做的一种方法是:
c = parallel.pool.Constant(42);
f = parfeval(pool, @(const) myFcn(const.Value), 1, c);这可确保仅在工作进程上计算const.Value表达式。(或者,您可以修改myFcn以了解它需要在其输入参数上调用const.Value。)
https://stackoverflow.com/questions/64178907
复制相似问题