Cyclops Tensor Framework
parallel arithmetic on multidimensional arrays
tensor.cxx
Go to the documentation of this file.
1 /*Copyright (c) 2011, Edgar Solomonik, all rights reserved.*/
2 
3 #include "../interface/common.h"
4 #include "world.h"
5 #include "idx_tensor.h"
6 #include "../tensor/untyped_tensor.h"
7 
8 
9 namespace CTF {
10 
11  template<typename dtype>
12  Tensor<dtype>::Tensor() : CTF_int::tensor() { }
13 
14 
15  template<typename dtype>
17  int const * len,
18  int const * sym,
19  World & world,
20  char const * name,
21  bool profile,
22  CTF_int::algstrct const & sr)
23  : CTF_int::tensor(&sr, order, len, sym, &world, 1, name, profile) {
24  IASSERT(sizeof(dtype)==this->sr->el_size);
25  }
26 
27  template<typename dtype>
29  int const * len,
30  int const * sym,
31  World & world,
32  CTF_int::algstrct const & sr,
33  char const * name,
34  bool profile)
35  : CTF_int::tensor(&sr, order, len, sym, &world, 1, name, profile) {
36  IASSERT(sizeof(dtype)==this->sr->el_size);
37  }
38 
39 
40  template<typename dtype>
42  bool is_sparse,
43  int const * len,
44  int const * sym,
45  World & world,
46  CTF_int::algstrct const & sr,
47  char const * name,
48  bool profile)
49  : CTF_int::tensor(&sr, order, len, sym, &world, 1, name, profile, is_sparse) {
50  IASSERT(sizeof(dtype)==this->sr->el_size);
51  }
52 
53  template<typename dtype>
55  bool is_sparse,
56  int const * len,
57  World & world,
58  CTF_int::algstrct const & sr,
59  char const * name,
60  bool profile)
61  : CTF_int::tensor(&sr, order, len, NULL, &world, 1, name, profile, is_sparse) {
62  IASSERT(sizeof(dtype)==this->sr->el_size);
63  }
64 
65 
66  template<typename dtype>
68  int const * len,
69  World & world,
70  CTF_int::algstrct const & sr,
71  char const * name,
72  bool profile)
73  : CTF_int::tensor(&sr, order, len, NULL, &world, 1, name, profile) {
74  IASSERT(sizeof(dtype)==this->sr->el_size);
75  }
76 
77 
78  template<typename dtype>
80  int const * len,
81  int const * sym,
82  World & world,
83  char const * idx,
84  Idx_Partition const & prl,
85  Idx_Partition const & blk,
86  char const * name,
87  bool profile,
88  CTF_int::algstrct const & sr_)
89  : CTF_int::tensor(&sr_, order, 0, len, sym, &world, idx, prl, blk, name, profile) {
90  IASSERT(sizeof(dtype)==this->sr->el_size);
91  }
92 
93  template<typename dtype>
95  bool is_sparse_,
96  int const * len,
97  int const * sym,
98  World & world,
99  char const * idx,
100  Idx_Partition const & prl,
101  Idx_Partition const & blk,
102  char const * name,
103  bool profile,
104  CTF_int::algstrct const & sr_)
105  : CTF_int::tensor(&sr_, order, is_sparse_, len, sym, &world, idx, prl, blk, name, profile) {
106  IASSERT(sizeof(dtype)==this->sr->el_size);
107  }
108 
109 
110  template<typename dtype>
112  tensor const & A)
113  : CTF_int::tensor(&A, copy) { }
114 
115  template<typename dtype>
117  : CTF_int::tensor(&A, true) { }
118 
119  template<typename dtype>
121  : CTF_int::tensor(&A, true) { }
122 
123  template<typename dtype>
125  World & world_)
126  : CTF_int::tensor(A.sr, A.order, A.lens, A.sym, &world_, 1, A.name, A.profile) { }
127 
128  template<typename dtype>
130  int const * new_sym)
131  : CTF_int::tensor(&A, new_sym){ }
132 
133  template<typename dtype>
134  Typ_Idx_Tensor<dtype> Tensor<dtype>::operator[](const char * idx_map_){
135  //IASSERT(strlen(idx_map_)==order);
136  Typ_Idx_Tensor<dtype> idxtsr(this, idx_map_);
137  return idxtsr;
138  }
139 
140  template<typename dtype>
141  Typ_Idx_Tensor<dtype> Tensor<dtype>::i(const char * idx_map_){
142  //IASSERT(strlen(idx_map_)==order);
143  Typ_Idx_Tensor<dtype> idxtsr(this, idx_map_);
144  return idxtsr;
145  }
146 
147 
148  template<typename dtype>
150 
151  template<typename dtype>
153  dtype * data;
154  tensor::get_raw_data((char**)&data, size);
155  return data;
156  }
157 
158  template<typename dtype>
159  void Tensor<dtype>::get_local_data(int64_t * npair,
160  int64_t ** global_idx,
161  dtype ** data,
162  bool nonzeros_only,
163  bool unpack_sym) const {
164  char * cpairs;
165  int ret, i;
166  if (nonzeros_only)
167  ret = CTF_int::tensor::read_local_nnz(npair,&cpairs,unpack_sym);
168  else
169  ret = CTF_int::tensor::read_local(npair,&cpairs,unpack_sym);
170  if (ret != CTF_int::SUCCESS){ printf("CTF ERROR: failed to execute function read_local\n"); IASSERT(0); return; }
171  *global_idx = (int64_t*)CTF_int::alloc((*npair)*sizeof(int64_t));
172  *data = (dtype*)sr->alloc((*npair));
173  CTF_int::PairIterator pairs(sr, cpairs);
174  for (i=0; i<(*npair); i++){
175  (*global_idx)[i] = pairs[i].k();
176  pairs[i].read_val((char*)((*data)+i));
177  }
178  if (cpairs != NULL) sr->pair_dealloc(cpairs);
179  }
180 
181  template<typename dtype>
182  void Tensor<dtype>::read_local(int64_t * npair,
183  int64_t ** global_idx,
184  dtype ** data,
185  bool unpack_sym) const {
186  char * cpairs;
187  int ret, i;
188  ret = CTF_int::tensor::read_local(npair,&cpairs,unpack_sym);
189  if (ret != CTF_int::SUCCESS){ printf("CTF ERROR: failed to execute function read_local\n"); IASSERT(0); return; }
190  *global_idx = (int64_t*)CTF_int::alloc((*npair)*sizeof(int64_t));
191  *data = (dtype*)CTF_int::alloc((*npair)*sizeof(dtype));
192  CTF_int::PairIterator pairs(sr, cpairs);
193  for (i=0; i<(*npair); i++){
194  (*global_idx)[i] = pairs[i].k();
195  pairs[i].read_val((char*)((*data)+i));
196  }
197  if (cpairs != NULL) sr->pair_dealloc(cpairs);
198  }
199 
200  template<typename dtype>
201  void Tensor<dtype>::get_local_pairs(int64_t * npair,
202  Pair<dtype> ** pairs,
203  bool nonzeros_only,
204  bool unpack_sym) const {
205  char * cpairs;
206  int ret;
207  if (nonzeros_only)
208  ret = CTF_int::tensor::read_local_nnz(npair,&cpairs,unpack_sym);
209  else
210  ret = CTF_int::tensor::read_local(npair,&cpairs,unpack_sym);
211  *pairs = (Pair<dtype>*)cpairs; //Pair<dtype>::cast_char_arr(cpairs, *npair, sr);
212  if (ret != CTF_int::SUCCESS){ printf("CTF ERROR: failed to execute function read_local\n"); IASSERT(0); return; }
213  }
214 
215  template<typename dtype>
216  void Tensor<dtype>::read_local(int64_t * npair,
217  Pair<dtype> ** pairs,
218  bool unpack_sym) const {
219  char * cpairs;
220  int ret = CTF_int::tensor::read_local(npair, &cpairs, unpack_sym);
221  *pairs = (Pair<dtype>*)cpairs; //Pair<dtype>::cast_char_arr(cpairs, *npair, sr);
222  if (ret != CTF_int::SUCCESS){ printf("CTF ERROR: failed to execute function read_local\n"); IASSERT(0); return; }
223  }
224 
225  template<typename dtype>
226  void Tensor<dtype>::read(int64_t npair,
227  int64_t const * global_idx,
228  dtype * data){
229  int ret;
230  int64_t i;
231  char * cpairs = sr->pair_alloc(npair);
232  Pair< dtype > * pairs =(Pair< dtype >*)cpairs;
233  for (i=0; i<npair; i++){
234  pairs[i].k = global_idx[i];
235  pairs[i].d = data[i];
236  }
237  ret = CTF_int::tensor::read(npair, cpairs);
238  if (ret != CTF_int::SUCCESS){ printf("CTF ERROR: failed to execute function read\n"); IASSERT(0); return; }
239  for (i=0; i<npair; i++){
240  data[i] = pairs[i].d;
241  }
242  sr->pair_dealloc(cpairs);
243  }
244 
245  template<typename dtype>
246  void Tensor<dtype>::read(int64_t npair,
247  Pair<dtype> * pairs){
248  //FIXME raises mem consumption
249  //char * cpairs = Pair<dtype>::scast_to_char_arr(pairs, npair);
250  char * cpairs = (char*)pairs; //Pair<dtype>::scast_to_char_arr(pairs, npair);
251  int ret = CTF_int::tensor::read(npair, cpairs);
252  IASSERT(cpairs == (char*)pairs);
253  /*if (cpairs != (char*)pairs){
254  for (int64_t i=0; i<npair; i++){
255  pairs[i].k = ipairs[i].k();
256  ipairs[i].read_val((char*)&(pairs[i].d));
257  }
258  sr->pair_dealloc(cpairs);
259  }*/
260  if (ret != CTF_int::SUCCESS){ printf("CTF ERROR: failed to execute function read\n"); IASSERT(0); return; }
261  }
262 
263  template<typename dtype>
264  void Tensor<dtype>::write(int64_t npair,
265  int64_t const * global_idx,
266  dtype const * data) {
267  int ret, i;
268  char * cpairs = sr->pair_alloc(npair);
269  Pair< dtype > * pairs =(Pair< dtype >*)cpairs;
270  for (i=0; i<npair; i++){
271  pairs[i].k = global_idx[i];
272  pairs[i].d = data[i];
273  }
274  /*char * cpairs = sr->pair_alloc(npair);
275  CTF_int::PairIterator pairs = CTF_int::PairIterator(sr, cpairs);
276  for (i=0; i<npair; i++){
277  pairs[i].write_key(global_idx[i]);
278  pairs[i].write_val((char*)&(data[i]));
279  }*/
280  ret = CTF_int::tensor::write(npair, sr->mulid(), sr->addid(), cpairs);
281  if (ret != CTF_int::SUCCESS){ printf("CTF ERROR: failed to execute function write\n"); IASSERT(0); return; }
282  sr->pair_dealloc(cpairs);
283  }
284 
285  template<typename dtype>
286  void Tensor<dtype>::write(int64_t npair,
287  Pair<dtype> const * pairs) {
288 
289  //FIXME raises mem consumption
290  char const * cpairs = (char const*)pairs; //Pair<dtype>::scast_to_char_arr(pairs, npair);
291  int ret = CTF_int::tensor::write(npair, sr->mulid(), sr->addid(), (char*)cpairs);
292  if (ret != CTF_int::SUCCESS){ printf("CTF ERROR: failed to execute function write\n"); IASSERT(0); return; }
293  /*if (cpairs != (char*)pairs)
294  sr->pair_dealloc(cpairs);*/
295  }
296 
297  template<typename dtype>
298  void Tensor<dtype>::write(int64_t npair,
299  dtype alpha,
300  dtype beta,
301  int64_t const * global_idx,
302  dtype const * data) {
303  int ret, i;
304  char * cpairs = sr->pair_alloc(npair);
305  Pair< dtype > * pairs =(Pair< dtype >*)cpairs;
306 
307  for (i=0; i<npair; i++){
308  pairs[i].k = global_idx[i];
309  pairs[i].d = data[i];
310  }
311 
312  /*Pair< dtype > * pairs;
313  pairs = (Pair< dtype >*)CTF_int::alloc(npair*sizeof(Pair< dtype >));
314  for (i=0; i<npair; i++){
315  pairs[i].k = global_idx[i];
316  pairs[i].d = data[i];
317  }*/
318  ret = CTF_int::tensor::write(npair, (char*)&alpha, (char*)&beta, cpairs);
319  if (ret != CTF_int::SUCCESS){ printf("CTF ERROR: failed to execute function write\n"); IASSERT(0); return; }
320  sr->pair_dealloc(cpairs);
321  }
322 
323  template<typename dtype>
324  void Tensor<dtype>::write(int64_t npair,
325  dtype alpha,
326  dtype beta,
327  Pair<dtype> const * pairs) {
328  char const * cpairs = (char const*)pairs; //Pair<dtype>::scast_to_char_arr(pairs, npair);
329 
330  int ret = CTF_int::tensor::write(npair, (char*)&alpha, (char*)&beta, (char*)cpairs);
331  //if (cpairs != (char*)pairs) sr->pair_dealloc(cpairs);
332  if (ret != CTF_int::SUCCESS){ printf("CTF ERROR: failed to execute function write\n"); IASSERT(0); return; }
333  }
334 
335  template<typename dtype>
336  void Tensor<dtype>::read(int64_t npair,
337  dtype alpha,
338  dtype beta,
339  int64_t const * global_idx,
340  dtype * data){
341  int ret, i;
342  char * cpairs = sr->pair_alloc(npair);
343  Pair< dtype > * pairs =(Pair< dtype >*)cpairs;
344  for (i=0; i<npair; i++){
345  pairs[i].k = global_idx[i];
346  pairs[i].d = data[i];
347  }
348  ret = CTF_int::tensor::read(npair, (char*)&alpha, (char*)&beta, cpairs);
349  if (ret != CTF_int::SUCCESS){ printf("CTF ERROR: failed to execute function read\n"); IASSERT(0); return; }
350  for (i=0; i<npair; i++){
351  data[i] = pairs[i].d;
352  }
353  sr->pair_dealloc(cpairs);
354  }
355 
356  template<typename dtype>
357  void Tensor<dtype>::read(int64_t npair,
358  dtype alpha,
359  dtype beta,
360  Pair<dtype> * pairs){
361  char * cpairs = (char*)pairs; //Pair<dtype>::scast_to_char_arr(pairs, npair);
362  int ret = CTF_int::tensor::read(npair, (char*)&alpha, (char*)&beta, cpairs);
363  IASSERT(cpairs == (char*)pairs);/*
364  {
365  CTF_int::PairIterator ipairs = CTF_int::PairIterator(sr, cpairs);
366  for (int64_t i=0; i<npair; i++){
367  pairs[i].k = ipairs[i].k();
368  ipairs[i].read_val((char*)&(pairs[i].d()));
369  }
370  sr->pair_dealloc(cpairs);
371  }*/
372  if (ret != CTF_int::SUCCESS){ printf("CTF ERROR: failed to execute function read\n"); IASSERT(0); return; }
373  }
374 
375 
376  template<typename dtype>
377  void Tensor<dtype>::read_all(int64_t * npair, dtype ** vals, bool unpack){
378  int ret;
379  ret = CTF_int::tensor::allread(npair, ((char**)vals), unpack);
380  if (ret != CTF_int::SUCCESS){ printf("CTF ERROR: failed to execute function read_all\n"); IASSERT(0); return; }
381  }
382 
383  template<typename dtype>
384  int64_t Tensor<dtype>::read_all(dtype * vals, bool unpack){
385  int ret;
386  int64_t npair;
387  ret = CTF_int::tensor::allread(&npair, (char*)vals, unpack);
388  if (ret != CTF_int::SUCCESS){ printf("CTF ERROR: failed to execute function read_all\n"); IASSERT(0); }
389  return npair;
390  }
391  template<typename dtype>
392  void Tensor<dtype>::set_name(char const * name_) {
394  }
395 
396  template<typename dtype>
399  }
400 
401  template<typename dtype>
404  }
405 
406  template<typename dtype>
407  void Tensor<dtype>::print(FILE* fp, dtype cutoff) const{
408  CTF_int::tensor::print(fp, (char *)&cutoff);
409  }
410 
411  template<typename dtype>
412  void Tensor<dtype>::print(FILE* fp) const{
413  CTF_int::tensor::print(fp, NULL);
414  }
415 
416  template<typename dtype>
417  void Tensor<dtype>::prnt() const{
418  CTF_int::tensor::print(stdout, NULL);
419  }
420 
421 
422 
423  template<typename dtype>
424  void Tensor<dtype>::compare(const Tensor<dtype>& A, FILE* fp, double cutoff){
425  CTF_int::tensor::compare(&A, fp, (char const *)&cutoff);
426  }
427 
428  template<typename dtype>
430  CTF_int::tensor & A,
431  int * const * perms_A,
432  dtype alpha){
433  int ret = CTF_int::tensor::permute(&A, perms_A, (char*)&alpha,
434  NULL, (char*)&beta);
435  if (ret != CTF_int::SUCCESS){ printf("CTF ERROR: failed to execute function\n"); IASSERT(0); return; }
436  }
437 
438  template<typename dtype>
439  void Tensor<dtype>::permute(int * const * perms_B,
440  dtype beta,
441  CTF_int::tensor & A,
442  dtype alpha){
443  int ret = CTF_int::tensor::permute(&A, NULL, (char*)&alpha,
444  perms_B, (char*)&beta);
445  if (ret != CTF_int::SUCCESS){ printf("CTF ERROR: failed to execute function permute\n"); IASSERT(0); return; }
446  }
447 
448  template<typename dtype>
450  int ret = CTF_int::tensor::sparsify();
451  if (ret != CTF_int::SUCCESS){ printf("CTF ERROR: failed to execute function sparsify\n"); IASSERT(0); return; }
452  }
453 
454  template<typename dtype>
455  void Tensor<dtype>::sparsify(dtype threshold, bool take_abs){
456  int ret = CTF_int::tensor::sparsify((char*)&threshold, take_abs);
457  if (ret != CTF_int::SUCCESS){ printf("CTF ERROR: failed to execute function sparsify\n"); IASSERT(0); return; }
458  }
459 
460  template<typename dtype>
461  void Tensor<dtype>::sparsify(std::function<bool(dtype)> filter){
462  int ret = CTF_int::tensor::sparsify([&](char const * c){ return filter(((dtype*)c)[0]); });
463  if (ret != CTF_int::SUCCESS){ printf("CTF ERROR: failed to execute function sparisfy\n"); IASSERT(0); return; }
464  }
465 
466  template <typename dtype>
467  void read_sparse_from_file_base(const char * fpath, bool with_vals, Tensor<dtype> * T){
468  char ** datastr;
469  int64_t my_nvals = CTF_int::read_data_mpiio<dtype>(T->wrld, fpath, &datastr);
470 
471  Pair<dtype> * pairs = (Pair<dtype>*)T->sr->pair_alloc(my_nvals);
472 
473  CTF_int::parse_sparse_tensor_data<dtype>(datastr, T->order, (dtype*)T->sr->mulid(), T->lens, my_nvals, pairs, with_vals);
474 
475  //strtok contains pointers to char array generated from file
476  if (datastr[0] != NULL) CTF_int::cdealloc(datastr[0]);
477  CTF_int::cdealloc(datastr);
478 
479  T->write(my_nvals,pairs);
480 
481  T->sr->pair_dealloc((char*)pairs);
482  }
483 
484  template<>
485  inline void Tensor<int>::read_sparse_from_file(const char * fpath, bool with_vals){
486  read_sparse_from_file_base<int>(fpath, with_vals, this);
487  }
488 
489  template<>
490  inline void Tensor<double>::read_sparse_from_file(const char * fpath, bool with_vals){
491  read_sparse_from_file_base<double>(fpath, with_vals, this);
492  }
493 
494  template<>
495  inline void Tensor<float>::read_sparse_from_file(const char * fpath, bool with_vals){
496  read_sparse_from_file_base<float>(fpath, with_vals, this);
497  }
498 
499  template<>
500  inline void Tensor<int64_t>::read_sparse_from_file(const char * fpath, bool with_vals){
501  read_sparse_from_file_base<int64_t>(fpath, with_vals, this);
502  }
503 
504 
505  template <typename dtype>
506  void write_sparse_to_file_base(const char * fpath, bool with_vals, Tensor<dtype> * T){
507  int64_t my_nvals;
508 
509  Pair<dtype> * pairs;
510  T->get_local_pairs(&my_nvals, &pairs, true);
511  int64_t str_len;
512  char * datastr = CTF_int::serialize_sparse_tensor_data<dtype>(T->order, T->lens, my_nvals, pairs, with_vals, str_len);
513  CTF_int::write_data_mpiio<dtype>(T->wrld, fpath, datastr, str_len);
514  CTF_int::cdealloc(datastr);
515  T->sr->pair_dealloc((char*)pairs);
516  }
517 
518  template<>
519  inline void Tensor<int>::write_sparse_to_file(const char * fpath, bool with_vals){
520  write_sparse_to_file_base<int>(fpath, with_vals, this);
521  }
522 
523  template<>
524  inline void Tensor<double>::write_sparse_to_file(const char * fpath, bool with_vals){
525  write_sparse_to_file_base<double>(fpath, with_vals, this);
526  }
527 
528  template<>
529  inline void Tensor<float>::write_sparse_to_file(const char * fpath, bool with_vals){
530  write_sparse_to_file_base<float>(fpath, with_vals, this);
531  }
532 
533  template<>
534  inline void Tensor<int64_t>::write_sparse_to_file(const char * fpath, bool with_vals){
535  write_sparse_to_file_base<int64_t>(fpath, with_vals, this);
536  }
537 
538 
539  template<typename dtype>
541  Tensor<dtype> * tsr,
542  dtype alpha,
543  dtype beta){
544  if (tsr == NULL){
545  tensor t = tensor();
546  t.sr = sr->clone();
547  CTF_int::tensor::add_to_subworld(&t, (char*)&alpha, (char*)&beta);
548  delete t.sr;
549  } else
550  CTF_int::tensor::add_to_subworld(tsr, (char*)&alpha, (char*)&beta);
551  }
552 
553  template<typename dtype>
555  Tensor<dtype> * tsr){
556  return add_to_subworld(tsr, sr->mulid(), sr->mulid());
557  }
558 
559  template<typename dtype>
561  Tensor<dtype> * tsr,
562  dtype alpha,
563  dtype beta){
564  if (tsr == NULL){
565  tensor t = tensor();
566  t.sr = sr->clone();
567  CTF_int::tensor::add_from_subworld(&t, (char*)&alpha, (char*)&beta);
568  delete t.sr;
569  } else
570  CTF_int::tensor::add_from_subworld(tsr, (char*)&alpha, (char*)&beta);
571  }
572 
573  template<typename dtype>
575  Tensor<dtype> * tsr){
576  if (tsr == NULL){
577  tensor t = tensor();
578  t.sr = sr->clone();
580  delete t.sr;
581  } else
583  }
584 
585  template<typename dtype>
586  void Tensor<dtype>::slice(int const * offsets,
587  int const * ends,
588  dtype beta,
589  CTF_int::tensor const & A,
590  int const * offsets_A,
591  int const * ends_A,
592  dtype alpha){
593  int np_A, np_B;
594  if (A.wrld->comm != wrld->comm){
595  MPI_Comm_size(A.wrld->comm, &np_A);
596  MPI_Comm_size(wrld->comm, &np_B);
597  if (np_A == np_B){
598  printf("CTF ERROR: number of processors should not match in slice if worlds are different\n");
599  IASSERT(0);
600  return;
601  }
602  //FIXME: was reversed?
604  offsets, ends, (char*)&beta, (Tensor *)&A,
605  offsets_A, ends_A, (char*)&alpha);
606  } else {
608  offsets, ends, (char*)&beta, (Tensor *)&A,
609  offsets_A, ends_A, (char*)&alpha);
610  }
611  }
612 
613  template<typename dtype>
614  void Tensor<dtype>::slice(int64_t corner_off,
615  int64_t corner_end,
616  dtype beta,
617  CTF_int::tensor const & A,
618  int64_t corner_off_A,
619  int64_t corner_end_A,
620  dtype alpha){
621  int * offsets, * ends, * offsets_A, * ends_A;
622 
623  CTF_int::cvrt_idx(this->order, this->lens, corner_off, &offsets);
624  CTF_int::cvrt_idx(this->order, this->lens, corner_end, &ends);
625  for (int i=0; i<order; i++){
626  ends[i]++;
627  }
628  CTF_int::cvrt_idx(A.order, A.lens, corner_off_A, &offsets_A);
629  CTF_int::cvrt_idx(A.order, A.lens, corner_end_A, &ends_A);
630  for (int i=0; i<A.order; i++){
631  ends_A[i]++;
632  }
633 
634  CTF_int::tensor::slice(offsets, ends, (char*)&beta, (Tensor *)&A, offsets_A, ends_A, (char*)&alpha);
635 
636  CTF_int::cdealloc(offsets);
637  CTF_int::cdealloc(ends);
638  CTF_int::cdealloc(offsets_A);
639  CTF_int::cdealloc(ends_A);
640  }
641 
642  template<typename dtype>
643  Tensor<dtype> Tensor<dtype>::slice(int const * offsets,
644  int const * ends) const {
645 
646  return slice(offsets, ends, wrld);
647  }
648 
649  template<typename dtype>
651  int64_t corner_end) const {
652 
653  return slice(corner_off, corner_end, wrld);
654  }
655 
656  template<typename dtype>
657  Tensor<dtype> Tensor<dtype>::slice(int const * offsets,
658  int const * ends,
659  World * owrld) const {
660  int i;
661  int * new_lens = (int*)CTF_int::alloc(sizeof(int)*order);
662  int * new_sym = (int*)CTF_int::alloc(sizeof(int)*order);
663  for (i=0; i<order; i++){
664  if (!(ends[i] - offsets[i] > 0 &&
665  offsets[i] >= 0 &&
666  ends[i] <= lens[i])){
667  printf("CTF ERROR: invalid slice dimensions\n");
668  IASSERT(0);
669  return Tensor<dtype>();
670  }
671  if (sym[i] != NS){
672  if (offsets[i] == offsets[i+1] && ends[i] == ends[i+1]){
673  new_sym[i] = sym[i];
674  } else {
675  if (!(ends[i+1] >= offsets[i])){
676  printf("CTF ERROR: slice dimensions don't respect tensor symmetry\n");
677  IASSERT(0);
678  return Tensor<dtype>();
679  }
680  new_sym[i] = NS;
681  }
682  } else new_sym[i] = NS;
683  new_lens[i] = ends[i] - offsets[i];
684  }
685  //FIXME: could discard sr qualifiers
686  Tensor<dtype> new_tsr(order, new_lens, new_sym, *owrld, *sr);
687 // Tensor<dtype> new_tsr = tensor(sr, order, new_lens, new_sym, owrld, 1);
688  std::fill(new_sym, new_sym+order, 0);
689  new_tsr.slice(new_sym, new_lens, *(dtype*)sr->addid(), *this, offsets, ends, *(dtype*)sr->mulid());
690 /* new_tsr.slice(
691  new_sym, new_lens, sr->addid(), this,
692  offsets, ends, sr->mulid());*/
693  CTF_int::cdealloc(new_lens);
694  CTF_int::cdealloc(new_sym);
695  return new_tsr;
696  }
697 
698  template<typename dtype>
700  int64_t corner_end,
701  World * owrld) const {
702 
703  int * offsets, * ends;
704 
705  CTF_int::cvrt_idx(this->order, this->lens, corner_off, &offsets);
706  CTF_int::cvrt_idx(this->order, this->lens, corner_end, &ends);
707  for (int i=0; i<order; i++){
708  ends[i]++;
709  }
710 
711  Tensor<dtype> tsr = slice(offsets, ends, owrld);
712 
713  CTF_int::cdealloc(offsets);
714  CTF_int::cdealloc(ends);
715 
716  return tsr;
717  }
718 
719  template<typename dtype>
721  if (A.wrld->cdt.cm != wrld->cdt.cm) {
722  printf("CTF ERROR: cannot align tensors on different CTF instances\n");
723  IASSERT(0);
724  return;
725  }
726  int ret = CTF_int::tensor::align(&A);
727  if (ret != CTF_int::SUCCESS){ printf("CTF ERROR: failed to execute function align\n"); IASSERT(0); return; }
728  }
729 
730  template<typename dtype>
732  int ret;
733  dtype ans;
734  switch (op) {
735  case OP_SUM:
736  if (sr->is_ordered()){
738  ret = reduce_sum((char*)&ans, &r);
739  } else {
741  ret = reduce_sum((char*)&ans, &r);
742  }
743 // ret = reduce_sum((char*)&ans);
744  break;
745  case OP_SUMABS:
746  if (sr->is_ordered()){
748  ret = reduce_sumabs((char*)&ans, &r);
749  } else {
751  ret = reduce_sumabs((char*)&ans, &r);
752  }
753  break;
754  case OP_SUMSQ:
755 /* if (sr->is_ordered()){
756  Ring<dtype,1> r = Ring<dtype,1>();
757  ret = reduce_sumsq((char*)&ans, &r);
758  } else {
759  Ring<dtype,0> r = Ring<dtype,0>();
760  ret = reduce_sumsq((char*)&ans, &r);
761  }*/
762  ret = reduce_sumsq((char*)&ans);
763  break;
764  case OP_MAX:
765  {
766  dtype minval;
767  sr->min((char*)&minval);
768  Monoid<dtype, 1> mmax = Monoid<dtype, 1>(minval, CTF_int::default_max<dtype, 1>, MPI_MAX);
769  ret = reduce_sum((char*)&ans, &mmax);
770  }
771  break;
772  case OP_MIN:
773  {
774  dtype maxval;
775  sr->max((char*)&maxval);
776  Monoid<dtype, 1> mmin = Monoid<dtype, 1>(maxval, CTF_int::default_min<dtype, 1>, MPI_MIN);
777  ret = reduce_sum((char*)&ans, &mmin);
778  }
779  break;
780  case OP_MAXABS:
781  {
782  dtype minval;
783  sr->min((char*)&minval);
784  Monoid<dtype, 1> mmax = Monoid<dtype, 1>(minval, CTF_int::default_max<dtype, 1>, MPI_MAX);
785  ret = reduce_sumabs((char*)&ans, &mmax);
786  }
787  break;
788  case OP_MINABS:
789  {
790  dtype maxval;
791  sr->max((char*)&maxval);
792  Monoid<dtype, 1> mmin = Monoid<dtype, 1>(maxval, CTF_int::default_min<dtype, 1>, MPI_MIN);
793  ret = reduce_sumabs((char*)&ans, &mmin);
794  }
795  break;
796  }
797  if (ret != CTF_int::SUCCESS){ printf("CTF ERROR: failed to execute function reduce\n"); IASSERT(0); }
798  return ans;
799  }
800 
801 
802  template<typename dtype>
803  void real_norm1(Tensor<dtype> & A, double & nrm){
804  char inds[A.order];
805  for (int i=0; i<A.order; i++){
806  inds[i] = 'a'+i;
807  }
808  nrm = Function<dtype,double>([](dtype a){ return (double)std::abs(a); })(A[inds]);
809  }
810 
811  template<>
812  inline void real_norm1<bool>(Tensor<bool> & A, double & nrm){
813  char inds[A.order];
814  for (int i=0; i<A.order; i++){
815  inds[i] = 'a'+i;
816  }
817  nrm = A[inds];
818  }
819 
820 
821 
822  template<typename dtype>
823  void Tensor<dtype>::norm1(double & nrm){
824  if (wrld->rank == 0)
825  printf("CTF ERROR: norm not available for the type of tensor %s\n",name);
826  IASSERT(0);
827  }
828 
829 #define NORM1_INST(dtype) \
830  template<> \
831  inline void Tensor<dtype>::norm1(double & nrm){ \
832  real_norm1<dtype>(*this, nrm); \
833  }
834 
835 NORM1_INST(bool)
836 NORM1_INST(int8_t)
837 NORM1_INST(int16_t)
838 NORM1_INST(int)
839 NORM1_INST(int64_t)
840 NORM1_INST(float)
841 NORM1_INST(double)
842 
843  template<typename dtype>
844  static void real_norm2(Tensor<dtype> & A, double & nrm){
845  char inds[A.order];
846  for (int i=0; i<A.order; i++){
847  inds[i] = 'a'+i;
848  }
849  //CTF::Scalar<double> dnrm(A.dw);
850  nrm = std::sqrt((double)Function<dtype,double>([](dtype a){ return (double)(a*a); })(A[inds]));
851  }
852 
853  template<typename dtype>
854  static void complex_norm2(Tensor<dtype> & A, double & nrm){
855  char inds[A.order];
856  for (int i=0; i<A.order; i++){
857  inds[i] = 'a'+i;
858  }
859  nrm = std::sqrt((double)Function<dtype,double>([](dtype a){ return (double)std::norm(a); })(A[inds]));
860  }
861 
862 
863  template<typename dtype>
864  void Tensor<dtype>::norm2(double & nrm){
865  if (wrld->rank == 0)
866  printf("CTF ERROR: norm not available for the type of tensor %s\n",name);
867  IASSERT(0);
868  }
869 
870 #define NORM2_REAL_INST(dtype) \
871  template<> \
872  inline void Tensor<dtype>::norm2(double & nrm){ \
873  real_norm2<dtype>(*this, nrm); \
874  }
875 
876 #define NORM2_COMPLEX_INST(dtype) \
877  template<> \
878  inline void Tensor< std::complex<dtype> >::norm2(double & nrm){ \
879  complex_norm2< std::complex<dtype> >(*this, nrm); \
880  }
881 
882 
883 NORM2_REAL_INST(bool)
884 NORM2_REAL_INST(int8_t)
885 NORM2_REAL_INST(int16_t)
886 NORM2_REAL_INST(int)
887 NORM2_REAL_INST(int64_t)
888 NORM2_REAL_INST(float)
889 NORM2_REAL_INST(double)
890 NORM2_COMPLEX_INST(float)
891 NORM2_COMPLEX_INST(double)
892 
893  template<typename dtype>
894  void Tensor<dtype>::norm_infty(double & nrm){
895  if (wrld->rank == 0)
896  printf("CTF ERROR: norm not available for the type of tensor %s\n",name);
897  IASSERT(0);
898  }
899 
900 #define NORM_INFTY_INST(dtype) \
901  template<> \
902  inline void Tensor<dtype>::norm_infty(double & nrm){ \
903  nrm = this->norm_infty(); \
904  }
905 
906 NORM_INFTY_INST(bool)
907 NORM_INFTY_INST(int8_t)
908 NORM_INFTY_INST(int16_t)
909 NORM_INFTY_INST(int)
910 NORM_INFTY_INST(int64_t)
911 NORM_INFTY_INST(float)
912 NORM_INFTY_INST(double)
913 
914 #undef NORM1_INST
915 #undef NORM2_REAL_INST
916 #undef NORM2_COMPLEX_INST
917 #undef NORM_INFTY_INST
918 
919  template<typename dtype>
921  dtype * data) const {
922  int ret;
923  ret = CTF_int::tensor::get_max_abs(n, data);
924  if (ret != CTF_int::SUCCESS){ printf("CTF ERROR: failed to execute function get_max_abs\n"); IASSERT(0); return; }
925  }
926 
927  template<typename dtype>
929  if (wrld->rank == 0)
930  printf("CTF ERROR: fill_random(rmin, rmax) not available for the type of tensor %s\n",name);
931  IASSERT(0);
932  }
933 
934  template <typename dtype>
935  void fill_random_base(dtype rmin, dtype rmax, Tensor<dtype> & T){
936  if (T.is_sparse){
937  printf("CTF ERROR: fill_random should not be called on a sparse tensor, use fill_random_sp instead\n");
938  IASSERT(0);
939  return;
940  }
941  for (int64_t i=0; i<T.size; i++){
942  ((dtype*)T.data)[i] = CTF_int::get_rand48()*(rmax-rmin)+rmin;
943  }
944  T.zero_out_padding();
945  }
946 
947  template<>
948  inline void Tensor<double>::fill_random(double rmin, double rmax){
949  fill_random_base<double>(rmin, rmax, *this);
950  }
951 
952  template<>
953  inline void Tensor<float>::fill_random(float rmin, float rmax){
954  fill_random_base<float>(rmin, rmax, *this);
955  }
956 
957  template<>
958  inline void Tensor<int64_t>::fill_random(int64_t rmin, int64_t rmax){
959  fill_random_base<int64_t>(rmin, rmax, *this);
960  }
961 
962  template<>
963  inline void Tensor<int>::fill_random(int rmin, int rmax){
964  fill_random_base<int>(rmin, rmax, *this);
965  }
966 
967 
968  template<typename dtype>
969  void Tensor<dtype>::fill_sp_random(dtype rmin, dtype rmax, double frac_sp){
970  if (wrld->rank == 0)
971  printf("CTF ERROR: fill_sp_random(rmin, rmax, frac_sp) not available for the type of tensor %s\n",name);
972  IASSERT(0);
973  }
974 
975  template <typename dtype>
976  void fill_sp_random_base(dtype rmin, dtype rmax, double frac_sp, Tensor<dtype> * T){
977  int64_t tot_size = 1; //CTF_int::packed_size(T.order, T.lens, T.sym);
978  for (int i=0; i<T->order; i++) tot_size *= T->lens[i];
979  double sf = tot_size*frac_sp;
980  double dg = 0.0;
981  //generate approximately tot_size*e^frac_sp rather than tot_size*frac_sp elements, to account for conflicts in writing them
982  for (int i=2; i<20; i++){
983  dg += sf;
984  sf *= frac_sp/i;
985  }
986  int64_t gen_size = (int64_t)(dg+.5);
987  int64_t my_gen_size = gen_size/T->wrld->np;
988  if (gen_size % T->wrld->np > T->wrld->rank){
989  my_gen_size++;
990  }
991  Pair<dtype> * pairs = (Pair<dtype>*)T->sr->pair_alloc(my_gen_size);
992  for (int64_t i=0; i<my_gen_size; i++){
993  pairs[i] = Pair<dtype>((int64_t)(CTF_int::get_rand48()*tot_size), 1.0);
994  }
995  T->write(my_gen_size,pairs);
996  T->sr->pair_dealloc((char*)pairs);
997  char str[T->order];
998  for (int i=0; i<T->order; i++){
999  str[i] = 'a'+i;
1000  }
1001 
1002  Transform<dtype>([=](dtype & d){ d=CTF_int::get_rand48()*(rmax-rmin)+rmin; })(T->operator[](str));
1003 
1004  /*std::vector<Pair<dtype>> pairs;
1005  pairs.reserve(size*frac_sp);
1006  int64_t npairs=0;
1007  for (int64_t i=wrld->rank; i<tot_sz; i+=wrld->np){
1008  if (CTF_int::get_rand48() < frac_sp){
1009  pairs.push_back(Pair<dtype>(i,CTF_int::get_rand48()*(rmax-rmin)+rmin));
1010  npairs++;
1011  }
1012  }
1013  this->write(npairs, pairs.data());*/
1014 
1015  }
1016 
1017  template<>
1018  inline void Tensor<double>::fill_sp_random(double rmin, double rmax, double frac_sp){
1019  fill_sp_random_base<double>(rmin, rmax, frac_sp, this);
1020  }
1021 
1022  template<>
1023  inline void Tensor<float>::fill_sp_random(float rmin, float rmax, double frac_sp){
1024  fill_sp_random_base<float>(rmin, rmax, frac_sp, this);
1025  }
1026 
1027  template<>
1028  inline void Tensor<int>::fill_sp_random(int rmin, int rmax, double frac_sp){
1029  fill_sp_random_base<int>(rmin, rmax, frac_sp, this);
1030  }
1031 
1032  template<>
1033  inline void Tensor<int64_t>::fill_sp_random(int64_t rmin, int64_t rmax, double frac_sp){
1034  fill_sp_random_base<int64_t>(rmin, rmax, frac_sp, this);
1035  }
1036 
1037  template<typename dtype>
1038  void Tensor<dtype>::contract(dtype alpha,
1039  CTF_int::tensor& A,
1040  const char * idx_A,
1041  CTF_int::tensor& B,
1042  const char * idx_B,
1043  dtype beta,
1044  const char * idx_C){
1045  if (A.wrld->cdt.cm != wrld->cdt.cm || B.wrld->cdt.cm != wrld->cdt.cm){
1046  printf("CTF ERROR: worlds of contracted tensors must match\n");
1047  IASSERT(0);
1048  return;
1049  }
1051  = CTF_int::contraction(&A, idx_A, &B, idx_B, (char*)&alpha, this, idx_C, (char*)&beta);
1052  ctr.execute();
1053  }
1054 
1055  template<typename dtype>
1056  void Tensor<dtype>::contract(dtype alpha,
1057  CTF_int::tensor& A,
1058  const char * idx_A,
1059  CTF_int::tensor& B,
1060  const char * idx_B,
1061  dtype beta,
1062  const char * idx_C,
1063  Bivar_Function<dtype> fseq){
1064  if (A.wrld->cdt.cm != wrld->cdt.cm || B.wrld->cdt.cm != wrld->cdt.cm){
1065  printf("CTF ERROR: worlds of contracted tensors must match\n");
1066  IASSERT(0);
1067  return;
1068  }
1070  = CTF_int::contraction(&A, idx_A, &B, idx_B, (char const *)&alpha, this, idx_C, (char const *)&beta, &fseq);
1071  ctr.execute();
1072  }
1073 
1074 
1075  template<typename dtype>
1076  void Tensor<dtype>::sum(dtype alpha,
1077  CTF_int::tensor& A,
1078  const char * idx_A,
1079  dtype beta,
1080  const char * idx_B){
1081  if (A.wrld->cdt.cm != wrld->cdt.cm){
1082  printf("CTF ERROR: worlds of summed tensors must match\n");
1083  IASSERT(0);
1084  return;
1085  }
1086 
1088  = CTF_int::summation(&A, idx_A, (char*)&alpha, this, idx_B, (char*)&beta);
1089 
1090  sum.execute();
1091 
1092  }
1093 
1094  template<typename dtype>
1095  void Tensor<dtype>::sum(dtype alpha,
1096  CTF_int::tensor& A,
1097  const char * idx_A,
1098  dtype beta,
1099  const char * idx_B,
1100  Univar_Function<dtype> fseq){
1101  if (A.wrld->cdt.cm != wrld->cdt.cm){
1102  printf("CTF ERROR: worlds of summed tensors must match\n");
1103  IASSERT(0);
1104  return;
1105  }
1106 
1107  CTF_int::summation sum = CTF_int::summation(&A, idx_A, (char const *)&alpha, this, idx_B, (char const *)&beta, &fseq);
1108 
1109  sum.execute();
1110  }
1111 
1112  template<typename dtype>
1113  void Tensor<dtype>::scale(dtype alpha,
1114  const char * idx_A){
1115  CTF_int::scaling scl = CTF_int::scaling(this, idx_A, (char*)&alpha);
1116  scl.execute();
1117  }
1118 
1119 
1120  template<typename dtype>
1121  void Tensor<dtype>::scale(dtype alpha,
1122  const char * idx_A,
1123  Endomorphism<dtype> fseq){
1124  CTF_int::scaling scl = CTF_int::scaling(this, idx_A, &fseq, (char const *)&alpha);
1125  scl.execute();
1126  }
1127 
1128  template<typename dtype>
1130  Idx_Partition const & prl,
1131  Idx_Partition const & blk,
1132  bool unpack){
1133  return (dtype*)CTF_int::tensor::read(idx, prl, blk, unpack);
1134  }
1135 
1136 
1137 
1138  template<typename dtype>
1140  set((char const*)&val);
1141 /* int64_t size;
1142  dtype* raw = get_raw_data(&size);
1143  //FIXME: Uuuuh, padding?
1144  IASSERT(0);
1145  std::fill(raw, raw+size, val);*/
1146  return *this;
1147  }
1148 
1149  template<typename dtype>
1151  CTF_int::tensor& A,
1152  const char * idx_A,
1153  CTF_int::tensor& B,
1154  const char * idx_B,
1155  const char * idx_C){
1157  = CTF_int::contraction(&A, idx_A, &B, idx_B, sr->mulid(), this, idx_C, sr->addid());
1158  return ctr.estimate_time();
1159  }
1160 
1161  template<typename dtype>
1163  CTF_int::tensor& A,
1164  const char * idx_A,
1165  const char * idx_B){
1166  CTF_int::summation sum = CTF_int::summation(&A, idx_A, sr->mulid(), this, idx_B, sr->addid());
1167 
1168  return sum.estimate_time();
1169 
1170  }
1171 
1172  template<typename dtype>
1174 
1175  free_self();
1176  init(A.sr, A.order, A.lens, A.sym, A.wrld, 0, A.name, A.profile, A.is_sparse);
1177  copy_tensor_data(&A);
1178  return *this;
1179 /*
1180  sr = A.sr;
1181  world = A.wrld;
1182  name = A.name;
1183  if (sym != NULL)
1184  CTF_int::cdealloc(sym);
1185  if (len != NULL)
1186  CTF_int::cdealloc(len);
1187  //CTF_int::cdealloc(len);
1188  ret = CTF_int::tensor::info(&A, &order, &len, &sym);
1189  if (ret != CTF_int::SUCCESS){ printf("CTF ERROR: failed to execute function\n"); IASSERT(0); return; }
1190  ret = CTF_int::tensor::define(sr, order, len, sym, &tid, 1, name, name != NULL);
1191  if (ret != CTF_int::SUCCESS){ printf("CTF ERROR: failed to execute function\n"); IASSERT(0); return; }
1192  //printf("Set tensor %d to be the same as %d\n", tid, A.tid);
1193  ret = CTF_int::tensor::copy(A.tid, tid);
1194  if (ret != CTF_int::SUCCESS){ printf("CTF ERROR: failed to execute function\n"); IASSERT(0); return; }*/
1195  }
1196 
1197 
1198  template<typename dtype>
1199  Sparse_Tensor<dtype> Tensor<dtype>::operator[](std::vector<int64_t> indices){
1200  Sparse_Tensor<dtype> stsr(indices,this);
1201  return stsr;
1202  }
1203 
1204 }
1205 
1206 
1207 
void read(int64_t num_pair, char const *alpha, char const *beta, int64_t const *inds, char *data)
read tensor data with <key, value> pairs where key is the global index for the value, which gets filled in with beta times the old values plus alpha times the values read from the tensor.
int reduce_sumabs(char *result)
Performs an elementwise absolute value summation reduction on a tensor.
void prnt() const
Definition: tensor.cxx:417
void profile_off()
turns off profiling for tensor
Definition: tensor.cxx:402
void contract(dtype alpha, CTF_int::tensor &A, char const *idx_A, CTF_int::tensor &B, char const *idx_B, dtype beta, char const *idx_C)
contracts C[idx_C] = beta*C[idx_C] + alpha*A[idx_A]*B[idx_B]
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
CTF_int::CommData cdt
communicator data for MPI comm defining this world
Definition: world.h:32
int * sym
symmetries among tensor dimensions
void execute()
run contraction
Definition: contraction.cxx:99
virtual algstrct * clone() const =0
&#39;&#39;copy constructor&#39;&#39;
void slice(int const *offsets_B, int const *ends_B, char const *beta, tensor *A, int const *offsets_A, int const *ends_A, char const *alpha)
accumulates out a slice (block) of this tensor = B B[offsets,ends)=beta*B[offsets,ends) + alpha*A[offsets_A,ends_A)
int reduce_sum(char *result)
Performs an elementwise summation reduction on a tensor.
void profile_on()
turns on profiling for tensor
Definition: tensor.cxx:397
dtype d
tensor value associated with index
Definition: tensor.h:34
#define NORM1_INST(dtype)
Definition: tensor.cxx:829
Ring class defined by a datatype and addition and multiplicaton functions addition must have an ident...
Definition: ring.h:18
virtual char * pair_alloc(int64_t n) const
allocate space for n (int64_t,dtype) pairs, necessary for object types
Definition: algstrct.cxx:681
void add_to_subworld(Tensor< dtype > *tsr, dtype alpha, dtype beta)
accumulates this tensor to a tensor object defined on a different world
Definition: tensor.cxx:540
Typ_Idx_Tensor< dtype > i(char const *idx_map)
Definition: tensor.cxx:141
void execute(bool run_diag=false)
run summation
Definition: summation.cxx:119
void get_local_pairs(int64_t *npair, Pair< dtype > **pairs, bool nonzeros_only=false, bool unpack_sym=false) const
gives the global indices and values associated with the local data
Definition: tensor.cxx:201
Tensor< dtype > slice(int const *offsets, int const *ends) const
cuts out a slice (block) of this tensor A[offsets,ends) result will always be fully nonsymmetric ...
Definition: tensor.cxx:643
void real_norm1< bool >(Tensor< bool > &A, double &nrm)
Definition: tensor.cxx:812
void profile_off()
turn off profiling
virtual bool is_ordered() const =0
#define NORM2_COMPLEX_INST(dtype)
Definition: tensor.cxx:876
void permute(dtype beta, CTF_int::tensor &A, int *const *perms_A, dtype alpha)
Apply permutation to matrix, potentially extracting a slice B[i,j,...] = beta*B[...] + alpha*A[perms_A[0][i],perms_A[1][j],...].
Definition: tensor.cxx:429
double get_rand48()
returns new random number in [0,1)
Definition: common.cxx:27
int sparsify(char const *threshold=NULL, bool take_abs=true)
reduce tensor to sparse format, storing only nonzero data, or data above a specified threshold...
int64_t size
current size of local tensor data chunk (mapping-dependent)
Semiring is a Monoid with an addition multiplicaton function addition must have an identity and be as...
Definition: semiring.h:359
void * alloc(int64_t len)
alloc abstraction
Definition: memcontrol.cxx:365
dtype reduce(OP op)
performs a reduction on the tensor
Definition: tensor.cxx:731
void read_all(int64_t *npair, dtype **data, bool unpack=false)
collects the entire tensor data on each process (not memory scalable)
Definition: tensor.cxx:377
custom bivariate function on two tensors: e.g. C["ij"] = f(A["ik"],B["kj"])
Definition: functions.h:137
Definition: common.h:37
virtual char const * addid() const
MPI datatype for pairs.
Definition: algstrct.cxx:89
an instance of the CTF library (world) on a MPI communicator
Definition: world.h:19
void set_name(char const *name)
sets tensor name
Definition: tensor.cxx:392
bool is_sparse
whether only the non-zero elements of the tensor are stored
int order
number of tensor dimensions
int64_t k
key, global index [i1,i2,...] specified as i1+len[0]*i2+...
Definition: tensor.h:31
#define IASSERT(...)
Definition: common.h:74
void read_val(char *buf) const
sets external value to the value pointed by the iterator
Definition: algstrct.cxx:801
void read_local(int64_t *npair, int64_t **global_idx, dtype **data, bool unpack_sym=false) const
Using get_local_data(), which returns an array that must be freed with delete [], is more efficient...
Definition: tensor.cxx:182
void compare(const Tensor< dtype > &A, FILE *fp=stdout, double cutoff=-1.0)
prints two sets of tensor data side-by-side to file using process 0
Definition: tensor.cxx:424
int permute(tensor *A, int *const *permutation_A, char const *alpha, int *const *permutation_B, char const *beta)
virtual char * alloc(int64_t n) const
allocate space for n items, necessary for object types
Definition: algstrct.cxx:685
#define NORM_INFTY_INST(dtype)
Definition: tensor.cxx:900
double estimate_time()
predicts execution time in seconds using performance models
Definition: summation.cxx:132
dtype norm2()
computes the frobenius norm of the tensor (needs sqrt()!)
Definition: tensor.h:811
int align(tensor const *B)
align mapping of thisa tensor to that of B
index-value pair used for tensor data input
Definition: tensor.h:28
void read_sparse_from_file(const char *fpath, bool with_vals=true)
read sparse tensor from file, entries of tensor must be stored one per line, as i_1 ...
CTF::World * wrld
distributed processor context on which tensor is defined
custom function f : X -> Y to be applied to tensor elemetns: e.g. B["ij"] = f(A["ij"]) ...
Definition: functions.h:55
def scl(self, s)
Definition: core.pyx:473
class for execution distributed scaling of a tensor
Definition: scaling.h:14
a sparse subset of a tensor
Definition: sparse_tensor.h:14
void fill_random(dtype rmin, dtype rmax)
fills local unique tensor elements to random values in the range [min,max] works only for dtype in {f...
Definition: tensor.cxx:928
int rank
rank of local processor
Definition: world.h:24
virtual void min(char const *a, char const *b, char *c) const
c = min(a,b)
Definition: algstrct.cxx:132
int write(int64_t num_pair, char const *alpha, char const *beta, char *mapped_data, char const rw='w')
Add tensor data new=alpha*new+beta*old with <key, value> pairs where key is the global index for the ...
class for execution distributed contraction of tensors
Definition: contraction.h:16
void print(FILE *fp=stdout, char const *cutoff=NULL) const
prints tensor data to file using process 0
int zero_out_padding()
sets padded portion of tensor to zero (this should be maintained internally)
int * lens
unpadded tensor edge lengths
void add_from_subworld(tensor *tsr_sub, char const *alpha, char const *beta)
accumulates this tensor from a tensor object defined on a different world
dtype norm_infty()
finds the max absolute value element of the tensor
Definition: tensor.h:816
int allread(int64_t *num_pair, char **all_data, bool unpack)
read entire tensor with each processor (in packed layout). WARNING: will use an &#39;unscalable&#39; amount o...
void sparsify()
reduce tensor to sparse format, storing only nonzero data, or data above a specified threshold...
Definition: tensor.cxx:449
~Tensor()
frees CTF tensor
Definition: tensor.cxx:149
void align(CTF_int::tensor const &A)
aligns data mapping with tensor A
Definition: tensor.cxx:720
Typ_Idx_Tensor< dtype > operator[](char const *idx_map)
associated an index map with the tensor for future operation
int64_t k() const
returns key of pair at head of ptr
Definition: algstrct.cxx:789
void compare(const tensor *A, FILE *fp, char const *cutoff)
prints two sets of tensor data side-by-side to file using process 0
void print(FILE *fp, dtype cutoff) const
prints tensor data to file using process 0 (modify print(...) overload in set.h if you would like a d...
Definition: tensor.cxx:407
algstrct * sr
algstrct on which tensor elements and operations are defined
virtual void pair_dealloc(char *ptr) const
deallocate given pointer containing contiguous array of pairs
Definition: algstrct.cxx:693
void read_sparse_from_file_base(const char *fpath, bool with_vals, Tensor< dtype > *T)
Definition: tensor.cxx:467
void fill_sp_random(dtype rmin, dtype rmax, double frac_sp)
generate roughly frac_sp*dense_tensor_size nonzeros between rmin and rmax, works only for dtype in {f...
Definition: tensor.cxx:969
double estimate_time()
predicts execution time in seconds using performance models
def abs(initA)
Definition: core.pyx:5440
void add_from_subworld(Tensor< dtype > *tsr, dtype alpha, dtype beta)
accumulates this tensor from a tensor object defined on a different world
Definition: tensor.cxx:560
void fill_sp_random_base(dtype rmin, dtype rmax, double frac_sp, Tensor< dtype > *T)
Definition: tensor.cxx:976
MPI_Comm cm
Definition: common.h:129
void write_sparse_to_file(const char *fpath, bool with_vals=true)
write sparse tensor to file, entries of tensor will be stored one per line, as i_1 ...
def copy(tensor, A)
Definition: core.pyx:3583
void real_norm1(Tensor< dtype > &A, double &nrm)
Definition: tensor.cxx:803
void get_local_data(int64_t *npair, int64_t **global_idx, dtype **data, bool nonzeros_only=false, bool unpack_sym=false) const
Gives the global indices and values associated with the local data.
Definition: tensor.cxx:159
Tensor()
default constructor
Definition: tensor.cxx:12
virtual void max(char const *a, char const *b, char *c) const
c = max(a,b)
Definition: algstrct.cxx:138
void fill_random_base(dtype rmin, dtype rmax, Tensor< dtype > &T)
Definition: tensor.cxx:935
void add_to_subworld(tensor *tsr_sub, char const *alpha, char const *beta)
accumulates this tensor to a tensor object defined on a different world
int el_size
size of each element of algstrct in bytes
Definition: algstrct.h:16
bool profile
whether profiling should be done for contractions/sums involving this tensor
int cdealloc(void *ptr)
free abstraction
Definition: memcontrol.cxx:480
dtype * get_raw_data(int64_t *size) const
gives the raw current local data with padding included
Definition: tensor.cxx:152
algstrct (algebraic structure) defines the elementwise operations computed in each tensor contraction...
Definition: algstrct.h:34
Definition: apsp.cxx:17
void sum(dtype alpha, CTF_int::tensor &A, char const *idx_A, dtype beta, char const *idx_B)
sums B[idx_B] = beta*B[idx_B] + alpha*A[idx_A]
char * data
tensor data, either the data or the key-value pairs should exist at any given time ...
A Monoid is a Set equipped with a binary addition operator &#39;+&#39; or a custom function addition must hav...
Definition: monoid.h:69
internal distributed tensor class
dtype norm1()
computes the entrywise 1-norm of the tensor
Definition: tensor.h:806
void profile_on()
turn on profiling
dtype * get_mapped_data(char const *idx, Idx_Partition const &prl, Idx_Partition const &blk=Idx_Partition(), bool unpack=true)
returns local data of tensor with parallel distribution prl and local blocking blk ...
Definition: tensor.cxx:1129
an instance of a tensor within a CTF world
Definition: tensor.h:74
int execute()
run scaling
Definition: scaling.cxx:64
void read(int64_t npair, Pair< dtype > *pairs)
Gives the values associated with any set of indices.
Definition: tensor.cxx:246
void scale(dtype alpha, char const *idx_A)
scales A[idx_A] = alpha*A[idx_A]
Tensor< dtype > & operator=(dtype val)
sets all values in the tensor to val
Definition: tensor.cxx:1139
#define NORM2_REAL_INST(dtype)
Definition: tensor.cxx:870
class for execution distributed summation of tensors
Definition: summation.h:15
int read_local_nnz(int64_t *num_pair, char **mapped_data, bool unpack_sym=false) const
read tensor data pairs local to processor that have nonzero values
double estimate_time(CTF_int::tensor &A, char const *idx_A, CTF_int::tensor &B, char const *idx_B, char const *idx_C)
estimate the time of a contraction C[idx_C] = A[idx_A]*B[idx_B]
void free_self()
destructor
virtual char const * mulid() const
identity element for multiplication i.e. 1
Definition: algstrct.cxx:93
int reduce_sumsq(char *result)
computes the sum of squares of the elements
void write_sparse_to_file_base(const char *fpath, bool with_vals, Tensor< dtype > *T)
Definition: tensor.cxx:506
void copy_tensor_data(tensor const *other)
copies all tensor data from other
void set_name(char const *name)
set the tensor name
void write(int64_t npair, int64_t const *global_idx, dtype const *data)
writes in values associated with any set of indices The sparse data is defined in coordinate format...
Definition: tensor.cxx:264
char * name
name given to tensor
int np
number of processors
Definition: world.h:26
void init(algstrct const *sr, int order, int const *edge_len, int const *sym, CTF::World *wrld, bool alloc_data, char const *name, bool profile, bool is_sparse)
initializes tensor data
MPI_Comm comm
set of processors making up this world
Definition: world.h:22
void get_max_abs(int n, dtype *data) const
obtains a small number of the biggest elements of the tensor in sorted order (e.g. eigenvalues)
Definition: tensor.cxx:920
int read_local(int64_t *num_pair, char **mapped_data, bool unpack_sym=false) const
read tensor data pairs local to processor including those with zero values WARNING: for sparse tensor...
int get_max_abs(int n, char *data) const
obtains the largest n elements (in absolute value) of the tensor
void cvrt_idx(int order, int const *lens, int64_t idx, int *idx_arr)
Definition: common.cxx:533