6.6 一个应用例子 |
例 6.6 图6.1的例子显示怎样在应用程序中使用网格定义和查询函数。一个偏微分方程,例如帕松等式,在规则域中解决。首先,进程将自己组织在二维结构中。每一进程然后在四个方向上(上,下,左,右)查询邻居的标识数。通过迭代方法解决数字问题,详细信息隐含在子程序relax中。
在每一松弛步中,每一进程在进程所拥有点上对解析格子函数计算新值。然后在交互进程的边缘上的值不得不同邻居进程相互交换。例如,交换子程序可以包含调用MPI_SEND(…, neigh_rank(1),…),以发送被更新的值到左手邻居(I-1, j)。
integer ndims, num_neigh
logical reorder
parameter (ndims=2, num_neigh=4, reorder=.true.)
integer comm, comm_cart, dims (ndims), neigh_def (ndims), ierr
integer neigh_rank (num_neigh), own_position (ndims), i , j
logical periods (ndims)
real*8 u (0:101, 0:101), f (0:101, 0:101)
data dims / ndims * 0 /
comm = MPI_COMM_WORLD
C Set process grid size and periodicity
call MPI_DIMS_CREATE (comm, ndims, dims, ierr)
periods (1) = .TRUE.
periods (2) = .TRUE.
C Create a grid structure in WORLD group and inquire about own position
call MPI_CART_CREATE ( comm, ndims, dims, periods, reorder, comm_cart, ierr)
call MPI_CART_GET (comm_cart, ndims, dims, periods, own_position, ierr)
C Look up the ranks for the neighbors, Own process coordinates are (i, j)。
C Neighbors are (I-1, j), (I, j+1), (I, j-1), (I, j+1)
I = own_position(1)
I = own_position(2)
neigh_def(1) = I-1
neigh_def(2) = j
call MPI_CART_RANK(comm_cart, neigh_def, neigh_rank(1), ierr)
neigh_def(1) = i+1
neigh_def(2) = j
call MPI_CART_RANK(comm_cart, neigh_def, neigh_rank(2), ierr)
neigh_def(1) = i
neigh_def(2) = j-1
call MPI_CART_RANK(comm_cart, neigh_def, neigh_rank(3), ierr)
neigh_def(1) = i
neigh_def(2) = j+1
call MPI_CART_RANK(comm_cart, neigh_def, neigh_rank(4), ierr)
C Initialize the grid functions and start the iteration
call init (u, f)
do 10 it = 1, 100
call relax (u, f)
C Exchange data with neighbor processes
call exchange (u, comm_cart, neigh_rank, num_neigh)
10 continue
call output (u)
end
Copyright: NPACT |