cupyx.scatter_add#
- cupyx.scatter_add(a, slices, value)[source]#
Adds given values to specified elements of an array.
It adds
value
to the specified elements ofa
. If all of the indices target different locations, the operation ofscatter_add()
is equivalent toa[slices] = a[slices] + value
. If there are multiple elements targeting the same location,scatter_add()
uses all of these values for addition. On the other hand,a[slices] = a[slices] + value
only adds the contribution from one of the indices targeting the same location.Note that just like an array indexing, negative indices are interpreted as counting from the end of an array.
Also note that
scatter_add()
behaves identically tonumpy.add.at()
.Example
>>> import cupy >>> import cupyx >>> a = cupy.zeros((6,), dtype=cupy.float32) >>> i = cupy.array([1, 0, 1]) >>> v = cupy.array([1., 1., 1.]) >>> cupyx.scatter_add(a, i, v); >>> a array([1., 2., 0., 0., 0., 0.], dtype=float32)
- Parameters:
a (ndarray) – An array that gets added.
slices – It is integer, slices, ellipsis, numpy.newaxis, integer array-like, boolean array-like or tuple of them. It works for slices used for
cupy.ndarray.__getitem__()
andcupy.ndarray.__setitem__()
.v (array-like) – Values to increment
a
at referenced locations.
Note
It only supports types that are supported by CUDA’s atomicAdd when an integer array is included in
slices
. The supported types arenumpy.float32
,numpy.int32
,numpy.uint32
,numpy.uint64
andnumpy.ulonglong
.Note
scatter_add()
does not raise an error when indices exceed size of axes. Instead, it wraps indices.See also