Apache SINGA
A distributed deep learning platform .
 All Classes Namespaces Files Functions Variables Typedefs Macros
blob.h
1 /************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21 
62 #ifndef SINGA_UTILS_BLOB_H_
63 #define SINGA_UTILS_BLOB_H_
64 
65 #include <glog/logging.h>
66 #include <memory>
67 #include <vector>
68 #include "proto/common.pb.h"
69 
70 namespace singa {
71 
72 inline void MallocHost(void** ptr, size_t size) {
73  *ptr = malloc(size);
74 }
75 
76 inline void FreeHost(void* ptr) {
77  free(ptr);
78 }
79 
86 class SyncedMemory {
87  public:
88  enum SyncedHead { UNINITIALIZED,
89  HEAD_AT_CPU,
90  HEAD_AT_GPU,
91  SYNCED };
92 
93  SyncedMemory() {}
94  explicit SyncedMemory(size_t size) : size_(size) {}
95  ~SyncedMemory();
96 
97  const void* cpu_data();
98  const void* gpu_data();
99  void* mutable_cpu_data();
100  void* mutable_gpu_data();
101  void set_cpu_data(void* data);
102  inline SyncedHead head() { return head_; }
103  inline size_t size() { return size_; }
104 
105  private:
106  void to_cpu();
107  void to_gpu();
108 
109  void* cpu_ptr_ = nullptr;
110  void* gpu_ptr_ = nullptr;
111  size_t size_ = 0;
112  SyncedHead head_ = UNINITIALIZED;
113  bool own_cpu_data_ = false;
114 }; // class SyncedMemory
115 
116 
117 template <typename Dtype>
118 class Blob {
119  public:
120  Blob() {}
121  explicit Blob(const std::vector<int>& shape) { Reshape(shape); }
136  void Reshape(const std::vector<int>& shape);
137  void ReshapeLike(const Blob& other);
146  void CopyFrom(const Blob<Dtype>& source);
147  void CopyFrom(const Blob<Dtype>& source, bool reshape);
148  void FromProto(const singa::BlobProto& proto);
149  void ToProto(singa::BlobProto* proto) const;
158  void ShareData(const Blob& other);
159  void Swap(Blob& other);
160  inline const std::vector<int>& shape() const { return shape_; }
161  inline int count() const { return count_; }
162  inline const int version() const { return version_; }
163  inline void set_version(int v) { version_ = v; }
164  inline const Dtype* cpu_data() const {
165  CHECK(data_);
166  return static_cast<const Dtype*>(data_->cpu_data());
167  }
168  inline void set_cpu_data(Dtype* data) {
169  CHECK(data);
170  data_->set_cpu_data(data);
171  }
172  inline const Dtype* gpu_data() const {
173  CHECK(data_);
174  return static_cast<const Dtype*>(data_->gpu_data());
175  }
176  inline Dtype* mutable_cpu_data() {
177  CHECK(data_);
178  return static_cast<Dtype*>(data_->mutable_cpu_data());
179  }
180  inline Dtype* mutable_gpu_data() {
181  CHECK(data_);
182  return static_cast<Dtype*>(data_->mutable_gpu_data());
183  }
185  Dtype asum_data() const;
186  Dtype sum_data() const;
187 
188  protected:
189  std::shared_ptr<SyncedMemory> data_ = nullptr;
190  std::vector<int> shape_;
191  int count_ = 0;
192  int capacity_ = 0;
193  int version_ = -1;
194 }; // class Blob
195 
196 } // namespace singa
197 
198 #endif // SINGA_UTILS_BLOB_H_
Dtype asum_data() const
Compute the sum of absolute values (L1 norm) of the data.
void Reshape(const std::vector< int > &shape)
Change the dimensions of the blob, allocating new memory if necessary.
void CopyFrom(const Blob< Dtype > &source)
Copy from a source Blob.
Definition: blob.h:118
Manages memory allocation and synchronization between the host (CPU) and device (GPU).
Definition: blob.h:86
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
void ShareData(const Blob &other)
Set the data_ shared_ptr to point to the SyncedMemory holding the data_ of Blob other – useful in Lay...