51 [[nodiscard]]
inline typename std::iterator_traits<Iter1>::difference_type
distance(Iter1 first, Iter2 last) {
53 if constexpr (std::is_same_v<typename std::iterator_traits<Iter1>::iterator_category, std::random_access_iterator_tag>) {
56 typename std::iterator_traits<Iter1>::difference_type r(0);
57 for (; first != last; ++first) ++r;
92 [[nodiscard]]
inline std::pair<std::ptrdiff_t, std::ptrdiff_t>
chunk_range(std::ptrdiff_t first, std::ptrdiff_t last,
long n_chunks,
long rank) {
93 auto total_size = last - first;
94 auto chunk_size = total_size / n_chunks;
95 auto n_large_nodes = total_size - n_chunks * chunk_size;
96 if (rank < n_large_nodes)
97 return {first + rank * (chunk_size + 1), first + (rank + 1) * (chunk_size + 1)};
99 return {first + n_large_nodes + rank * chunk_size, first + n_large_nodes + (rank + 1) * chunk_size};
std::pair< std::ptrdiff_t, std::ptrdiff_t > chunk_range(std::ptrdiff_t first, std::ptrdiff_t last, long n_chunks, long rank)
Given an integer range [first, last), divide it as equally as possible into N chunks.