OpenM++ runtime library (libopenm)
msgMpiPacked.h
Go to the documentation of this file.
1
5// Copyright (c) 2013-2015 OpenM++
6// This code is licensed under the MIT license (see LICENSE.txt for details)
7
8#ifndef MSG_MPI_PACKED_H
9#define MSG_MPI_PACKED_H
10
11using namespace std;
12
13#include "msgCommon.h"
14
15namespace openm
16{
19 {
20 public:
30 template<typename TVal>
31 static void pack(TVal i_value, int i_packedSize, void * io_packedData, int & io_packPos)
32 {
33 int mpiRet = MPI_Pack(&i_value, 1, toMpiType(typeid(TVal)), io_packedData, i_packedSize, &io_packPos, MPI_COMM_WORLD);
34 if (mpiRet != MPI_SUCCESS) throw MpiException(mpiRet);
35 }
36
45 template<typename TVal>
46 static TVal unpack(int i_packedSize, void * i_packedData, int & io_packPos)
47 {
48 TVal val;
49 int mpiRet = MPI_Unpack(i_packedData, i_packedSize, &io_packPos, &val, 1, toMpiType(typeid(TVal)), MPI_COMM_WORLD);
50 if (mpiRet != MPI_SUCCESS) throw MpiException(mpiRet);
51
52 return val;
53 }
54
56 static void pack(const string & i_value, int i_packedSize, void * io_packedData, int & io_packPos);
57
59 static string unpackStr(int i_packedSize, void * i_packedData, int & io_packPos);
60
62 static unique_ptr<uint8_t[]> packArray(const type_info & i_type, size_t i_size, void * i_valueArr);
63
65 static unique_ptr<uint8_t[]> packArray(size_t i_size, const string * i_valueArr);
66
68 static void unpackArray(int i_packedSize, void * i_packedData, size_t i_size, string * io_valueArr);
69
71 static int packedSize(const type_info & i_type, size_t i_size);
72
74 static int packedSize(size_t i_size, const string * i_valueArr);
75
77 static int packedSize(const type_info & i_type);
78
80 static int packedSize(const string & i_value);
81
83 static MPI_Datatype toMpiType(const type_info & i_type);
84 };
85
90 template<typename TRow> struct RowMpiPackedAdapter
91 {
92 public:
101 static void pack(const IRowBaseUptr & i_row, int i_packedSize, void * io_packedData, int & io_packPos);
102
111 static void unpackTo(const IRowBaseUptr & io_row, int i_packedSize, void * i_packedData, int & io_packPos);
112
118 static int packedSize(const IRowBaseUptr & i_row);
119 };
120
127 template <MsgTag rowMsgTag, typename TRow> struct MetaMpiPackedAdapter : public IPackedAdapter
128 {
129 public:
131 MsgTag tag(void) const noexcept override { return rowMsgTag; }
132
138 const vector<uint8_t> pack(const IRowBaseVec & i_rowVec) const override
139 {
140 lock_guard<recursive_mutex> lck(msgMutex);
141
142 int packSize = packedSize(i_rowVec);
143 vector<uint8_t> packedData(packSize);
144
145 int packPos = 0;
146 int rowCount = (int)i_rowVec.size();
147 MpiPacked::pack<int>(rowCount, packSize, packedData.data(), packPos);
148
149 for (IRowBaseVec::const_iterator rowIt = i_rowVec.begin(); rowIt != i_rowVec.end(); rowIt++) {
150 RowMpiPackedAdapter<TRow>::pack(*rowIt, packSize, packedData.data(), packPos);
151 }
152 return packedData;
153 }
154
162 void unpackTo(int i_packSize, void * i_packedData, IRowBaseVec & io_rowVec) const override
163 {
164 lock_guard<recursive_mutex> lck(msgMutex);
165
166 int packPos = 0;
167 int rowCount = MpiPacked::unpack<int>(i_packSize, i_packedData, packPos);
168
169 for (int nRow = 0; nRow < rowCount; nRow++) {
170 IRowBaseUptr row(new TRow());
171 RowMpiPackedAdapter<TRow>::unpackTo(row, i_packSize, i_packedData, packPos);
172 io_rowVec.push_back(std::move(row));
173 }
174 }
175
176 private:
182 int packedSize(const IRowBaseVec & i_rowVec) const
183 {
184 int packSize = MpiPacked::packedSize(typeid(int));
185
186 for (IRowBaseVec::const_iterator rowIt = i_rowVec.begin(); rowIt != i_rowVec.end(); rowIt++) {
187 packSize += RowMpiPackedAdapter<TRow>::packedSize(*rowIt);
188 }
189 return packSize;
190 }
191 };
192}
193
194#endif // MSG_MPI_PACKED_H
messaging library MPI exception
Definition: msgMpi.h:19
wrapper class around of MPI_Pack and MPI_Unpack
Definition: msgMpiPacked.h:19
static void pack(TVal i_value, int i_packedSize, void *io_packedData, int &io_packPos)
MPI_Pack value of primitive type into data buffer at i_packPos position.
Definition: msgMpiPacked.h:31
static void unpackArray(int i_packedSize, void *i_packedData, size_t i_size, string *io_valueArr)
unpack MPI_Pack'ed string array from i_packedData into supplied io_valueArr.
Definition: msgMpiPacked.cpp:128
static int packedSize(const type_info &i_type, size_t i_size)
return MPI pack size for array of specified primitive type values.
Definition: msgMpiPacked.cpp:151
static unique_ptr< uint8_t[]> packArray(const type_info &i_type, size_t i_size, void *i_valueArr)
return an MPI_Pack'ed copy of source array.
Definition: msgMpiPacked.cpp:75
static MPI_Datatype toMpiType(const type_info &i_type)
return MPI type corresponding to source primitive type.
Definition: msgMpiPacked.cpp:218
static string unpackStr(int i_packedSize, void *i_packedData, int &io_packPos)
MPI_Unpack string from data buffer at i_packPos position and return the string.
Definition: msgMpiPacked.cpp:48
static TVal unpack(int i_packedSize, void *i_packedData, int &io_packPos)
MPI_Unpack value of primitive type from data buffer at i_packPos position and return the value.
Definition: msgMpiPacked.h:46
OpenM++: message passing library common classes and interfaces.
openM++ namespace
Definition: log.h:32
MsgTag
tag to identify message content
Definition: msg.h:29
std::unique_ptr< IRowBase > IRowBaseUptr
unique pointer to db row
Definition: omHelper.h:236
recursive_mutex msgMutex
mutex to lock messaging operations
Definition: msgExecBase.cpp:14
std::vector< IRowBaseUptr > IRowBaseVec
db rows: vector of unique pointers to db row
Definition: omHelper.h:239
public interface to pack and unpack rows of metadata db-table
Definition: msg.h:99
MPI-based adapter to pack and unpack vector of metadata db rows.
Definition: msgMpiPacked.h:128
const vector< uint8_t > pack(const IRowBaseVec &i_rowVec) const override
pack vector of metadata db rows into byte vector.
Definition: msgMpiPacked.h:138
void unpackTo(int i_packSize, void *i_packedData, IRowBaseVec &io_rowVec) const override
unpack from byte[] message bufer into vector of metadata db rows.
Definition: msgMpiPacked.h:162
MsgTag tag(void) const noexcept override
return message tag
Definition: msgMpiPacked.h:131
MPI-based adapter to pack and unpack metadata db row.
Definition: msgMpiPacked.h:91
static void pack(const IRowBaseUptr &i_row, int i_packedSize, void *io_packedData, int &io_packPos)
pack db row into MPI message.
static void unpackTo(const IRowBaseUptr &io_row, int i_packedSize, void *i_packedData, int &io_packPos)
unpack MPI message into db row.
static int packedSize(const IRowBaseUptr &i_row)
return byte size to pack db row into MPI message.