Cyclops Tensor Framework
parallel arithmetic on multidimensional arrays
tensor.h
Go to the documentation of this file.
1 #ifndef __TENSOR_H__
2 #define __TENSOR_H__
3 
4 #include "functions.h"
5 #include "set.h"
6 #include "../tensor/untyped_tensor.h"
7 #include "world.h"
8 #include "partition.h"
9 #include <vector>
10 
11 namespace CTF {
12 
13  template<typename dtype=double>
14  class Typ_Idx_Tensor;
15 
16  template <typename dtype>
17  class Sparse_Tensor;
18 
27  template<typename dtype=double>
28  class Pair {
29  public:
31  int64_t k;
32 
35 
41  Pair(int64_t k_, dtype d_){
42  this->k = k_;
43  d = d_;
44  }
45 
49  Pair(){
50  //k=0;
51  //d=0; //(not possible if type has no zero!)
52  }
53 
57  bool operator<(Pair<dtype> other) const {
58  return k<other.k;
59  }
60 
61  };
62 
63  template<typename dtype>
64  inline bool comp_pair(Pair<dtype> i,
65  Pair<dtype> j) {
66  return (i.k<j.k);
67  }
68 
69 
73  template <typename dtype=double>
74  class Tensor : public CTF_int::tensor {
75  public:
79  Tensor();
80 
91  Tensor(int order,
92  int const * len,
93  int const * sym,
94  World & wrld=get_universe(),
95  char const * name=NULL,
96  bool profile=0,
97  CTF_int::algstrct const & sr=Ring<dtype>());
98 
109  Tensor(int order,
110  int const * len,
111  int const * sym,
112  World & wrld,
113  CTF_int::algstrct const & sr,
114  char const * name=NULL,
115  bool profile=0);
116 
126  Tensor(int order,
127  int const * len,
128  World & wrld=get_universe(),
129  CTF_int::algstrct const & sr=Ring<dtype>(),
130  char const * name=NULL,
131  bool profile=0);
132 
144  Tensor(int order,
145  bool is_sparse,
146  int const * len,
147  int const * sym,
148  World & wrld=get_universe(),
149  CTF_int::algstrct const & sr=Ring<dtype>(),
150  char const * name=NULL,
151  bool profile=0);
152 
153 
164  Tensor(int order,
165  bool is_sparse,
166  int const * len,
167  World & wrld=get_universe(),
168  CTF_int::algstrct const & sr=Ring<dtype>(),
169  char const * name=NULL,
170  bool profile=0);
171 
176  Tensor(Tensor<dtype> const & A);
177 
182  Tensor(tensor const & A);
183 
184 
190  Tensor(bool copy,
191  tensor const & A);
192 
200  Tensor(tensor & A,
201  int const * new_sym);
202 
208  Tensor(tensor const & A,
209  World & wrld);
210 
211 
212 
226  Tensor(int order,
227  int const * len,
228  int const * sym,
229  World & wrld,
230  char const * idx,
231  Idx_Partition const & prl,
232  Idx_Partition const & blk=Idx_Partition(),
233  char const * name=NULL,
234  bool profile=0,
235  CTF_int::algstrct const & sr=Ring<dtype>());
236 
237 
252  Tensor(int order,
253  bool is_sparse,
254  int const * len,
255  int const * sym,
256  World & wrld,
257  char const * idx,
258  Idx_Partition const & prl,
259  Idx_Partition const & blk=Idx_Partition(),
260  char const * name=NULL,
261  bool profile=0,
262  CTF_int::algstrct const & sr=Ring<dtype>());
263 
264 
269  Typ_Idx_Tensor<dtype> operator[](char const * idx_map);
270  Typ_Idx_Tensor<dtype> i(char const * idx_map);
271 
277  void read(int64_t npair,
278  Pair<dtype> * pairs);
279 
289  void read(int64_t npair,
290  int64_t const * global_idx,
291  dtype * data);
292 
300  void read(int64_t npair,
301  dtype alpha,
302  dtype beta,
303  Pair<dtype> * pairs);
304 
305 
314  void read(int64_t npair,
315  dtype alpha,
316  dtype beta,
317  int64_t const * global_idx,
318  dtype * data);
319 
328  void get_local_data(int64_t * npair,
329  int64_t ** global_idx,
330  dtype ** data,
331  bool nonzeros_only=false,
332  bool unpack_sym=false) const;
333 
345  void read_local(int64_t * npair,
346  int64_t ** global_idx,
347  dtype ** data,
348  bool unpack_sym=false) const;
349 
357  void get_local_pairs(int64_t * npair,
358  Pair<dtype> ** pairs,
359  bool nonzeros_only=false,
360  bool unpack_sym=false) const;
361 
370  void read_local(int64_t * npair,
371  Pair<dtype> ** pairs,
372  bool unpack_sym=false) const;
373 
380  void get_all_data(int64_t * npair,
381  dtype ** data,
382  bool unpack=false);
383 
390  void read_all(int64_t * npair,
391  dtype ** data,
392  bool unpack=false);
393 
399  int64_t read_all(dtype * data, bool unpack=false);
400 
401 
411  void write(int64_t npair,
412  int64_t const * global_idx,
413  dtype const * data);
414 
420  void write(int64_t npair,
421  Pair<dtype> const * pairs);
422 
431  void write(int64_t npair,
432  dtype alpha,
433  dtype beta,
434  int64_t const * global_idx,
435  dtype const * data);
436 
444  void write(int64_t npair,
445  dtype alpha,
446  dtype beta,
447  Pair<dtype> const * pairs);
448 
449 
450 
461  void contract(dtype alpha,
462  CTF_int::tensor & A,
463  char const * idx_A,
464  CTF_int::tensor & B,
465  char const * idx_B,
466  dtype beta,
467  char const * idx_C);
468 
480  void contract(dtype alpha,
481  CTF_int::tensor & A,
482  char const * idx_A,
483  CTF_int::tensor & B,
484  char const * idx_B,
485  dtype beta,
486  char const * idx_C,
487  Bivar_Function<dtype> fseq);
488 
497  void sum(dtype alpha,
498  CTF_int::tensor & A,
499  char const * idx_A,
500  dtype beta,
501  char const * idx_B);
502 
512  void sum(dtype alpha,
513  CTF_int::tensor & A,
514  char const * idx_A,
515  dtype beta,
516  char const * idx_B,
518 
524  void scale(dtype alpha,
525  char const * idx_A);
526 
533  void scale(dtype alpha,
534  char const * idx_A,
535  Endomorphism<dtype> fseq);
536 
545  dtype * get_mapped_data(char const * idx,
546  Idx_Partition const & prl,
547  Idx_Partition const & blk=Idx_Partition(),
548  bool unpack=true);
549 
558  void write(char const * idx,
559  dtype const * data,
560  Idx_Partition const & prl,
561  Idx_Partition const & blk=Idx_Partition(),
562  bool unpack=true);
563 
564 
574  double estimate_time(CTF_int::tensor & A,
575  char const * idx_A,
576  CTF_int::tensor & B,
577  char const * idx_B,
578  char const * idx_C);
579 
587  double estimate_time(CTF_int::tensor & A,
588  char const * idx_A,
589  char const * idx_B);
590 
598  Tensor<dtype> slice(int const * offsets,
599  int const * ends) const;
600 
608  Tensor<dtype> slice(int64_t corner_off,
609  int64_t corner_end) const;
610 
620  Tensor<dtype> slice(int const * offsets,
621  int const * ends,
622  World * oworld) const;
623 
633  Tensor<dtype> slice(int64_t corner_off,
634  int64_t corner_end,
635  World * oworld) const;
636 
637 
649  void slice(int const * offsets,
650  int const * ends,
651  dtype beta,
652  CTF_int::tensor const & A,
653  int const * offsets_A,
654  int const * ends_A,
655  dtype alpha);
656 
668  void slice(int64_t corner_off,
669  int64_t corner_end,
670  dtype beta,
671  CTF_int::tensor const & A,
672  int64_t corner_off_A,
673  int64_t corner_end_A,
674  dtype alpha);
675 
690  void permute(dtype beta,
691  CTF_int::tensor & A,
692  int * const * perms_A,
693  dtype alpha);
694 
709  void permute(int * const * perms_B,
710  dtype beta,
711  CTF_int::tensor & A,
712  dtype alpha);
713 
719  void sparsify();
720 
728  void sparsify(dtype threshold,
729  bool take_abs=true);
730 
731 
736  void sparsify(std::function<bool(dtype)> filter);
737 
744  void read_sparse_from_file(const char * fpath, bool with_vals=true);
745 
752  void write_sparse_to_file(const char * fpath, bool with_vals=true);
753 
761  void add_to_subworld(Tensor<dtype> * tsr,
762  dtype alpha,
763  dtype beta);
770  void add_to_subworld(Tensor<dtype> * tsr);
771 
779  void add_from_subworld(Tensor<dtype> * tsr,
780  dtype alpha,
781  dtype beta);
782 
788  void add_from_subworld(Tensor<dtype> * tsr);
789 
790 
795  void align(CTF_int::tensor const & A);
796 
801  dtype reduce(OP op);
802 
806  dtype norm1(){ return reduce(OP_SUMABS); };
807 
811  dtype norm2(){ return sqrt(reduce(OP_SUMSQ)); };
812 
816  dtype norm_infty(){ return reduce(OP_MAXABS); };
817 
821  void norm1(double & nrm);
822 
826  void norm2(double & nrm);
827 
831  void norm_infty(double & nrm);
832 
838  dtype * get_raw_data(int64_t * size) const;
839 
845  const dtype * raw_data(int64_t * size) const;
854  void get_max_abs(int n,
855  dtype * data) const;
856 
864  void fill_random(dtype rmin, dtype rmax);
865 
874  void fill_sp_random(dtype rmin, dtype rmax, double frac_sp);
875 
879  void profile_on();
880 
884  void profile_off();
885 
890  void set_name(char const * name);
891 
895  Tensor<dtype>& operator=(dtype val);
896 
900  Tensor<dtype>& operator=(const Tensor<dtype> A);
901 
906  Sparse_Tensor<dtype> operator[](std::vector<int64_t> indices);
907 
914  void print(FILE * fp, dtype cutoff) const;
915  void print(FILE * fp = stdout) const;
916  void prnt() const;
917 
924  void compare(const Tensor<dtype>& A, FILE * fp = stdout, double cutoff = -1.0);
925 
929  ~Tensor();
930  };
934 }
935 
936 #include "graph_io_aux.cxx"
937 #include "tensor.cxx"
938 #include "vector.h"
939 #include "scalar.h"
940 #include "matrix.h"
941 #include "sparse_tensor.h"
942 
943 
944 #endif
945 
void permute(int order, int const *perm, int *arr)
permute an array
Definition: util.cxx:205
OP
reduction types for tensor data deprecated types: OP_NORM1=OP_SUMABS, OP_NORM2=call norm2()...
Definition: common.h:51
custom scalar function on tensor: e.g. A["ij"] = f(A["ij"])
Definition: functions.h:23
def sum(tensor, init_A, axis=None, dtype=None, out=None, keepdims=None)
Definition: core.pyx:4261
dtype d
tensor value associated with index
Definition: tensor.h:34
Ring class defined by a datatype and addition and multiplicaton functions addition must have an ident...
Definition: ring.h:18
Pair()
default constructor
Definition: tensor.h:49
custom bivariate function on two tensors: e.g. C["ij"] = f(A["ik"],B["kj"])
Definition: functions.h:137
an instance of the CTF library (world) on a MPI communicator
Definition: world.h:19
int64_t k
key, global index [i1,i2,...] specified as i1+len[0]*i2+...
Definition: tensor.h:31
dtype norm2()
computes the frobenius norm of the tensor (needs sqrt()!)
Definition: tensor.h:811
index-value pair used for tensor data input
Definition: tensor.h:28
custom function f : X -> Y to be applied to tensor elemetns: e.g. B["ij"] = f(A["ij"]) ...
Definition: functions.h:55
a sparse subset of a tensor
Definition: sparse_tensor.h:14
def scale(self, scl)
Definition: core.pyx:325
CTF::Tensor Tensor
Definition: back_comp.h:9
dtype norm_infty()
finds the max absolute value element of the tensor
Definition: tensor.h:816
Pair(int64_t k_, dtype d_)
constructor builds pair
Definition: tensor.h:41
World & get_universe()
Definition: world.cxx:309
def copy(tensor, A)
Definition: core.pyx:3583
algstrct (algebraic structure) defines the elementwise operations computed in each tensor contraction...
Definition: algstrct.h:34
Definition: apsp.cxx:17
internal distributed tensor class
dtype norm1()
computes the entrywise 1-norm of the tensor
Definition: tensor.h:806
an instance of a tensor within a CTF world
Definition: tensor.h:74
bool comp_pair(Pair< dtype > i, Pair< dtype > j)
Definition: tensor.h:64