Fortran Coder

标题: 创建矩阵如何使用并行计算 [打印本页]

作者: 739977719    时间: 2022-5-8 10:12
标题: 创建矩阵如何使用并行计算

  !===========================================
  ! A subroutine for assembling Laplace matrix
  ! in the compressed row storage format
  ! (sequential version)
  !===========================================
  use header
  implicit none

  ! Variables as per assignment
  integer, intent(in) :: m
  type(Matrix), intent(inout) :: Delta

  ! Loop counters: total, horizontal, vertical, nonzeros, neighbour
  integer ::       irow,  i,          j,        inz,      next
  ! Total problem size
  integer :: n

  n = m*m
  Delta%n = n

  allocate(Delta%ii(n+1))
  allocate(Delta%aa(5*n))
  allocate(Delta%jj(5*n))

  inz = 1 ! index of current nonzero element
  do irow=1,n
     ! Calculate Cartesian index splitting
     j = (irow-1)/m + 1
     i = irow - (j-1)*m

     ! Init current row position
     Delta%ii(irow) = inz

     ! Diagonal element
     Delta%aa(inz) = -4.0_rk * m**2 ! -4/h^2
     Delta%jj(inz) = irow           ! column index = row index on diagonal
     ! Done with diagonal element, shift the counter of elements
     inz = inz + 1

     ! Off-diagonal elements, including periodically looped ones
     ! left
     Delta%aa(inz) = m**2           ! 1/h^2
     next = i-1
     if (next<1) next = m           ! loop over if we are on a boundary
     Delta%jj(inz) = next + (j-1)*m ! column index back in global range
     ! shift the nnz counter
     inz = inz + 1

     ! right
     Delta%aa(inz) = m**2
     next = i+1
     if (next>m) next = 1
     Delta%jj(inz) = next + (j-1)*m
     inz = inz + 1

     ! bottom
     Delta%aa(inz) = m**2
     next = j-1
     if (next<1) next = m
     Delta%jj(inz) = i + (next-1)*m
     inz = inz + 1

     ! top
     Delta%aa(inz) = m**2
     next = j+1
     if (next>m) next = 1
     Delta%jj(inz) = i + (next-1)*m
     inz = inz + 1
  end do

  ! Finalise row positions
  Delta%ii(irow) = inz
  Delta%nnz = inz-1


end subroutine create_matrix



这是一个创建矩阵的子程序,请问怎么写mpi并行计算






欢迎光临 Fortran Coder (http://bbs.fcode.cn/) Powered by Discuz! X3.2