Fix type conversion issues

-Wconversion
parent 59ecf053
......@@ -31,6 +31,7 @@ namespace unsafe_mpi {
template <typename T>
void allgatherv_serialize(const boost::mpi::communicator &comm, const std::vector<T> &in, std::vector<T> &out) {
const size_t comm_size = static_cast<size_t>(comm.size());
// Step 1: serialize input data
boost::mpi::packed_oarchive oa(comm);
if (!in.empty())
......@@ -40,14 +41,14 @@ void allgatherv_serialize(const boost::mpi::communicator &comm, const std::vecto
// Need to cast to int because this is what MPI uses as size_t...
const int in_size = static_cast<int>(in.size()),
transmit_size = (in.empty() ? 0 : static_cast<int>(oa.size()));
std::vector<int> in_sizes(comm.size()), transmit_sizes(comm.size());
std::vector<int> in_sizes(comm_size), transmit_sizes(comm_size);
boost::mpi::all_gather(comm, in_size, in_sizes.data());
boost::mpi::all_gather(comm, transmit_size, transmit_sizes.data());
// Step 3: calculate displacements from sizes (prefix sum)
std::vector<int> displacements(comm.size() + 1);
std::vector<int> displacements(comm_size + 1);
displacements[0] = sizeof(boost::mpi::packed_iarchive);
for (int i = 1; i <= comm.size(); ++i) {
for (size_t i = 1; i <= comm_size; ++i) {
displacements[i] = displacements[i-1] + transmit_sizes[i-1];
}
......@@ -68,22 +69,24 @@ void allgatherv_serialize(const boost::mpi::communicator &comm, const std::vecto
// Step 5: deserialize received data
// Preallocate storage to prevent reallocations
std::vector<T> temp;
size_t largest_size = *std::max_element(in_sizes.begin(), in_sizes.end());
temp.reserve(largest_size);
out.reserve(std::accumulate(in_sizes.begin(), in_sizes.end(), 0));
const int largest_size = *std::max_element(in_sizes.begin(), in_sizes.end());
temp.reserve(static_cast<size_t>(largest_size));
out.reserve(static_cast<size_t>(
std::accumulate(in_sizes.begin(), in_sizes.end(), 0)));
// Deserialize archives one by one, inserting elements at the end of ̀out̀
for (int i = 0; i < comm.size(); ++i) {
for (size_t i = 0; i < comm_size; ++i) {
if (in_sizes[i] == 0) {
// We can ignore processes which didn't have anything to send
continue;
}
boost::mpi::packed_iarchive archive(comm);
archive.resize(transmit_sizes[i]);
memcpy(archive.address(), recv + displacements[i], transmit_sizes[i]);
auto transmit_size_i = static_cast<size_t>(transmit_sizes[i]);
archive.resize(transmit_size_i);
memcpy(archive.address(), recv + displacements[i], transmit_size_i);
temp.clear();
temp.resize(in_sizes[i]);
temp.resize(static_cast<size_t>(in_sizes[i]));
archive >> temp;
out.insert(out.end(), temp.begin(), temp.end());
}
......@@ -99,18 +102,19 @@ void allgatherv_unsafe(const boost::mpi::communicator &comm, const std::vector<T
// We need to compute the displacement array, specifying for each PE
// at which position in out to place the data received from it
// Need to cast to int because this is what MPI uses as size_t...
const int factor = sizeof(T) / sizeof(transmit_type);
const int in_size = static_cast<int>(in.size()) * factor;
std::vector<int> sizes(comm.size());
const size_t comm_size = static_cast<size_t>(comm.size());
const size_t factor = sizeof(T) / sizeof(transmit_type);
const int in_size = static_cast<int>(in.size() * factor);
std::vector<int> sizes(comm_size);
boost::mpi::all_gather(comm, in_size, sizes.data());
// Step 2: calculate displacements from sizes
// Compute prefix sum to compute displacements from sizes
std::vector<int> displacements(comm.size() + 1);
std::vector<int> displacements(comm_size + 1);
displacements[0] = 0;
std::partial_sum(sizes.begin(), sizes.end(), displacements.begin() + 1);
// divide by factor by which T is larger than transmit_type
out.resize(displacements.back() / factor);
out.resize(static_cast<size_t>(displacements.back()) / factor);
// Step 3: MPI_Allgatherv
const transmit_type *sendptr = reinterpret_cast<const transmit_type*>(in.data());
......
......@@ -66,24 +66,26 @@ void broadcast(const boost::mpi::communicator &comm, std::vector<T> &data, int r
boost::mpi::packed_oarchive oa(comm);
oa << data;
// Broadcast archive size
int archive_size = static_cast<int>(oa.size());
size_t archive_size = oa.size();
boost::mpi::broadcast(comm, archive_size, root);
// Broadcast archive data
auto sendptr = const_cast<void*>(oa.address());
int status = MPI_Bcast(sendptr, archive_size, MPI_PACKED, root, comm);
int status = MPI_Bcast(sendptr, static_cast<int>(archive_size),
MPI_PACKED, root, comm);
if (status != 0) {
ERR << "MPI_Bcast returned non-zero value " << status
<< ", errno: " << errno << std::endl;
}
} else {
// Receive archive size and allocate space
int archive_size;
size_t archive_size;
boost::mpi::broadcast(comm, archive_size, root);
boost::mpi::packed_iarchive ia(comm);
ia.resize(archive_size);
// Receive broadcast archive data
auto recvptr = ia.address();
int status = MPI_Bcast(recvptr, archive_size, MPI_PACKED, root, comm);
int status = MPI_Bcast(recvptr, static_cast<int>(archive_size),
MPI_PACKED, root, comm);
if (status != 0) {
ERR << "MPI_Bcast returned non-zero value " << status
<< ", errno: " << errno << std::endl;
......
......@@ -46,7 +46,7 @@ void gatherv_trivial(const boost::mpi::communicator &comm,
if (comm.rank() == root) {
// Receive sizes
std::vector<int> sizes;
sizes.reserve(comm.size());
sizes.reserve(static_cast<size_t>(comm.size()));
boost::mpi::gather(comm, sendsize, sizes, root);
// Calculate displacements from spaces
......@@ -86,14 +86,15 @@ void gatherv_serialize(const boost::mpi::communicator &comm, const std::vector<T
auto sendptr = const_cast<void*>(oa.address());
if (comm.rank() == root) {
std::vector<int> in_sizes(comm.size()), transmit_sizes(comm.size());
const size_t comm_size = static_cast<size_t>(comm.size());
std::vector<int> in_sizes(comm_size), transmit_sizes(comm_size);
boost::mpi::gather<int>(comm, in_size, in_sizes.data(), root);
boost::mpi::gather<int>(comm, transmit_size, transmit_sizes.data(), root);
// Step 3: calculate displacements from sizes (prefix sum)
std::vector<int> displacements(comm.size() + 1);
std::vector<int> displacements(comm_size + 1);
displacements[0] = sizeof(boost::mpi::packed_iarchive);
for (int i = 1; i <= comm.size(); ++i) {
for (size_t i = 1; i <= comm_size; ++i) {
displacements[i] = displacements[i-1] + transmit_sizes[i-1];
}
......@@ -113,19 +114,20 @@ void gatherv_serialize(const boost::mpi::communicator &comm, const std::vector<T
// Step 5: deserialize received data
// Preallocate storage to prevent reallocations
std::vector<T> temp;
size_t largest_size = *std::max_element(in_sizes.begin(), in_sizes.end());
auto largest_size = *std::max_element(in_sizes.begin(), in_sizes.end());
temp.reserve(largest_size);
out.reserve(std::accumulate(in_sizes.begin(), in_sizes.end(), 0));
// Deserialize archives one by one, inserting elements at the end of ̀out̀
for (int i = 0; i < comm.size(); ++i) {
for (size_t i = 0; i < comm_size; ++i) {
if (in_sizes[i] == 0) {
// We can ignore processes which didn't have anything to send
continue;
}
boost::mpi::packed_iarchive archive(comm);
archive.resize(transmit_sizes[i]);
memcpy(archive.address(), recv + displacements[i], transmit_sizes[i]);
const size_t transmit_size_i = static_cast<size_t>(transmit_sizes[i]);
archive.resize(transmit_size_i);
memcpy(archive.address(), recv + displacements[i], transmit_size_i);
temp.clear();
temp.resize(in_sizes[i]);
......
......@@ -31,9 +31,9 @@ void send(const boost::mpi::communicator &comm, int dest, int tag, const T *data
if (trivial) {
auto sendptr = reinterpret_cast<const transmit_type*>(data);
auto sendsize = size * sizeof(T)/sizeof(transmit_type);
comm.send(dest, tag, sendptr, sendsize);
comm.send(dest, tag, sendptr, static_cast<int>(sendsize));
} else {
comm.send(dest, tag, data, size);
comm.send(dest, tag, data, static_cast<int>(size));
}
}
......@@ -60,9 +60,9 @@ void recv(const boost::mpi::communicator &comm, int src, int tag, std::vector<T>
if (trivial) {
auto recvptr = reinterpret_cast<transmit_type*>(data.data());
auto recvsize = size * sizeof(T)/sizeof(transmit_type);
comm.recv(src, tag, recvptr, recvsize);
comm.recv(src, tag, recvptr, static_cast<int>(recvsize));
} else {
comm.recv(src, tag, data.data(), size);
comm.recv(src, tag, data.data(), static_cast<int>(size));
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment