Can you write a program in the simplest form It\'s for parallel programming nd L
ID: 3865729 • Letter: C
Question
Can you write a program in the simplest form It's for parallel programmingnd Lab Chapter 6 Synchronous Computation Untitled Name ID This exercise is from question 6-10 of the textbook at page 195. write a parallel program to solve the one-dimensional problem based upon the finite difference equation x[i] 10 and x[1000] 250. (x[i-1] + x(it1)/2 foro die 1000, given that x[0] = Use block partitioning to distribute array elements across processes. Your MPI program should be general (no assumptions on the number of processes used). Use 0.001 as the tolerance to determine the convergence of the iteration and set the maximum number of iterations to 1,000. output requirement: Print the values of xi 1001 where 0 i 10 in one line after every 50 iterations (a gather routine should be used to gather all values to Process 0 before printing from Process 0). uggested Section 6.3.2 for the algorithm and the codes. ted reading: Check the two-dimensional heat distribution problem described in Demonstrate your program in the class on the due date.
Explanation / Answer
c A simplest form Programm for parallel Programming.
c **************************************************************
c
program main
C
include "mpif.h"
integer maxn
parameter (maxn = 10)
parameter (iter = 1000)
parameter (tolerance = 0.001)
integer nx, ny
integer myid, numprocs, ierr
integer comm1d, nbrbottom, nbrtop, s, e, it
double precision a(maxn,maxn), b(maxn,maxn), f(maxn,maxn)
double precision diff, diffnorm, dwork
double precision t1, t2
Root = 0
call MPI_INIT( ierr )
call MPI_COMM_RANK( MPI_COMM_WORLD, myid, ierr )
call MPI_COMM_SIZE( MPI_COMM_WORLD, numprocs, ierr )
c
if (myid .eq. Root) then
c Get the number of cells in X- and Y- direction
write(6,100)
100 format(//3x,'Number of Cells in X- and Y- direction is same'/
$ 5x,' Give number of cells in X-direction')
read(5,*) nx
endif
call MPI_BCAST(nx,1,MPI_INTEGER,0,MPI_COMM_WORLD,ierr)
if( (mod(nx,numprocs) .ne. 0) .or. (nx .ge. maxn) ) then
if(myid .eq. Root) write(6,103)
103 format(4x,'Number of cells exceeds the dimension of the array'/
$ 'Number of cells should be divisible by no of processors')
go to 30
endif
ny = nx
c
c Get a new communicator for a decomposition of the domain
c
call MPI_CART_CREATE( MPI_COMM_WORLD, 1, numprocs, .false.,
$ .true., comm1d, ierr )
c
c Get my position in this communicator, and my neighbors
c
call MPI_COMM_RANK( comm1d, myid, ierr )
call MPI_Cart_shift( comm1d, 0, 1, nbrbottom, nbrtop, ierr )
c
c Compute the actual decomposition
c
call MPE_DECOMP1D( ny, numprocs, myid, s, e )
c
c Initialize the right-hand-side (f) and the initial solution guess (a)
c
call onedinit( a, b, f, nx, s, e )
c
c Actually do the computation. Note the use of a collective operation to
c check for convergence, and a do-loop to bound the number of iterations.
c
call MPI_BARRIER( MPI_COMM_WORLD, ierr )
t1 = MPI_WTIME()
do 10 it = 1, iter
call exchng1( a, nx, s, e, comm1d, nbrbottom, nbrtop )
call sweep1d( a, f, nx, s, e, b )
call exchng1( b, nx, s, e, comm1d, nbrbottom, nbrtop )
call sweep1d( b, f, nx, s, e, a )
dwork = diff( a, b, nx, s, e )
call MPI_Allreduce( dwork, diffnorm, 1, MPI_DOUBLE_PRECISION,
$ MPI_SUM, comm1d, ierr )
if (diffnorm .lt. tolerance) goto 20
if (myid .eq. Root) print *, 2*it, ' Difference is ', diffnorm
10 continue
if (myid .eq. Root) then
write(6,105) it, t2-t1
105 format(5x,'Failed to converge after',2x, i8,
$ 'Iterations in',2x, E17.8, 'seconds ')
endif
go to 30
20 continue
t2 = MPI_WTIME()
if (myid .eq. Root) then
write(6,106) it, t2 -t1
106 format(4x,'Converged after ',2x, i8,' Iterations and the time
$ taken is ', F20.10//15x, '*** Successful exit ***')
endif
c
30 call MPI_FINALIZE(ierr)
stop
end
c
c ---------------------------------------------------------------
c
c
subroutine onedinit( a, b, f, nx, s, e )
integer nx, s, e
double precision a(0:nx+1, s-1:e+1), b(0:nx+1, s-1:e+1),
& f(0:nx+1, s-1:e+1)
c
integer i, j
c
do 10 j=s-1,e+1
do 10 i=0,nx+1
a(i,j) = 0.0d0
b(i,j) = 0.0d0
f(i,j) = 0.0d0
10 continue
c
c Handle boundary conditions
c
do 20 j=s,e
a(0,j) = 1.0d0
b(0,j) = 1.0d0
a(nx+1,j) = 0.0d0
b(nx+1,j) = 0.0d0
20 continue
if (s .eq. 1) then
do 30 i=1,nx
a(i,0) = 2.0d0
b(i,0) = 2.0d0
30 continue
endif
c
return
end
c
c This file contains a routine for producing a decomposition of a 1-d array
c when given a number of processors. It may be used in "direct" product
c decomposition. The values returned assume a "global" domain in [1:n]
c
SUBROUTINE MPE_DECOMP1D( n, numprocs, myid, s, e )
integer n, numprocs, myid, s, e
integer nlocal
integer deficit
c
nlocal = n / numprocs
s = myid * nlocal + 1
deficit = mod(n,numprocs)
s = s + min(myid,deficit)
if (myid .lt. deficit) then
nlocal = nlocal + 1
endif
e = s + nlocal - 1
if (e .gt. n .or. myid .eq. numprocs-1) e = n
return
end
c
c......THIS SUBROUTINE USES BUFFER SEND AND RECEIVE MPI LIBRARY CALLS.........
subroutine exchng1( a, nx, s, e, comm1d, nbrbottom, nbrtop )
include "mpif.h"
integer maxn
parameter (maxn = 10)
integer nx, s, e
double precision a(0:nx+1,s-1:e+1)
integer comm1d, nbrbottom, nbrtop
integer status(MPI_STATUS_SIZE), ierr
integer BufSize
C double precision Buffer(nx+MPI_BSEND_OVEREAD)
double precision Buffer(maxn+maxn)
BufSize = maxn+maxn
C BufSize = nx + MPI_BSEND_OVERHEAD
c
call MPI_Buffer_attach(Buffer, BufSize)
call MPI_BSEND( a(1,e), nx, MPI_DOUBLE_PRECISION, nbrtop, 0,
$ comm1d, ierr )
call MPI_Buffer_detach(Buffer, BufSize)
call MPI_RECV( a(1,s-1), nx, MPI_DOUBLE_PRECISION, nbrbottom, 0,
$ comm1d, status, ierr )
call MPI_Buffer_attach(Buffer, BufSize)
call MPI_BSEND( a(1,s), nx, MPI_DOUBLE_PRECISION, nbrbottom, 1,
$ comm1d, ierr )
call MPI_Buffer_detach(Buffer, BufSize)
call MPI_RECV( a(1,e+1), nx, MPI_DOUBLE_PRECISION, nbrtop, 1,
$ comm1d, status, ierr )
return
end
c
c The rest of the 1-d program
c
double precision function diff( a, b, nx, s, e )
integer nx, s, e
double precision a(0:nx+1, s-1:e+1), b(0:nx+1, s-1:e+1)
c
double precision sum
integer i, j
c
sum = 0.0d0
do 10 j=s,e
do 10 i=1,nx
sum = sum + (a(i,j) - b(i,j)) ** 2
10 continue
c
diff = sum
return
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.