How can use parallel programming in the below code?

조회 수: 1 (최근 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 CenterFile Exchange에서 Boundary Conditions에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by