Skip to content

Commit ffaacc7

Browse files
authored
Update ch09-00-coarray.md
1 parent 2449843 commit ffaacc7

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

src/ch09-00-coarray.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,35 @@ write(*,*)"image=",this_images(),z
115115
```
116116
同时,还有精确控制单个images的同步语句`sync images(a)`,这里`a`可以时标量,也可以时一维数组,也可以是`*`,表示同步等待该线程。**为了避免死锁,执行`sync images()`语句时,images Q 等待images P的次数
117117
要和images P 等待images Q的次数相等**
118+
119+
## 事件(event)
120+
Fortran2018中引入了`event`,可以更加优雅的控制不同的任务。每个`image`都可以通过`event post`发布一个事件,image可以通过`event wait`等待事件执行到当前位置之后,再继续执行。
121+
122+
``` fortran
123+
! 代码取自 Milan Curcic/Modern Fortran
124+
program push_notification
125+
use iso_fortran_env, only: event_type
126+
implicit none
127+
type(event_type) :: notification[*]
128+
if (num_images() /= 2) error stop &
129+
'This program must be run on 2 images'
130+
if (this_image() == 1) then
131+
print *, 'Image', this_image(), 'working a long job'
132+
call execute_command_line('sleep 5')
133+
print *, 'Image', this_image(), 'done and notifying image 2'
134+
event post(notification[2])
135+
else
136+
print *, 'Image', this_image(), 'waiting for image 1'
137+
event wait(notification)
138+
print *, 'Image', this_image(), 'notified from image 1'
139+
end if
140+
end program push_notification
141+
```
142+
``` sh
143+
$ fpm run --compiler="caf" --runner="cafrun -n 2"
144+
Image 1 working a long job
145+
Image 2 waiting for image 1
146+
Image 1 done and notifying image 2
147+
Image 2 notified from image 1
148+
```
149+
同时使用`call event_query(event_var, count[, stat])`子程序可以查询事件的发布次数。

0 commit comments

Comments
 (0)