24小时热门版块排行榜    

北京石油化工学院2026年研究生招生接收调剂公告
查看: 585  |  回复: 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 的主题更新
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[考研] 349求调剂 +4 zwjjjjjj 2026-03-31 4/200 2026-03-31 21:00 by yuq
[考研] 070300化学求调剂 +12 小黄鸭宝 2026-03-30 12/600 2026-03-31 19:15 by 253863592
[考研] 315求调剂 +6 akie... 2026-03-28 7/350 2026-03-31 16:48 by asdfzly
[考研] 08工科,295,接受跨专业调剂 +4 lmnlzy 2026-03-31 4/200 2026-03-31 15:25 by 西京学院招办
[考研] 354求调剂 +3 lxb598 2026-03-31 4/200 2026-03-31 13:42 by sophie2180
[考研] 一志愿南昌大学324求调剂 +6 hanamiko 2026-03-30 6/300 2026-03-31 12:19 by 唐沐儿
[考研] 一志愿大连理工大学,机械工程学硕,341 +3 西瓜田的守望者 2026-03-30 3/150 2026-03-31 11:08 by asdfzly
[考研] 085600材料与化工调剂 +16 kikiki7 2026-03-30 16/800 2026-03-31 10:03 by 氯化亚硝酰
[考研] 一志愿 南京航空航天大学 ,080500材料科学与工程学硕 +5 @taotao 2026-03-30 5/250 2026-03-31 09:41 by zhshch
[考研] 293分求调剂,外语为俄语 +5 加一一九 2026-03-31 5/250 2026-03-31 09:39 by zhshch
[考研] 327求调剂 +5 小卡不卡. 2026-03-29 5/250 2026-03-30 19:30 by Wang200018
[考研] 310求调剂 +10 争取九点睡 2026-03-30 10/500 2026-03-30 16:45 by ztnimte
[考研] 327求调剂 +6 汲亦昊 2026-03-29 6/300 2026-03-29 13:40 by peike
[考研] 一志愿北京理工大学本科211材料工程294求调剂 +8 mikasa的围巾 2026-03-28 8/400 2026-03-29 12:48 by 无际的草原
[考研] 本科双非材料,跨考一志愿华电085801电气,283求调剂,任何专业都可以 +6 芝士雪baoo 2026-03-28 8/400 2026-03-29 08:16 by 松花缸1201
[考研] 11408软件工程求调剂 +3 Qiu学ing 2026-03-28 3/150 2026-03-28 21:50 by zhq0425
[考研] 0856,材料与化工321分求调剂 +12 大馋小子 2026-03-27 13/650 2026-03-28 10:56 by self2008
[考研] 08开头275求调剂 +4 拉谁不重要 2026-03-26 4/200 2026-03-27 14:12 by Delta2012
[考研] 303求调剂 +6 蓝山月 2026-03-25 6/300 2026-03-25 22:47 by 418490947
[考研] 各位老师您好:本人初试372分 +5 jj涌77 2026-03-25 6/300 2026-03-25 14:15 by mapenggao
信息提示
请填处理意见