62 #ifndef SINGA_UTILS_BLOB_H_
63 #define SINGA_UTILS_BLOB_H_
65 #include <glog/logging.h>
68 #include "proto/common.pb.h"
72 inline void MallocHost(
void** ptr,
size_t size) {
76 inline void FreeHost(
void* ptr) {
88 enum SyncedHead { UNINITIALIZED,
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_; }
109 void* cpu_ptr_ =
nullptr;
110 void* gpu_ptr_ =
nullptr;
112 SyncedHead head_ = UNINITIALIZED;
113 bool own_cpu_data_ =
false;
117 template <
typename Dtype>
121 explicit Blob(
const std::vector<int>& shape) {
Reshape(shape); }
136 void Reshape(
const std::vector<int>& shape);
137 void ReshapeLike(
const Blob& other);
148 void FromProto(
const singa::BlobProto& proto);
149 void ToProto(singa::BlobProto* proto)
const;
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 {
166 return static_cast<const Dtype*
>(data_->cpu_data());
168 inline void set_cpu_data(Dtype* data) {
170 data_->set_cpu_data(data);
172 inline const Dtype* gpu_data()
const {
174 return static_cast<const Dtype*
>(data_->gpu_data());
176 inline Dtype* mutable_cpu_data() {
178 return static_cast<Dtype*
>(data_->mutable_cpu_data());
180 inline Dtype* mutable_gpu_data() {
182 return static_cast<Dtype*
>(data_->mutable_gpu_data());
186 Dtype sum_data()
const;
189 std::shared_ptr<SyncedMemory> data_ =
nullptr;
190 std::vector<int> shape_;
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.
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...