Skip to content

Commit

Permalink
adding a bcast function
Browse files Browse the repository at this point in the history
  • Loading branch information
patflick committed Jul 25, 2016
1 parent 010ed8e commit ace91ce
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 9 deletions.
90 changes: 90 additions & 0 deletions include/mxx/bcast.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Copyright 2016 Georgia Institute of Technology
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http:https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* @file bcast.hpp
* @author Patrick Flick <[email protected]>
* @group collective
* @brief Bcast operations.
*
*/

#ifndef MXX_BCAST_HPP
#define MXX_BCAST_HPP

#include <mpi.h>
#include <vector>
#include <string>

// mxx includes
#include "common.hpp"
#include "datatypes.hpp"
#include "comm_fwd.hpp"
#include "reduction.hpp"

namespace mxx {

/// Generic bcast
template <typename T>
void bcast(T* data, size_t count, int root, const mxx::comm& comm) {
mxx::datatype dt = mxx::get_datatype<T>();
if (count >= mxx::max_int) {
mxx::datatype bigdt = dt.contiguous(count);
MPI_Bcast(data, 1, bigdt.type(), root, comm);
} else {
MPI_Bcast(data, count, dt.type(), root, comm);
}
}

/// Broadcast single value
template <typename T>
void bcast(T& value, int root, const mxx::comm& comm) {
mxx::datatype dt = mxx::get_datatype<T>();
MPI_Bcast(&value, 1, dt.type(), root, comm);
}

// container bcast implementation
namespace impl {
template <typename Container>
struct container_bcast {
static void do_bcast(Container& c, int root, const mxx::comm& comm) {
// first bcast the size, so that receiving can allocate the string
size_t size = c.size();
mxx::bcast(size, root, comm);
if (comm.rank() != root)
c.resize(size);
mxx::bcast(&c[0], size, root, comm);
}
};

} // namespace impl

// broadcast a vector
template <typename T, typename Alloc = std::allocator<T>>
void bcast(std::vector<T, Alloc>& vec, int root, const mxx::comm& comm) {
impl::container_bcast<std::vector<T, Alloc>>::do_bcast(vec, root, comm);
}

// broadcast a string
template <typename CharT = char, typename Traits = std::char_traits<CharT>,
typename Alloc = std::allocator<CharT>>
void bcast(std::basic_string<CharT, Traits, Alloc>& str, int root, const mxx::comm& comm) {
impl::container_bcast<std::basic_string<CharT, Traits, Alloc>>::do_bcast(str, root, comm);
}

} // namespace mxx

#endif // MXX_BCAST_HPP
14 changes: 5 additions & 9 deletions include/mxx/collective.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include "datatypes.hpp"
#include "comm_fwd.hpp"
#include "reduction.hpp"
#include "bcast.hpp"
#include "future.hpp"
#include "big_collective.hpp"

Expand Down Expand Up @@ -196,8 +197,7 @@ template <typename T>
std::vector<T> scatter(const std::vector<T>& msgs, int root, const mxx::comm& comm = mxx::comm()) {
size_t size = msgs.size() / comm.size();
MXX_ASSERT(comm.rank() != 0 || msgs.size() % (size_t)comm.size() == 0);
mxx::datatype sizedt = mxx::get_datatype<size_t>();
MPI_Bcast(&size, 1, sizedt.type(), root, comm);
mxx::bcast(size, root, comm);
// now everybody knows the size
std::vector<T> result = scatter(msgs, size, root, comm);
return result;
Expand All @@ -223,8 +223,7 @@ std::vector<T> scatter(const std::vector<T>& msgs, int root, const mxx::comm& co
template <typename T>
std::vector<T> scatter_recv(int root, const mxx::comm& comm = mxx::comm()) {
size_t size;
mxx::datatype sizedt = mxx::get_datatype<size_t>();
MPI_Bcast(&size, 1, sizedt.type(), root, comm);
mxx::bcast(size, root, comm);
// now everybody knows the size
std::vector<T> result = scatter_recv<T>(size, root, comm);
return result;
Expand Down Expand Up @@ -323,8 +322,7 @@ void scatterv(const T* msgs, const std::vector<size_t>& sizes, T* out, size_t re
MXX_ASSERT(root != comm.rank() || sizes.size() == static_cast<size_t>(comm.size()));
// get total send size
size_t send_size = std::accumulate(sizes.begin(), sizes.end(), static_cast<size_t>(0));
mxx::datatype sizedt = mxx::get_datatype<size_t>();
MPI_Bcast(&send_size, 1, sizedt.type(), root, comm);
mxx::bcast(send_size, root, comm);
// check if we need to use the custom BIG scatterv
if (send_size >= mxx::max_int) {
// own scatter for large messages
Expand Down Expand Up @@ -702,9 +700,7 @@ std::vector<T> gather(const T& x, int root, const mxx::comm& comm = mxx::comm())
template <typename T>
void gatherv(const T* data, size_t size, T* out, const std::vector<size_t>& recv_sizes, int root, const mxx::comm& comm = mxx::comm()) {
size_t total_size = std::accumulate(recv_sizes.begin(), recv_sizes.end(), static_cast<size_t>(0));
mxx::datatype mpi_sizet = mxx::get_datatype<size_t>();
// tell everybody about the total size
MPI_Bcast(&total_size, 1, mpi_sizet.type(), root, comm);
mxx::bcast(total_size, root, comm);
if (total_size >= mxx::max_int) {
// use custom implementation for large sizes
impl::gatherv_big(data, size, out, recv_sizes, root, comm);
Expand Down

0 comments on commit ace91ce

Please sign in to comment.