How can use parallel programming in the below code?

2 ビュー (過去 30 日間)
Amir Torabi
Amir Torabi 2020 年 1 月 11 日
コメント済み: Amir Torabi 2020 年 1 月 18 日
Hello. The below code calculates a PDE equation. I'd like to use parallel to speed up in finer discritization.
How is it possible to use parallel in this structure ?
Thanks.
clc
close all
%Time
delt=0.2;
totalTime = 1e3;
timestepn=round(totalTime/delt);%size(delt,2);
% parameters
L=0.29;
gamma=3;
kappa=2;
% geometry settings; % phase field numbers
mboxsize=64; % system grid numbers in x direction
nboxsize=64; % system grid numbers in y direction
delx=2; % Delta x
Nx=mboxsize;
Ny=Nx;
dx=delx;
dy=delx;
% calculation-loading data
load etas
ngrain=25;
eta2 = zeros(Nx,Ny,ngrain);
eta = etas;
eta2 = eta;
p=ngrain;
%evolution
for tn=1:10
for i=1:mboxsize
for j=1:nboxsize
% calculation of nabla square eta(Laplacian)
del2=1/delx^2*(0.5*(eta(indg(i+1,nboxsize),j,:)-2*eta(i,j,:)+eta(indg(i-1,nboxsize),j,:))...
+0.25*(eta(indg(i+2,nboxsize),j,:)-2*eta(i,j,:)+eta(indg(i-2,nboxsize),j,:)))...
+1/delx^2*(0.5*(eta(i,indg(j+1,mboxsize),:)-2*eta(i,j,:)+eta(i,indg(j-1,mboxsize),:))...
+0.25*(eta(i,indg(j+2,mboxsize),:)-2*eta(i,j,:)+eta(i,indg(j-2,mboxsize),:)));
% summation
sumterm=eta(i,j,:)*sum(eta(i,j,:).^2)-eta(i,j,:).^3;
detadtM=(-eta(i,j,:)+eta(i,j,:).^3-kappa*del2);
detadt=-L*(detadtM+2*gamma*(sumterm));
eta2(i,j,:)=eta(i,j,:)+delt*detadt;
% for making sure eta is not outside the equilibrium values
% actually it is unnecessary
for pind=1:p
if eta2(i,j,pind)>1
eta2(i,j,pind)=1;
end
if eta2(i,j,pind)<0
eta2(i,j,pind)=0;
end
end
end
end
eta=eta2;
phi=sum(eta(:,:,1:p).^2,3);
imagesc(phi)
colorbar
title(tn)
waitbar(tn/10)
end

回答 (1 件)

Thiago Henrique Gomes Lobato
Thiago Henrique Gomes Lobato 2020 年 1 月 12 日
You can indeed try to do the code in parallel with a parfor loop in the "i" loop and some tweak in the eta2 variable bound like this:
clc
close all
%Time
delt=0.2;
totalTime = 1e3;
timestepn=round(totalTime/delt);%size(delt,2);
% parameters
L=0.29;
gamma=3;
kappa=2;
% geometry settings; % phase field numbers
mboxsize=64; % system grid numbers in x direction
nboxsize=64; % system grid numbers in y direction
delx=2; % Delta x
Nx=mboxsize;
Ny=Nx;
dx=delx;
dy=delx;
% calculation-loading data
load etas
ngrain=25;
eta2 = zeros(Nx,Ny,ngrain);
eta = etas;
eta2 = eta;
p=ngrain;
%evolution
for tn=1:10
parfor i=1:mboxsize
for j=1:nboxsize
% calculation of nabla square eta(Laplacian)
del2=1/delx^2*(0.5*(eta(indg(i+1,nboxsize),j,:)-2*eta(i,j,:)+eta(indg(i-1,nboxsize),j,:))...
+0.25*(eta(indg(i+2,nboxsize),j,:)-2*eta(i,j,:)+eta(indg(i-2,nboxsize),j,:)))...
+1/delx^2*(0.5*(eta(i,indg(j+1,mboxsize),:)-2*eta(i,j,:)+eta(i,indg(j-1,mboxsize),:))...
+0.25*(eta(i,indg(j+2,mboxsize),:)-2*eta(i,j,:)+eta(i,indg(j-2,mboxsize),:)));
% summation
sumterm=eta(i,j,:)*sum(eta(i,j,:).^2)-eta(i,j,:).^3;
detadtM=(-eta(i,j,:)+eta(i,j,:).^3-kappa*del2);
detadt=-L*(detadtM+2*gamma*(sumterm));
eta2(i,j,:)=eta(i,j,:)+delt*detadt;
end
end
% for making sure eta is not outside the equilibrium values
% actually it is unnecessary
eta2(eta2>1) = 1;
eta2(eta2<0) = 0;
eta=eta2;
phi=sum(eta(:,:,1:p).^2,3);
imagesc(phi)
colorbar
title(tn)
waitbar(tn/10)
end
I however don't think this is the best solution, since the only loop you actually need to calculate is the time on. If you substitute your i and j loops for vector operations (calculate all matrix values at once) it will probably be way faster than the parallelization and your problem will have only one loop.
  3 件のコメント
Thiago Henrique Gomes Lobato
Thiago Henrique Gomes Lobato 2020 年 1 月 12 日
In your new example you're looping for "i" and then replacing it inside the loop
Amir Torabi
Amir Torabi 2020 年 1 月 18 日
I modified it.

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

カテゴリ

Help Center および File ExchangeGeneral PDEs についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by