Skip to contents

Creates an array of a given shape and data type, filled with a scalar value. The advantage of using this function instead of e.g. doing nv_array(1, shape = c(100, 100)) is that lowering of prim_fill() is efficiently represented in the compiled program, while the latter uses 100 * 100 * 4 bytes of memory.

Usage

prim_fill(value, shape, dtype, device = NULL)

Arguments

value

(numeric(1))
Scalar value to fill the array with. It has to be something dtype can hold: a whole number in its range for an integer data type, a non-negative one for an unsigned integer, and a logical or 0 / 1 for bool.

shape

(integer())
Shape of the output array.

dtype

(character(1) | DataType)
Data type of the result.

device

(NULL | character(1) | device)
The device the data lives on, given either as:

  • a device string naming the platform (e.g. "cpu", "cuda", "cuda:<n>"), which is resolved against the backend in use, or

  • a device object as returned by nv_device(): a PJRTDevice for the "pjrt" backend or a quickr_device for the "quickr" backend. Because a device object is backend-specific, it also determines the backend.

The default (NULL) uses default_device().

Value

(arrayish)
Has the given shape and dtype.

Implemented Rules

  • stablehlo

  • quickr

StableHLO

Lowers to hlo_tensor(), stablehlo's constant builder, specified under constant.

See also

Examples

# the R double is built at the requested data type
prim_fill(3.14, shape = c(2, 3), dtype = "f32")
#> AnvlArray
#>  3.1400 3.1400 3.1400
#>  3.1400 3.1400 3.1400
#> [ CPUf32{2,3} ]