Cyclops Tensor Framework
parallel arithmetic on multidimensional arrays
idx_tensor.h
Go to the documentation of this file.
1 #ifndef __EXPRESSION_H__
2 #define __EXPRESSION_H__
3 
4 #include "term.h"
5 #include "functions.h"
6 
7 namespace CTF {
15  class Idx_Tensor : public CTF_int::Term {
16  public:
18  char * idx_map;
19  int is_intm;
20 
21 
22  // derived clone calls copy constructor
23  CTF_int::Term * clone(std::map< CTF_int::tensor*, CTF_int::tensor* >* remap = NULL) const;
24 
31  Idx_Tensor(CTF_int::tensor * parent_,
32  const char * idx_map_,
33  int copy=0);
34 
41  Idx_Tensor(CTF::Idx_Tensor const & other,
42  int copy=0,
43  std::map<CTF_int::tensor*, CTF_int::tensor*>* remap=NULL);
44 
50  Idx_Tensor(CTF_int::algstrct const * sr, double scl);
51  Idx_Tensor(CTF_int::algstrct const * sr, int64_t scl);
52  ~Idx_Tensor();
53 
54 
61  Idx_Tensor execute(std::vector<char> out_inds) const;
62 
67  void execute(Idx_Tensor output) const;
68 
73  double estimate_time(Idx_Tensor output) const;
74 
81  Idx_Tensor estimate_time(double & cost, std::vector<char> out_inds) const;
82 
87  std::vector<char> get_uniq_inds() const;
88 
92  void get_inputs(std::set<Idx_Tensor*, CTF_int::tensor_name_less >* inputs_set) const;
93 
98  void operator=(CTF_int::Term const & B);
99  void operator=(Idx_Tensor const & B);
100 
101  //same as in parent (Term) but not inherited in C++
102  void operator=(double scl);
103  void operator+=(double scl);
104  void operator-=(double scl);
105  void operator*=(double scl);
106  void multeq(double scl);
107  void operator=(int64_t scl);
108  void operator+=(int64_t scl);
109  void operator-=(int64_t scl);
110  void operator*=(int64_t scl);
111  void operator=(int scl);
112  void operator+=(int scl);
113  void operator-=(int scl);
114  void operator*=(int scl);
115 
120  void operator+=(CTF_int::Term const & B);
121 
126  void operator-=(CTF_int::Term const & B);
127 
131 // Idx_Tensor operator-() const;
132 
133 
138  void operator*=(CTF_int::Term const & B);
139 
144  //void operator/(IdxTensor& tsr);
145 
149  //void run(Idx_Tensor* output, dtype beta);
150 
151  /*operator CTF_int::Term* (){
152  Idx_Tensor * tsr = new Idx_Tensor(*this);
153  return tsr;
154  }*/
155 
156 
160  World * where_am_i() const;
161  };
162 
163  template<typename dtype>
165 
166 
167  template<typename dtype_A, typename dtype_B>
168  class Univar_Transform;
169 
170  template<typename dtype_A, typename dtype_B, typename dtype_C>
172 
173  template<typename dtype_A, typename dtype_B>
175  public:
176 
178 
179  void operator()(std::function<void(dtype_A, dtype_B&)> f){
180  (Transform<dtype_A,dtype_B>(f))(*operands[0],*operands[1]);
181  }
182 
183  void operator,(std::function<void(dtype_A, dtype_B&)> f){
184  (Transform<dtype_A,dtype_B>(f))(*operands[0],*operands[1]);
185  }
186 
187  void operator()(std::function<dtype_B(dtype_A)> f){
188  ((Function<dtype_A,dtype_B>(f))(*operands[0])).execute(operands[1]->execute(this->get_uniq_inds()));
189  }
190 
191  void operator,(std::function<dtype_B(dtype_A)> f){
192  ((Function<dtype_A,dtype_B>(f))(*operands[0])).execute(operands[1]->execute(this->get_uniq_inds()));
193  }
194 
195 
196  template<typename dtype_C>
199  }
200  };
201 
202  template<typename dtype_A, typename dtype_B, typename dtype_C>
204  public:
208  : Contract_Term(S.operands[0]->clone(),
209  S.operands[1]->clone()) {
210  C = C_;
211  }
212 
213  void operator,(std::function<dtype_C(dtype_A, dtype_B)> f){
214  ((Function<dtype_A,dtype_B,dtype_C>(f))(*operands[1],*operands[0])).execute(*C);
215  }
216 
217  void operator,(std::function<void(dtype_A, dtype_B, dtype_C&)> f){
218  ((Transform<dtype_A,dtype_B,dtype_C>(f)))(*operands[1],*operands[0],*C);
219  }
220 
221  };
222 
223  template<typename dtype>
225 
226  template<typename dtype>
227  class Typ_Idx_Tensor : public Idx_Tensor {
228  public:
230 
238  const char * idx_map_,
239  int copy=0) : Idx_Tensor(parent_, idx_map_, copy) {}
240 
248  int copy=0,
249  std::map<CTF_int::tensor*, CTF_int::tensor*>* remap=NULL) : Idx_Tensor(B, copy, remap) {}
250 
251 
252  Typ_Idx_Tensor<dtype> * tclone() const { return new Typ_Idx_Tensor<dtype>(*this); }
253 
254  CTF_int::Term * clone(std::map< CTF_int::tensor*, CTF_int::tensor* >* remap = NULL) const { return new Typ_Idx_Tensor<dtype>(*this, 0, remap); }
255 
258  void operator=(double scl){ Idx_Tensor::operator=(scl); }
259  void operator=(int64_t scl){ Idx_Tensor::operator=(scl); }
261 
262 
263 
264  template <typename dtype_B>
266  return Typ_Sum_Term<dtype, dtype_B>(this->tclone(), B.tclone());
267  }
268 
269  template <typename dtype_A, typename dtype_B>
271  return Typ_Contract_Term<dtype_A,dtype_B,dtype>(this->tclone(), t);
272  }
273  /*
274  template <typename dtype_A, typename dtype_B>
275  Typ_Contract_Term<dtype_A, dtype_B, dtype> operator&=(Typ_Sum_Term<dtype_A,dtype_B> t){
276  sr->safecopy(scale,sr->addid());
277  return Typ_Contract_Term<dtype_A,dtype_B,dtype>(this->clone(), t);
278  }*/
279 
280  template <typename dtype_A, typename dtype_B>
282  sr->safecopy(scale,sr->addid());
284  }
285 
287  return Typ_AIdx_Tensor<dtype>(*this);
288  }
289 
290 
291  template <typename dtype_A>
293  sr->safecopy(scale,sr->addid());
294  return Typ_Sum_Term<dtype_A,dtype>(t.tclone(), this->tclone());
295  }
296 
297  template <typename dtype_A>
299  return Typ_Sum_Term<dtype_A,dtype>(t.tclone(), this->tclone());
300  }
301 
304  void operator+=(double scl){ Idx_Tensor::operator+=(scl); }
305  void operator+=(int64_t scl){ Idx_Tensor::operator+=(scl); }
307 
308 
309  void operator,(std::function<void(dtype&)> f){
310  ((Transform<dtype>(f)))(*this);
311  }
312 
313  void operator()(std::function<void(dtype&)> f){
314  ((Transform<dtype>(f)))(*this);
315  }
316 
317  };
318 
319  template<typename dtype>
320  class Typ_AIdx_Tensor : public Typ_Idx_Tensor<dtype> {
321  public:
323  };
327 }
328 
329 
330 //include here because requires above defs
331 #include "../tensor/untyped_tensor_tmpl.h"
332 #endif
void operator+=(CTF_int::Term const &B)
Definition: idx_tensor.h:302
a term is an abstract object representing some expression of tensors
Definition: term.h:33
algstrct * sr
Definition: term.h:36
Typ_Contract_Term< dtype_A, dtype_B, dtype > operator=(Typ_Sum_Term< dtype_A, dtype_B > t)
Definition: idx_tensor.h:281
void operator,(std::function< void(dtype_A, dtype_B &)> f)
Definition: idx_tensor.h:183
void operator+=(double scl)
Definition: idx_tensor.cxx:219
void operator+=(int scl)
Definition: idx_tensor.h:306
void safecopy(char *&a, char const *b) const
copies element b to element a, , with checks for NULL and alloc as necessary
Definition: algstrct.cxx:529
char * idx_map
Definition: idx_tensor.h:18
Typ_Contract_Term< dtype_A, dtype_B, dtype_C > operator&(Typ_Idx_Tensor< dtype_C > C)
Definition: idx_tensor.h:197
void operator=(Idx_Tensor const &B)
Definition: idx_tensor.h:257
void operator,(std::function< void(dtype_A, dtype_B, dtype_C &)> f)
Definition: idx_tensor.h:217
Typ_Sum_Term< dtype_A, dtype > operator=(Typ_AIdx_Tensor< dtype_A > t)
Definition: idx_tensor.h:292
virtual char const * addid() const
MPI datatype for pairs.
Definition: algstrct.cxx:89
void get_inputs(std::set< Idx_Tensor *, CTF_int::tensor_name_less > *inputs_set) const
appends the tensors this depends on to the input set
Definition: idx_tensor.cxx:320
an instance of the CTF library (world) on a MPI communicator
Definition: world.h:19
void operator=(double scl)
Definition: idx_tensor.h:258
void operator=(CTF_int::Term const &B)
Definition: idx_tensor.h:256
Typ_Contract_Term< dtype_A, dtype_B, dtype > operator+=(Typ_Sum_Term< dtype_A, dtype_B > t)
Definition: idx_tensor.h:270
def scl(self, s)
Definition: core.pyx:473
Typ_AIdx_Tensor(Typ_Idx_Tensor< dtype > const &A)
Definition: idx_tensor.h:322
void operator()(std::function< dtype_B(dtype_A)> f)
Definition: idx_tensor.h:187
Typ_Idx_Tensor< dtype > * tclone() const
Definition: idx_tensor.h:252
Typ_Sum_Term< dtype, dtype_B > operator&(Typ_Idx_Tensor< dtype_B > B)
Definition: idx_tensor.h:265
void operator,(std::function< dtype_B(dtype_A)> f)
Definition: idx_tensor.h:191
Typ_Contract_Term(Typ_Idx_Tensor< dtype_C > *C_, Typ_Sum_Term< dtype_A, dtype_B > S)
Definition: idx_tensor.h:206
CTF_int::Term * clone(std::map< CTF_int::tensor *, CTF_int::tensor * > *remap=NULL) const
base classes must implement this copy function to retrieve pointer
Definition: idx_tensor.cxx:168
Typ_AIdx_Tensor< dtype > operator~()
Definition: idx_tensor.h:286
Idx_Tensor(CTF_int::tensor *parent_, const char *idx_map_, int copy=0)
constructor takes in a parent tensor and its indices
Definition: idx_tensor.cxx:83
def copy(tensor, A)
Definition: core.pyx:3583
void operator+=(Idx_Tensor const &B)
Definition: idx_tensor.h:303
double estimate_time(Idx_Tensor output) const
estimates the cost of a contraction
Definition: idx_tensor.cxx:294
std::vector< char > get_uniq_inds() const
find list of unique indices that are involved in this term
Definition: idx_tensor.cxx:311
void operator()(std::function< void(dtype_A, dtype_B &)> f)
Definition: idx_tensor.h:179
Typ_Sum_Term(Typ_Idx_Tensor< dtype_A > *A, Typ_Idx_Tensor< dtype_B > *B)
Definition: idx_tensor.h:177
void operator=(int64_t scl)
Definition: idx_tensor.h:259
Idx_Tensor execute(std::vector< char > out_inds) const
evalues the expression to produce an intermediate with all expression indices remaining ...
Definition: idx_tensor.cxx:290
algstrct (algebraic structure) defines the elementwise operations computed in each tensor contraction...
Definition: algstrct.h:34
Definition: apsp.cxx:17
internal distributed tensor class
void operator=(int scl)
Definition: idx_tensor.h:260
void operator+=(int64_t scl)
Definition: idx_tensor.h:305
CTF_int::Term * clone(std::map< CTF_int::tensor *, CTF_int::tensor * > *remap=NULL) const
base classes must implement this copy function to retrieve pointer
Definition: idx_tensor.h:254
Typ_Idx_Tensor< dtype_C > * C
Definition: idx_tensor.h:205
void operator=(CTF_int::Term const &B)
A = B, compute any operations on operand B and set.
Definition: idx_tensor.cxx:192
char * scale
Definition: term.h:35
Typ_Sum_Term< dtype_A, dtype > operator+=(Typ_AIdx_Tensor< dtype_A > t)
Definition: idx_tensor.h:298
void operator-=(double scl)
Definition: idx_tensor.cxx:220
void operator()(std::function< void(dtype &)> f)
Definition: idx_tensor.h:313
An experession representing a contraction of a set of tensors contained in operands.
Definition: term.h:275
Typ_Idx_Tensor(Typ_Idx_Tensor const &B, int copy=0, std::map< CTF_int::tensor *, CTF_int::tensor * > *remap=NULL)
copy constructor
Definition: idx_tensor.h:247
custom function f : (X * Y) -> X applied on two tensors as summation: e.g. B["ij"] = f(A["ij"]...
Definition: functions.h:96
a tensor with an index map associated with it (necessary for overloaded operators) ...
Definition: idx_tensor.h:15
World * where_am_i() const
figures out what world this term lives on
Definition: idx_tensor.cxx:172
void operator,(std::function< dtype_C(dtype_A, dtype_B)> f)
Definition: idx_tensor.h:213
void operator*=(double scl)
Definition: idx_tensor.cxx:221
void operator,(std::function< void(dtype &)> f)
Definition: idx_tensor.h:309
void multeq(double scl)
Definition: idx_tensor.cxx:222
void operator+=(double scl)
Definition: idx_tensor.h:304
Typ_Idx_Tensor(CTF_int::tensor *parent_, const char *idx_map_, int copy=0)
constructor takes in a parent tensor and its indices
Definition: idx_tensor.h:237
CTF_int::tensor * parent
Definition: idx_tensor.h:17