10 const int nranks = ippl::Comm->size();
11 const int rank = ippl::Comm->rank();
15 if (totalToSample == 0) {
19 const size_t capacity =
pc_m->R.size();
20 const size_t localNum =
pc_m->getLocalNum();
21 const size_t spaceLeft = (localNum <= capacity) ? (capacity - localNum) : size_t(0);
23 std::vector<unsigned long> spaceLeftAll(
static_cast<size_t>(nranks), 0);
24 unsigned long mySpace =
static_cast<unsigned long>(spaceLeft);
26 &mySpace, 1, MPI_UNSIGNED_LONG, spaceLeftAll.data(), 1, MPI_UNSIGNED_LONG,
27 ippl::Comm->getCommunicator());
29 const size_t nranks_u =
static_cast<size_t>(nranks);
30 const size_t base = totalToSample / nranks_u;
31 const size_t rem = totalToSample % nranks_u;
33 std::vector<size_t> nlocal(
static_cast<size_t>(nranks), 0);
35 for (
int r = 0; r < nranks; ++r) {
36 size_t ideal = base + (
static_cast<size_t>(r) < rem ? 1 : 0);
37 size_t cap =
static_cast<size_t>(spaceLeftAll[
static_cast<size_t>(r)]);
38 nlocal[
static_cast<size_t>(r)] = std::min(ideal, cap);
39 sum += nlocal[
static_cast<size_t>(r)];
42 size_t deficit = totalToSample - sum;
45 for (
int r = 0; r < nranks && deficit > 0; ++r) {
46 size_t cap =
static_cast<size_t>(spaceLeftAll[
static_cast<size_t>(r)]);
47 if (nlocal[
static_cast<size_t>(r)] < cap) {
55 nlocal[
static_cast<size_t>(chosen)] += 1;
59 return nlocal[
static_cast<size_t>(rank)];