MagmaDNN  1.0
c++NeuralNetworkFramework
tensor.h
Go to the documentation of this file.
1 
9 #pragma once
10 
11 #include <vector>
12 #include "types.h"
13 #include "memory/memorymanager.h"
14 #include "tensor_internal.h"
15 #include "utilities_internal.h"
16 
17 #if defined(_HAS_CUDA_)
18 #include "cudnn.h"
19 #endif
20 
21 namespace magmadnn {
22 
23 /* Default values for tensors.
24  Initialize to CPU 0 if not indicated otherwise.
25  And don't fill the tensor on creation unless specified.
26 */
27 const memory_t TENSOR_DEFAULT_MEM_TYPE = HOST;
28 const device_t TENSOR_DEFAULT_DEVICE_ID = (device_t) 0;
29 const tensor_fill_t TENSOR_DEFAULT_FILL_TYPE = NONE;
30 const tensor_filler_t<float> TENSOR_DEFAULT_FILLER = { TENSOR_DEFAULT_FILL_TYPE, {} };
31 
32 
33 template <typename T>
34 class Tensor {
35 public:
36 
40  Tensor(std::vector<unsigned int> shape);
41 
46  Tensor(std::vector<unsigned int> shape, memory_t mem_type);
47 
53  Tensor(std::vector<unsigned int> shape, memory_t mem_type, device_t device_id);
54 
59  Tensor(std::vector<unsigned int> shape, tensor_filler_t<T> filler);
60 
66  Tensor(std::vector<unsigned int> shape, tensor_filler_t<T> filler, memory_t mem_type);
67 
74  Tensor(std::vector<unsigned int> shape, tensor_filler_t<T> filler, memory_t mem_type, device_t device_id);
75 
78  ~Tensor();
79 
80 
87  magmadnn_error_t copy_from(const Tensor<T>& src, unsigned int begin_idx, unsigned int size);
88 
93  magmadnn_error_t copy_from(const Tensor<T>& src);
94 
95 
101  magmadnn_error_t copy_from(const Tensor<T>& src, const std::vector<unsigned int>& dims);
102 
103 
108  T get(const std::vector<int>& idx) const;
109 
114  T get(const std::vector<unsigned int>& idx) const;
115 
120  T get(const std::initializer_list<int>& idx) const;
121 
126  T get(const std::initializer_list<unsigned int>& idx) const;
127 
132  T get(unsigned int flattened_idx) const;
133 
138  const T operator[](unsigned int idx) const;
139 
140  const T operator[](const std::initializer_list<unsigned int>& idx);
141 
146  void set(const std::vector<int>& idx, T val);
147 
152  void set(const std::vector<unsigned int>& idx, T val);
153 
158  void set(const std::initializer_list<int>& idx, T val);
159 
164  void set(const std::initializer_list<unsigned int>& idx, T val);
165 
170  void set(unsigned int flattened_idx, T val);
171 
172 
176  MemoryManager<T>* get_memory_manager() const { return this->mem_manager; }
177 
181  std::vector<unsigned int> get_shape() const { return this->shape; }
182 
187  unsigned int get_shape(unsigned int idx) const;
188 
192  unsigned int get_size() const { return this->size; }
193 
197  T* get_ptr() { return this->mem_manager->get_ptr(); }
198 
202  memory_t get_memory_type() const { return this->mem_type; }
203 
207  device_t get_device_id() const { return this->device_id; }
208 
209  #if defined(_HAS_CUDA_)
210  cudnnTensorDescriptor_t get_cudnn_tensor_descriptor() { return desc; }
211  #endif
212 
216  void reshape(const std::vector<unsigned int>& dims);
217 
220  void squeeze();
221 
225  void unsqueeze(unsigned int dim = 0);
226 
227 private:
228  void init(std::vector<unsigned int>& shape, tensor_filler_t<T> filler, memory_t mem_type, device_t device_id);
229  unsigned int get_flattened_index(const std::vector<unsigned int>& idx) const;
230  unsigned int get_flattened_index_old(const std::vector<unsigned int>& idx) const;
231 
232  /* device specific code */
233  #if defined(_HAS_CUDA_)
234  void init_cudnn_descriptor();
235  void free_cudnn_descriptor();
236 
237  cudnnTensorDescriptor_t desc;
238  #endif
239 
240  MemoryManager<T> *mem_manager; /* allocated by init */
241 
242  std::vector<unsigned int> shape; /* tensor axes (shape) */
243  std::vector<unsigned int> strides; /* axis strides in memory */
244  unsigned int size; /* total number of elements in tensor */
245  memory_t mem_type; /* the type of memory to use for this tensor */
246  device_t device_id; /* device number i.e. gpu0 or cpu1 */
247 
248 };
249 
253 typedef Tensor<float> tensorf_t;
254 typedef Tensor<double> tensord_t;
255 
256 } // namespace magmadnn
std::vector< unsigned int > get_shape() const
Definition: tensor.h:181
Definition: types.h:64
unsigned int get_size() const
Definition: tensor.h:192
void squeeze()
Definition: tensor.cpp:265
void unsqueeze(unsigned int dim=0)
Definition: tensor.cpp:275
Definition: addop.cpp:11
magmadnn_error_t copy_from(const Tensor< T > &src, unsigned int begin_idx, unsigned int size)
Definition: tensor.cpp:91
~Tensor()
Definition: tensor.cpp:45
Tensor(std::vector< unsigned int > shape)
Definition: tensor.cpp:14
Definition: tensor.h:34
MemoryManager< T > * get_memory_manager() const
Definition: tensor.h:176
memory_t get_memory_type() const
Definition: tensor.h:202
device_t get_device_id() const
Definition: tensor.h:207
tensor_fill_t
Definition: types.h:47
T * get_ptr()
Definition: tensor.h:197
void reshape(const std::vector< unsigned int > &dims)
Definition: tensor.cpp:221
Definition: memorymanager.h:29
const T operator[](unsigned int idx) const
Definition: tensor.cpp:154