mxnet
tensor_container.h
Go to the documentation of this file.
1 
7 #ifndef MSHADOW_TENSOR_CONTAINER_H_
8 #define MSHADOW_TENSOR_CONTAINER_H_
9 #include "./tensor.h"
10 #include "./io.h"
11 
12 namespace mshadow {
21 template<typename Device, int dimension, typename DType = default_real_t>
22 class TensorContainer: public Tensor<Device, dimension, DType> {
23  public:
29  this->pad_ = pad;
30  this->dptr_ = data_.dptr_ = NULL;
31  this->shape_[0] = 0;
32  this->stride_ = 0;
33  this->data_.stride_ = 0;
34  this->data_.shape_[0] = 0;
35  }
40  explicit TensorContainer(const Shape<dimension> &shape) {
41  this->pad_ = MSHADOW_ALLOC_PAD;
42  data_.dptr_ = NULL;
43  this->AllocByShape(shape);
44  }
50  explicit TensorContainer(const Shape<dimension> &shape, DType initv) {
51  this->pad_ = MSHADOW_ALLOC_PAD;
52  data_.dptr_ = NULL;
53  this->AllocByShape(shape);
54  (*this) = initv;
55  }
62  : pad_(src.pad_) {
63  this->dptr_ = data_.dptr_ = NULL;
64  this->shape_[0] = 0;
65  this->stride_ = 0;
66  this->data_.stride_ = 0;
67  this->data_.shape_[0] = 0;
68  this->stream_ = src.stream_;
69  if (src.dptr_ != NULL) {
70  this->AllocByShape(src.shape_);
71  mshadow::Copy(*this, src, this->stream_);
72  }
73  }
75  this->Release();
76  }
81  inline void Resize(const Shape<dimension> &shape) {
82  Shape<2> s2 = shape.FlatTo2D();
83  if (s2.shape_[1] > data_.stride_ || s2.shape_[0] > data_.size(0)) {
84  this->AllocByShape(shape);
85  } else {
86  this->shape_ = shape;
87  if (this->pad_) {
88  this->stride_ = data_.stride_;
89  } else {
90  this->stride_ = s2.shape_[1];
91  }
92  }
93  }
99  inline void Resize(const Shape<dimension> &shape, DType initv) {
100  this->Resize(shape);
101  (*this) = initv;
102  }
104  inline void set_pad(bool pad) {
105  this->pad_ = pad;
106  }
112  template<typename TStream>
113  inline void SaveBinary(TStream &fo) const { // NOLINT(*)
114  mshadow::SaveBinary(fo, *this);
115  }
121  template<typename TStream>
122  inline void LoadBinary(TStream &fi) { // NOLINT(*)
124  mshadow::LoadBinary(fi, &tmp, false);
125  this->Resize(tmp.shape_);
126  Stream<Device> stream;
127  Copy(*this, tmp, &stream);
128  mshadow::FreeSpace(&tmp);
129  }
135  inline TensorContainer &operator=
137  this->pad_ = src.pad_;
138  this->stream_ = src.stream_;
139  if (src.dptr_ != NULL) {
140  this->Resize(src.shape_);
141  mshadow::Copy(*this, src, this->stream_);
142  }
143  return *this;
144  }
147  return this->__assign(s);
148  }
150  template<typename E>
153  return this->__assign(exp);
154  }
156  template<typename E>
159  return this->__assign(exp);
160  }
162  template<typename E>
165  return this->__assign(exp);
166  }
172  inline void Release(void) {
173  if (data_.dptr_ != NULL) {
174  this->shape_[0] = 0;
175  this->stride_ = 0;
176  this->data_.stride_ = 0;
177  this->data_.shape_[0] = 0;
178  try {
179  mshadow::FreeSpace(&data_);
180  } catch (const dmlc::Error &e) {
181  this->dptr_ = data_.dptr_ = NULL;
182  throw e;
183  }
184  this->dptr_ = data_.dptr_ = NULL;
185  }
186  }
187 
188  private:
190  bool pad_;
193 
194  inline void AllocByShape(const Shape<dimension>& shape) {
195  if (data_.dptr_ != NULL) this->Release();
196  data_.shape_ = shape.FlatTo2D();
197  mshadow::AllocSpace(&data_, pad_);
198  this->dptr_ = data_.dptr_;
199  this->shape_ = shape;
200  if (this->pad_) {
201  this->stride_ = data_.stride_;
202  } else {
203  this->stride_ = data_.size(1);
204  }
205  }
206 };
207 } // namespace mshadow
208 #endif // MSHADOW_TENSOR_CONTAINER_H_
void FreeSpace(Tensor< cpu, dim, DType > *obj)
CPU/GPU: free the space of tensor, will set obj.dptr to NULL.
Definition: tensor_cpu-inl.h:122
void Resize(const Shape< dimension > &shape, DType initv)
resize the container to given shape, and initialize, content is NOT preserved
Definition: tensor_container.h:99
PaddingExp< SrcExp, DType, ExpInfo< SrcExp >::kDim > pad(const Exp< SrcExp, DType, etype > &src, index_t pad)
padding expression, pad a image with zeros on boundaries, padding affects shape[0], and shape[1]
Definition: pad.h:53
Tensor< Device, dimension, DType > & operator=(const expr::Exp< E, DType, expr::type::kComplex > &exp)
functions to fit expression template
Definition: tensor_container.h:164
DType * dptr_
pointer to the data
Definition: tensor.h:416
TensorContainer(const Shape< dimension > &shape, DType initv)
constructor
Definition: tensor_container.h:50
void LoadBinary(TStream &fi, Tensor< cpu, dim, DType > *dst, bool pre_alloc)
CPU/GPU: load a tensor by binary format, for GPU version, a temp Tensor<cpu,dim> storage will be allo...
Definition: io.h:107
void Copy(Tensor< cpu, dim, DType > dst, const Tensor< cpu, dim, DType > &src, Stream< cpu > *stream=NULL)
copy data from one tensor to another, with same shape
Definition: tensor_cpu-inl.h:127
shape of a tensor
Definition: tensor.h:35
void set_pad(bool pad)
set whether padding is allowed in tensor
Definition: tensor_container.h:104
Shape< dimension > shape_
shape of the tensor
Definition: tensor.h:418
TensorContainer(bool pad=MSHADOW_ALLOC_PAD)
constructor
Definition: tensor_container.h:28
Tensor< Device, dimension, DType > & operator=(const expr::Exp< E, DType, expr::type::kChainer > &exp)
functions to fit expression template
Definition: tensor_container.h:158
void Release(void)
Release the llocated space, The TensorContainer is still functionable, but will restart allocating sp...
Definition: tensor_container.h:172
MSHADOW_XINLINE Shape< 2 > FlatTo2D(void) const
Definition: tensor.h:114
#define MSHADOW_ALLOC_PAD
whether do padding during allocation
Definition: base.h:54
void AllocSpace(Tensor< cpu, dim, DType > *obj, bool pad=MSHADOW_ALLOC_PAD)
CPU/CPU: allocate space for CTensor, according to the shape in the obj this function is responsible t...
Definition: tensor_cpu-inl.h:98
TensorContainer(const Shape< dimension > &shape)
constructor
Definition: tensor_container.h:40
Tensor< Device, dimension, DType > & operator=(DType s)
functions to fit expression template
Definition: tensor_container.h:146
tensor container that does memory allocation and resize like STL, use it to save the lines of FreeSpa...
Definition: tensor_container.h:22
void SaveBinary(TStream &fo) const
save by binary format
Definition: tensor_container.h:113
index_t shape_[kDimension]
storing the dimension information
Definition: tensor.h:57
~TensorContainer(void) MSHADOW_THROW_EXCEPTION
Definition: tensor_container.h:74
void LoadBinary(TStream &fi)
load by binary format, a temp Tensor<cpu,dim> storage will be allocated
Definition: tensor_container.h:122
defines how expression exp can be evaluated and stored into dst
Definition: expression.h:61
namespace for mshadow
Definition: base.h:282
#define MSHADOW_THROW_EXCEPTION
Definition: base.h:234
MSHADOW_XINLINE index_t size(int idx) const
return size of i-th dimension, start counting from highest dimension
Definition: tensor.h:487
Tensor< Device, dimension, DType > & operator=(const expr::Exp< E, DType, expr::type::kMapper > &exp)
functions to fit expression template
Definition: tensor_container.h:152
void SaveBinary(TStream &fo, const Tensor< cpu, dim, DType > &src)
CPU/GPU: save a tensor by binary format, for GPU version, a temp Tensor<cpu,dim> storage will be allo...
Definition: io.h:89
index_t stride_
storing the stride information in x dimension this is used to deal with pitch allocation in gpu or ss...
Definition: tensor.h:423
Tensor< Device, dimension, DType > & __assign(DType s)
operator overload
Definition: expression.h:160
general tensor
Definition: tensor.h:402
void Resize(const Shape< dimension > &shape)
resize the container to given shape, content is NOT preserved
Definition: tensor_container.h:81
Stream< Device > * stream_
stream where the computation lies stream is a device dependency concept where each computation ...
Definition: tensor.h:428
computaion stream structure, used for asynchronous computations
Definition: tensor.h:365