TIMPI
post_wait_unpack_nested_buffer.h
Go to the documentation of this file.
1 // The TIMPI Message-Passing Parallelism Library.
2 // Copyright (C) 2002-2025 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License, or (at your option) any later version.
8 
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
13 
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 
18 
19 #ifndef TIMPI_POST_WAIT_UNPACK_NESTED_BUFFER_H
20 #define TIMPI_POST_WAIT_UNPACK_NESTED_BUFFER_H
21 
22 // TIMPI includes
23 #include "timpi/communicator.h"
24 #include "timpi/post_wait_work.h"
25 #include "timpi/standard_type.h"
26 
27 namespace TIMPI
28 {
29 
30 // PostWaitWork specialization for MPI_Unpack of nested buffers.
31 // Container will most likely be vector<vector<T>>
32 template <typename Container>
34  PostWaitUnpackNestedBuffer(const std::vector<char> & buffer,
35  Container & out,
36  const DataType & timpi_mpi_var(T_type),
37  const Communicator & comm_in) :
38  recvbuf(buffer), recv(out), comm(comm_in) {
39  timpi_call_mpi(MPI_Type_dup(T_type, &(type.operator data_type &())));
40 #ifndef TIMPI_HAVE_MPI
41  timpi_not_implemented(); // This makes no sense without MPI_Unpack
42 #endif
43  }
44 
46 #ifdef TIMPI_HAVE_MPI
47  // Not bothering with return type; we can't throw in a destructor
48  MPI_Type_free(&(type.operator data_type &()));
49 #endif
50  }
51 
52  virtual void run() override {
53 #ifdef TIMPI_HAVE_MPI
54 
55 #if MPI_VERSION > 3
56 # define TIMPI_COUNT_TYPE MPI_COUNT
57 # define TIMPI_UNPACK MPI_Unpack_c
58 #else
59 # define TIMPI_COUNT_TYPE MPI_INT
60 # define TIMPI_UNPACK MPI_Unpack
61 #endif
62 
63  // We should at least have one header datum, for outer vector size
64  timpi_assert (!recvbuf.empty());
65 
66  // Unpack the received buffer
67  CountType bufsize = cast_int<CountType>(recvbuf.size());
68  CountType recvsize, pos=0;
69  timpi_call_mpi
70  (TIMPI_UNPACK(recvbuf.data(), bufsize, &pos, &recvsize, 1,
71  TIMPI_COUNT_TYPE, comm.get()));
72 
73  // ... size the outer buffer
74  recv.resize (recvsize);
75 
76  const std::size_t n_vecs = recvsize;
77  for (std::size_t i = 0; i != n_vecs; ++i)
78  {
79  CountType subvec_size;
80 
81  timpi_call_mpi
82  (TIMPI_UNPACK(recvbuf.data(), bufsize, &pos, &subvec_size, 1,
83  TIMPI_COUNT_TYPE, comm.get()));
84 
85  // ... size the inner buffer
86  recv[i].resize (subvec_size);
87 
88  // ... unpack the inner buffer if it is not empty
89  if (!recv[i].empty())
90  timpi_call_mpi
91  (TIMPI_UNPACK(recvbuf.data(), bufsize, &pos, recv[i].data(),
92  subvec_size, type, comm.get()));
93  }
94 #endif //TIMPI_HAVE_MPI
95  }
96 
97 private:
98  const std::vector<char> & recvbuf;
99  Container & recv;
102 };
103 
104 } // namespace TIMPI
105 
106 #endif // TIMPI_POST_WAIT_UNPACK_NESTED_BUFFER_H
PostWaitUnpackNestedBuffer(const std::vector< char > &buffer, Container &out, const DataType &timpi_mpi_var(T_type), const Communicator &comm_in)
Encapsulates the MPI_Datatype.
Definition: data_type.h:50
Encapsulates the MPI_Comm object.
Definition: communicator.h:108
An abstract base class that can be subclassed to allow other code to perform work after a MPI_Wait su...
MPI_Count CountType
Definition: status.h:47
communicator & get()
Definition: communicator.h:167