PURIFY
Next-generation radio interferometric imaging
random_update_factory.cc
Go to the documentation of this file.
2 #include <algorithm>
3 #include <random>
4 
5 namespace purify {
6 namespace random_updater {
7 std::function<bool()> random_updater(const sopt::mpi::Communicator& comm, const t_int total,
8  const t_int update_size,
9  const std::shared_ptr<bool> update_pointer,
10  const std::string& update_name) {
11  if (update_size > comm.size())
12  throw std::runtime_error(
13  "Number of random updates cannot be greater than number of MPI processors in the "
14  "communicator " +
15  std::to_string(comm.size()) + " < " + std::to_string(update_size) + " .");
16  std::shared_ptr<std::vector<t_int>> ind = std::make_shared<std::vector<t_int>>(total, 0);
17  for (t_int i = 0; i < total; i++) (*ind)[i] = i;
18  std::random_device rng;
19  std::shared_ptr<std::mt19937> urng = std::make_shared<std::mt19937>(rng());
20  return [update_pointer, update_size, update_name, ind, comm, urng]() -> bool {
21  if (comm.is_root()) {
22  std::shuffle(std::begin(*ind), std::end(*ind), *urng);
23  };
24  *ind = comm.broadcast(*ind);
25  *update_pointer = false;
26  for (t_int i = 0; i < update_size; i++)
27  if (comm.rank() == ind->at(i)) {
28  SOPT_DEBUG("Process {} doing {}", comm.rank(), update_name);
29  *update_pointer = true;
30  }
31  return *update_pointer;
32  };
33 }
34 } // namespace random_updater
35 } // namespace purify
std::function< bool()> random_updater(const sopt::mpi::Communicator &comm, const t_int total, const t_int update_size, const std::shared_ptr< bool > update_pointer, const std::string &update_name)