24小时热门版块排行榜    

查看: 2531  |  回复: 8
当前只显示满足指定条件的回帖,点击这里查看本话题的所有回帖

mhyuan

金虫 (小有名气)

[求助] 关于MPI并行编程的基本问题

想要自学MPI并行编程,由于以前从未接触过MPI,有熟悉的希望能帮个忙,多谢。

我现在的想要的是,给我举一个MPI的实际例子,下面是我写的一个简单的Fortran程序,是关于计算两个数组相乘的。程序不是很复杂,希望熟悉MPI的兄弟姐妹帮我改编成MPI并行程序,并详细说明一下如何运行(由于初学MPI,对这方面的知识还处于空白状态),希望有人帮忙,不胜感激!

  Program Matrix
  Implicit None
  Integer :: I,J,K
  Integer :: A(4,4),B(4,4),C(4,4)
  Data A/1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7/
  Data B/9,8,7,6,5,4,3,2,1,9,8,7,6,5,4,3/
  Do I=1,4
    Do J=1,4
      C(I,J)=0
      Do k=1,4
        C(I,J)=C(I,J)+A(I,K)*B(K,J)
      Enddo
    Enddo
  Write(1101,*) (C(I,J),J=1,4)
  Enddo
  End

[ Last edited by mhyuan on 2011-7-11 at 21:40 ]
回复此楼
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

mhyuan

金虫 (小有名气)


jjdg(金币+1): 感谢参与程序语言版讨论 2011-07-16 13:31:29
引用回帖:
Originally posted by tsiangsun at 2011-07-12 23:48:18:
mpi是cpu之间传递信息(数据)的协议。你要明确指定每个cpu要做什么计算,不是说把这个程序编程mpi就行了的。

网上和书店就有卖教材的,看了以后再来问

正在学习过程中......呵呵。
3楼2011-07-16 09:25:26
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
查看全部 9 个回答

tsiangsun

木虫 (小有名气)

【答案】应助回帖


xzhdty(金币+1): 欢迎常来程序语言版讨论 2011-07-13 01:07:47
mhyuan(金币+5): 2011-07-16 09:23:21
mpi是cpu之间传递信息(数据)的协议。你要明确指定每个cpu要做什么计算,不是说把这个程序编程mpi就行了的。

网上和书店就有卖教材的,看了以后再来问
2楼2011-07-12 23:48:18
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

mhyuan

金虫 (小有名气)

刚入门,写了一个最简单的并行程序(Matrix.f90)。如下:
        Program Matrix
        Implicit None
        Include 'mpif.h'
       
        Integer :: I,J,K
        Integer :: iam,np,ierr
        integer :: status(MPI_STATUS_SIZE)
        Integer :: A(4,4),B(4,4),C(4,4)

        Call MPI_INIT(Ierr)
        Call MPI_COMM_RANK(mpi_comm_world,iam,ierr)
        Call mpi_comm_size(mpi_comm_world,np,ierr)
       
          Data A/1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7/
          Data B/9,8,7,6,5,4,3,2,1,9,8,7,6,5,4,3/
          Data C/0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0/

        Do J=1,4
          Do I=1,4
            C((iam+1),J)=C((iam+1),J)+A((iam+1),I)*B(I,J)
          Enddo
        Enddo

        If (iam==0) then
          Call MPI_Recv(c(2,1),1,MPI_Integer,1,0,mpi_comm_world,status,ierr)
          Call MPI_Recv(c(2,2),1,MPI_Integer,1,0,mpi_comm_world,status,ierr)
          Call MPI_Recv(c(2,3),1,MPI_Integer,1,0,mpi_comm_world,status,ierr)
          Call MPI_Recv(c(2,4),1,MPI_Integer,1,0,mpi_comm_world,status,ierr)

          Call MPI_Recv(c(3,1),1,MPI_Integer,2,0,mpi_comm_world,status,ierr)
          Call MPI_Recv(c(3,2),1,MPI_Integer,2,0,mpi_comm_world,status,ierr)
          Call MPI_Recv(c(3,3),1,MPI_Integer,2,0,mpi_comm_world,status,ierr)
          Call MPI_Recv(c(3,4),1,MPI_Integer,2,0,mpi_comm_world,status,ierr)

          Call MPI_Recv(c(4,1),1,MPI_Integer,3,0,mpi_comm_world,status,ierr)
          Call MPI_Recv(c(4,2),1,MPI_Integer,3,0,mpi_comm_world,status,ierr)
          Call MPI_Recv(c(4,3),1,MPI_Integer,3,0,mpi_comm_world,status,ierr)
          Call MPI_Recv(c(4,4),1,MPI_Integer,3,0,mpi_comm_world,status,ierr)
        Elseif (iam==1) then
            Call MPI_send(c(2,1),1,MPI_Integer,0,0,mpi_comm_world,ierr)
            Call MPI_send(c(2,2),1,MPI_Integer,0,0,mpi_comm_world,ierr)
            Call MPI_send(c(2,3),1,MPI_Integer,0,0,mpi_comm_world,ierr)
            Call MPI_send(c(2,4),1,MPI_Integer,0,0,mpi_comm_world,ierr)
        Elseif (iam==2) then
            Call MPI_send(c(3,1),1,MPI_Integer,0,0,mpi_comm_world,ierr)
            Call MPI_send(c(3,2),1,MPI_Integer,0,0,mpi_comm_world,ierr)
            Call MPI_send(c(3,3),1,MPI_Integer,0,0,mpi_comm_world,ierr)
            Call MPI_send(c(3,4),1,MPI_Integer,0,0,mpi_comm_world,ierr)
        Elseif (iam==3) then
            Call MPI_send(c(4,1),1,MPI_Integer,0,0,mpi_comm_world,ierr)
            Call MPI_send(c(4,2),1,MPI_Integer,0,0,mpi_comm_world,ierr)
            Call MPI_send(c(4,3),1,MPI_Integer,0,0,mpi_comm_world,ierr)
            Call MPI_send(c(4,4),1,MPI_Integer,0,0,mpi_comm_world,ierr)
        Endif

        If (iam==0) then
          Do I=1,4
            Write(1102,*) (C(I,J),J=1,4)
          Enddo
        Endif

        Call MPI_FINALIZE(ierr)
        End
运行方式为:
    mpif90 Matrix.f90 -o Matrix
       mpirun -np 4 ./Matrix
(当然在并行机环境下)
运行结果(fort.1102):
     136          60         146          79
         103          47          99          61
         133          61         124          79
         163          75         149          97
(与上面的串行程序结果(fort.1101)完全一样)
(这个并行程序很低级,有待继续改进......)
4楼2011-07-21 09:01:54
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

mhyuan

金虫 (小有名气)


dubo(金币+1): 欢迎常来程序语言版讨论 2011-07-31 13:22:45
简单版本
        Program Matrix
        Implicit None
        Include 'mpif.h'
       
        Integer :: I,J,K
        Integer :: iam,np,ierr
        Integer :: A(4,4),B(4,4),C(4,4)
        Integer :: AA(4),CC(16)

        Call MPI_INIT(Ierr)
        Call MPI_COMM_RANK(mpi_comm_world,iam,ierr)
        Call mpi_comm_size(mpi_comm_world,np,ierr)
       
          Data A/1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7/
          Data B/9,8,7,6,5,4,3,2,1,9,8,7,6,5,4,3/
          Data CC/0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0/

        Do I=1,4
          AA(I)=A((iam+1),I)
        Enddo

        Do J=1,4
          Do I=1,4
            CC(J)=CC(J)+AA(I)*B(I,J)
          Enddo
        Enddo

        Call MPI_Gather(CC,4,MPI_Integer,CC,4,MPI_Integer,0,mpi_comm_world,ierr)

        If (iam==0) then
        K=0
        Do I=1,4
          Do J=1,4
            K=K+1
                C(I,J)=CC(K)
          Enddo
          Write(1103,*) (C(I,J),J=1,4)
        Enddo
        Endif

        Call MPI_FINALIZE(ierr)
        End
运行方式一样
运行结果一样
5楼2011-07-22 11:21:26
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[考研] 材料专硕英一数二306 +3 z1z2z3879 2026-03-18 3/150 2026-03-18 17:18 by yeahyou
[考研] 一志愿天大材料与化工(085600)总分338 +3 蔡大美女 2026-03-13 3/150 2026-03-18 15:53 by jhhcooi
[考研] 344求调剂 +5 knight344 2026-03-16 6/300 2026-03-18 15:17 by knight344
[考研] 材料与化工一志愿南昌大学327求调剂推荐 +8 Ncdx123456 2026-03-13 9/450 2026-03-18 14:40 by haxia
[考研] 297求调剂 +8 戏精丹丹丹 2026-03-17 8/400 2026-03-18 14:30 by laoshidan
[考研] 331求调剂(0703有机化学 +7 ZY-05 2026-03-13 8/400 2026-03-18 14:13 by 007_lilei
[考研] 0703化学调剂 +3 妮妮ninicgb 2026-03-17 3/150 2026-03-18 10:29 by macy2011
[基金申请] 被我言中:新模板不强调格式了,假专家开始管格式了 +4 beefly 2026-03-14 4/200 2026-03-17 22:04 by 黄鸟于飞Chao
[考研] 考研化学学硕调剂,一志愿985 +4 张vvvv 2026-03-15 6/300 2026-03-17 17:15 by ruiyingmiao
[考研] 302求调剂 +9 负心者当诛 2026-03-11 9/450 2026-03-17 17:13 by ruiyingmiao
[考研] 085600材料与化工求调剂 +5 绪幸与子 2026-03-17 5/250 2026-03-17 16:40 by laoshidan
[考研] 东南大学364求调剂 +5 JasonYuiui 2026-03-15 5/250 2026-03-16 21:28 by 木瓜膏
[考研] 304求调剂 +5 素年祭语 2026-03-15 5/250 2026-03-16 17:00 by 我的船我的海
[考研] 0703一志愿211 285分求调剂 +5 ly3471z 2026-03-13 5/250 2026-03-16 16:16 by 哦哦123
[考研] 中科院材料273求调剂 +4 yzydy 2026-03-15 4/200 2026-03-16 15:59 by Gaodh_82
[考研] 277材料科学与工程080500求调剂 +3 自由煎饼果子 2026-03-16 3/150 2026-03-16 14:10 by 运气yunqi
[考研] 材料与化工(0856)304求B区调剂 +6 邱gl 2026-03-12 7/350 2026-03-13 23:24 by 邱gl
[考研] 材料工程调剂 +9 咪咪空空 2026-03-12 9/450 2026-03-13 22:05 by 星空星月
[考研] 281求调剂 +9 Koxui 2026-03-12 11/550 2026-03-13 20:50 by Koxui
[考研] 求b区学校调剂 +3 周56 2026-03-11 3/150 2026-03-13 16:20 by JourneyLucky
信息提示
请填处理意见