Apache SINGA
A distributed deep learning platform .
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Macros
blob.h
1 
41 #ifndef INCLUDE_UTILS_BLOB_
42 #define INCLUDE_UTILS_BLOB_
43 #include <memory>
44 #include <vector>
45 #include <glog/logging.h>
46 #include "proto/model.pb.h"
47 using std::shared_ptr;
48 using std::vector;
49 
50 #define NOT_IMPLEMENTED LOG(FATAL) << "Not implemented function"
51 inline void MallocHost(void** ptr, size_t size) {
52  *ptr = malloc(size);
53 }
54 
55 inline void FreeHost(void* ptr) {
56  free(ptr);
57 }
58 
65 class SyncedMemory {
66  public:
67  SyncedMemory()
68  : cpu_ptr_(NULL), gpu_ptr_(NULL), size_(0), head_(UNINITIALIZED),
69  own_cpu_data_(false) {}
70  explicit SyncedMemory(size_t size)
71  : cpu_ptr_(NULL), gpu_ptr_(NULL), size_(size), head_(UNINITIALIZED),
72  own_cpu_data_(false) {}
73  ~SyncedMemory();
74  const void* cpu_data();
75  void set_cpu_data(void* data);
76  const void* gpu_data();
77  void* mutable_cpu_data();
78  void* mutable_gpu_data();
79  enum SyncedHead { UNINITIALIZED, HEAD_AT_CPU, HEAD_AT_GPU, SYNCED };
80  SyncedHead head() { return head_; }
81  size_t size() { return size_; }
82 
83  private:
84  void to_cpu();
85  void to_gpu();
86  void* cpu_ptr_;
87  void* gpu_ptr_;
88  size_t size_;
89  SyncedHead head_;
90  bool own_cpu_data_;
91 
92 }; // class SyncedMemory
93 
94 
95 template <typename Dtype>
96 class Blob {
97  public:
98  Blob(): count_(0), capacity_(0) , version_(-1){}
99  Blob(const vector<int>&shape);
114  void Reshape(const vector<int>& shape);
115  void ReshapeLike(const Blob& other);
116  const vector<int>& shape() const{
117  return shape_;
118  }
119  inline int count() const { return count_; }
120  void set_version(int v){
121  version_=v;
122  }
123  const int version() const {
124  return version_;
125  }
134  void CopyFrom(const Blob<Dtype>& source, bool reshape = false);
135 
136  inline const shared_ptr<SyncedMemory>& data() const {
137  CHECK(data_);
138  return data_;
139  }
140 
141  const Dtype* cpu_data() const;
142  void set_cpu_data(Dtype* data);
143  const Dtype* gpu_data() const;
144  Dtype* mutable_cpu_data();
145  Dtype* mutable_gpu_data();
146  /*
147  void FromProto(const BlobProto& proto);
148  */
149  void ToProto(singa::BlobProto* proto) const;
150 
152  Dtype asum_data() const;
153  Dtype sum_data() const;
154 
163  void ShareData(const Blob& other);
164  void Swap(Blob& other);
165  shared_ptr<SyncedMemory> data_;
166  protected:
167  vector<int> shape_;
168  int count_;
169  int capacity_;
170  int version_;
171 }; // class Blob
172 
173 #endif // INCLUDE_UTILS_BLOB_
Definition: blob.h:96
Manages memory allocation and synchronization between the host (CPU) and device (GPU).
Definition: blob.h:65
Definition: model.pb.h:3695
void ShareData(const Blob &other)
Set the data_ shared_ptr to point to the SyncedMemory holding the data_ of Blob other – useful in Lay...
Dtype asum_data() const
Compute the sum of absolute values (L1 norm) of the data.
void Reshape(const vector< int > &shape)
Change the dimensions of the blob, allocating new memory if necessary.
void CopyFrom(const Blob< Dtype > &source, bool reshape=false)
Copy from a source Blob.
ReshapeExp< SrcExp, dimdst, ExpInfo< SrcExp >::kDim > reshape(const Exp< SrcExp, etype > &src, Shape< dimdst > oshape)
a expression that reshapes a tensor to another shape
Definition: tensor_expr_ext.h:406