12namespace fpmas {
namespace random {
47 template<
typename T,
typename Generator_t>
49 const std::vector<T>& local_items, std::size_t n,
52 0, local_items.size()-1);
53 std::vector<T> random_items(n);
54 for(std::size_t i = 0; i < n; i++)
55 random_items[i] = local_items[local_random_item(gen)];
70 const std::vector<T>& local_items, std::size_t n) {
114 const std::vector<T>& local_items, std::size_t n) {
116 std::vector<std::size_t> item_counts
119 std::unordered_map<int, std::size_t> requests;
122 for(std::size_t i = 0; i < n; i++)
124 requests = int_mpi.
allToAll(requests);
126 std::unordered_map<int, std::vector<T>> random_items;
127 for(
auto item : requests)
128 random_items[item.first]
133 std::vector<std::vector<T>> results(comm.
getSize());
134 for(
auto item : item_mpi.
allToAll(random_items))
135 results[item.first] = item.second;
171 const std::vector<T>& local_items, std::size_t n) {
177 std::vector<std::size_t> item_counts
182 std::unordered_map<int, std::size_t> requests;
187 std::size_t local_items_count = 0;
188 for(std::size_t i = 0; i < n; i++) {
190 int process = rd_process(gen);
214 template<
typename T,
typename Generator_t>
216 const std::vector<T>& local_items, std::size_t n,
218 std::vector<T> sample(std::min(local_items.size(), n));
220 for(std::size_t i = 0; i < sample.size(); i++)
221 sample[i] = local_items[i];
224 for(std::size_t i = sample.size(); i < local_items.size(); i++) {
226 std::size_t k = rd_index(gen);
227 if(k < sample.size())
228 sample[k] = local_items[i];
244 const std::vector<T>& local_items, std::size_t n
291 template<
typename Index_t,
typename Generator_t>
293 Index_t begin, Index_t end,
298 std::vector<Index_t> result_indexes(n);
302 for(std::size_t i = 0; i < n; i++) {
303 result_indexes[i] = p++;
310 std::size_t k = random_k(gen);
312 result_indexes[k] = p;
317 return result_indexes;
359 const std::map<K, std::size_t>* item_counts;
360 typename std::map<K, std::size_t>::const_iterator it;
371 Index(
const std::map<K, std::size_t>* item_counts)
372 : item_counts(item_counts) {
381 const std::map<K, std::size_t>* item_counts,
382 typename std::map<K, std::size_t>::const_iterator it
384 : item_counts(item_counts), it(it), _offset(0) {
392 const std::map<K, std::size_t>* item_counts,
396 item_counts(item_counts),
397 it(this->item_counts->find(
key)),
418 static Index begin(
const std::map<K, std::size_t>* item_counts);
423 static Index end(
const std::map<K, std::size_t>* item_counts);
472 Index<K> _begin(item_counts, item_counts->begin());
473 while(_begin.it != item_counts->end() && _begin.it->second == 0)
480 Index<K> _end(item_counts, item_counts->end());
486 if(it != item_counts->end()) {
487 if(_offset >= it->second-1) {
491 while(it != item_counts->end() && it->second == 0)
511 if(index._offset+n < index.it->second) {
516 n -= index.it->second - index.
offset();
519 while(n >= index.it->second) {
520 n-= index.it->second;
531 if(i1.it != i1.item_counts->end() && i2.it != i2.item_counts->end()) {
532 if(i1.
key() == i2.
key()) {
535 std::size_t result = i1.it->second - i1.
offset();
538 while(it->first != i2.it->first) {
546 if(i1.it == i1.item_counts->end() && i2.it == i2.item_counts->end())
551 std::size_t result = i1.it->second - i1.
offset();
554 while(it != i1.item_counts->end()) {
563 if(it == item_counts->end() && index.it == index.item_counts->end())
565 if(it != item_counts->end() && index.it != index.item_counts->end())
566 return key() == index.
key() && offset() == index.
offset();
573 return !(*
this==index);
578 if(this->it == this->item_counts->end()
579 && index.it != index.item_counts->end())
582 if(index.it == index.item_counts->end()
583 && this->it != this->item_counts->end())
586 if(index.it == index.item_counts->end()
587 && this->it == this->item_counts->end())
591 if(this->key() == index.
key())
592 return this->offset() < index.
offset();
593 return this->key() < index.
key();
655 const std::vector<T> &local_items, std::size_t n
659 std::vector<std::size_t> item_counts
661 std::map<int, std::size_t> item_counts_map;
662 for(std::size_t i = 0; i < item_counts.size(); i++)
663 item_counts_map[i] = item_counts[i];
666 std::vector<DistributedIndex> result_indexes
672 std::accumulate(item_counts.begin(), item_counts.end(), 0ul)
677 std::unordered_map<int, std::vector<std::size_t>> requests;
678 for(
auto& item : result_indexes)
679 requests[item.key()].push_back(item.offset());
680 requests = request_mpi.
allToAll(requests);
684 std::unordered_map<int, std::vector<T>> items;
685 for(
auto item : requests)
686 for(
auto index : item.second)
687 items[item.first].push_back(local_items[index]);
691 std::vector<std::vector<T>> results(comm.
getSize());
692 for(
auto item : items)
693 results[item.first] = item.second;
748 const std::vector<T> &local_items, std::size_t n
755 std::vector<std::size_t> item_counts
757 std::map<int, std::size_t> item_counts_map;
758 for(std::size_t i = 0; i < item_counts.size(); i++)
759 item_counts_map[i] = item_counts[i];
762 std::vector<DistributedIndex> result_indexes
768 std::accumulate(item_counts.begin(), item_counts.end(), 0ul)
774 std::vector<T> result;
775 for(
auto& item : result_indexes)
776 if(item.key() == comm.
getRank())
777 result.push_back(local_items[item.offset()]);
Definition: communication.h:251
virtual int getRank() const =0
virtual int getSize() const =0
std::vector< T > allGather(const T &) override
Definition: communication.h:492
std::unordered_map< int, T > allToAll(std::unordered_map< int, T > export_map) override
Definition: communication.h:446
Definition: distribution.h:126
Definition: distribution.h:24
Definition: generator.h:113
UniformRandomBitGenerator< Generator_t >::result_type result_type
Definition: generator.h:120
Index operator+(std::size_t n) const
Definition: random.h:508
Index(const std::map< K, std::size_t > *item_counts)
Definition: random.h:371
static Index end(const std::map< K, std::size_t > *item_counts)
Definition: random.h:479
bool operator!=(const Index &index) const
Definition: random.h:572
std::size_t offset() const
Definition: random.h:410
static std::size_t distance(const Index &i1, const Index &i2)
Definition: random.h:529
Index(const std::map< K, std::size_t > *item_counts, K key, std::size_t offset)
Definition: random.h:391
K key() const
Definition: random.h:404
Index & operator++()
Definition: random.h:485
bool operator<(const Index &index) const
Definition: random.h:577
Index operator++(int)
Definition: random.h:501
Index(const std::map< K, std::size_t > *item_counts, typename std::map< K, std::size_t >::const_iterator it)
Definition: random.h:380
static Index begin(const std::map< K, std::size_t > *item_counts)
Definition: random.h:471
bool operator==(const Index &index) const
Definition: random.h:562
std::vector< std::vector< T > > distributed_sample(api::communication::MpiCommunicator &comm, const std::vector< T > &local_items, std::size_t n)
Definition: random.h:653
Index< int > DistributedIndex
Definition: random.h:613
DistributedGenerator rd_choices
Definition: random.cpp:4
const std::mt19937::result_type default_seed
Definition: random.cpp:5
std::vector< T > split_sample(api::communication::MpiCommunicator &comm, const std::vector< T > &local_items, std::size_t n)
Definition: random.h:746
std::vector< T > local_choices(const std::vector< T > &local_items, std::size_t n, Generator_t &gen)
Definition: random.h:48
std::vector< Index_t > sample_indexes(Index_t begin, Index_t end, std::size_t n, Generator_t &gen)
Definition: random.h:292
std::vector< T > split_choices(api::communication::MpiCommunicator &comm, const std::vector< T > &local_items, std::size_t n)
Definition: random.h:169
mt19937::result_type seed
Definition: random.cpp:6
std::vector< T > local_sample(const std::vector< T > &local_items, std::size_t n, Generator_t &gen)
Definition: random.h:215
std::vector< std::vector< T > > distributed_choices(api::communication::MpiCommunicator &comm, const std::vector< T > &local_items, std::size_t n)
Definition: random.h:112
Definition: communication.h:585