Apache SINGA
A distributed deep learning platform .
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Macros
tensor_container.h
Go to the documentation of this file.
1 #ifndef MSHADOW_TENSOR_CONTAINER_H
2 #define MSHADOW_TENSOR_CONTAINER_H
3 
8 #include "tensor.h"
9 #include "tensor_io.h"
10 
11 namespace mshadow{
20  template<typename Device, int dimension>
21  class TensorContainer: public Tensor<Device,dimension>{
22  public:
28  this->pad_ = pad;
29  this->dptr = data_.dptr = NULL;
30  this->shape[0] = 0;
31  this->shape.stride_ = 0;
32  this->data_.shape.stride_ = 0;
33  this->data_.shape[1] = 0;
34  }
40  this->pad_ = MSHADOW_ALLOC_PAD;
41  data_.dptr = NULL;
42  this->AllocByShape( shape );
43  }
50  this->pad_ = MSHADOW_ALLOC_PAD;
51  data_.dptr = NULL;
52  this->AllocByShape( shape );
53  (*this) = initv;
54  }
55  ~TensorContainer( void ){
56  this->FreeSpace();
57  }
62  inline void Resize( const Shape<dimension> &shape ){
63  Shape<2> s2 = shape.FlatTo2D();
64  if( s2.shape_[0] > data_.shape.stride_ || s2.shape_[1] > data_.shape[1] ){
65  this->AllocByShape( shape );
66  }else{
67  this->shape = shape;
68  if( this->pad_ ){
69  this->shape.stride_ = data_.shape.stride_;
70  }else{
71  this->shape.stride_ = this->shape[ 0 ];
72  }
73  }
74  }
80  inline void Resize( const Shape<dimension> &shape, real_t initv ){
81  this->Resize( shape );
82  (*this) = initv;
83  }
85  inline void set_pad( bool pad ){
86  this->pad_ = pad;
87  }
93  template<typename TStream>
94  inline void SaveBinary( TStream &fo ) const{
95  mshadow::SaveBinary( fo, *this );
96  }
102  template<typename TStream>
103  inline void LoadBinary( TStream &fi ) {
105  mshadow::LoadBinary( fi, tmp, false );
106  this->Resize( tmp.shape );
107  Copy( *this, tmp );
108  mshadow::FreeSpace( tmp );
109  }
110  public:
111  // functions to fit exp template
112  inline Tensor<Device,dimension>& operator=( real_t s ){
113  return this->__assign( s );
114  }
115  template<typename E>
116  inline Tensor<Device,dimension>& operator=( const expr::Exp<E,expr::type::kMapper> &exp ){
117  return this->__assign( exp );
118  }
119  template<typename E>
120  inline Tensor<Device,dimension>& operator=( const expr::Exp<E,expr::type::kComplex> &exp ){
121  return this->__assign( exp );
122  }
123  private:
125  bool pad_;
127  Tensor<Device, 2> data_;
128  private:
129  inline void FreeSpace (void){
130  if( data_.dptr != NULL ){
131  mshadow::FreeSpace( data_ );
132  data_.dptr = this->dptr = NULL;
133  }
134  }
135  inline void AllocByShape (const Shape<dimension>& shape){
136  if( data_.dptr != NULL ){
137  this->FreeSpace();
138  }
139  data_.shape = shape.FlatTo2D();
140  mshadow::AllocSpace( data_, pad_ );
141  this->dptr = data_.dptr;
142  this->shape = shape;
143  if( this->pad_ ){
144  this->shape.stride_ = data_.shape.stride_;
145  }else{
146  this->shape.stride_ = shape[0];
147  }
148  }
149  };
150 };// namespace mshadow
151 
152 #endif
void Resize(const Shape< dimension > &shape, real_t initv)
resize the container to given shape, and initialize, content is NOT preserved
Definition: tensor_container.h:80
void set_pad(bool pad)
set whether padding is allowed in tensor
Definition: tensor_container.h:85
void Resize(const Shape< dimension > &shape)
resize the container to given shape, content is NOT preserved
Definition: tensor_container.h:62
TensorContainer(bool pad=MSHADOW_ALLOC_PAD)
constructor
Definition: tensor_container.h:27
shape of a tensor IMPORTANT NOTE: this shape is different from numpy.shape shape[0] gives the lowest ...
Definition: tensor.h:23
void FreeSpace(Tensor< cpu, dim > &obj)
CPU/GPU: free the space of tensor, will set obj.dptr to NULL.
Definition: tensor_cpu-inl.hpp:36
MSHADOW_XINLINE Shape< 2 > FlatTo2D(void) const
Definition: tensor.h:68
index_t shape_[kMaxShape]
storing the dimension information
Definition: tensor.h:129
void LoadBinary(TStream &fi)
load by binary format, a temp Tensor<cpu,dim> storage will be allocated
Definition: tensor_container.h:103
float real_t
type that will be used for content
Definition: tensor_base.h:118
header file of tensor data structure and functions covention: this lib requires explicit memory alloc...
void SaveBinary(TStream &fo, const Tensor< cpu, dim > &src)
CPU/GPU: save a tensor by binary format, for GPU version, a temp Tensor<cpu,dim> storage will be allo...
Definition: tensor_io.h:92
tensor container that does memory allocation and resize like STL, use it to save the lines of FreeSpa...
Definition: tensor_container.h:21
TensorContainer(const Shape< dimension > &shape, real_t initv)
constructor
Definition: tensor_container.h:49
Tensor< Device, dimension > & __assign(real_t s)
operator overload
Definition: tensor_expr.h:119
#define MSHADOW_ALLOC_PAD
whether do padding during allocation
Definition: tensor_base.h:23
void SaveBinary(TStream &fo) const
save by binary format
Definition: tensor_container.h:94
real_t * dptr
pointer to the data
Definition: tensor.h:215
void Copy(Tensor< cpu, dim > dst, const Tensor< cpu, dim > &src)
copy data from one tensor to another, with same shape
Definition: tensor_cpu-inl.hpp:42
Shape< dimension > shape
shape of the tensor
Definition: tensor.h:217
void AllocSpace(Tensor< cpu, dim > &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.hpp:14
general tensor
Definition: tensor.h:206
PaddingExp< SrcExp, ExpInfo< SrcExp >::kDim > pad(const Exp< SrcExp, etype > &src, index_t pad)
padding expression, pad a image with zeros on boundaries, padding affects shape[0], and shape[1]
Definition: tensor_expr_ext.h:496
TensorContainer(const Shape< dimension > &shape)
constructor
Definition: tensor_container.h:39
void LoadBinary(TStream &fi, Tensor< cpu, dim > &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: tensor_io.h:110
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:134
definitions of I/O functions for mshadow tensor