fusion_array#

Overview#

The fusion_array class is the core of Parrot, providing fused evaluation of array operations.

Note

You can create arrays in various ways:

// Using template argument deduction with initializer list
auto arr2 = parrot::array({1, 2, 3, 4});      // Deduces int
auto arr3 = parrot::array({1.5, 2.5, 3.5});   // Deduces double

// Creating a range (1-indexed)
auto arr4 = parrot::range(10);                // Creates [1,2,3...10]

// Creating a scalar array
auto scalar_arr = parrot::scalar(5);

// Creating a matrix with all elements set to a value
auto mat = parrot::matrix(3.14, {3, 4});      // Creates a 3x4 matrix filled with 3.14

Class Documentation#

Fused Operations#

1-index Maps (Unary)#

template<typename UnaryFunctor>
inline auto parrot::fusion_array::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

inline auto parrot::fusion_array::abs() const#

Calculate absolute value of each element.

Returns:

A new fusion_array with absolute values

inline auto parrot::fusion_array::dble() const#

Double each element (multiply by 2)

Returns:

A new fusion_array with doubled values

inline auto parrot::fusion_array::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 parrot::fusion_array::even() const#

Check if each element is even.

Returns:

A new fusion_array containing 1 for even elements, 0 for odd

inline auto parrot::fusion_array::half() const#

Halve each element (divide by 2)

Returns:

A new fusion_array with halved values

inline auto parrot::fusion_array::log() const#

Calculate natural logarithm of each element.

Returns:

A new fusion_array with logarithm values

inline auto parrot::fusion_array::exp() const#

Calculate exponential of each element.

Returns:

A new fusion_array with exponential values

inline auto parrot::fusion_array::neg() const#

Check if each element is odd.

Returns:

A new fusion_array containing 1 for odd elements, 0 for even

inline auto parrot::fusion_array::odd() const#

Check if each element is odd.

Returns:

A new fusion_array containing 1 for odd elements, 0 for even

inline auto parrot::fusion_array::rand() const#

Generate random values between 0 and each element.

Returns:

A new fusion_array with random values

inline auto parrot::fusion_array::sign() const#

Get sign of each element.

Returns:

A new fusion_array containing 1 for positive, 0 for zero, -1 for negative

inline auto parrot::fusion_array::sq() const#

Square each element.

Returns:

A new fusion_array with squared values

inline auto parrot::fusion_array::sqrt() const#

Calculate square root of each element.

Returns:

A new fusion_array with square root values

1-index Maps (Binary)#

Warning

doxygenfunction: Unable to resolve function “parrot::fusion_array::map2” with arguments (const fusion_array<OtherIterator>&, BinaryFunctor) const in doxygen xml output for project “parrot” from directory: ./doxyoutput/xml/. Potential matches:

- template<typename OtherIterator, typename OtherMaskIterator, typename BinaryFunctor> auto map2(const fusion_array<OtherIterator, OtherMaskIterator> &value, BinaryFunctor binary_op) const
- template<typename T, typename BinaryFunctor> auto map2(const T &value, BinaryFunctor binary_op) const -> decltype(auto)
template<typename T, typename BinaryFunctor>
inline auto parrot::fusion_array::map2(const T &value, BinaryFunctor binary_op) const -> decltype(auto)#
template<typename T>
inline auto parrot::fusion_array::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>
inline auto parrot::fusion_array::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 parrot::fusion_array::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 parrot::fusion_array::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 parrot::fusion_array::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 parrot::fusion_array::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 parrot::fusion_array::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 parrot::fusion_array::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 = int>
inline auto parrot::fusion_array::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 parrot::fusion_array::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 parrot::fusion_array::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 = int>
inline auto parrot::fusion_array::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 OtherIterator>
inline auto parrot::fusion_array::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

2-index Maps#

template<typename BinaryOp>
inline auto parrot::fusion_array::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 parrot::fusion_array::deltas() const#

Compute differences between adjacent elements.

Returns:

A new fusion_array containing the differences

inline auto parrot::fusion_array::differ() const#

Check if adjacent elements are different.

Returns:

A new fusion_array containing 1 where adjacent elements differ, 0 otherwise

Joins#

template<typename T = int>
inline auto parrot::fusion_array::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 parrot::fusion_array::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

Products#

template<typename OtherIterator>
inline auto parrot::fusion_array::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.

See also

replicate, cycle, pairs

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

Note

The cross method computes the cartesian product of two arrays. For arrays [1, 2] and [a, b], it returns pairs [(1, a), (1, b), (2, a), (2, b)]. The implementation replicates each element of the first array by the size of the second array, and cycles the second array to match the total size, then pairs them together.

template<typename OtherIterator, typename BinaryOp>
inline auto parrot::fusion_array::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.

See also

cross, reshape, map

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

Note

The outer method computes the outer product of two arrays as a 2D matrix using a binary operation. For arrays [1, 2] and [a, b] with operation op, it returns a 2×2 matrix: [op(1, a), op(1, b)] [op(2, a), op(2, b)] This is equivalent to cross(other).map(binary_op_adapter(op)).reshape({this.size(), other.size()}).

Reshapes#

inline auto parrot::fusion_array::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 parrot::fusion_array::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 parrot::fusion_array::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 parrot::fusion_array::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

cycle

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

Note

The reshape method returns a new fusion_array with the specified shape. 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.

inline auto parrot::fusion_array::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

reshape

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

Note

The cycle method returns a new fusion_array with 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, it behaves like reshape().

inline auto parrot::fusion_array::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

cycle

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

Copying#

inline auto parrot::fusion_array::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

Note

Scalar overload (Fused/Lazy): replicate(int n) repeats each element of the array n times. For an array [1, 2, 3] with n=2, it returns [1, 1, 2, 2, 3, 3]. This operation uses lazy iterators for efficiency and can be fused with subsequent operations. This is different from repeat() which only works on scalar arrays (rank = 0).

Permutations#

inline auto parrot::fusion_array::rev() const#

Reverse the array (lazy operation)

Returns:

A new fusion_array with elements in reverse order

template<typename IndexIterator>
inline auto parrot::fusion_array::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

Conditionally Fused Operations#

Compactions#

template<typename MaskIteratorKeep>
inline auto parrot::fusion_array::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 Predicate>
inline auto parrot::fusion_array::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 parrot::fusion_array::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

inline auto parrot::fusion_array::uniq() const#

Remove adjacent duplicate elements (lazy operation)

Returns:

A masked fusion_array that will filter out adjacent duplicates when evaluated

inline auto parrot::fusion_array::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

Materializing Operations#

Reductions#

template<int Axis = 0, typename T, typename BinaryOp>
inline auto parrot::fusion_array::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 parrot::fusion_array::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

template<int Axis = 0, typename T = int>
inline auto parrot::fusion_array::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 parrot::fusion_array::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<typename KeyExtractor>
inline auto parrot::fusion_array::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

Note

The max_by_key method finds the maximum element based on a key extractor function. It’s especially useful when working with pairs or complex objects where you want to find the maximum based on a specific field or computation.

template<int Axis = 0, typename T = int>
inline auto parrot::fusion_array::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)

inline auto parrot::fusion_array::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

template<int Axis = 0, typename T = int>
inline auto parrot::fusion_array::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

template<int Axis = 0, typename T = int>
inline auto parrot::fusion_array::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

Scans#

template<int Axis = 0, typename BinaryOp>
inline auto parrot::fusion_array::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.

See also

sums, prods, mins, maxs, anys, alls for common predefined scans

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

template<int Axis = 0>
inline auto parrot::fusion_array::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 parrot::fusion_array::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 parrot::fusion_array::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 parrot::fusion_array::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 parrot::fusion_array::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>
inline auto parrot::fusion_array::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

Permutations#

inline auto parrot::fusion_array::sort() const#

Sort the array (eager operation)

Returns:

A new fusion_array with sorted elements

template<typename BinaryComp>
inline auto parrot::fusion_array::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 parrot::fusion_array::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

Compactions#

inline auto parrot::fusion_array::rle() const#

Run-length encode the array (eager operation)

Returns:

A fusion_array of thrust::pairs with value and count

Copying#

template<typename MaskIterType>
inline auto parrot::fusion_array::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

Note

Mask overload (Materializing): replicate(mask_array) repeats each element according to the corresponding value in the mask array. For an array [1, 2, 3] with mask [2, 1, 3], it returns [1, 1, 2, 3, 3, 3]. This operation requires materialization due to the variable repetition pattern and uses efficient index generation with permutation iterators. This is useful for operations like duplicating zeros: arr.replicate(arr.eq(0).add(1)).

Split-Reductions#

template<typename BinPred, typename BinaryOp>
inline auto parrot::fusion_array::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

Comparisons#

template<typename OtherIterator>
inline auto parrot::fusion_array::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

Properties#

inline auto parrot::fusion_array::size() const -> int#

Get the size of the array.

Returns:

The number of elements

inline auto parrot::fusion_array::rank() const -> int#

Get the rank (dimensionality) of the array.

Returns:

The number of dimensions in the array’s shape

inline auto parrot::fusion_array::shape() const -> std::vector<int>#

Accessors#

inline auto parrot::fusion_array::value() const#

Get the first value (useful for extracting reduction results)

Returns:

The first value in the array, properly handling thrust pairs

inline auto parrot::fusion_array::front() const#

Get the first element of the array.

Returns:

The first element in the array

inline auto parrot::fusion_array::back() const#

Get the last element of the array.

Returns:

The last element in the array

inline auto parrot::fusion_array::to_host() const#

Copy elements from device to host memory.

Returns:

A std::vector containing the host-side values

Array Creation#

Parrot provides several convenient functions for creating fusion_array objects. These functions handle memory management automatically and provide efficient lazy evaluation where possible.

template<typename T>
auto parrot::array(std::initializer_list<T> init_list)

Create a fusion_array from an initializer list with automatic type deduction.

Template Parameters:

T – The element type (automatically deduced)

Parameters:

init_list – The initializer list containing the array elements

Returns:

A fusion_array containing the initializer list’s data on the device

template<typename T>
auto parrot::array(const std::vector<T> &host_vec)

Create a fusion_array from a std::vector.

Template Parameters:

T – The element type of the vector

Parameters:

host_vec – The host vector to copy data from

Returns:

A fusion_array containing the vector’s data on the device

inline auto parrot::range(int end) -> fusion_array<thrust::counting_iterator<int>>

Create a 1-indexed range array from 1 to n.

Parameters:

end – The upper bound of the range (inclusive)

Throws:

std::invalid_argument – if end <= 0

Returns:

A fusion_array containing integers from 1 to end

template<typename T>
auto parrot::scalar(T value)

Create a scalar (rank 0) array containing a single value.

Template Parameters:

T – The element type (automatically deduced)

Parameters:

value – The scalar value to store in the array

Returns:

A fusion_array with rank 0 containing the single value

Warning

doxygenfunction: Unable to resolve function “parrot::matrix” with arguments None in doxygen xml output for project “parrot” from directory: ./doxyoutput/xml/. Potential matches:

- template<typename T> auto matrix(T value, std::initializer_list<int> shape)
- template<typename T> auto matrix(std::initializer_list<std::initializer_list<T>> nested_list)

I/O#

inline auto parrot::fusion_array::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

Function Objects#

Accessors#

struct parrot::fst {
    template <typename T, typename U>
    __host__ __device__
    T operator()(const thrust::pair<T, U>& p) const;
};

The fst function extracts the first element of a pair. Useful with functions like max_by_key.

Example:

// Find the pair with the maximum first element
auto result = pairs.max_by_key(parrot::fst());
struct parrot::snd {
    template <typename T, typename U>
    __host__ __device__
    U operator()(const thrust::pair<T, U>& p) const;
};

The snd function extracts the second element of a pair. Useful with functions like max_by_key.

Example:

// Find the pair with the maximum second element
auto result = pairs.max_by_key(parrot::snd());

Binary Operations#

struct parrot::eq {
    template <typename T>
    __host__ __device__
    bool operator()(const T& a, const T& b) const;
};
struct parrot::gt {
    template <typename T>
    __host__ __device__
    bool operator()(const T& a, const T& b) const;
};
struct parrot::gte {
    template <typename T>
    __host__ __device__
    bool operator()(const T& a, const T& b) const;
};
struct parrot::lt {
    template <typename T>
    __host__ __device__
    bool operator()(const T& a, const T& b) const;
};
struct parrot::lte {
    template <typename T>
    __host__ __device__
    bool operator()(const T& a, const T& b) const;
};
struct parrot::max {
    template <typename T>
    __host__ __device__
    T operator()(const T& a, const T& b) const;
};
struct parrot::min {
    template <typename T>
    __host__ __device__
    T operator()(const T& a, const T& b) const;
};
struct parrot::mul {
    template <typename T>
    __host__ __device__
    T operator()(const T& a, const T& b) const;
};
struct parrot::add {
    template <typename T>
    __host__ __device__
    T operator()(const T& a, const T& b) const;
};

Statistical Functions#

template<typename Iterator, typename MaskIterator>
auto parrot::stats::norm_cdf(const fusion_array<Iterator, MaskIterator> &arr)

Note

This function computes the cumulative distribution function of the standard normal distribution. It’s particularly useful for statistical computations and probability calculations.

template<typename Iterator, typename MaskIterator>
auto parrot::stats::mode(const fusion_array<Iterator, MaskIterator> &arr)

Note

This function computes the statistical mode (most frequently occurring value) of an array. It returns the value that appears most frequently in the array. If all values appear with equal frequency, it returns the smallest value after sorting. The implementation uses efficient operations: sorting, run-length encoding, and finding the maximum by count.