ATOMIC_ADD(3) - [ATOMIC] Atomic ADD operation
call atomic_add (atom, value [,stat] )
subroutine atomic_add(atom,value,stat)integer(atomic_int_kind) :: atom[*] integer(atomic_int_kind),intent(in) :: value integer,intent(out),intent(out) :: stat
o ATOM is a scalar coarray or coindexed variable of integer type with atomic_int_kind kind. o VALUE is a ,scalar of the same type as ATOM. If the kind is different, the value is converted to the kind of ATOM. o STAT is a scalar default-kind integer variable.
ATOMIC_ADD(ATOM, VALUE, STAT) atomically adds the value of VALUE to the variable ATOM. This operation ensures thread safety in parallel environments, such as when using coarrays. It is part of the atomic operations in Fortran 2008 and later, typically used with the ISO_FORTRAN_ENV module.
Unlike ATOMIC_FETCH_ADD(3), this procedure does not return the previous value of ATOM.
Use sync all to ensure consistent coarray state across images.
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.
o ATOM : Scalar coarray or coindexed variable of integer type with kind ATOMIC_INT_KIND.(from ISO_FORTRAN_ENV). 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. Set to 0 on success, or a positive value (e.g., STAT_STOPPED_IMAGE or STAT_FAILED_IMAGE from ISO_FORTRAN_ENV) on failure.
Sample program:
program demo_atomic_add 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() call atomic_add(counter[1], me, 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_add
> Final counter: 10
Fortran 2008 and later, per TS 18508
ATOMIC_DEFINE(3), ATOMIC_FETCH_ADD(3), ATOMIC_AND(3), ATOMIC_OR(3), ATOMIC_XOR(3) ISO_FORTRAN_ENV(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_add (3) | June 29, 2025 |