<ATOMIC_FETCH_AND Example Program>
Program atomic_fetch_and_example
Use iso_fortran_env
Implicit None
Integer,Parameter :: ntasks = 12345
Integer,Parameter :: tasks_per_word = Bit_Size(0_atomic_int_kind)
Integer,Parameter :: nwords = (ntasks+tasks_per_word-1)/tasks_per_word
Integer(atomic_int_kind) taskmask(nwords)[*],oldmask,taskbit,taskcount[*]
Integer i,j
If (This_Image()==1) Then
taskmask(:nwords-1) = Not(0_atomic_int_kind)
taskmask(nwords) = Maskr(ntasks-(nwords-1)*tasks_per_word,atomic_int_kind)
taskcount = 0
End If
Sync All
Do i=1,nwords
Do j=1,tasks_per_word
taskbit = Shiftl(1_atomic_int_kind,j-1)
Call atomic_fetch_and(taskmask(i)[1],Not(taskbit),oldmask)
If (Iand(taskbit,oldmask)==taskbit) Then
! Do task (i,j).
! Since this is an example, we just add one to the task count.
Call atomic_add(taskcount[1],1)
End If
End Do
End Do
Sync All
If (This_Image()==1) Then
Print *,taskcount,'tasks completed.'
End If
End Program
■ Execution Results
12345 tasks completed.