mxnet
io.h
Go to the documentation of this file.
1 
7 #ifndef MSHADOW_IO_H_
8 #define MSHADOW_IO_H_
9 #include "./tensor.h"
10 
11 namespace mshadow {
12 namespace utils {
18 class IStream {
19  public:
26  virtual size_t Read(void *ptr, size_t size) = 0;
32  virtual void Write(const void *ptr, size_t size) = 0;
34  virtual ~IStream(void) {}
35 };
36 } // namespace utils
45 template<int dim, typename DType, typename TStream>
46 inline void SaveBinary(TStream &fo, const Tensor<cpu, dim, DType> &src); // NOLINT(*)
55 template<int dim, typename DType, typename TStream>
56 inline void SaveBinary(TStream &fo, const Tensor<gpu, dim, DType> &src); // NOLINT(*)
68 template<int dim, typename DType, typename TStream>
69 inline void LoadBinary(TStream &fi, // NOLINT(*)
70  Tensor<cpu, dim, DType> *dst, bool pre_alloc);
83 template<int dim, typename DType, typename TStream>
84 inline void LoadBinary(TStream &fi, // NOLINT(*)
85  Tensor<gpu, dim, DType> *dst, bool pre_alloc);
86 
87 // implementations
88 template<int dim, typename DType, typename TStream>
89 inline void SaveBinary(TStream &fo, const Tensor<cpu, dim, DType> &src_) { // NOLINT(*)
90  fo.Write(&src_.shape_, sizeof(src_.shape_));
91  Tensor<cpu, 2, DType> src = src_.FlatTo2D();
92  for (index_t i = 0; i < src.size(0); ++i) {
93  fo.Write(src[i].dptr_, sizeof(DType) * src.size(1));
94  }
95 }
96 template<int dim, typename DType, typename TStream>
97 inline void SaveBinary(TStream &fo, const Tensor<gpu, dim, DType> &src) { // NOLINT(*)
98  // copy to CPU, then save
100  AllocSpace(&tmp);
101  Stream<gpu> stream;
102  Copy(tmp, src, &stream);
103  SaveBinary(fo, tmp);
104  FreeSpace(&tmp);
105 }
106 template<int dim, typename DType, typename TStream>
107 inline void LoadBinary(TStream &fi, // NOLINT(*)
108  Tensor<cpu, dim, DType> *dst_, bool pre_alloc) {
109  Shape<dim> shape;
110  CHECK_NE(fi.Read(&shape, sizeof(shape)), 0) << "mshadow::LoadBinary";
111  if (pre_alloc) {
112  CHECK_EQ(shape, dst_->shape_) << "LoadBinary, shape do not match pre-allocated shape";
113  } else {
114  dst_->shape_ = shape; AllocSpace(dst_);
115  }
116  Tensor<cpu, 2, DType> dst = dst_->FlatTo2D();
117  if (dst.size(0) == 0) return;
118  for (index_t i = 0; i < dst.size(0); ++i) {
119  CHECK_NE(fi.Read(dst[i].dptr_, sizeof(DType) * dst.size(1)), 0) << "mshadow::LoadBinary";
120  }
121 }
122 template<int dim, typename DType, typename TStream>
123 inline void LoadBinary(TStream &fi, // NOLINT(*)
124  Tensor<gpu, dim, DType> *dst, bool pre_alloc) {
126  LoadBinary(fi, &tmp, false);
127  if (pre_alloc) {
128  CHECK_EQ(tmp.shape, dst->shape_) << "LoadBinary, shape do not match pre-allocated shape";
129  } else {
130  dst->shape = tmp.shape; AllocSpace(dst);
131  }
132  Stream<gpu> stream;
133  Copy(*dst, tmp, &stream);
134  FreeSpace(&tmp);
135 }
136 } // namespace mshadow
137 #endif // MSHADOW_IO_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
DType * dptr_
pointer to the data
Definition: tensor.h:416
virtual size_t Read(void *ptr, size_t size)=0
read data from stream
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
Definition: stream_gpu-inl.h:19
Shape< dimension > shape_
shape of the tensor
Definition: tensor.h:418
MSHADOW_XINLINE Tensor< Device, 2, DType > FlatTo2D(void) const
flatten the tensor to 2 dimension, collapse the higher dimensions together
Definition: tensor.h:501
int32_t index_t
type that will be used for index
Definition: base.h:291
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
namespace for mshadow
Definition: base.h:282
interface of stream I/O, used to serialize data, mshadow does not restricted to only this interface i...
Definition: io.h:18
MSHADOW_XINLINE index_t size(int idx) const
return size of i-th dimension, start counting from highest dimension
Definition: tensor.h:487
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
virtual ~IStream(void)
virtual destructor
Definition: io.h:34
general tensor
Definition: tensor.h:402
virtual void Write(const void *ptr, size_t size)=0
write data to stream