DIY 3.0 data-parallel out-of-core C++ library

- Main Page
- Modules
- Namespaces
- Classes
- Files
- Examples

- File List

All Classes Namespaces Functions Typedefs Groups Pages
datatypes.hpp
1 #ifndef DIY_MPI_DATATYPES_HPP
2 #define DIY_MPI_DATATYPES_HPP
3
4 #include <vector>
5
6 namespace diy
7 {
8 namespace mpi
9 {
10 namespace detail
11 {
12 template<class T> MPI_Datatype get_mpi_datatype();
13
14 struct true_type {};
15 struct false_type {};
16
17 /* is_mpi_datatype */
18 template<class T>
19 struct is_mpi_datatype { typedef false_type type; };
20
21 #define DIY_MPI_DATATYPE_MAP(cpp_type, mpi_type) \
22 template<> inline MPI_Datatype get_mpi_datatype<cpp_type>() { return mpi_type; } \
23 template<> struct is_mpi_datatype<cpp_type> { typedef true_type type; }; \
24 template<> struct is_mpi_datatype< std::vector<cpp_type> > { typedef true_type type; };
25
26 DIY_MPI_DATATYPE_MAP(char, MPI_BYTE);
27 DIY_MPI_DATATYPE_MAP(unsigned char, MPI_BYTE);
28 DIY_MPI_DATATYPE_MAP(bool, MPI_BYTE);
29 DIY_MPI_DATATYPE_MAP(int, MPI_INT);
30 DIY_MPI_DATATYPE_MAP(unsigned, MPI_UNSIGNED);
31 DIY_MPI_DATATYPE_MAP(long, MPI_LONG);
32 DIY_MPI_DATATYPE_MAP(unsigned long, MPI_UNSIGNED_LONG);
33 DIY_MPI_DATATYPE_MAP(long long, MPI_LONG_LONG_INT);
34 DIY_MPI_DATATYPE_MAP(unsigned long long, MPI_UNSIGNED_LONG_LONG);
35 DIY_MPI_DATATYPE_MAP(float, MPI_FLOAT);
36 DIY_MPI_DATATYPE_MAP(double, MPI_DOUBLE);
37
38 /* mpi_datatype: helper routines, specialized for std::vector<...> */
39 template<class T>
40 struct mpi_datatype
41 {
42 static MPI_Datatype datatype() { return get_mpi_datatype<T>(); }
43 static const void* address(const T& x) { return &x; }
44 static void* address(T& x) { return &x; }
45 static int count(const T&) { return 1; }
46 };
47
48 template<class U>
49 struct mpi_datatype< std::vector<U> >
50 {
51 typedef std::vector<U> VecU;
52
53 static MPI_Datatype datatype() { return get_mpi_datatype<U>(); }
54 static const void* address(const VecU& x) { return &x[0]; }
55 static void* address(VecU& x) { return &x[0]; }
56 static int count(const VecU& x) { return static_cast<int>(x.size()); }
57 };
58
59 }
60 }
61 }
62
63 #endif