Skip to content

Commit

Permalink
MpiDataype packer
Browse files Browse the repository at this point in the history
  • Loading branch information
cwpearson committed Mar 12, 2024
1 parent ed29d9e commit 9b2ef2c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/KokkosComm_pack_traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ struct PackTraits {
/*! \brief This can be specialized to do custom behavior for a particular view*/
template <KokkosView View>
struct PackTraits<View> {
using packer_type = Impl::Packer::DeepCopy<View>;
// using packer_type = Impl::Packer::DeepCopy<View>;
using packer_type = Impl::Packer::MpiDatatype<View>;

static bool needs_unpack(const View &v) {
return !Traits<View>::is_contiguous(v);
Expand Down
42 changes: 42 additions & 0 deletions src/impl/KokkosComm_packer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#pragma once

#include "KokkosComm_concepts.hpp"
#include "KokkosComm_types.hpp"
#include "KokkosComm_include_mpi.hpp"

namespace KokkosComm::Impl {
Expand Down Expand Up @@ -82,5 +83,46 @@ struct DeepCopy {
}
};

template <KokkosView View>
struct MpiDatatype {
using non_const_packed_view_type = View;
using args_type = MpiArgs<non_const_packed_view_type>;

// don't actually allocate - return the provided view, but with
// a datatype that describes the data in the view
template <typename ExecSpace>
static args_type allocate_packed_for(const ExecSpace &space,
const std::string & /*label*/,
const View &src) {
using ValueType = typename View::value_type;

MPI_Datatype type = mpi_type<ValueType>();
for (size_t d = 0; d < KokkosComm::Traits<View>::rank(); ++d) {
MPI_Datatype newtype;
MPI_Type_create_hvector(src.extent(d) /*count*/, 1 /*block length*/,
src.stride(d) * sizeof(ValueType), type,
&newtype);
type = newtype;
}
MPI_Type_commit(&type);
MPI_Aint lb, extent;
MPI_Type_get_extent(type, &lb, &extent);
return args_type(src, type, 1);
}

// pack is a no-op: rely on MPI's datatype engine
template <typename ExecSpace>
static args_type pack(const ExecSpace &space, const View &src) {
return allocate_packed_for(space, "", src);
}

// unpack is a no-op: rely on MPI's datatype engine
template <typename ExecSpace>
static void unpack_into(const ExecSpace &space, const View &dst,
const non_const_packed_view_type &src) {
return;
}
};

} // namespace Packer
} // namespace KokkosComm::Impl

0 comments on commit 9b2ef2c

Please sign in to comment.