Glen 发表于 2023-10-4 20:42:46

sync image的问题

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 from1 out of 4
Image from1 out of 4
Image from2 out of 4
Image from2 out of 4
Image from3 out of 4
Image from3 out of 4
Image from4 out of 4
Image from4 out of 4


而我预想的结果是

Image from1 out of 4
Image from2 out of 4
Image from3 out of 4
Image from4 out of 4
Image from4 out of 4
Image from3 out of 4
Image from2 out of 4
Image from1 out of 4



风平老涡 发表于 2023-10-4 22:10:25

因为对于 image1 和 image2 俩个if语句产生竟速,结果不可预测。

Glen 发表于 2023-10-6 10:48:33

本帖最后由 Glen 于 2023-10-6 14:01 编辑

风平老涡 发表于 2023-10-4 22:10
因为对于 image1 和 image2 俩个if语句产生竟速,结果不可预测。
解释在这里SYNC IMAGES (intel.com)

DescriptionWhen 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行),如下:
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 from1 out of 4 Image from2 out of 4 Image from3 out of 4 Image from4 out of 4 Image from4 out of 4 Image from3 out of 4 Image from2 out of 4 Image from1 out of 4

页: [1]
查看完整版本: sync image的问题