MATLAB: “Not enough input arguments” in PARFOR and parallel.pool.Constant (line 120)

loopnot enough input argumentsparallel computingparallel.pool.constantparfor

Hi everyone! I tried to use parallel.pool.Constant with parfor loop to speedup my code. Here it is a code:
p = ProgressBar(length(kk));
R_square_vec=zeros(length(kk),1);
Parameters=zeros(length(kk),8);
C_R_square_vec = parallel.pool.Constant(R_square_vec);
C_Parameters = parallel.pool.Constant(Parameters);
parfor k=1:length(kk)
for p1=0.2:0.05:1
for p2=0.1:0.1:0.4
for p3=0.1*10^(-7):2*10^(-7):10*10^(-7)
for p4=0.1*10^(-15):2*10^(-15):10*10^(-15)
for p5=0.5:2/5:20/5
for p6=1/400:10^(-3):1/100
for p7=1/400:10^(-3):1/150
for lam_p=0.001:0.009:0.04
[T,s]=ode45(ODEFUN, t, s0); %solves differential equations
R_exp_in=interp1(time_exp_dyn,R_exp,T);
R_square_R=sqrt(sum((R_exp_in-s(:,2).*2/3).^2));
C_R_square_vec(k)=R_square_R;
C_Parameters(k,:)=[p1 p2 p3 p4 p5 p6 p7 lam_p];
p.progress;
end
end
end
end
end
end
end
end
end
p.stop;
"kk" is an number of iterations approx ~ 6000000 or more. The idea is to find a minim R square deviation and than extract the parameters for which vector R_square_R is minimum. But that`s not the problem, it`s working when I don`t use "parallel.pool.Constant" but the simulation is too long (for 4 workers). The error message I get is:
Error using parallel.pool.Constant (line 120)
Not enough input arguments.
Error in R_square_min_find_param_plazmid1_parfor (line 143)
parfor k=1:length(kk)
And when I follow the link it goes to the "constant.m" file on the line:
narginchk(1,2);
so basically it says that here I don`t have enough input arguments?!?
Can anyone help me?
I already have tried to delete in C:\Users\xxx\AppData\Roaming\MathWorks\MATLAB folder "local_cluster_jobs" and even restart computer and matlab but it doesn`t work.

Best Answer

The way you're trying to work with parallel.pool.Constant values is not quite right. To access the contents of a parallel.pool.Constant, you should access the Value field. Also, you should not be attempting to assign into the Constant - they are designed for read-only data that is shared by multiple parfor loops. In your case with only a single parfor loop, there is no benefit to using a Constant in any case.