counting_set
-
template<typename Key, typename CountValue = size_t>
class counting_set : public ygm::container::detail::base_count<counting_set<Key, size_t>, std::tuple<Key, size_t>>, public ygm::container::detail::base_contains<counting_set<Key, size_t>, std::tuple<Key, size_t>>, public ygm::container::detail::base_misc<counting_set<Key, size_t>, std::tuple<Key, size_t>>, public ygm::container::detail::base_iterators<counting_set<Key, size_t>>, public ygm::container::detail::base_iteration_key_value<counting_set<Key, size_t>, std::tuple<Key, size_t>> ygm::container::mapthat is specialized for counting occurrences of items in a stream. Mapped type can be specified if size_t is not appropriate.Adds a local cache of objects to reduce sends of frequently-occurring items.
Public Types
-
using self_type = counting_set<Key, CountValue>
-
using mapped_type = CountValue
-
using size_type = size_t
-
using for_all_args = std::tuple<Key, CountValue>
-
using container_type = ygm::container::counting_set_tag
-
using iterator = typename internal_container_type::iterator
-
using const_iterator = typename internal_container_type::const_iterator
Public Functions
-
inline counting_set(ygm::comm &comm)
counting_set constructor
- Parameters:
comm – Communicator to use for communication
-
counting_set() = delete
-
inline counting_set(ygm::comm &comm, std::initializer_list<Key> l)
counting_set constructor from std::initializer_list of values
Initializer list is assumed to be replicated on all ranks.
- Parameters:
comm – Communicator to use for communication
l – Initializer list of values to put in counting_set
-
inline counting_set(ygm::comm &comm, std::ranges::input_range auto &&range)
Construct counting_set from std::ranges::input_range of values.
Input range is assumed to be unique on all ranks.
- Parameters:
comm – Communicator to use for communication
range – Input range of values to put in counting_set
-
inline ~counting_set()
-
inline counting_set &operator=(const self_type &other)
-
inline counting_set &operator=(self_type &&other)
-
inline bool operator==(const self_type &other) const
Check if two counting sets are equal.
- Parameters:
other – Counting set to compare with
- Returns:
true if counting sets are equal, false otherwise
-
inline iterator local_begin()
Access to begin iterator of locally-held items.
Does not call
barrier().- Returns:
Local iterator to beginning of items held by process.
-
inline const_iterator local_begin() const
Access to begin const_iterator of locally-held items for const counting_set.
Does not call
barrier().- Returns:
Local const iterator to beginning of items held by process.
-
inline const_iterator local_cbegin() const
Access to begin const_iterator of locally-held items for const counting_set.
Does not call
barrier().- Returns:
Local const iterator to beginning of items held by process.
-
inline iterator local_end()
Access to end iterator of locally-held items.
Does not call
barrier().- Returns:
Local iterator to end of items held by process.
-
inline const_iterator local_end() const
Access to end const_iterator of locally-held items for const counting_set.
Does not call
barrier().- Returns:
Local const iterator to ending of items held by process.
-
inline const_iterator local_cend() const
Access to end const_iterator of locally-held items for const counting_set.
Does not call
barrier().- Returns:
Local const iterator to ending of items held by process.
-
inline void async_insert(const key_type &key)
Asynchronously insert an item for counting.
Inserts item into local cache before sending count to remote location
- Parameters:
key – Item to count
-
template<typename Function>
inline void local_for_all(Function &&fn) Execute a functor on every locally-held item and count.
- Template Parameters:
Function – functor type
- Parameters:
fn – Functor to execute on items and counts
-
template<typename Function>
inline void local_for_all(Function &&fn) const Execute a functor on every locally-held item and count for a const container.
- Template Parameters:
Function – functor type
- Parameters:
fn – Functor to execute on items and counts
-
inline void local_clear()
Clear the local storage of the counting_set.
-
inline void clear()
Clear the global storage of the counting_set.
-
inline size_t local_size() const
Get the number of items held locally.
- Returns:
Number of locally-held items
-
inline mapped_type local_count(const key_type &key) const
Get the total number of times a locally-held item has been counted so far.
Counts can be inaccurate before a
barrier()due to items still being cached on other processes or waiting to be sent inygm::commbuffers.- Returns:
Number of times a locally-held item has been counted
-
inline bool local_contains(const key_type &key) const
Check if a locally-held item exists.
- Parameters:
val – Value to check for
- Returns:
true if value exists locally, false otherwise
-
inline mapped_type count_all()
Count the total number of items counted.
- Returns:
Sum of all item counts
-
inline std::map<key_type, mapped_type> gather_keys(const std::vector<key_type> &keys)
Collective operation to look up item counts from each rank.
- Parameters:
keys – Keys local rank wants to collect counts for
- Returns:
std::mapof provided keys and their counts
-
inline ygm::ygm_ptr<self_type> get_ygm_ptr() const
Access to the ygm_ptr used by the container.
- Returns:
ygm_ptrused by the container when identifying itself inasynccalls on theygm::comm
-
inline void serialize(const std::string &fname)
Serialize counting set contents to collection of files.
- Parameters:
fname – Filename prefix to create names for files used by each rank
-
inline void deserialize(const std::string &fname)
Deserialize counting set contents from collection of files.
- Parameters:
fname – Filename prefix to create names for files used by each rank
-
inline auto count(const typename std::tuple_element<0, for_all_args>::type &value) const
Counts all occurrences of a value within a container.
- Parameters:
value – Value to search for within container (key in the case of containers with keys)
- Returns:
Count of times
valueis seen in container
-
inline bool contains(const typename std::tuple_element<0, for_all_args>::type &value) const
Checks for the presence of a value within a container.
- Parameters:
value – Value to search for within container (key in the case of containers with keys)
- Returns:
True if
valueexists in container; false otherwise.
-
inline size_t size() const
Gets number of elements in a YGM container.
- Returns:
Container size
-
inline void swap(derived_type &other)
Swaps the contents of a YGM container.
- Parameters:
other – Container to swap with
-
inline ygm::comm &comm() const
Access to underlying YGM communicator.
- Returns:
YGM communicator used for communication by container
-
inline ygm::ygm_ptr<derived_type> get_ygm_ptr()
Access to the ygm_ptr used by the container.
- Returns:
ygm_ptrused by the container when identifying itself inasynccalls on theygm::comm
-
inline auto begin()
Returns an iterator to the beginning of the local container’s data.
This function is primarly a convienience function for range based for loops
Warning
The iterator is a local iterator, not a global iterator
- Returns:
iterator to the beginning of the local container’s data.
-
inline auto begin() const
Returns a const_iterator to the beginning of the local container’s data.
This function is primarly a convienience function for range based for loops
Warning
The const_iterator is a local iterator, not a global iterator
- Returns:
auto const_iterator to the beginning of the local container’s data.
-
inline auto cbegin() const
Returns a const_iterator to the beginning of the local container’s data.
This function is primarly a convienience function for range based for loops
Warning
The const_iterator is a local iterator, not a global iterator
- Returns:
auto const_iterator to the beginning of the local container’s data.
-
inline auto end()
Returns an iterator to the end of the local container’s data.
This function is primarly a convienience function for range based for loops
Warning
The iterator is a local iterator, not a global iterator
- Returns:
iterator to the end of the local container’s data.
-
inline auto end() const
Returns a const_iterator to the end of the local container’s data.
This function is primarly a convienience function for range based for loops
Warning
The const_iterator is a local iterator, not a global iterator
- Returns:
auto const_iterator to the end of the local container’s data.
-
inline auto cend() const
Returns a const_iterator to the end of the local container’s data.
This function is primarly a convienience function for range based for loops
Warning
The const_iterator is a local iterator, not a global iterator
- Returns:
auto const_iterator to the end of the local container’s data.
-
template<typename Function>
inline void for_all(Function fn) Iterates over all items in a container and executes a user-provided function object on each.
The user-provided function is expected to take a single key and value as separate arguments that make a (key, value) pair within the container. If the user provides a lambda as their function object, the lambda is allowed to capture.
- Template Parameters:
Function – Type of user-provided function
- Parameters:
fn – User-provided function
-
template<typename Function>
inline void for_all(Function &&fn) const Const version of for_all that iterates over all items and passes them to the user function as const *.
The user-provided function is expected to take a single key and value as separate arguments that make a (key, value) pair within the container. If the user provides a lambda as their function object, the lambda is allowed to capture.
- Template Parameters:
Function – Type of user-provided function
- Parameters:
fn – User-provided function
-
template<typename STLContainer>
inline void gather(STLContainer >o, int rank) const Gather all values in an STL container.
Requires STL container to have a
value_typethat is (key, value) pairs from the YGM container- Template Parameters:
STLContainer – Type of STL container to gather to
- Parameters:
gto – Container to store results in
rank – Rank to tather results on. Use -1 to gather to all ranks
-
template<typename STLContainer>
inline void gather(STLContainer >o) const Gather all values in an STL container on all ranks.
Equivalent to
gather(gto, -1)- Template Parameters:
STLContainer – Type of STL container to gather to
- Parameters:
gto – Container to store results in
-
template<typename Compare = std::greater<std::pair<key_type, mapped_type>>>
inline std::vector<std::pair<key_type, mapped_type>> gather_topk(size_t k, Compare comp = Compare()) const Gather the k “largest” key-value pairs according to provided comparison function.
- Template Parameters:
Compare – Type of comparison operator
- Parameters:
k – Number of key-value pairs to gather
comp – Comparison function for identifying elements to gather
- Returns:
vector of largest key-value pairs
-
template<typename YGMContainer>
inline void collect(YGMContainer &c) const Collects all items in a new YGM container.
- Template Parameters:
YGMContainer – Container type
- Parameters:
c – Container to collect into
-
template<typename MapType, typename ReductionOp>
inline void reduce_by_key(MapType &map, ReductionOp reducer) const Reduces all values in key-value pairs with matching keys.
- Template Parameters:
MapType – Result YGM container type
ReductionOp – Functor type
- Parameters:
map – YGM container to hold result
reducer – Functor for combining values
-
template<typename TransformFunction>
transform_proxy_key_value<derived_type, TransformFunction> transform(TransformFunction &&ffn) Creates proxy that transforms key-value pairs in a container that are presented to user
for_allcalls.The underlying items within the container are not modified.
- Template Parameters:
TransformFunction – functor type
- Parameters:
ffn – Function to transform items in container
-
inline auto keys()
Access to container presenting only keys.
- Returns:
Transform object that returns only keys to user
-
inline auto values()
Access to container presenting only values.
- Returns:
Transform object that returns only values to user
-
inline flatten_proxy_key_value<derived_type> flatten()
Flattens STL containers of values to allow a function to be called on inner items individually.
Underlying container is not modified.
-
template<typename FilterFunction>
filter_proxy_key_value<derived_type, FilterFunction> filter(FilterFunction &&ffn) Filters items in a container so only allow
for_allto execute on those that satisfy a given predicate function.Filtered items are not removed from underlying container.
- Template Parameters:
FilterFunction – Functor type
- Parameters:
ffn – Function used to filter items in container.
Public Members
Friends
- friend struct detail::base_misc< counting_set< Key, CountValue >, std::tuple< Key, CountValue > >
-
using self_type = counting_set<Key, CountValue>