24小时热门版块排行榜    

北京石油化工学院2026年研究生招生接收调剂公告
查看: 581  |  回复: 2

tang76

铜虫 (小有名气)

[求助] 初学MPI,以下Fortran程序请教

以下程序编译后运行:
mpiexec -n 4 test.exe

出现如下结果:
myid=           2
results(           3 )=          10
myid=           1
results(           2 )=          10
myid=           3
results(           4 )=          10
myid=           0
results(           1 )=          10
results(           1 )=          10
results(           2 )=  -842150451
results(           3 )=  -842150451
results(           4 )=  -842150451

期望所有输出结果均为10,请问哪里错了?

谢谢!


附:Fortran程序:

    program test
    implicit none
    include 'mpif.h'

        integer::i
    integer rank, size, myid, ierror,commandsize
        integer,allocatable::results(
    commandsize=4
    allocate(results(commandsize))
    call MPI_INIT(ierror)
    call MPI_COMM_SIZE(MPI_COMM_WORLD, size, ierror)
    call MPI_COMM_RANK(MPI_COMM_WORLD, myid, ierror)

    write(*,*)'myid=',myid
    results(myid+1)=10
    write(*,*)'results(',myid+1,')=',results(myid+1)
    call MPI_ALLGATHER(results,commandsize,MPI_INTEGER,results,commandsize,MPI_INTEGER,MPI_COMM_WORLD,ierror)
    if(myid==0)then
      do i=1,commandsize
        write(*,*)'results(',i,')=',results(i)
      enddo
    endif
    call MPI_FINALIZE(ierror)
    deallocate(results)
    end
回复此楼

» 收录本帖的淘帖专辑推荐

资源收集

» 猜你喜欢

» 本主题相关价值贴推荐,对您同样有帮助:

已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

baghnac

金虫 (小有名气)

【答案】应助回帖

tang76(金币+1): 2011-10-26 00:08:22
引用回帖:
1楼: Originally posted by tang76 at 2011-10-24 12:46:46:
以下程序编译后运行:
mpiexec -n 4 test.exe

出现如下结果:
myid=           2
results(           3 )=          10
myid=           1
results(           2 )=          10
myid=           3 ...

⑴发送与接收缓冲区如果一样,会冲突
⑵接收缓冲区应大于各发送缓冲区的总大小
虽然书上没有找到这么说,但是我试出来的是这个结果
结果如下
myid=0
results(           1 )=          10
results(           2 )=           0
results(           3 )=           0
results(           4 )=           0
recvs(           1 )=          10
recvs(           2 )=           0
recvs(           3 )=           0
recvs(           4 )=           0
recvs(           5 )=           0
recvs(           6 )=          10
recvs(           7 )=           0
recvs(           8 )=           0
recvs(           9 )=           0
recvs(          10 )=           0
recvs(          11 )=          10
recvs(          12 )=           0
recvs(          13 )=           0
recvs(          14 )=           0
recvs(          15 )=           0
recvs(          16 )=          10
myid=1
results(           1 )=           0
results(           2 )=          10
results(           3 )=           0
results(           4 )=           0
myid=2
results(           1 )=           0
results(           2 )=           0
results(           3 )=          10
results(           4 )=           0
myid=3
results(           1 )=           0
results(           2 )=           0
results(           3 )=           0
results(           4 )=          10

附程序:
program test
    implicit none
    include 'mpif.h'

        integer::i
    integer rank, size, myid, ierror,commandsize,sendcnt,recvcnt,sendssize
        integer,allocatable::results(
        integer,allocatable::recvs(
    commandsize=4
        sendssize=16
    allocate(results(commandsize))
    allocate(recvs(sendssize))
    call MPI_INIT(ierror)
    call MPI_COMM_SIZE(MPI_COMM_WORLD, size, ierror)
    call MPI_COMM_RANK(MPI_COMM_WORLD, myid, ierror)

!    write(*,*)'myid=',myid
    results(myid+1)=10
!    write(*,*)'results(',myid+1,')=',results(myid+1)
    call MPI_ALLGATHER(results,commandsize,MPI_INTEGER,recvs,commandsize,MPI_INTEGER,MPI_COMM_WORLD,ierror)
    if(myid==0)then
      do i=1,commandsize
        write(*,*)'results(',i,')=',results(i)
      enddo
      do i=1,sendssize
        write(*,*)'recvs(',i,')=',recvs(i)
      enddo
    endif
    call MPI_FINALIZE(ierror)
    deallocate(results)
    end

运行结果

Beagreathackernotacracker!
2楼2011-10-25 14:29:53
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

tang76

铜虫 (小有名气)

非常感谢,我也找到这个问题了,应改成:
call MPI_ALLGATHER(results(myid+1),1,MPI_INTEGER,recvs,commandsize,MPI_INTEGER,MPI_COMM_WORLD,ierror)
3楼2011-10-26 00:09:47
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
相关版块跳转 我要订阅楼主 tang76 的主题更新
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[考研] 一志愿武理材料工程302调剂环化或化工 +7 Doleres 2026-03-31 7/350 2026-03-31 18:19 by JourneyLucky
[考研] 324分 085600材料与化工 +4 呆鹅oor 2026-03-27 4/200 2026-03-31 18:08 by 无际的草原
[考研] 考研生物与医药调剂 +6 铁憨憨123425 2026-03-31 6/300 2026-03-31 17:48 by 544594351
[考研] 材料调剂 5+5 想要一壶桃花水 2026-03-25 11/550 2026-03-31 13:34 by Huaxue_Wang
[考研] 274求调剂 +6 xiao爱同学 2026-03-30 6/300 2026-03-31 10:04 by cal0306
[考研] 085600材料与化工329分求调剂 +5 这是有太阳哇 2026-03-25 5/250 2026-03-31 09:59 by luoyongfeng
[考研] 抱歉 +3 田洪有 2026-03-30 3/150 2026-03-30 19:11 by 迷糊CCPs
[考研] 329求调剂 +8 星野? 2026-03-26 8/400 2026-03-30 13:41 by chemdavid
[考研] 0703本科郑州大学求调剂 +7 nhj_ 2026-03-25 7/350 2026-03-30 12:44 by fangnagu
[考研] 0703化学/290求调剂/本科经历丰富/工科也可 +13 丹青奶盖 2026-03-26 15/750 2026-03-30 12:35 by fangnagu
[考研] 375求调剂 +6 雨夏整夜 2026-03-29 6/300 2026-03-30 10:21 by herarysara
[考研] 337求调剂 +6 《树》 2026-03-29 6/300 2026-03-30 10:15 by herarysara
[考研] 311求调剂 +6 冬十三 2026-03-24 6/300 2026-03-29 20:45 by 无际的草原
[考研] 复试调剂 +3 raojunqi0129 2026-03-28 3/150 2026-03-28 15:27 by 落睿可思
[考研] 085701环境工程,267求调剂 +16 minht 2026-03-26 16/800 2026-03-28 12:16 by zllcz
[考研] 265求调剂 +8 小木虫085600 2026-03-27 8/400 2026-03-27 22:16 by 无际的草原
[考研] 266分求材料化工冶金矿业等专业的调剂 +4 哇呼哼呼哼 2026-03-26 4/200 2026-03-27 17:02 by zhyzzh
[考研] 中国科学院深圳先进技术研究院-光纤传感课题组招生-中国科学院大学、深圳理工大学联培 +5 YangTyu1 2026-03-26 5/250 2026-03-26 18:27 by 猫咪猫咪呀
[考研] 309求调剂 +4 gajsj 2026-03-25 5/250 2026-03-26 00:27 by Dyhoer
[考研] 网络空间安全0839招调剂 +4 w320357296 2026-03-25 6/300 2026-03-25 17:59 by 255671
信息提示
请填处理意见