Skip to content

Commit

Permalink
fixing a problem with partition
Browse files Browse the repository at this point in the history
Problem description
-------------------

Say the input file is size `n` and we have `p` processes.

The `distribute_file` function loads `n / p` elements/chars into each of
the first p-1 PEs. The last PE gets a local size of `n / p + n % p` elements.

This was not accounted for in the `partition` implementation and
consequently resulted in the last PE loading wrong characters during the
global trie construction.

This fix adapts the `partition` implementation to keep track of a new
member, `min_local_size` which is set to `n / p`, thus allowing all
processors to compute target PEs and offsets correctly.
  • Loading branch information
patflick committed Apr 4, 2019
1 parent e7225ad commit adad6ea
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions dpt/util/partition.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,27 +39,31 @@ class partition {

public:
partition(dpt::mpi::environment env = dpt::mpi::environment()) : env_(env), global_size_(0),
local_size_(0) { }
local_size_(0), min_local_size_(0) { }

partition(const GlobalIndex global_size, const GlobalIndex local_size,
std::vector<Alphabet>&& local_data, dpt::mpi::environment env = dpt::mpi::environment())
: env_(env),
global_size_(global_size),
local_size_(local_size),
min_local_size_(global_size_ / env_.size()),
local_data_(std::move(local_data)) { }

partition(const partition& other) : env_(other.env_),
global_size_(other.global_size_), local_size_(other.local_size_),
min_local_size_(other.min_local_size_),
local_data_(other.local_data_) { }

partition(partition&& other) : env_(std::move(other.env_)),
global_size_(other.global_size_), local_size_(other.local_size_),
min_local_size_(other.min_local_size_),
local_data_(std::move(other.local_data_)) { }

partition& operator = (partition&& other) {
env_ = std::move(other.env_);
global_size_ = other.global_size_;
local_size_ = other.local_size_;
min_local_size_ = other.min_local_size_;
local_data_ = std::move(other.local_data_);
return *this;
}
Expand Down Expand Up @@ -99,22 +103,23 @@ class partition {

inline int32_t pe(const GlobalIndex index) const {
return std::min(
static_cast<int32_t>(index / local_size_), env_.size() - 1);
static_cast<int32_t>(index / min_local_size_), env_.size() - 1);
}

inline pe_and_position pe_and_norm_position(
const GlobalIndex index) const {
const auto pe =
std::min(static_cast<int32_t>(index / local_size_), env_.size() - 1);
std::min(static_cast<int32_t>(index / min_local_size_), env_.size() - 1);
return pe_and_position { pe,
static_cast<LocalIndex>(index - (pe * local_size_)) };
static_cast<LocalIndex>(index - (pe * min_local_size_)) };
}

private:
dpt::mpi::environment env_;

size_t global_size_;
size_t local_size_;
size_t min_local_size_; // the local size of PEs excl the last one
std::vector<Alphabet> local_data_;

}; // class partition
Expand Down

0 comments on commit adad6ea

Please sign in to comment.