ATOMIC_DEFINE(3) - [ATOMIC] Atomically define the value of a variable
call atomic_define (atom, value [,stat] )
subroutine atomic_define(atom, value, stat)TYPE(kind=atomic_KIND_kind) :: atom[*] TYPE(kind=KIND) :: value integer,intent(out),optional :: stat
o ATOM : Scalar coarray or coindexed variable of either integer type with ATOMIC_INT_KIND kind or logical type with ATOMIC_LOGICAL_KIND kind. o VALUE : Scalar of the same type as ATOM. If the kind is different, the value is converted to the kind of ATOM. o STAT : (optional) Scalar default-kind integer variable.
ATOMIC_DEFINE(ATOM, VALUE, STAT) atomically sets the value of ATOM to VALUE. This ensures thread-safe assignment in parallel environments.
Use for simple atomic assignments, unlike ATOMIC_CAS(3) which involves comparison.
Only one image should call ATOMIC_DEFINE(3) to avoid undefined behavior in this context.
o ATOM : Scalar coarray or coindexed variable to atomically assign the value VALUE to. kind. o VALUE : value to assign to ATOM o STAT : When STAT is present and the invocation was successful, it is assigned the value 0. If it is present and the invocation has failed, it is assigned a positive value; in particular, for a coindexed ATOM, if the remote image has stopped, it is assigned the value of iso_fortran_envs stat_stopped_image and if the remote image has failed, the value stat_failed_image.
Sample program:
program demo_atomic_define use iso_fortran_env implicit none integer(atomic_int_kind) :: counter[*] integer :: stat, meExpected Output (4 images)if (this_image() == 1) counter = 0 sync all
me = this_image() if (me == 2) call atomic_define(counter[1], 42, stat)
if (stat /= 0) print *, "Image", me, ": Failed with STAT =", stat sync all
if (this_image() == 1) print *, "Final counter:", counter end program demo_atomic_define
> Final counter: 42
Fortran 2008 ; with STAT, TS 18508
ATOMIC_REF(3), ATOMIC_CAS(3), ISO_FORTRAN_ENV(3), ATOMIC_ADD(3), ATOMIC_AND(3), ATOMIC_OR(3), ATOMIC_XOR(3)
See ISO_FORTRAN_ENV for constants like ATOMIC_INT_KIND, STAT_STOPPED_IMAGE, and STAT_FAILED_IMAGE.
Fortran intrinsic descriptions
Nemo Release 3.1 | atomic_define (3) | June 29, 2025 |