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 
44 /// @brief comparison function object for two std::type_info pointers
45 /// is implemented using the before() member function of the std::type_info class
46 struct type_info_compare
47 {
operatortype_info_compare48   bool operator()(std::type_info const* lhs, std::type_info const* rhs) const
49   {
50     return lhs->before(*rhs);
51   }
52 };
53 
54 
55 class MPIDataTypeCache
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:
clear()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   }
~MPIDataTypeCache()75   ~MPIDataTypeCache()
76   {
77     	clear();
78   }
get(const std::type_info * t)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 
set(const std::type_info * t,MPI_Datatype datatype)88   void set(const std::type_info* t, MPI_Datatype datatype)
89   {
90      	 map[t] = datatype;
91   }
92 };
93 
94 
95 /**
96   * C++ type to MPIType conversion is done through functions returning the mpi types
97   * The templated function is explicitly instantiated for every C++ type
98   * that has a correspoinding MPI type. For all others, a data type is created
99   * assuming it's some sort of struct. Each created data type is committed only once
100   **/
101 
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>
MPIType(void)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
140