COMBINATORIAL_BLAS  1.6
MPIType.h
Go to the documentation of this file.
1 /****************************************************************/
2 /* Parallel Combinatorial BLAS Library (for Graph Computations) */
3 /* version 1.6 -------------------------------------------------*/
4 /* date: 6/15/2017 ---------------------------------------------*/
5 /* authors: Ariful Azad, Aydin Buluc --------------------------*/
6 /****************************************************************/
7 /*
8  Copyright (c) 2010-2017, The Regents of the University of California
9 
10  Permission is hereby granted, free of charge, to any person obtaining a copy
11  of this software and associated documentation files (the "Software"), to deal
12  in the Software without restriction, including without limitation the rights
13  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14  copies of the Software, and to permit persons to whom the Software is
15  furnished to do so, subject to the following conditions:
16 
17  The above copyright notice and this permission notice shall be included in
18  all copies or substantial portions of the Software.
19 
20  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26  THE SOFTWARE.
27  */
28 
29 
30 #ifndef _MPI_TYPE_H
31 #define _MPI_TYPE_H
32 
33 #include <iostream>
34 #include <typeinfo>
35 #include <map>
36 #include <mpi.h>
37 #include <stdint.h>
38 
39 namespace combblas {
40 
41 
42 // A datatype cache inspired by (mostly copied from) Boost http://www.boost.org/LICENSE_1_0.txt)
43 
47 {
48  bool operator()(std::type_info const* lhs, std::type_info const* rhs) const
49  {
50  return lhs->before(*rhs);
51  }
52 };
53 
54 
56 {
57 private:
58  typedef std::map<std::type_info const*,MPI_Datatype,type_info_compare> stored_map_type;
59  stored_map_type map;
60 
61 public:
62  void clear()
63  {
64  int is_finalized=0;
65  MPI_Finalized(&is_finalized);
66  if (! is_finalized ) // do not free after call to MPI_FInalize
67  {
68  // ignore errors in the destructor
69  for (stored_map_type::iterator it=map.begin(); it != map.end(); ++it)
70  {
71  MPI_Type_free(&(it->second));
72  }
73  }
74  }
76  {
77  clear();
78  }
79  MPI_Datatype get(const std::type_info* t)
80  {
81  stored_map_type::iterator pos = map.find(t);
82  if (pos != map.end())
83  return pos->second;
84  else
85  return MPI_DATATYPE_NULL;
86  }
87 
88  void set(const std::type_info* t, MPI_Datatype datatype)
89  {
90  map[t] = datatype;
91  }
92 };
93 
94 
102 extern MPIDataTypeCache mpidtc; // global variable
103 // Global variables have program scope, which means they can be accessed everywhere in the program, and they are only destroyed when the program ends.
104 
105 template <typename T>
106 MPI_Datatype MPIType ( void )
107 {
108  std::type_info const* t = &typeid(T);
109  MPI_Datatype datatype = mpidtc.get(t);
110 
111  if (datatype == MPI_DATATYPE_NULL)
112  {
113  MPI_Type_contiguous(sizeof(T), MPI_CHAR, &datatype );
114  MPI_Type_commit(&datatype);
115  int myrank;
116  MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
117  mpidtc.set(t, datatype);
118  }
119  return datatype;
120 };
121 
122 template<> MPI_Datatype MPIType< signed char >( void );
123 template<> MPI_Datatype MPIType< signed short int >( void );
124 template<> MPI_Datatype MPIType< unsigned char >( void );
125 template<> MPI_Datatype MPIType< unsigned short int >( void );
126 template<> MPI_Datatype MPIType< int32_t >( void );
127 template<> MPI_Datatype MPIType< uint32_t >( void );
128 template<> MPI_Datatype MPIType< int64_t >( void );
129 template<> MPI_Datatype MPIType< uint64_t >( void );
130 template<> MPI_Datatype MPIType< float >( void );
131 template<> MPI_Datatype MPIType< double >( void );
132 template<> MPI_Datatype MPIType< long double >( void );
133 template<> MPI_Datatype MPIType< bool >( void );
134 
135 
136 
137 }
138 
139 #endif
bool operator()(std::type_info const *lhs, std::type_info const *rhs) const
Definition: MPIType.h:48
MPI_Datatype MPIType< int64_t >(void)
Definition: MPIType.cpp:64
MPI_Datatype MPIType< uint64_t >(void)
Definition: MPIType.cpp:68
MPI_Datatype MPIType< unsigned short int >(void)
Definition: MPIType.cpp:52
void set(const std::type_info *t, MPI_Datatype datatype)
Definition: MPIType.h:88
MPI_Datatype MPIType< int32_t >(void)
Definition: MPIType.cpp:56
MPI_Datatype MPIType< float >(void)
Definition: MPIType.cpp:72
MPI_Datatype MPIType< unsigned char >(void)
Definition: MPIType.cpp:44
MPI_Datatype MPIType< double >(void)
Definition: MPIType.cpp:76
MPIDataTypeCache mpidtc
Definition: MPIType.h:102
MPI_Datatype MPIType(void)
Definition: MPIType.h:106
MPI_Datatype MPIType< signed short int >(void)
Definition: MPIType.cpp:48
MPI_Datatype MPIType< uint32_t >(void)
Definition: MPIType.cpp:60
MPI_Datatype get(const std::type_info *t)
Definition: MPIType.h:79
Definition: CCGrid.h:4
MPI_Datatype MPIType< bool >(void)
Definition: MPIType.cpp:84
MPI_Datatype MPIType< long double >(void)
Definition: MPIType.cpp:80
MPI_Datatype MPIType< signed char >(void)
Definition: MPIType.cpp:40
comparison function object for two std::type_info pointers is implemented using the before() member f...
Definition: MPIType.h:46