Fortran Coder

查看: 1788|回复: 0
打印 上一主题 下一主题

[线性代数] 创建矩阵如何使用并行计算

[复制链接]

1

帖子

1

主题

0

精华

新人

F 币
10 元
贡献
3 点
跳转到指定楼层
楼主
发表于 2022-5-8 10:12:28 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

  !===========================================
  ! 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并行计算

分享到:  微信微信
收藏收藏 点赞点赞 点踩点踩
您需要登录后才可以回帖 登录 | 极速注册

本版积分规则

捐赠本站|Archiver|关于我们 About Us|小黑屋|Fcode ( 京ICP备18005632-2号 )

GMT+8, 2024-11-23 10:28

Powered by Tencent X3.4

© 2013-2024 Tencent

快速回复 返回顶部 返回列表