|
10 | 10 | #include <variant>
|
11 | 11 | #include <vector>
|
12 | 12 | #include <map>
|
| 13 | +#include <string> |
13 | 14 | #include <mpi.h>
|
14 | 15 |
|
15 | 16 | /**
|
@@ -730,6 +731,91 @@ struct serializer<std::vector<T>>
|
730 | 731 | }
|
731 | 732 | };
|
732 | 733 |
|
| 734 | +/** specialization for serialization for vector types */ |
| 735 | +template <class CharT> |
| 736 | +struct serializer<std::basic_string<CharT>> |
| 737 | +{ |
| 738 | + /** is the type serializable using MPI_Datatypes for both the sender and |
| 739 | + * receiver at compile time?*/ |
| 740 | + using mpi_type = std::false_type; |
| 741 | + /** \returns a string representing the name of the type */ |
| 742 | + static std::string name() { |
| 743 | + std::stringstream n; |
| 744 | + n << "std::string<" << serializer<CharT>::name() << ">"; |
| 745 | + return n.str(); |
| 746 | + } |
| 747 | + /** \returns a MPI_Datatype to represent the type if mpi_type is true, else MPI_INT */ |
| 748 | + static MPI_Datatype dtype() { return MPI_INT; } |
| 749 | + /** |
| 750 | + * Sends a data type from one location to another |
| 751 | + * \param[in] t the data to send |
| 752 | + * \param[in] dest the MPI rank to send to |
| 753 | + * \param[in] tag the MPI tag to send to |
| 754 | + * \param[in] comm the MPI_Comm to send to |
| 755 | + * \returns an error code from the underlying send */ |
| 756 | + static int send(std::basic_string<CharT> const& t, int dest, int tag, MPI_Comm comm) |
| 757 | + { |
| 758 | + int ret = 0; |
| 759 | + size_t size = t.size(); |
| 760 | + ret = MPI_Send(&size, 1, mpi_size_t(), dest, tag, comm); |
| 761 | + if(serializer<CharT>::mpi_type::value) { |
| 762 | + return MPI_Send(&t.front(), t.size(), serializer<CharT>::dtype(), dest, tag, comm); |
| 763 | + } else { |
| 764 | + for (auto const& item : t) { |
| 765 | + ret |= serializer<CharT>::send(item, dest, tag, comm); |
| 766 | + } |
| 767 | + } |
| 768 | + return ret; |
| 769 | + } |
| 770 | + /** |
| 771 | + * Recv a data type from another location |
| 772 | + * \param[in] t the data to recv from |
| 773 | + * \param[in] source the MPI rank to recv from |
| 774 | + * \param[in] tag the MPI tag to recv from |
| 775 | + * \param[in] comm the MPI_Comm to recv from |
| 776 | + * \param[in] status the MPI_Status to recv from |
| 777 | + * \returns an error code from the underlying recv */ |
| 778 | + static int recv(std::basic_string<CharT>& t, int source, int tag, MPI_Comm comm, |
| 779 | + MPI_Status* status) |
| 780 | + { |
| 781 | + MPI_Status alt_status; |
| 782 | + if(status == MPI_STATUS_IGNORE) status = &alt_status; |
| 783 | + size_t size, ret=0; |
| 784 | + ret = MPI_Recv(&size, 1, mpi_size_t(), source, tag, comm, status); |
| 785 | + t.resize(size); |
| 786 | + |
| 787 | + if (serializer<CharT>::mpi_type::value) { |
| 788 | + return MPI_Recv(&t.front(), t.size(), serializer<CharT>::dtype(), status->MPI_SOURCE, status->MPI_TAG, comm, status); |
| 789 | + } else { |
| 790 | + for (auto& item : t) { |
| 791 | + ret |= serializer<CharT>::recv(item, status->MPI_SOURCE, status->MPI_TAG, comm, status); |
| 792 | + } |
| 793 | + } |
| 794 | + return ret; |
| 795 | + } |
| 796 | + |
| 797 | + /** |
| 798 | + * Broadcast a data type from another location |
| 799 | + * \param[in] t the data to broadcast from |
| 800 | + * \param[in] root the MPI rank to broadcast from |
| 801 | + * \param[in] comm the MPI_Comm to broadcast from |
| 802 | + * \returns an error code from the underlying MPI_Bcast(s) */ |
| 803 | + static int bcast(std::basic_string<CharT>& t, int root, MPI_Comm comm) |
| 804 | + { |
| 805 | + size_t size = t.size(), ret=0; |
| 806 | + ret = MPI_Bcast(&size, 1, mpi_size_t(), root, comm); |
| 807 | + t.resize(size); |
| 808 | + if (serializer<CharT>::mpi_type::value) { |
| 809 | + return MPI_Bcast(&t.front(), t.size(), serializer<CharT>::dtype(), root, comm); |
| 810 | + } else { |
| 811 | + for (auto& item : t) { |
| 812 | + ret |= serializer<CharT>::bcast(item, root, comm); |
| 813 | + } |
| 814 | + } |
| 815 | + return ret; |
| 816 | + } |
| 817 | +}; |
| 818 | + |
733 | 819 | template <class T, size_t N>
|
734 | 820 | struct serializer<std::array<T, N>>
|
735 | 821 | {
|
|
0 commit comments