]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - jacinto-ai/caffe-jacinto.git/blob - src/caffe/blob.cpp
minor updates
[jacinto-ai/caffe-jacinto.git] / src / caffe / blob.cpp
1 #include <cublas_v2.h>
3 #include "caffe/blob.hpp"
4 #include "caffe/common.hpp"
5 #include "caffe/syncedmem.hpp"
7 namespace caffe {
9 template <typename Dtype>
10 void Blob<Dtype>::Reshape(const int num, const int channels, const int height,
11     const int width) {
12   CHECK_GT(num, 0);
13   CHECK_GT(channels, 0);
14   CHECK_GT(height, 0);
15   CHECK_GT(width, 0);
16   num_ = num;
17   channels_ = channels;
18   height_ = height;
19   width_ = width;
20   count_ = num_ * channels_ * height_ * width_;
21   data_.reset(new SyncedMemory(count_ * sizeof(Dtype)));
22   diff_.reset(new SyncedMemory(count_ * sizeof(Dtype)));
23 }
25 template <typename Dtype>
26 Blob<Dtype>::Blob(const int num, const int channels, const int height,
27     const int width) {
28   Reshape(num, channels, height, width);
29 }
31 template <typename Dtype>
32 Blob<Dtype>::Blob(const Blob<Dtype>& source) {
33   if (source.count() == 0) {
34     Blob();
35   } else {
36     Reshape(source.num(), source.channels(), source.height(), source.width());
37     // create the synced memories.
38     data_.reset(new SyncedMemory(count_ * sizeof(Dtype)));
39     diff_.reset(new SyncedMemory(count_ * sizeof(Dtype)));
40     // Copy the data.
41     memcpy(data_->mutable_cpu_data(), source.cpu_data(), count_ * sizeof(Dtype));
42     memcpy(diff_->mutable_cpu_data(), source.cpu_diff(), count_ * sizeof(Dtype));
43   }
44 }
46 template <typename Dtype>
47 const Dtype* Blob<Dtype>::cpu_data() const {
48   CHECK(data_);
49   return (const Dtype*)data_->cpu_data();
50 }
52 template <typename Dtype>
53 const Dtype* Blob<Dtype>::gpu_data() const {
54   CHECK(data_);
55   return (const Dtype*)data_->gpu_data();
56 }
58 template <typename Dtype>
59 const Dtype* Blob<Dtype>::cpu_diff() const {
60   CHECK(diff_);
61   return (const Dtype*)diff_->cpu_data();
62 }
64 template <typename Dtype>
65 const Dtype* Blob<Dtype>::gpu_diff() const {
66   CHECK(diff_);
67   return (const Dtype*)diff_->gpu_data();
68 }
70 template <typename Dtype>
71 Dtype* Blob<Dtype>::mutable_cpu_data() {
72   CHECK(data_);
73   return (Dtype*)data_->mutable_cpu_data();
74 }
76 template <typename Dtype>
77 Dtype* Blob<Dtype>::mutable_gpu_data() {
78   CHECK(data_);
79   return (Dtype*)data_->mutable_gpu_data();
80 }
82 template <typename Dtype>
83 Dtype* Blob<Dtype>::mutable_cpu_diff() {
84   CHECK(diff_);
85   return (Dtype*)diff_->mutable_cpu_data();
86 }
88 template <typename Dtype>
89 Dtype* Blob<Dtype>::mutable_gpu_diff() {
90   CHECK(diff_);
91   return (Dtype*)diff_->mutable_gpu_data();
92 }
94 template <typename Dtype>
95 void Blob<Dtype>::Update() {
96   // not implemented yet.
97   LOG(FATAL) << "not implemented";
98 }
100 template <typename Dtype>
101 void Blob<Dtype>::FromProto(const BlobProto& proto) {
102   Reshape(proto.num(), proto.channels(), proto.height(), proto.width());
103   // copy data
104   Dtype* data_vec = mutable_cpu_data();
105   for (int i = 0; i < count_; ++i) {
106     data_vec[i] = proto.data(i);
107   }
108   Dtype* diff_vec = mutable_cpu_diff();
109   for (int i = 0; i < count_; ++i) {
110     diff_vec[i] = proto.diff(i);
111   }
114 template <typename Dtype>
115 void Blob<Dtype>::ToProto(BlobProto* proto) {
116   proto->set_num(num_);
117   proto->set_channels(channels_);
118   proto->set_height(height_);
119   proto->set_width(width_);
120   proto->clear_data();
121   proto->clear_diff();
122   const Dtype* data_vec = cpu_data();
123   for (int i = 0; i < count_; ++i) {
124     proto->add_data(data_vec[i]);
125   }
126   const Dtype* diff_vec = cpu_diff();
127   for (int i = 0; i < count_; ++i) {
128     proto->add_diff(diff_vec[i]);
129   }
132 INSTANTIATE_CLASS(Blob);
134 }  // namespace caffe