broadcast.h

Functions

static inline bool is_broadcastable(const std::vector<std::size_t> &src_shape, const std::vector<std::size_t> &dst_shape)

Test if src_shape can be broadcast to dst_shape

static inline std::vector<std::size_t> smallest_broadcastable_shape(const std::vector<std::size_t> &shape1, const std::vector<std::size_t> &shape2)

Get the smallest broadcastable shape from shape1 and shape2. Return an empty vector _.size() == 0 if the shapes can not be broadcast together.

template<typename FUNC>
static inline std::size_t src_to_dst_idx(std::size_t i, FUNC &&broadcast_rule, const std::vector<std::size_t> &strides)

Compute the destination index of source index i based on a broadcasting_rule. The destination index is weighted using strides. This function assumes that strides.size() == broadcast_rule.size()

template<typename RANDOM_ACCESS_CONST_ITERATOR, typename RANDOM_ACCESS_ITERATOR>
static inline void broadcast_data_copy(RANDOM_ACCESS_CONST_ITERATOR src, RANDOM_ACCESS_ITERATOR dst, const std::vector<std::size_t> &src_shape, const std::vector<std::size_t> &dst_shape, std::size_t itemsize = 1)

Perform a broadcast by copying data. This function assumes that src_shape can be broadcast to dst_shape, i.e., is_broadcastable(src_shape, dst_shape) == true. It further assumes that the data in src is stored in C-style order and that dst has enough space to store the broadcast result.