MATLAB: Error using parallel.p​ool.consta​nt/get value

Parallel Computing Toolboxparfeval

  1. so this is my code (not sure about function handle in parfeval). The error I get is on line 11. I dont understand the error
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
I got an error
{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,:));
the function cores_random is as the following:
[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

Best Answer

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, 2, i, 1); %%line 11
f2(i) = parfeval(poolobj,@cores_random, 2, i, 2);
f3(i) = parfeval(poolobj,@cores_random, 2, i, 3);
f4(i) = parfeval(poolobj,@cores_random, 2, i, 4);
f5(i) = parfeval(poolobj,@cores_random, 2, i, 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
function [uu,oo,ep] = cores_random(i, zidx)
up = U2.Value(i)
op = O2.Value(i);
z = C1.Value(zidx,:);
%%doing some calculations here
%%z is of size 1*1e3
%%up is scalar op is scalar
end
Related Question