Cyclops Tensor Framework
parallel arithmetic on multidimensional arrays
sparse_tensor.cxx
Go to the documentation of this file.
1 /*Copyright (c) 2011, Edgar Solomonik, all rights reserved.*/
2 
3 #include "common.h"
4 
5 namespace CTF {
6 
7  template<typename dtype>
9  parent = NULL;
10  }
11 
12  template<typename dtype>
13  Sparse_Tensor<dtype>::Sparse_Tensor(std::vector<int64_t> indices_,
14  Tensor<dtype> * parent_){
15  parent = parent_;
16  indices = indices_;
17  scale = *(dtype*)parent_->sr->mulid();
18  }
19 
20  template<typename dtype>
22  int64_t * indices_,
23  Tensor<dtype> * parent_){
24  parent = parent_;
25  indices = std::vector<int64_t>(indices_,indices_+n);
26  scale = *(dtype*)parent_->sr->mulid();
27  }
28 
29  template<typename dtype>
31  dtype * values,
32  dtype beta){
33  parent->write(indices.size(),alpha,beta,&indices[0],&values[0]);
34  }
35 
36  // C++ overload special-cases of above method
37  template<typename dtype>
38  void Sparse_Tensor<dtype>::operator=(std::vector<dtype> values){
39  write(*(dtype const*)parent->sr->mulid(), &values[0], *(dtype const*)parent->sr->addid());
40  }
41  template<typename dtype>
43  write(*(dtype const*)parent->sr->mulid(), values, *(dtype const*)parent->sr->addid());
44  }
45 
46  template<typename dtype>
47  void Sparse_Tensor<dtype>::operator+=(std::vector<dtype> values){
48  write(*(dtype const*)parent->sr->mulid(), &values[0], *(dtype const*)parent->sr->mulid());
49  }
50 
51  template<typename dtype>
53  write(*(dtype const*)parent->sr->mulid(), values, *(dtype const*)parent->sr->mulid());
54  }
55 
56  template<typename dtype>
57  void Sparse_Tensor<dtype>::operator-=(std::vector<dtype> values){
58  write(-*(dtype const*)parent->sr->mulid(), &values[0], *(dtype const*)parent->sr->mulid());
59  }
60 
61  template<typename dtype>
63  write(-*(dtype const*)parent->sr->mulid(), values, *(dtype const*)parent->sr->mulid());
64  }
65 
66  template<typename dtype>
68  dtype * values,
69  dtype beta){
70  parent->read(indices.size(),alpha,beta,&indices[0],values);
71  }
72  template<typename dtype>
73  Sparse_Tensor<dtype>::operator std::vector<dtype>(){
74  std::vector<dtype> values(indices.size());
75  read(parent->sr->mulid(), &values[0], parent->sr->addid());
76  return values;
77  }
78 
79  template<typename dtype>
81  dtype * values = (dtype*)malloc(sizeof(dtype)*indices.size());
82  read(parent->sr->mulid(), values, parent->sr->addid());
83  return values;
84  }
85 }
void read(dtype alpha, dtype *values, dtype beta)
read the sparse set of indices on the parent tensor to values forall(j) i = indices[j]; values[j] = a...
Sparse_Tensor()
base constructor
void write(dtype alpha, dtype *values, dtype beta)
set the sparse set of indices on the parent tensor to values forall(j) i = indices[j]; parent[i] = be...
void operator-=(std::vector< dtype > values)
a sparse subset of a tensor
Definition: sparse_tensor.h:14
def scale(self, scl)
Definition: core.pyx:325
void operator=(std::vector< dtype > values)
void operator+=(std::vector< dtype > values)
algstrct * sr
algstrct on which tensor elements and operations are defined
Definition: apsp.cxx:17
an instance of a tensor within a CTF world
Definition: tensor.h:74