Fortran Coder

查看: 724|回复: 2
打印 上一主题 下一主题

[并行] sync image的问题

[复制链接]

9

帖子

3

主题

0

精华

入门

F 币
49 元
贡献
21 点
跳转到指定楼层
楼主
发表于 2023-10-4 20:42:46 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
[Fortran] 纯文本查看 复制代码
program test2
    implicit none
    integer :: me
    me = this_image() ! 需要先声明me再赋值,如果直接在声明初始化,会保存到常量区,变成大家共有的变量
    if(me > 1) sync images(me-1)!编号高的image需要等待前一个image彻底运行完到程序结束?
    write(*,*) "Image from ", this_image(), "out of", num_images()
    if(me < num_images()) sync images(me+1)!image1 这里不需要等待image2?
    write(*,*) "Image from ", this_image(), "out of", num_images()
end program test2


最诡异的是当image1运行到第二个if时,不需要与其他image同步,于是最终输出结果为

Image from  1 out of 4
Image from  1 out of 4
Image from  2 out of 4
Image from  2 out of 4
Image from  3 out of 4
Image from  3 out of 4
Image from  4 out of 4
Image from  4 out of 4


而我预想的结果是

Image from  1 out of 4
Image from  2 out of 4
Image from  3 out of 4
Image from  4 out of 4
Image from  4 out of 4
Image from  3 out of 4
Image from  2 out of 4
Image from  1 out of 4



分享到:  微信微信
收藏收藏 点赞点赞 点踩点踩

213

帖子

2

主题

0

精华

宗师

F 币
2126 元
贡献
875 点

规矩勋章

沙发
发表于 2023-10-4 22:10:25 | 只看该作者
因为对于 image1 和 image2 俩个if语句产生竟速,结果不可预测。

9

帖子

3

主题

0

精华

入门

F 币
49 元
贡献
21 点
板凳
 楼主| 发表于 2023-10-6 10:48:33 | 只看该作者
本帖最后由 Glen 于 2023-10-6 14:01 编辑
风平老涡 发表于 2023-10-4 22:10
因为对于 image1 和 image2 俩个if语句产生竟速,结果不可预测。

解释在这里SYNC IMAGES (intel.com)

Description
When SYNC IMAGES statements are executed on images C and G, they correspond if the number of times image C has executed a SYNC IMAGES statement in the current team with G in its image set is the same as the number of times image G has executed a SYNC IMAGES statement with C in its image set in this team.
就是说images之间互相等待的次数要扯平,然后images才会听你的sync images命令,否则就不执行。所以image1对于第7行直接无视,因为第7行的时候image1发现image2等了我一次,但是我没有等待过image2,因此没有扯平,既然这样image1不会听从第7行的指令,虽然第7行起到了计数扯平的作用,但是image1不会去correspond(respond?),而是会直接执行第8行。想要实现我预计的结果,需要把sync命令成对出现,即多写2行sync命令(新增第8、10行),如下:
[Fortran] 纯文本查看 复制代码
program test2
    implicit none
    integer :: me
    me = this_image() ! 需要先声明me再赋值,如果直接在声明初始化,会保存到常量区,变成大家共有的变量
    if(me > 1) sync images(me-1)
    write(*,*) "Image from ", this_image(), "out of", num_images()
    if(me < num_images()) sync images(me+1)   
    if(me < num_images()) sync images(me+1)
    write(*,*) "Image from ", this_image(), "out of", num_images()
    if(me > 1) sync images(me-1)
end program test2
Image from  1 out of 4
Image from  2 out of 4
Image from  3 out of 4
Image from  4 out of 4
Image from  4 out of 4
Image from  3 out of 4
Image from  2 out of 4
Image from  1 out of 4


您需要登录后才可以回帖 登录 | 极速注册

本版积分规则

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

GMT+8, 2024-5-2 17:04

Powered by Tencent X3.4

© 2013-2024 Tencent

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