Cyclops Tensor Framework
parallel arithmetic on multidimensional arrays
fun_term.cxx
Go to the documentation of this file.
1 /*Copyright (c) 2013, Edgar Solomonik, all rights reserved.*/
2 #include "fun_term.h"
3 #include "common.h"
4 #include "../tensor/algstrct.h"
5 #include "../scaling/scaling.h"
6 #include "../shared/util.h"
7 #include "functions.h"
8 #include "idx_tensor.h"
9 
10 namespace CTF_int {
12  univar_function const * func_) : Term(A_->sr) {
13  A = A_;
14  func = func_;
15  }
16 
18  delete A;
19  }
20 
21  Term * Unifun_Term::clone(std::map<tensor*, tensor*>* remap) const{
22  return new Unifun_Term(*this, remap);
23  }
24 
26  Unifun_Term const & other,
27  std::map<tensor*, tensor*>* remap) : Term(other.sr) {
28  sr->safecopy(this->scale, other.scale);
29  func = other.func;
30  A = other.A->clone(remap);
31  }
32 
34  CTF::Idx_Tensor opA = A->execute(this->get_uniq_inds());
35  summation s(opA.parent, opA.idx_map, opA.scale, output.parent, output.idx_map, output.scale, func);
36  s.execute();
37  }
38 
39  CTF::Idx_Tensor Unifun_Term::execute(std::vector<char> out_inds) const {
40  printf("Univar Unifunction applications cannot currently be a part of a longer algebraic expression\n");
41  assert(0);
42  return CTF::Idx_Tensor(NULL);
43  }
44 
45  CTF::Idx_Tensor Unifun_Term::estimate_time(double & cost, std::vector<char> out_inds) const {
46  printf("Univar Unifunction applications cannot currently be a part of a longer algebraic expression\n");
47  assert(0);
48  return CTF::Idx_Tensor(NULL);
49  }
51  double cost = 0.0;
52  CTF::Idx_Tensor opA = A->estimate_time(cost, this->get_uniq_inds());
53  summation s(opA.parent, opA.idx_map, opA.scale, output.parent, output.idx_map, output.scale, func);
54  cost += s.estimate_time();
55  return cost;
56  }
57 
58  std::vector<char> Unifun_Term::get_uniq_inds() const{
59  return A->get_uniq_inds();
60  }
61 
62  void Unifun_Term::get_inputs(std::set<CTF::Idx_Tensor*, CTF_int::tensor_name_less >* inputs_set) const {
63  A->get_inputs(inputs_set);
64  }
65 
67  return A->where_am_i();
68  }
69 
71  Term * B_,
72  bivar_function const * func_) : Term(A_->sr) {
73  A = A_;
74  B = B_;
75  func = func_;
76  }
77 
79  delete A;
80  delete B;
81  }
82 
83  Term * Bifun_Term::clone(std::map<tensor*, tensor*>* remap) const{
84  return new Bifun_Term(*this, remap);
85  }
86 
88  Bifun_Term const & other,
89  std::map<tensor*, tensor*>* remap) : Term(other.sr) {
90  sr->safecopy(this->scale, other.scale);
91  func = other.func;
92  A = other.A->clone(remap);
93  B = other.B->clone(remap);
94  }
95 
96  void Bifun_Term::execute(CTF::Idx_Tensor output) const {
99 /* char * scl;
100  scl = NULL;
101  if (opA.scale != NULL || opB.scale != NULL)
102  opA.sr->safemul(opA.scale, opB.scale, scl);*/
103  if (!opA.sr->isequal(opA.scale, opA.sr->mulid()) ||
104  !opB.sr->isequal(opB.scale, opB.sr->mulid()) /*||
105  !output.sr->isequal(output.scale, output.sr->mulid())*/){
106  if (opA.parent->wrld->rank == 0)
107  printf("CTF ERROR: cannot scale tensors when using bilinear function or transform, aborting.\n");
108  ASSERT(0);
109  assert(0);
110  }
111  contraction c(opA.parent, opA.idx_map, opB.parent, opB.idx_map, output.sr->mulid(), output.parent, output.idx_map, output.scale, func);
112  //contraction c(opA.parent, opA.idx_map, opB.parent, opB.idx_map, NULL, output.parent, output.idx_map, output.scale, func);
113  c.execute();
114 // if (scl != NULL) cdealloc(scl);
115  }
116 
117  CTF::Idx_Tensor Bifun_Term::execute(std::vector<char> out_inds) const {
118  printf("Bivar Bifunction applications cannot currently be a part of a longer algebraic expression\n");
119  assert(0);
120  return CTF::Idx_Tensor(NULL);
121  }
122 
123  CTF::Idx_Tensor Bifun_Term::estimate_time(double & cost, std::vector<char> out_inds) const {
124  printf("Bivar Bifunction applications cannot currently be a part of a longer algebraic expression\n");
125  assert(0);
126  return CTF::Idx_Tensor(NULL);
127  }
129  double cost = 0.0;
130  CTF::Idx_Tensor opA = A->estimate_time(cost, A->get_uniq_inds());
131  CTF::Idx_Tensor opB = B->estimate_time(cost, B->get_uniq_inds());
132  contraction c(opA.parent, opA.idx_map, opB.parent, opB.idx_map, output.sr->mulid(), output.parent, output.idx_map, output.scale, func);
133  cost += c.estimate_time();
134  return cost;
135  }
136 
137  std::vector<char> Bifun_Term::get_uniq_inds() const{
138  std::vector<char> iA = A->get_uniq_inds();
139  std::vector<char> iB = B->get_uniq_inds();
140 
141  std::set<char> uniq_inds;
142  for (int i=0; i<(int)iA.size(); i++){
143  uniq_inds.insert(iA[i]);
144  }
145  for (int i=0; i<(int)iB.size(); i++){
146  uniq_inds.insert(iB[i]);
147  }
148  return std::vector<char>(uniq_inds.begin(), uniq_inds.end());
149  }
150 
151  void Bifun_Term::get_inputs(std::set<CTF::Idx_Tensor*, CTF_int::tensor_name_less >* inputs_set) const {
152  A->get_inputs(inputs_set);
153  B->get_inputs(inputs_set);
154  }
155 
157  if (A->where_am_i() != NULL)
158  return A->where_am_i();
159  else
160  return B->where_am_i();
161  }
162 
163 
164 }
a term is an abstract object representing some expression of tensors
Definition: term.h:33
CTF::World * where_am_i() const
figures out what world this term lives on
Definition: fun_term.cxx:156
void execute(CTF::Idx_Tensor output) const
evalues the expression, which just scales by default
Definition: fun_term.cxx:33
algstrct * sr
Definition: term.h:36
virtual void execute(CTF::Idx_Tensor output) const =0
evalues the expression, which just scales by default
void execute()
run contraction
Definition: contraction.cxx:99
bivar_function const * func
Definition: fun_term.h:46
std::vector< char > get_uniq_inds() const
find list of unique indices that are involved in this term
Definition: fun_term.cxx:137
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
void execute(bool run_diag=false)
run summation
Definition: summation.cxx:119
virtual bool isequal(char const *a, char const *b) const
returns true if algstrct elements a and b are equal
Definition: algstrct.cxx:340
char * idx_map
Definition: idx_tensor.h:18
#define ASSERT(...)
Definition: util.h:88
CTF::World * where_am_i() const
figures out what world this term lives on
Definition: fun_term.cxx:66
untyped internal class for doubly-typed univariate function
Definition: sum_tsr.h:14
an instance of the CTF library (world) on a MPI communicator
Definition: world.h:19
untyped internal class for triply-typed bivariate function
Definition: ctr_comm.h:16
double estimate_time()
predicts execution time in seconds using performance models
Definition: summation.cxx:132
CTF::World * wrld
distributed processor context on which tensor is defined
int rank
rank of local processor
Definition: world.h:24
class for execution distributed contraction of tensors
Definition: contraction.h:16
virtual Term * clone(std::map< tensor *, tensor * > *remap=NULL) const =0
base classes must implement this copy function to retrieve pointer
univar_function const * func
Definition: fun_term.h:15
Term * clone(std::map< tensor *, tensor * > *remap=NULL) const
base classes must implement this copy function to retrieve pointer
Definition: fun_term.cxx:83
CTF::Idx_Tensor estimate_time(double &cost, std::vector< char > out_inds) const
estimates the cost the expression to produce an intermediate with all expression indices remaining ...
Definition: fun_term.cxx:123
void get_inputs(std::set< CTF::Idx_Tensor *, tensor_name_less > *inputs_set) const
appends the tensors this depends on to the input set
Definition: fun_term.cxx:62
double estimate_time()
predicts execution time in seconds using performance models
void execute(CTF::Idx_Tensor output) const
evalues the expression, which just scales by default
Definition: fun_term.cxx:96
virtual double estimate_time(CTF::Idx_Tensor output) const =0
estimates the cost of a contraction/sum/.. term
virtual std::vector< char > get_uniq_inds() const =0
find list of unique indices that are involved in this term
void get_inputs(std::set< CTF::Idx_Tensor *, tensor_name_less > *inputs_set) const
appends the tensors this depends on to the input set
Definition: fun_term.cxx:151
CTF::Idx_Tensor estimate_time(double &cost, std::vector< char > out_inds) const
estimates the cost the expression to produce an intermediate with all expression indices remaining ...
Definition: fun_term.cxx:45
Term * clone(std::map< tensor *, tensor * > *remap=NULL) const
base classes must implement this copy function to retrieve pointer
Definition: fun_term.cxx:21
Bifun_Term(Term *A, Term *B, bivar_function const *func)
Definition: fun_term.cxx:70
virtual CTF::World * where_am_i() const =0
figures out what world this term lives on
char * scale
Definition: term.h:35
Unifun_Term(Term *A, univar_function const *func)
Definition: fun_term.cxx:11
class for execution distributed summation of tensors
Definition: summation.h:15
std::vector< char > get_uniq_inds() const
find list of unique indices that are involved in this term
Definition: fun_term.cxx:58
virtual void get_inputs(std::set< CTF::Idx_Tensor *, tensor_name_less > *inputs_set) const =0
appends the tensors this depends on to the input set
virtual char const * mulid() const
identity element for multiplication i.e. 1
Definition: algstrct.cxx:93
a tensor with an index map associated with it (necessary for overloaded operators) ...
Definition: idx_tensor.h:15
CTF_int::tensor * parent
Definition: idx_tensor.h:17