Cyclops Tensor Framework
parallel arithmetic on multidimensional arrays
monoid.h
Go to the documentation of this file.
1 #ifndef __MONOID_H__
2 #define __MONOID_H__
3 
4 namespace CTF_int {
5  template <typename dtype>
7  return a+b;
8  }
9 
10  template <typename dtype, void (*fxpy)(int, dtype const *, dtype *)>
11  void default_mxpy(void * X,
12  void * Y,
13  int * n,
14  MPI_Datatype * d){
15  fxpy(*n, (dtype const*)X, (dtype *)Y);
16  }
17 
18  template <typename dtype>
19  void default_fxpy(int n,
20  dtype const * X,
21  dtype * Y){
22  for (int i=0; i<n; i++){
23  Y[i] = X[i] + Y[i];
24  }
25  }
26 
27  template <typename dtype>
29  //FIXME: assumes + operator commutes
30  MPI_Op newop;
31 // default_mxpy<dtype,default_fxpy<dtype>>(NULL, NULL, 0);
32  MPI_Op_create(&default_mxpy< dtype, default_fxpy<dtype> >, 1, &newop);
33  return newop;
34  }
35 
36  //c++ sucks...
37  template <> inline MPI_Op get_default_maddop<char>(){ return MPI_SUM; }
38  template <> inline MPI_Op get_default_maddop<bool>(){ return MPI_SUM; }
39  template <> inline MPI_Op get_default_maddop<int>(){ return MPI_SUM; }
40  template <> inline MPI_Op get_default_maddop<int64_t>(){ return MPI_SUM; }
41  template <> inline MPI_Op get_default_maddop<unsigned int>(){ return MPI_SUM; }
42  template <> inline MPI_Op get_default_maddop<uint64_t>(){ return MPI_SUM; }
43  template <> inline MPI_Op get_default_maddop<float>(){ return MPI_SUM; }
44  template <> inline MPI_Op get_default_maddop<double>(){ return MPI_SUM; }
45  template <> inline MPI_Op get_default_maddop<long double>(){ return MPI_SUM; }
46  template <> inline MPI_Op get_default_maddop< std::complex<float> >(){ return MPI_SUM; }
47  template <> inline MPI_Op get_default_maddop< std::complex<double> >(){ return MPI_SUM; }
48 
49  template <typename dtype>
50  MPI_Op get_maddop(void (*fxpy)(int, dtype const *, dtype *)){
51  //FIXME: assumes + operator commutes
52  MPI_Op newop;
53  MPI_Op_create(&default_mxpy<dtype, fxpy>, 1, &newop);
54  return newop;
55  }
56 }
57 
58 namespace CTF {
68  template <typename dtype=double, bool is_ord=CTF_int::get_default_is_ord<dtype>()>
69  class Monoid : public Set<dtype, is_ord> {
70  public:
73  MPI_Op taddmop;
74 
75  Monoid(Monoid const & other) : Set<dtype, is_ord>(other), taddid(other.taddid), fadd(other.fadd), taddmop(other.taddmop) {
76  }
77 
78  virtual CTF_int::algstrct * clone() const {
79  return new Monoid<dtype, is_ord>(*this);
80  }
81  Monoid() : Set<dtype, is_ord>(), taddid(0) {
82  fadd = &CTF_int::default_add<dtype>;
83  taddmop = CTF_int::get_default_maddop<dtype>();
84  }
85 
86  Monoid(dtype taddid_) : Set<dtype, is_ord>(), taddid(taddid_) {
87  fadd = &CTF_int::default_add<dtype>;
88  taddmop = CTF_int::get_default_maddop<dtype>();
89  }
90 
91 
92 
93  Monoid(dtype taddid_,
94  dtype (*fadd_)(dtype a, dtype b),
95  MPI_Op addmop_)
96  : Set<dtype, is_ord>(), taddid(taddid_) {
97  fadd = fadd_;
98  taddmop = addmop_;
99  }
100 
101  void add(char const * a,
102  char const * b,
103  char * c) const {
104  ((dtype*)c)[0] = fadd(((dtype*)a)[0],((dtype*)b)[0]);
105  }
106 
107  char const * addid() const {
108  return (char const *)&taddid;
109  }
110 
111  MPI_Op addmop() const {
112  return taddmop;
113  }
114 
115  void init(int64_t n, char * arr) const {
116  std::fill((dtype*)arr,((dtype*)arr)+n,taddid);
117  }
118 
119  void axpy(int n,
120  char const * alpha,
121  char const * X,
122  int incX,
123  char * Y,
124  int incY) const {
125  //assert(alpha == NULL);
126  for (int64_t i=0; i<n; i++){
127  add(X+sizeof(dtype)*i*incX,Y+sizeof(dtype)*i*incY,Y+sizeof(dtype)*i*incY);
128  }
129  }
130 
132  char * csr_add(char * cA, char * cB) const {
133  return CTF_int::algstrct::csr_add(cA, cB);
134  }
135 
136  };
137  template <>
138  char * Monoid<double,1>::csr_add(char *, char *) const;
139 
143 }
144 
145 #include "group.h"
146 #endif
147 
Set class defined by a datatype and a min/max function (if it is partially ordered i...
Definition: set.h:280
virtual char * csr_add(char *cA, char *cB) const
adds CSR matrices A (stored in cA) and B (stored in cB) to create matric C (pointer to all_data retur...
Definition: algstrct.cxx:362
MPI_Op get_default_maddop< float >()
Definition: monoid.h:43
void init(int64_t n, char *arr) const
initialize n objects to zero
Definition: monoid.h:115
MPI_Op get_default_maddop< char >()
Definition: monoid.h:37
Monoid(dtype taddid_, dtype(*fadd_)(dtype a, dtype b), MPI_Op addmop_)
Definition: monoid.h:93
MPI_Op get_default_maddop< int64_t >()
Definition: monoid.h:40
MPI_Op get_default_maddop< double >()
Definition: monoid.h:44
MPI_Op get_default_maddop< unsigned int >()
Definition: monoid.h:41
MPI_Op get_maddop(void(*fxpy)(int, dtype const *, dtype *))
Definition: monoid.h:50
MPI_Op get_default_maddop< uint64_t >()
Definition: monoid.h:42
MPI_Op get_default_maddop< int >()
Definition: monoid.h:39
MPI_Op taddmop
Definition: monoid.h:73
void add(char const *a, char const *b, char *c) const
c = a+b
Definition: monoid.h:101
virtual CTF_int::algstrct * clone() const
&#39;&#39;copy constructor&#39;&#39;
Definition: monoid.h:78
char * csr_add(char *cA, char *cB) const
adds CSR matrices A (stored in cA) and B (stored in cB) to create matric C (pointer to all_data retur...
Definition: monoid.h:132
MPI_Op get_default_maddop< bool >()
Definition: monoid.h:38
Monoid(dtype taddid_)
Definition: monoid.h:86
char const * addid() const
MPI datatype for pairs.
Definition: monoid.h:107
Monoid(Monoid const &other)
Definition: monoid.h:75
MPI_Op addmop() const
MPI addition operation for reductions.
Definition: monoid.h:111
MPI_Op get_default_maddop()
Definition: monoid.h:28
void axpy(int n, char const *alpha, char const *X, int incX, char *Y, int incY) const
Y["i"]+=alpha*X["i"];.
Definition: monoid.h:119
dtype default_add(dtype a, dtype b)
Definition: monoid.h:6
Monoid()
Definition: monoid.h:81
MPI_Op get_default_maddop< long double >()
Definition: monoid.h:45
algstrct (algebraic structure) defines the elementwise operations computed in each tensor contraction...
Definition: algstrct.h:34
Definition: apsp.cxx:17
A Monoid is a Set equipped with a binary addition operator &#39;+&#39; or a custom function addition must hav...
Definition: monoid.h:69
void default_mxpy(void *X, void *Y, int *n, MPI_Datatype *d)
Definition: monoid.h:11
dtype taddid
Definition: monoid.h:71
dtype(* fadd)(dtype a, dtype b)
Definition: monoid.h:72
void default_fxpy(int n, dtype const *X, dtype *Y)
Definition: monoid.h:19