Template Class fusion_array#
Defined in File parrot.hpp
Template Parameter Order#
Class Documentation#
-
template<typename Iterator, typename MaskIterator>
class fusion_array# -
Public Functions
-
inline const std::shared_ptr<void> &storage() const#
-
inline auto apply() const#
-
inline auto shape() const -> std::vector<int>#
-
inline auto rank() const -> int#
Get the rank (dimensionality) of the array.
- Returns:
The number of dimensions in the array’s shape
-
inline auto nrows() const -> int#
Get the number of rows in a 2D array.
- Throws:
std::runtime_error – if the array rank is not 2
- Returns:
The number of rows (first dimension)
-
inline auto ncols() const -> int#
Get the number of columns in a 2D array.
- Throws:
std::runtime_error – if the array rank is not 2
- Returns:
The number of columns (second dimension)
-
template<typename OtherIterator, typename OtherMaskIterator, typename BinaryFunctor>
inline auto map2(const fusion_array<OtherIterator, OtherMaskIterator> &value, BinaryFunctor binary_op) const#
-
template<typename T, typename BinaryFunctor>
inline auto map2(const T &value, BinaryFunctor binary_op) const -> decltype(auto)#
-
template<typename UnaryFunctor>
inline auto map(UnaryFunctor op) const# Apply a unary functor to each element of the array.
- Template Parameters:
UnaryFunctor – The type of the unary functor
- Parameters:
op – The unary operation to apply
- Returns:
A new fusion_array with the operation applied
-
template<typename T = int>
inline auto times(T scalar) const# Multiply each element by a scalar.
- Parameters:
scalar – The value to multiply by
- Returns:
A new fusion_array with the operation applied
-
template<typename T>
inline auto add(const T &value) const# Add a scalar or another fusion_array element-wise.
- Parameters:
value – The scalar or fusion_array to add
- Returns:
A new fusion_array with the operation applied
-
template<typename T = int>
inline auto min(T scalar) const# Return elementwise minimum with a scalar.
- Parameters:
scalar – The value to compare with
- Returns:
A new fusion_array with the operation applied
-
template<typename T = int>
inline auto max(T scalar) const# Return elementwise maximum with a scalar.
- Parameters:
scalar – The value to compare with
- Returns:
A new fusion_array with the operation applied
-
template<typename T>
inline auto div(const T &arg) const# Divide each element by a scalar or perform element-wise division with another fusion_array.
- Template Parameters:
T – The type of the argument, which can be a scalar or another fusion_array.
- Parameters:
arg – The scalar value to divide by or the other fusion_array for element-wise division.
- Throws:
std::invalid_argument – if
arg
is a fusion_array and shapes are incompatible for element-wise operations.- Returns:
A new fusion_array with the division operation applied.
-
template<typename T = int>
inline auto idiv(T scalar) const# Integer divide each element by a scalar (truncating any fractional part)
- Parameters:
scalar – The value to divide by
- Returns:
A new fusion_array with the operation applied
-
template<typename T = int>
inline auto minus(T scalar) const# Subtract a scalar from each element.
- Parameters:
scalar – The value to subtract
- Returns:
A new fusion_array with the operation applied
-
template<typename T = int>
inline auto gte(T scalar) const# Check if each element is greater than or equal to a scalar.
- Parameters:
scalar – The value to compare with
- Returns:
A new fusion_array containing 1 where the condition is true, 0 otherwise
-
template<typename T = int>
inline auto lte(T scalar) const# Check if each element is less than or equal to a scalar.
- Parameters:
scalar – The value to compare with
- Returns:
A new fusion_array containing 1 where the condition is true, 0 otherwise
-
template<typename T = int>
inline auto gt(T scalar) const# Check if each element is greater than a scalar.
- Parameters:
scalar – The value to compare with
- Returns:
A new fusion_array containing 1 where the condition is true, 0 otherwise
-
template<typename T = int>
inline auto lt(T scalar) const# Check if each element is less than a scalar.
- Parameters:
scalar – The value to compare with
- Returns:
A new fusion_array containing 1 where the condition is true, 0 otherwise
-
template<typename T = int>
inline auto eq(T scalar) const# Check if each element is equal to a scalar.
- Parameters:
scalar – The value to compare with
- Returns:
A new fusion_array containing 1 where the condition is true, 0 otherwise
-
template<typename BinaryOp>
inline auto map_adj(BinaryOp op) const -> fusion_array<thrust::transform_iterator<thrust::zip_function<BinaryOp>, thrust::zip_iterator<thrust::tuple<Iterator, Iterator>>>># Apply a binary operation to adjacent elements.
- Parameters:
op – The binary operation to apply
- Returns:
A new fusion_array with the operation applied
-
inline auto differ() const#
Check if adjacent elements are different.
- Returns:
A new fusion_array containing 1 where adjacent elements differ, 0 otherwise
-
inline auto deltas() const#
Compute differences between adjacent elements.
- Returns:
A new fusion_array containing the differences
-
inline auto dble() const#
Double each element (multiply by 2)
- Returns:
A new fusion_array with doubled values
-
inline auto half() const#
Halve each element (divide by 2)
- Returns:
A new fusion_array with halved values
-
inline auto abs() const#
Calculate absolute value of each element.
- Returns:
A new fusion_array with absolute values
-
inline auto log() const#
Calculate natural logarithm of each element.
- Returns:
A new fusion_array with logarithm values
-
inline auto exp() const#
Calculate exponential of each element.
- Returns:
A new fusion_array with exponential values
-
inline auto neg() const#
Check if each element is odd.
- Returns:
A new fusion_array containing 1 for odd elements, 0 for even
-
inline auto odd() const#
Check if each element is odd.
- Returns:
A new fusion_array containing 1 for odd elements, 0 for even
-
inline auto even() const#
Check if each element is even.
- Returns:
A new fusion_array containing 1 for even elements, 0 for odd
-
inline auto sign() const#
Get sign of each element.
- Returns:
A new fusion_array containing 1 for positive, 0 for zero, -1 for negative
-
inline auto sqrt() const#
Calculate square root of each element.
- Returns:
A new fusion_array with square root values
-
inline auto sq() const#
Square each element.
- Returns:
A new fusion_array with squared values
-
template<typename TargetType>
inline auto as() const# Cast each element to a different type.
- Template Parameters:
TargetType – The target type to cast elements to
- Returns:
A new fusion_array with elements cast to the target type
-
inline auto rand() const#
Generate random values between 0 and each element.
- Returns:
A new fusion_array with random values
-
inline auto size() const -> int#
Get the size of the array.
- Returns:
The number of elements
-
template<typename T = int>
inline auto append(T value) const# Append a value to the array.
- Parameters:
value – The value to append
- Returns:
A new fusion_array with the appended value
-
template<typename T = int>
inline auto prepend(T value) const# Prepend a value to the array.
- Parameters:
value – The value to prepend
- Returns:
A new fusion_array with the prepended value
-
inline auto sort() const#
Sort the array (eager operation)
- Returns:
A new fusion_array with sorted elements
-
template<typename BinaryComp>
inline auto sort_by(BinaryComp comp) const# Sort the array using a custom comparator (eager operation)
- Parameters:
comp – The binary comparator to use for sorting
- Returns:
A new fusion_array with sorted elements
-
template<typename KeyFunc>
inline auto sort_by_key(KeyFunc key_func) const -> fusion_array<typename thrust::device_vector<typename cuda::std::iterator_traits<Iterator>::value_type>::iterator># Sort the array based on keys produced by a unary function (eager operation)
- Parameters:
key_func – The unary function to extract keys for comparison
- Returns:
A new fusion_array with sorted elements
-
template<int Axis = 0, typename T, typename BinaryOp>
inline auto reduce(T init, BinaryOp op, std::integral_constant<int, Axis> axis = {}) const# Generic reduction with custom binary operation (eager operation)
- Template Parameters:
Axis – The axis along which to reduce (0=all elements, 2=row-wise)
- Parameters:
init – The initial value for the reduction
op – The binary operation to apply for reduction
axis – Optional integral_constant parameter for axis (allows 2_ic syntax)
- Returns:
A fusion_array containing the reduction result(s)
-
template<int Axis = 0, typename T = int>
inline auto maxr(std::integral_constant<int, Axis> axis = {}) const# Maximum reduction (eager operation)
- Template Parameters:
Axis – The axis along which to reduce (0=all elements, 2=row-wise)
- Parameters:
axis – Optional integral_constant parameter for axis (allows 2_ic syntax)
- Returns:
A fusion_array containing the maximum value(s)
-
template<int Axis = 0, typename T = int>
inline auto minr(std::integral_constant<int, Axis> axis = {}) const# Minimum reduction (eager operation)
- Template Parameters:
Axis – The axis along which to reduce (0=all elements, 2=row-wise)
- Parameters:
axis – Optional integral_constant parameter for axis (allows 2_ic syntax)
- Returns:
A fusion_array containing the minimum value(s)
-
template<int Axis = 0, typename T = int>
inline auto sum(std::integral_constant<int, Axis> axis = {}) const# Sum reduction (eager operation)
- Template Parameters:
Axis – The axis along which to reduce (0=all elements, 2=row-wise)
- Parameters:
axis – Optional integral_constant parameter for axis (allows 2_ic syntax)
- Returns:
A fusion_array containing the sum of elements
-
template<int Axis = 0, typename T = int>
inline auto any(std::integral_constant<int, Axis> axis = {}) const# Check if any element is non-zero (eager operation)
- Template Parameters:
Axis – The axis along which to reduce (0=all elements, 2=row-wise)
- Parameters:
axis – Optional integral_constant parameter for axis (allows 2_ic syntax)
- Returns:
A fusion_array containing true if any element is non-zero, false otherwise
-
template<int Axis = 0, typename T = int>
inline auto all(std::integral_constant<int, Axis> axis = {}) const# Check if all elements are non-zero (eager operation)
- Template Parameters:
Axis – The axis along which to reduce (0=all elements, 2=row-wise)
- Parameters:
axis – Optional integral_constant parameter for axis (allows 2_ic syntax)
- Returns:
A fusion_array containing true if all elements are non-zero, false otherwise
-
inline auto value() const#
Get the first value (useful for extracting reduction results)
- Returns:
The first value in the array, properly handling thrust pairs
-
inline auto front() const#
Get the first element of the array.
- Returns:
The first element in the array
-
inline auto back() const#
Get the last element of the array.
- Returns:
The last element in the array
-
inline auto to_host() const#
Copy elements from device to host memory.
- Returns:
A std::vector containing the host-side values
-
template<int Axis = 0, typename T = int>
inline auto prod() const# Product reduction (eager operation)
- Template Parameters:
Axis – The axis along which to reduce (0=all elements, 2=row-wise)
- Returns:
A fusion_array containing the product of elements
-
inline auto where() const#
Get indices where elements are non-zero (lazy operation)
- Returns:
A masked fusion_array containing the 1-indexed positions where elements are non-zero
-
template<typename MaskIteratorKeep>
inline auto keep(const fusion_array<MaskIteratorKeep> &mask) const# Filter elements based on a mask array (lazy operation)
- Parameters:
mask – The mask array (elements kept where mask is non-zero)
- Returns:
A masked fusion_array that will filter elements when evaluated
-
template<typename IndexIterator>
inline auto gather(const fusion_array<IndexIterator> &indices) const# Gather elements from this array using the provided indices.
- Parameters:
indices – Array of indices to gather from
- Returns:
A new fusion_array containing the gathered elements
-
template<typename OtherIterator>
inline auto match(const fusion_array<OtherIterator> &other) const -> bool# Check if arrays match element by element (eager operation)
- Parameters:
other – The array to compare with
- Returns:
True if arrays match exactly, false otherwise
-
template<int Axis = 0>
inline auto anys(std::integral_constant<int, Axis> axis = {}) const# Inclusive scan with logical OR (eager operation)
- Template Parameters:
Axis – The axis along which to scan (0=all elements, 1=column-wise, 2=row-wise)
- Parameters:
axis – Optional integral_constant parameter for axis (allows 2_ic syntax)
- Returns:
A new fusion_array containing running OR of elements
-
template<int Axis = 0>
inline auto alls(std::integral_constant<int, Axis> axis = {}) const# Inclusive scan with logical AND (eager operation)
- Template Parameters:
Axis – The axis along which to scan (0=all elements, 1=column-wise, 2=row-wise)
- Parameters:
axis – Optional integral_constant parameter for axis (allows 2_ic syntax)
- Returns:
A new fusion_array containing running AND of elements
-
template<int Axis = 0>
inline auto mins(std::integral_constant<int, Axis> axis = {}) const# Inclusive scan with minimum (eager operation)
- Template Parameters:
Axis – The axis along which to scan (0=all elements, 1=column-wise, 2=row-wise)
- Parameters:
axis – Optional integral_constant parameter for axis (allows 2_ic syntax)
- Returns:
A new fusion_array containing running minimum of elements
-
template<int Axis = 0>
inline auto maxs(std::integral_constant<int, Axis> axis = {}) const# Inclusive scan with maximum (eager operation)
- Template Parameters:
Axis – The axis along which to scan (0=all elements, 1=column-wise, 2=row-wise)
- Parameters:
axis – Optional integral_constant parameter for axis (allows 2_ic syntax)
- Returns:
A new fusion_array containing running maximum of elements
-
template<int Axis = 0>
inline auto sums(std::integral_constant<int, Axis> axis = {}) const# Inclusive scan with addition (eager operation)
- Template Parameters:
Axis – The axis along which to scan (0=all elements, 1=column-wise, 2=row-wise)
- Parameters:
axis – Optional integral_constant parameter for axis (allows 2_ic syntax)
- Returns:
A new fusion_array containing running sum of elements
-
template<int Axis = 0>
inline auto prods(std::integral_constant<int, Axis> axis = {}) const# Inclusive scan with multiplication (eager operation)
- Template Parameters:
Axis – The axis along which to scan (0=all elements, 1=column-wise, 2=row-wise)
- Parameters:
axis – Optional integral_constant parameter for axis (allows 2_ic syntax)
- Returns:
A new fusion_array containing running product of elements
-
template<int Axis = 0, typename BinaryOp>
inline auto scan(BinaryOp op, std::integral_constant<int, Axis> axis = {}) const# Generic inclusive scan with custom binary operation (eager operation)
Allows applying any custom binary scan operation to the array elements. This provides flexibility beyond the predefined scans like sums, prods, mins, maxs.
- Parameters:
op – The binary operation to apply for the scan (must be associative)
axis – Optional integral_constant parameter for axis (allows 2_ic syntax)
- Returns:
A new fusion_array containing the running results of applying the operation
-
inline auto uniq() const#
Remove adjacent duplicate elements (lazy operation)
- Returns:
A masked fusion_array that will filter out adjacent duplicates when evaluated
-
inline auto distinct() const#
Remove duplicate elements from the array (lazy-ish operation)
If the array is already sorted (_is_sorted is true), this method applies a unique operation to remove adjacent duplicates. If the array is not sorted, it first sorts the array and then removes duplicates.
- Returns:
A new fusion_array containing only unique elements
-
inline auto _mark_sorted() const#
-
inline auto rle() const#
Run-length encode the array (eager operation)
- Returns:
A fusion_array of thrust::pairs with value and count
-
template<typename BinPred, typename BinaryOp>
inline auto chunk_by_reduce(BinPred pred, BinaryOp binop) const# Group consecutive elements by a predicate and reduce each group.
- Template Parameters:
BinPred – The predicate for grouping consecutive elements
BinaryOp – The binary operation for reduction within each group
- Parameters:
pred – The grouping predicate
binop – The reduction operation
- Returns:
A fusion_array containing the reduced values for each group
-
inline auto reshape(std::initializer_list<int> new_shape) const#
Reshape the array to the specified dimensions.
The total size of the new shape must be less than or equal to the current array size. Some data may be truncated if the new size is smaller. For cycling data to fill larger shapes, use cycle() instead.
See also
- Parameters:
new_shape – The new shape as an initializer list of dimensions
- Throws:
std::invalid_argument – if total_size > size() or either size is 0
- Returns:
A new fusion_array with the specified shape
-
inline auto cycle(std::initializer_list<int> new_shape) const#
Cycle the array data to fill the specified shape.
If the new shape’s total size is greater than the array size, the current data is cycled to fill the new shape. If the new shape’s total size is less than or equal to the array size, behaves like reshape().
See also
- Parameters:
new_shape – The new shape as an initializer list of dimensions
- Throws:
std::invalid_argument – if either size is 0
- Returns:
A new fusion_array with the specified shape
-
inline auto repeat(int n) const#
Repeat a scalar value to create a 1D array of the specified size.
This method only works on scalar arrays (rank = 0). It repeats the scalar value n times to create a 1D array. For arrays with rank > 0, use cycle() instead.
See also
- Parameters:
n – The number of times to repeat the scalar value
- Throws:
std::invalid_argument – if rank != 0 or n <= 0
- Returns:
A new fusion_array with n elements all equal to the scalar value
-
inline auto replicate(int n) const#
Repeat each element of the array n times (lazy operation with storage preservation)
For an array [1, 2, 3] with n=2, returns [1, 1, 2, 2, 3, 3]. This operation uses lazy iterators but ensures storage safety by preserving the source array’s storage. This is different from repeat() which only works on scalars, and from the mask-based replicate() which materializes.
See also
repeat, replicate(const fusion_array<MaskIterType>&)
- Parameters:
n – The number of times to repeat each element
- Throws:
std::invalid_argument – if n <= 0
- Returns:
A new fusion_array with each element repeated n times
-
template<typename MaskIterType>
inline auto replicate(const fusion_array<MaskIterType> &mask) const -> fusion_array<typename thrust::device_vector<value_type>::iterator, no_mask_t># Repeat each element of the array by the corresponding mask value (materializing operation using gather pattern)
For an array [1, 2, 3] with mask [2, 1, 3], returns [1, 1, 2, 3, 3, 3]. Each element at index i is repeated mask[i] times. This operation requires materialization due to the variable repetition pattern and uses efficient index generation and permutation iterator pattern. Unlike the scalar replicate(), this cannot be efficiently represented as a lazy iterator.
See also
replicate(int)
- Parameters:
mask – Array of integer values specifying how many times to repeat each element
- Throws:
std::invalid_argument – if mask size doesn’t match array size or any mask value < 0
- Returns:
A new fusion_array with each element repeated according to the mask
-
template<typename OtherIterator>
inline auto cross(const fusion_array<OtherIterator> &other) const# Compute the cartesian product with another array (lazy operation)
For arrays [1, 2] and [a, b], returns [(1, a), (1, b), (2, a), (2, b)]. The implementation replicates each element of this array by the size of the other array, and cycles the other array to match the total size, then pairs them together.
- Parameters:
other – The other array to compute the cartesian product with
- Throws:
std::invalid_argument – if either array is empty
- Returns:
A new fusion_array containing pairs representing the cartesian product
-
template<typename OtherIterator, typename BinaryOp>
inline auto outer(const fusion_array<OtherIterator> &other, BinaryOp binary_op) const# Compute the outer product with another array as a 2D matrix (lazy operation)
For arrays [1, 2] and [a, b] with operation
op
, returns a 2×2 matrix: [op(1, a), op(1, b)] [op(2, a), op(2, b)] This is much more efficient than the cycle-based approach.- Parameters:
other – The other array to compute the outer product with
binary_op – The binary operation to apply to each pair of elements
- Throws:
std::invalid_argument – if either array is empty
- Returns:
A new fusion_array containing the results in a 2D matrix shape
-
inline auto print(std::ostream &os = std::cout, const char *delimiter = " ") const#
Print the array contents to a stream.
- Parameters:
os – The output stream (defaults to std::cout)
delimiter – The string to print between elements (defaults to space)
- Returns:
For masked arrays, returns the unmasked array; otherwise returns a reference to this array
-
inline auto minmax() const#
Find the minimum and maximum values in the array (eager operation)
- Returns:
A fusion_array containing a thrust::pair with first=minimum and second=maximum
-
inline auto take(int n) const#
Take the first n elements of the array.
- Parameters:
n – The number of elements to take
- Throws:
std::invalid_argument – if n > size() or n < 0
- Returns:
A new fusion_array containing only the first n elements
-
inline auto drop(int n) const#
Drop the first n elements of the array.
- Parameters:
n – The number of elements to drop
- Throws:
std::invalid_argument – if n > size() or n < 0
- Returns:
A new fusion_array containing all elements except the first n
-
inline auto flatten() const#
Flatten the array to rank 1 (modifies shape only, no data copy)
- Returns:
A new fusion_array with the same data but flattened to 1D
-
inline auto row(int row_idx) const#
Get a copy of a specific row (with data copy)
- Parameters:
row_idx – The row index to extract
- Throws:
std::runtime_error – if array rank != 2 or row_idx out of bounds
- Returns:
A fusion_array copy of the specified row
-
template<typename T>
inline auto index_of(const T &value) const -> int# Find the first index of a value in the array (0-based)
- Parameters:
value – The value to search for
- Throws:
std::runtime_error – if array rank != 1
- Returns:
The 0-based index of the first occurrence, or -1 if not found
-
template<typename T>
inline auto last_index_of(const T &value) const -> int# Find the last index of a value in the array (0-based)
- Parameters:
value – The value to search for
- Throws:
std::runtime_error – if array rank != 1
- Returns:
The 0-based index of the last occurrence, or -1 if not found
-
template<typename KeyExtractor>
inline auto max_by_key(KeyExtractor key_extractor) const -> fusion_array<typename thrust::device_vector<typename cuda::std::iterator_traits<Iterator>::value_type>::iterator># Find the maximum element based on key extractor.
- Template Parameters:
KeyExtractor – The type of the key extractor functor
- Parameters:
key_extractor – A functor that extracts a key for comparison from each element
- Returns:
A fusion_array containing the maximum element based on the extracted key
-
template<typename OtherIterator>
inline auto pairs(const fusion_array<OtherIterator> &other) const# Create pairs from two arrays of the same length.
- Template Parameters:
OtherIterator – The iterator type of the second array
- Parameters:
other – The second array to pair with this array
- Throws:
std::invalid_argument – if arrays have different sizes
- Returns:
A new fusion_array containing pairs of elements from both arrays
-
inline auto enumerate() const#
Create pairs from array elements and their indices (lazy operation)
Creates pairs of (element, index) starting from index 0. For array [a, b, c], returns [(a, 0), (b, 1), (c, 2)]. Useful for operations that need to track element positions.
- Returns:
A new fusion_array containing pairs of (element, index)
-
inline auto rev() const#
Reverse the array (lazy operation)
- Returns:
A new fusion_array with elements in reverse order
-
template<typename Predicate>
inline auto filter(Predicate predicate) const# Filter elements based on a predicate (lazy operation)
- Parameters:
predicate – A unary predicate that returns true for elements to keep
- Returns:
A masked fusion_array that will filter elements when evaluated
-
inline auto transpose() const#
Transpose a 2D array (lazy operation)
- Throws:
std::invalid_argument – if the array is not rank 2
- Returns:
A new fusion_array representing the transpose of the original
-
inline auto operator/(auto const &arg) const#
-
inline auto operator-(auto const &arg) const#
-
inline auto operator+(auto const &arg) const#
-
inline auto operator*(auto const &arg) const#
-
inline auto operator<(auto const &arg) const#
-
inline auto operator<=(auto const &arg) const#
-
inline auto operator>(auto const &arg) const#
-
inline auto operator>=(auto const &arg) const#
-
inline auto operator==(auto const &arg) const#
-
inline const std::shared_ptr<void> &storage() const#