1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
| !--------------------------------------------------------------------------
!programme parallele : méthode de gradient conjugué !
!--------------------------------------------------------------------------
program gradconjpara
!######################################################################
!-----------------------Déclaration des variables--------------------------
implicit none
include "mpif.h"
!----------------------Déclaration des entiers réelles---------------------
real*8 :: eps,alpha,beta,dx,dy,dt,Lx,Ly,D1,T,x,y,error,c,c1,c2
integer :: i,j,m,nx,ny,nt,n,l,ns
!----------------------Déclaration des tableaux---------------------------
real*8, dimension(: , : ) , allocatable ::A,A1
real*8, dimension( : ), allocatable :: k,B,r,r1,d,w
!--------------------------mpiv------------------------------------------
!--------------------Déclaration variables parallèle----------------------
integer :: me,statinfo,np,nyloc,i1,in,dim,dim1,j1,jn
real*8 :: somme,somme1,somme2
integer, parameter :: Msgtag=50
integer, dimension(MPI_STATUS_SIZE) :: status
!----------------------Lecture fichier données----------------------------
open(11,file='data',status='old',form='formatted')
read(11,*) nx
read(11,*) ny
read(11,*) nt
read(11,*)Lx
read(11,*)Ly
read(11,*)T
read(11,*)D1
read(11,*)eps
read(11,*)m
!-------------------------Maillage---------------------------------------
dx= real(Lx)/(nx+1)
dy= real(Ly)/(ny+1)
dt=real(T)/(nt+1)
c= (2.*D1)/(dx*dx) + (2.*D1)/(dy*dy)
c1=D1/(dx*dx)
c2= D1/(dy*dy)
ny=4
nx=5
ns=nx*ny
!----------------------main-----------------------------------------------
!----------------------mpi-----------------------------------------------
call MPI_INIT(statinfo)
call MPI_COMM_SIZE(MPI_COMM_WORLD,np,statinfo)
call MPI_COMM_RANK(MPI_COMM_WORLD, me, statinfo)
call repartitioncharge(me, ns, np, i1, in)
!print*,i1,in
!--------------------Allocation dynamique des tableaux-------------------
allocate(A(1:ns,1:ns))
allocate(B(1:ns))
allocate(k(1:ns))
allocate(r(1:ns))
allocate(d(1:ns))
allocate(w(1:ns))
allocate(r1(1:ns))
allocate(A1(i1:in,i1:in+nx))
!--------------------Initialisation----------------------------------------
if (np/=ny ) stop 'Numbers of processors must be nx!!!'
A=0.
k=0.
B=1.
!----------------------Appel subroutine-----------------------------------
call mat(c,c1,c2,nx,ny,A)
call gradientconj(i1,in,nx,ny,m,eps,b,k,A)
do j= 0,np-1
call repartitioncharge(j, ns, np, j1, jn)
dim1=jn-j1+1
call MPI_BCAST(k(j1:jn),dim1, MPI_real8 ,j, MPI_COMM_WORLD ,statinfo)
end do
if (me==0) then
print*,k
end if
!--------------------main-------------------------------------------------
!--------------------Désallouement dynamique------------------------------
deallocate(A)
deallocate(b)
deallocate(k)
deallocate(r)
deallocate(d)
deallocate(w)
deallocate(r1)
call MPI_FINALIZE( statinfo)
contains
!--------------------Définition de la matrice A----------------------------
subroutine mat(c,c1,c2,nx,ny,A)
implicit none
real*8, dimension(1:ny*nx,1:ny*nx), intent(inout) :: A
real*8, intent(in) :: c,c1,c2
integer, intent(in):: nx, ny
integer ::i,j
do i=1,nx*ny
a(i,i)=c
if( i .gt. Nx ) a(i,i-Nx) = -c2
if( i .lt. (nx*ny-nx+1) ) a(i,i+nx) = -c2
end do
do i=2,nx*ny
a(i,i-1)=-c1
end do
do i=1, nx*ny-1
a(i,i+1)=-c1
end do
do i=1,ny-1
a(i*nx+1,i*nx)=0
a(i*nx,i*nx +1)=0
end do
end subroutine mat
!--------------------------------------------------------------------------
!subroutine :parallélisation de la méthode de gradient conjugué !
!--------------------------------------------------------------------------
subroutine gradientconj(i1,in,nx,ny,m,eps,b,k,A)
implicit none
integer, intent(in) ::m,i1,in,nx,ny
integer :: ns, l,i,j1,jn,dim,dim1
real*8, dimension(1:nx*ny,1:nx*ny), intent(in) :: A
real*8, dimension(1:nx*ny), intent(inout) :: k
real*8, dimension(1:nx*ny), intent(in) :: b
real*8, intent(in) :: eps
real*8, dimension(1:nx*ny) :: d,r,w
real*8 :: alpha, beta,somme
ns=ny*nx
l=0
if (i1==1)then
r(i1:in)=matmul(A(i1:in,i1:in+nx),k(i1:in+nx))-B(i1:in)
elseif (in==ns) then
r(i1:in)=matmul(A(i1:in,i1-nx:in),k(i1-nx:in))-B(i1:in)
else
r(i1:in)=matmul(A(i1:in,i1-nx:in+nx),k(i1-nx:in+nx))-B(i1:in)
end if
d(i1:in)=r(i1:in)
do while ( 0<=l .and. l<=m .and. sqrt(dot_product(r,r))>eps)
do j= 0,np-1
call repartitioncharge(j, ns, np, j1, jn)
dim1=jn-j1+1
call MPI_BCAST(d(j1:jn),dim1, MPI_real8 ,j, MPI_COMM_WORLD ,statinfo)
end do
if (i1==1)then
w(i1:in)=matmul(A(i1:in,i1:in+nx),d(i1:in+nx))
elseif (in==ns) then
w(i1:in)=matmul(A(i1:in,i1-nx:in),d(i1-nx:in))
else
9 w(i1:in)=matmul(A(i1:in,i1-nx:in+nx),d(i1-nx:in+nx))
end if
alpha=(dot_product(d(i1:in),r(i1:in)))/(dot_product(d(i1:in),w(i1:in)))
call MPI_ALLREDUCE (alpha,somme,1, MPI_REAL , MPI_SUM , MPI_COMM_WORLD ,statinfo)
k(i1:in)=k(i1:in)-alpha*d(i1:in)
beta=1./(dot_product(r(i1:in),r(i1:in)))
call MPI_ALLREDUCE (beta,somme,1, MPI_REAL8 , MPI_SUM , MPI_COMM_WORLD ,statinfo)
r(i1:in)=r(i1:in)-alpha*w(i1:in)
beta=beta*(dot_product(r(i1:in),r(i1:in)))
call MPI_ALLREDUCE (beta, somme,1, MPI_REAL8 , MPI_SUM , MPI_COMM_WORLD ,statinfo)
d(i1:in)=r(i1:in)+beta*d(i1:in)
l=l+1
end do
end subroutine gradientconj
!---------------------------------charge-------------------------------------------------------------------
subroutine repartitioncharge(me, ns, np, i1, in)
implicit none
integer, intent(in) :: ns, np, me
integer, intent(out) :: i1, in
integer :: r
r=ns- np*(ns/np)
if (me<r) then
i1=me*(ns/np+1) +1
in = (me+1)*(ns/np+1)+1
else
i1=me*ns/np +r+1
in = i1 + ns/np-1
end if
end subroutine repartitioncharge
end program gradconjpara |