Muster
 All Classes Namespaces Files Functions Variables Typedefs Macros
id_pair.h
Go to the documentation of this file.
1 //////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2010, Lawrence Livermore National Security, LLC.
3 // Produced at the Lawrence Livermore National Laboratory
4 // LLNL-CODE-433662
5 // All rights reserved.
6 //
7 // This file is part of Muster. For details, see http://github.com/tgamblin/muster.
8 // Please also read the LICENSE file for further information.
9 //
10 // Redistribution and use in source and binary forms, with or without modification, are
11 // permitted provided that the following conditions are met:
12 //
13 // * Redistributions of source code must retain the above copyright notice, this list of
14 // conditions and the disclaimer below.
15 // * Redistributions in binary form must reproduce the above copyright notice, this list of
16 // conditions and the disclaimer (as noted below) in the documentation and/or other materials
17 // provided with the distribution.
18 // * Neither the name of the LLNS/LLNL nor the names of its contributors may be used to endorse
19 // or promote products derived from this software without specific prior written permission.
20 //
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
22 // OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
24 // LAWRENCE LIVERMORE NATIONAL SECURITY, LLC, THE U.S. DEPARTMENT OF ENERGY OR CONTRIBUTORS BE
25 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29 // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 //////////////////////////////////////////////////////////////////////////////////////////////////
31 
32 ///
33 /// @file id_pair.h
34 /// @brief MPI-packable, templated struct for shipping around an MPI-packable
35 /// object plus the id of the process it came from.
36 ///
37 #ifndef ID_PAIR_H
38 #define ID_PAIR_H
39 
40 #include <mpi.h>
41 #include "mpi_bindings.h"
42 
43 #include <cstdlib>
44 #include <ostream>
45 
46 namespace cluster {
47 
48  ///
49  /// MPI-packable struct for an MPI-packable type plus its object id.
50  ///
51  /// Each id_pair<T> has an element and an id for that element and supports
52  /// packed_size(), pack(), and unpack() methods for transferring these
53  /// things with MPI.
54  ///
55  /// @tparam T Type of contained element.
56  /// T Must support MPI pack(), packed_size(), and unpack() methods.
57  ///
58  template <class T>
59  struct id_pair {
60  T element; ///< The object wrapped by this id_pair.
61  size_t id; ///< Id of the rank where element came from.
62 
63  /// Template typedef for declaring vectors of id_pair<T>
64  typedef std::vector< id_pair<T> > vector;
65 
66  id_pair() { }
67  id_pair(const T& elt, size_t _id) : element(elt), id(_id) { }
68 
69  int packed_size(MPI_Comm comm) const {
70  return element.packed_size(comm) + cmpi_packed_size(1, MPI_SIZE_T, comm);
71  }
72 
73  void pack(void *buf, int bufsize, int *position, MPI_Comm comm) const {
74  element.pack(buf, bufsize, position, comm);
75  CMPI_Pack(const_cast<size_t*>(&id), 1, MPI_SIZE_T, buf, bufsize, position, comm);
76  }
77 
78  static id_pair unpack(void *buf, int bufsize, int *position, MPI_Comm comm) {
79  T t = T::unpack(buf, bufsize, position, comm);
80  size_t id;
81  CMPI_Unpack(buf, bufsize, position, &id, 1, MPI_SIZE_T, comm);
82  return id_pair(t, id);
83  }
84  };
85 
86  ///
87  /// Helper function for making arbitrary id_pairs with type inference.
88  ///
89  template <class T>
90  id_pair<T> make_id_pair(const T& elt, int id) {
91  return id_pair<T>(elt, id);
92  }
93 
94  ///
95  /// Print out an id_pair as a tuple of its element and its source rank.
96  ///
97  /// @tparam T inferred from the id_pair<T> this is called on. Must support operator<<.
98  ///
99  /// @param out Output stream to write the id_pair p onto
100  /// @param p An id_pair<T>. T must support operator<<.
101  ///
102  template <class T>
103  std::ostream& operator<<(std::ostream& out, const id_pair<T>& p) {
104  out << "<" << p.element << ", " << p.id << ">";
105  return out;
106  }
107 
108 } // namespace cluster
109 
110 #endif // ID_PAIR_H
#define MPI_SIZE_T
Definition: mpi_utils.h:39
std::vector< id_pair< T > > vector
Template typedef for declaring vectors of id_pair&lt;T&gt;
Definition: id_pair.h:64
static id_pair unpack(void *buf, int bufsize, int *position, MPI_Comm comm)
Definition: id_pair.h:78
#define CMPI_Pack
Definition: mpi_bindings.h:89
int packed_size(MPI_Comm comm) const
Definition: id_pair.h:69
#define cmpi_packed_size
Definition: mpi_bindings.h:100
id_pair< T > make_id_pair(const T &elt, int id)
Helper function for making arbitrary id_pairs with type inference.
Definition: id_pair.h:90
T element
The object wrapped by this id_pair.
Definition: id_pair.h:60
id_pair(const T &elt, size_t _id)
Definition: id_pair.h:67
MPI-packable struct for an MPI-packable type plus its object id.
Definition: id_pair.h:59
#define CMPI_Unpack
Definition: mpi_bindings.h:92
#defines for switching between MPI and PMPI bindings.
void pack(void *buf, int bufsize, int *position, MPI_Comm comm) const
Definition: id_pair.h:73
size_t id
Id of the rank where element came from.
Definition: id_pair.h:61
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