1 // Copyright 2013 Yangqing Jia
3 #ifndef CAFFE_BLOB_HPP_
4 #define CAFFE_BLOB_HPP_
6 #include "caffe/common.hpp"
7 #include "caffe/syncedmem.hpp"
8 #include "caffe/proto/layer_param.pb.h"
10 namespace caffe {
12 template <typename Dtype>
13 class Blob {
14 public:
15 Blob()
16 : num_(0), channels_(0), height_(0), width_(0), count_(0), data_(),
17 diff_() {}
18 explicit Blob(const int num, const int channels, const int height,
19 const int width);
20 Blob(const Blob<Dtype>& source);
21 virtual ~Blob() {}
22 void Reshape(const int num, const int height,
23 const int width, const int channels);
24 inline int num() const { return num_; }
25 inline int channels() const { return channels_; }
26 inline int height() const { return height_; }
27 inline int width() const { return width_; }
28 inline int count() const {return count_; }
29 inline int offset(const int n, const int c = 0, const int h = 0,
30 const int w = 0) const {
31 return ((n * channels_ + c) * height_ + h) * width_ + w;
32 }
34 inline Dtype data_at(const int n, const int c, const int h,
35 const int w) const {
36 return *(cpu_data() + offset(n, c, h, w));
37 }
39 inline Dtype diff_at(const int n, const int c, const int h,
40 const int w) const {
41 return *(cpu_diff() + offset(n, c, h, w));
42 }
44 const Dtype* cpu_data() const;
45 const Dtype* gpu_data() const;
46 const Dtype* cpu_diff() const;
47 const Dtype* gpu_diff() const;
48 Dtype* mutable_cpu_data();
49 Dtype* mutable_gpu_data();
50 Dtype* mutable_cpu_diff();
51 Dtype* mutable_gpu_diff();
52 void Update();
53 void FromProto(const BlobProto& proto);
54 void ToProto(BlobProto* proto, bool write_diff = false);
56 private:
57 shared_ptr<SyncedMemory> data_;
58 shared_ptr<SyncedMemory> diff_;
59 int num_;
60 int channels_;
61 int height_;
62 int width_;
63 int count_;
64 }; // class Blob
66 } // namespace caffe
68 #endif // CAFFE_BLOB_HPP_