Problem in Using Parallel Feature for a "For Loop"!

7 ビュー (過去 30 日間)
Maryam
Maryam 2018 年 7 月 17 日
コメント済み: Maryam 2018 年 7 月 17 日
I have written a code for Newton Raphson which includes two "for loops" inside of each other. Basically, I have a 1000 nonlinear equations, which are in a function called "f_int". In the "for loop" I try to construct tangent stiffness of these 1000 equations by finite difference, which is as below:
f_int = internal_eqns1000(X);
for i=1:1000
for j=1:nvar
X(j)=X(j)+h;
f_intp1 = internal_eqns1000(X);
k_k(i,j)=(f_intp1(i)-f_int(i))/h;
X(j)=X(j)-h;
end
end
After the completion of tangent stiffness, another "X" will be created and the loop will run again until convergence. This for loop might be required to run more than 100 times, and each time it takes 30 minutes average. I would like to use parallel to decrease the computational timing, but I was not successful. The change I made is as below:
f_int = internal_eqns1000(X);
parfor i=1:1000
for j=1:nvar
X(j)=X(j)+h;
f_intp1 = internal_eqns1000(X);
k_k(i,j)=(f_intp1(i)-f_int(i))/h;
X(j)=X(j)-h;
end
end
But this does not work and gives me an error: "due to the way "X" is defined, parallel computation cannot be used here".
Please let me know how can I write the code such that it will be run with parallel feature. Thank you.

採用された回答

OCDER
OCDER 2018 年 7 月 17 日
編集済み: OCDER 2018 年 7 月 17 日
But I didn't realize Newton Raphson iterations can be done in parallel. Or are you doing independent NR calc for many situations?
X = zeros(1000, nvar);
f_int = internal_eqns1000(X);
parfor i=1:1000
for j=1:nvar
X(i,j)=X(i,j)+h;
f_intp1 = internal_eqns1000(X(i,j));
k_k(i,j)=(f_intp1(i)-f_int(i))/h;
X(i,j)=X(i,j)-h;
end
end
  11 件のコメント
OCDER
OCDER 2018 年 7 月 17 日
You can't get more workers than the number of cores in your computer. To change the number of workers to less than maximum, you could do this:
NumCores = 2; %Want 2 cores
delete(gcp('nocreate'));
parpool(NumCores);
Maryam
Maryam 2018 年 7 月 17 日
Thank you!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMATLAB Parallel Server についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by