C Library Functions  - atomic_add (3)

NAME

ATOMIC_ADD(3) - [ATOMIC] Atomic ADD operation

SYNOPSIS

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

CHARACTERISTICS

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.

DESCRIPTION

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_env’s STAT_STOPPED_IMAGE and if the remote image has failed, the value STAT_FAILED_IMAGE.

OPTIONS

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.

EXAMPLES

Sample program:

    program demo_atomic_add
      use iso_fortran_env
      implicit none
      integer(atomic_int_kind) :: counter[*]
      integer :: stat, me

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

Expected Output (4 images)

        > Final counter: 10

STANDARD

Fortran 2008 and later, per TS 18508

SEE ALSO

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
Generated by manServer 1.08 from 3480369c-7cdf-44e9-9bac-88fadecf93c5 using man macros.