Fortran Coder

标题: sync image的问题 [打印本页]

作者: Glen    时间: 2023-10-4 20:42
标题: sync image的问题
[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




作者: 风平老涡    时间: 2023-10-4 22:10
因为对于 image1 和 image2 俩个if语句产生竟速,结果不可预测。
作者: Glen    时间: 2023-10-6 10:48
本帖最后由 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







欢迎光临 Fortran Coder (http://bbs.fcode.cn/) Powered by Discuz! X3.2