G+Smo  24.08.0
Geometry + Simulation Modules
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
gsBinaryFunctions.h
Go to the documentation of this file.
1 
18 #pragma once
19 
20 // #include <functional>
21 // #include <algorithm>
22 
23 namespace gismo
24 {
25 template<typename Type>
26 struct Min
27  : std::binary_function<Type,Type,Type>
28 {
29  Type operator()(const Type& t1, const Type& t2) const
30  {
31  return std::min(t1,t2);
32  }
33 };
34 
35 template<typename Type>
36 struct Max
37  : std::binary_function<Type,Type,Type>
38 {
39  Type operator()(const Type& t1, const Type& t2) const
40  {
41  return std::max(t1,t2);
42  }
43 };
44 
45 
46 
47 template<typename Type, typename BinaryFunction>
48 class Generic_MPI_Op
49 {
50 
51 public:
52  static MPI_Op get ()
53  {
54  if (!op) // if op is null then create new op
55  {
56  op = memory::shared_ptr<MPI_Op>(new MPI_Op);
57  MPI_Op_create((void (*)(void*, void*, int*, MPI_Datatype*))&operation,true,op.get());
58  }
59  return *op;
60  }
61 private:
62  static void operation (Type *in, Type *inout, int *len, MPI_Datatype*)
63  {
64  BinaryFunction func;
65 
66  for (int i=0; i< *len; ++i, ++in, ++inout) {
67  Type temp;
68  temp = func(*in, *inout);
69  *inout = temp;
70  }
71  }
72  Generic_MPI_Op () {}
73  Generic_MPI_Op (const Generic_MPI_Op& ) {}
74  static memory::shared_ptr<MPI_Op> op; // to check: can do with static MPI_Op ?
75 };
76 
77 
78 template<typename Type, typename BinaryFunction>
79 memory::shared_ptr<MPI_Op> Generic_MPI_Op<Type,BinaryFunction>::op = memory::shared_ptr<MPI_Op>(static_cast<MPI_Op*>(0));
80 
81 #define ComposeMPIOp(type,func,op) \
82  template<> \
83  class Generic_MPI_Op<type, func<type> >{ \
84  public: \
85  static MPI_Op get(){ \
86  return op; \
87  } \
88  private: \
89  Generic_MPI_Op () {} \
90  Generic_MPI_Op (const Generic_MPI_Op & ) {} \
91  }
92 
93 
94 ComposeMPIOp(char, std::plus, MPI_SUM);
95 ComposeMPIOp(unsigned char, std::plus, MPI_SUM);
96 ComposeMPIOp(short, std::plus, MPI_SUM);
97 ComposeMPIOp(unsigned short, std::plus, MPI_SUM);
98 ComposeMPIOp(int, std::plus, MPI_SUM);
99 ComposeMPIOp(unsigned int, std::plus, MPI_SUM);
100 ComposeMPIOp(long, std::plus, MPI_SUM);
101 ComposeMPIOp(unsigned long, std::plus, MPI_SUM);
102 ComposeMPIOp(float, std::plus, MPI_SUM);
103 ComposeMPIOp(double, std::plus, MPI_SUM);
104 ComposeMPIOp(long double, std::plus, MPI_SUM);
105 
106 ComposeMPIOp(char, std::multiplies, MPI_PROD);
107 ComposeMPIOp(unsigned char, std::multiplies, MPI_PROD);
108 ComposeMPIOp(short, std::multiplies, MPI_PROD);
109 ComposeMPIOp(unsigned short, std::multiplies, MPI_PROD);
110 ComposeMPIOp(int, std::multiplies, MPI_PROD);
111 ComposeMPIOp(unsigned int, std::multiplies, MPI_PROD);
112 ComposeMPIOp(long, std::multiplies, MPI_PROD);
113 ComposeMPIOp(unsigned long, std::multiplies, MPI_PROD);
114 ComposeMPIOp(float, std::multiplies, MPI_PROD);
115 ComposeMPIOp(double, std::multiplies, MPI_PROD);
116 ComposeMPIOp(long double, std::multiplies, MPI_PROD);
117 
118 ComposeMPIOp(char, Min, MPI_MIN);
119 ComposeMPIOp(unsigned char, Min, MPI_MIN);
120 ComposeMPIOp(short, Min, MPI_MIN);
121 ComposeMPIOp(unsigned short, Min, MPI_MIN);
122 ComposeMPIOp(int, Min, MPI_MIN);
123 ComposeMPIOp(unsigned int, Min, MPI_MIN);
124 ComposeMPIOp(long, Min, MPI_MIN);
125 ComposeMPIOp(unsigned long, Min, MPI_MIN);
126 ComposeMPIOp(float, Min, MPI_MIN);
127 ComposeMPIOp(double, Min, MPI_MIN);
128 ComposeMPIOp(long double, Min, MPI_MIN);
129 
130 ComposeMPIOp(char, Max, MPI_MAX);
131 ComposeMPIOp(unsigned char, Max, MPI_MAX);
132 ComposeMPIOp(short, Max, MPI_MAX);
133 ComposeMPIOp(unsigned short, Max, MPI_MAX);
134 ComposeMPIOp(int, Max, MPI_MAX);
135 ComposeMPIOp(unsigned int, Max, MPI_MAX);
136 ComposeMPIOp(long, Max, MPI_MAX);
137 ComposeMPIOp(unsigned long, Max, MPI_MAX);
138 ComposeMPIOp(float, Max, MPI_MAX);
139 ComposeMPIOp(double, Max, MPI_MAX);
140 ComposeMPIOp(long double, Max, MPI_MAX);
141 
142 #undef ComposeMPIOp
143 
144 
145 }