Muster
 All Classes Namespaces Files Functions Variables Typedefs Macros
packable_vector.h
Go to the documentation of this file.
1 #ifndef MUSTER_PACKABLE_VECTOR_H
2 #define MUSTER_PACKABLE_VECTOR_H
3 
4 #include <cstdlib>
5 #include <vector>
6 #include <boost/shared_ptr.hpp>
7 #include "mpi_utils.h"
8 #include "mpi_bindings.h"
9 
10 namespace cluster {
11 
12  /// Pass this to a shared pointer if you do NOT want it to own its object
13  struct null_deleter {
14  void operator()(void const *) const { }
15  };
16 
17 
18  ///
19  /// This class allows a vector of packable objects to be packed as though
20  /// it were a packable object itself.
21  ///
22  template <class T>
23  struct packable_vector {
24  boost::shared_ptr< std::vector<T> > _packables;
25 
26  packable_vector(std::vector<T> *packables, bool owned = true) {
27  if (owned) {
28  _packables = boost::shared_ptr< std::vector<T> >(packables);
29  } else {
30  _packables = boost::shared_ptr< std::vector<T> >(packables, null_deleter());
31  }
32  }
33 
34 
36  packable_vector() : _packables(new std::vector<T>()) { }
37 
39 
40  ///
41  /// Assignment
42  ///
44  if (this == &other) return *this;
45  _packables = other._packables;
46  return *this;
47  }
48 
49  ///
50  /// get the number of bytes required to pack this buffer.
51  ///
52  int packed_size(MPI_Comm comm) const {
53  // figure out size of packed buffer
54  int size = 0;
55  size += cmpi_packed_size(1, MPI_SIZE_T, comm); // num packables for trial.
56  for (size_t i=0; i < _packables->size(); i++) { // size of packables.
57  size += (*_packables)[i].packed_size(comm);
58  }
59  return size;
60  }
61 
62  ///
63  /// Pack onto an MPI buffer.
64  ///
65  void pack(void *buf, int bufsize, int *pos, MPI_Comm comm) const {
66  // pack buffer with medoid objects
67  size_t num_packables = _packables->size();
68  CMPI_Pack(&num_packables, 1, MPI_SIZE_T, buf, bufsize, pos, comm);
69  for (size_t i=0; i < num_packables; i++) {
70  (*_packables)[i].pack(buf, bufsize, pos, comm);
71  }
72  }
73 
74  ///
75  /// Unpack from an input buffer. Note that this creates a new vector.
76  ///
77  static packable_vector unpack(void *buf, int bufsize, int *pos, MPI_Comm comm) {
78  size_t num_packables;
79  CMPI_Unpack(buf, bufsize, pos, &num_packables, 1, MPI_SIZE_T, comm);
80 
81  packable_vector vec;
82  vec._packables->resize(num_packables);
83  for (size_t i=0; i < vec._packables->size(); i++) {
84  (*vec._packables)[i] = T::unpack(buf, bufsize, pos, comm);
85  }
86  return vec;
87  }
88  };
89 
90  ///
91  /// Tempate function adaptor so we can have type inference when making
92  /// packable vectors.
93  ///
94  template <class T>
95  packable_vector<T> make_packable_vector(std::vector<T> *vec, bool owned = true) {
96  return packable_vector<T>(vec, owned);
97  }
98 
99 } // namespace cluster
100 
101 #endif // MUSTER_PACKABLE_VECTOR_H
#define MPI_SIZE_T
Definition: mpi_utils.h:39
This class allows a vector of packable objects to be packed as though it were a packable object itsel...
#define CMPI_Pack
Definition: mpi_bindings.h:89
#define cmpi_packed_size
Definition: mpi_bindings.h:100
Pass this to a shared pointer if you do NOT want it to own its object.
packable_vector(const packable_vector &other)
void operator()(void const *) const
Overloaded utility functions to convert between arbitrary C/C++ types and MPI types, custom typedefs for cstdlib types like size_t, and a wrapper for MPI_Pack_Size.
void pack(void *buf, int bufsize, int *pos, MPI_Comm comm) const
Pack onto an MPI buffer.
packable_vector & operator=(const packable_vector &other)
Assignment.
int packed_size(MPI_Comm comm) const
get the number of bytes required to pack this buffer.
packable_vector< T > make_packable_vector(std::vector< T > *vec, bool owned=true)
Tempate function adaptor so we can have type inference when making packable vectors.
static packable_vector unpack(void *buf, int bufsize, int *pos, MPI_Comm comm)
Unpack from an input buffer.
#define CMPI_Unpack
Definition: mpi_bindings.h:92
#defines for switching between MPI and PMPI bindings.
packable_vector(std::vector< T > *packables, bool owned=true)
boost::shared_ptr< std::vector< T > > _packables
Muster. Copyright © 2010, Lawrence Livermore National Laboratory, LLNL-CODE-433662.
Distribution of Muster and its documentation is subject to terms of the Muster LICENSE.
Generated on Thu Sep 1 2016 using Doxygen 1.8.5