parfor problem with fmincon body
조회 수: 1 (최근 30일)
이전 댓글 표시
Hi, I was wondering if someone can help me to use here parfor. If the code below is run without parfor, code works well.
When I put parfor rather than for loop I get a msg that " The variable S1 in a parfor cannot be classified."
For each i, I run the code 9 times, where for each k, the variable init_s(i), get the last value of previous S1(end,k,i).
When the code finishes with k=9, the new init_S is got from line 2 and than the process continues as before and so on.
Does anyone know how to adjust the code so I can run parallel computing for each i. So I can use my 12 cores and run each i on one core for k=1:9?
Thanks a lot!!!!
parfor i = 1:M
init_s=maxS;
for k=1:9
options = optimset('MaxFunEvals',Inf,'MaxIter',10000,...
'Algorithm','interior-point','Display','iter');
tic
[x1(:,k,i),fval1(:,k,i)] = fmincon(@(x)revenue(x,N,sumprice1(k,1),init_s(i),inFlow1(:,k,i),alpha_par(i),b_par(i)),x01(:,k,i),A,b(:,k,i),Aeq,beq(k,i),LB(:,k,i),UB(:,k,i),[],options); %good
toc
S1(:,k,i)= storage(init_s(i),inFlow1(:,k,i),x1(:,k,i),N);
init_s(i)=S1(365,k,i);
end
end
댓글 수: 0
답변 (1개)
OCDER
2018년 8월 28일
Here's a work around, though I don't like the fact that this copies S1... How big is S1? If it's small, maybe not a huge issue.
parfor i = 1:M
init_s = maxS;
for k = 1:9
options = optimset('MaxFunEvals',Inf,'MaxIter',10000,'Algorithm','interior-point','Display','iter');
[x1(:,k,i),fval1(:,k,i)] = fmincon(@(x)revenue(x,N,sumprice1(k,1),init_s(i),inFlow1(:,k,i),alpha_par(i),b_par(i)),x01(:,k,i),A,b(:,k,i),Aeq,beq(k,i),LB(:,k,i),UB(:,k,i),[],options); %good
S1(:,k,i) = storage(init_s(i),inFlow1(:,k,i),x1(:,k,i),N);
T = S1(:,k,i);
init_s(i) = T(365,k,i)
end
end
The trick is that whenever you use parfor, all variables that use the parfor counter variable ("i" in your case) must CONSISTENTLY use the same indexing scheme.
parfor i = 1:M
A(i, 1) = i;
A(i, 2) = i+1; %NOT OK. "(i, 1)" and "(i, 2)" are 2 different indexing.
end
parfor i = 1:M
T = A(i, :);
T(1, 1) = i;
T(1, 2) = i+1;
A(i, :) = T; %OK. Always used the same indexing, "(i, :)".
end
댓글 수: 2
OCDER
2018년 8월 28일
S seems small enough. To see if parfor is faster, you'll have to do some time tests. Often, it's hard to be certain if the parfor is faster than regular for loops until you test it for your application. Sometimes, it's slower due to overhead in data transfer.
tic
parfor i 1:M
...
end
toc
tic
for i = 1:M
...
end
toc
How fast is it without parfor?
참고 항목
카테고리
Help Center 및 File Exchange에서 Parallel for-Loops (parfor)에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!