Skip to content
fn

shape_numel

→std::size_t
int shape_numel(const int & shape)

Returns the total number of elements implied by a shape — the one element-count rule; TensorMeta::numel and every allocation sized by a shape go through it.

The product is computed in std::size_t so the result can size an allocation directly.

Parameters

shapeShape
A concrete shape. May be empty (scalar).

Returns

std::size_t

Product of all dimensions. 1 for an empty shape (scalar convention).

Raises

InvalidArgument
A dimension is negative (throw_negative_dim). It has no count: as a size_t it is 2^64 - 1, and a kernel that walks that count runs off its buffer.

Examples

::
    shape_numel({})            // 1   (scalar)
    shape_numel({3, 4})        // 12
    shape_numel({-1, 8})       // throws InvalidArgument