// Copyright 2014 BVLC and contributors. #include "caffe/blob.hpp" #include "caffe/common.hpp" #include "caffe/syncedmem.hpp" #include "caffe/util/math_functions.hpp" namespace caffe { template void Blob::Reshape(const int num, const int channels, const int height, const int width) { CHECK_GE(num, 0); CHECK_GE(channels, 0); CHECK_GE(height, 0); CHECK_GE(width, 0); num_ = num; channels_ = channels; height_ = height; width_ = width; count_ = num_ * channels_ * height_ * width_; if (count_) { data_.reset(new SyncedMemory(count_ * sizeof(Dtype))); diff_.reset(new SyncedMemory(count_ * sizeof(Dtype))); } else { data_.reset(reinterpret_cast(NULL)); diff_.reset(reinterpret_cast(NULL)); } } template void Blob::ReshapeLike(const Blob& other) { Reshape(other.num(), other.channels(), other.height(), other.width()); } template Blob::Blob(const int num, const int channels, const int height, const int width) { Reshape(num, channels, height, width); } template const Dtype* Blob::cpu_data() const { CHECK(data_); return (const Dtype*)data_->cpu_data(); } template void Blob::set_cpu_data(Dtype* data) { CHECK(data); data_->set_cpu_data(data); } template const Dtype* Blob::gpu_data() const { CHECK(data_); return (const Dtype*)data_->gpu_data(); } template const Dtype* Blob::cpu_diff() const { CHECK(diff_); return (const Dtype*)diff_->cpu_data(); } template const Dtype* Blob::gpu_diff() const { CHECK(diff_); return (const Dtype*)diff_->gpu_data(); } template Dtype* Blob::mutable_cpu_data() { CHECK(data_); return static_cast(data_->mutable_cpu_data()); } template Dtype* Blob::mutable_gpu_data() { CHECK(data_); return static_cast(data_->mutable_gpu_data()); } template Dtype* Blob::mutable_cpu_diff() { CHECK(diff_); return static_cast(diff_->mutable_cpu_data()); } template Dtype* Blob::mutable_gpu_diff() { CHECK(diff_); return static_cast(diff_->mutable_gpu_data()); } template void Blob::ShareData(const Blob& other) { CHECK_EQ(count_, other.count()); data_ = other.data(); } template void Blob::ShareDiff(const Blob& other) { CHECK_EQ(count_, other.count()); diff_ = other.diff(); } // The "update" method is used for parameter blobs in a Net, which are stored // as Blob or Blob -- hence we do not define it for // Blob or Blob. template <> void Blob::Update() { NOT_IMPLEMENTED; } template <> void Blob::Update() { NOT_IMPLEMENTED; } template void Blob::Update() { // We will perform update based on where the data is located. switch (data_->head()) { case SyncedMemory::HEAD_AT_CPU: // perform computation on CPU caffe_axpy(count_, Dtype(-1), static_cast(diff_->cpu_data()), static_cast(data_->mutable_cpu_data())); break; case SyncedMemory::HEAD_AT_GPU: case SyncedMemory::SYNCED: #ifndef CPU_ONLY // perform computation on GPU caffe_gpu_axpy(count_, Dtype(-1), static_cast(diff_->gpu_data()), static_cast(data_->mutable_gpu_data())); #else NO_GPU; #endif break; default: LOG(FATAL) << "Syncedmem not initialized."; } } template void Blob::CopyFrom(const Blob& source, bool copy_diff, bool reshape) { if (num_ != source.num() || channels_ != source.channels() || height_ != source.height() || width_ != source.width()) { if (reshape) { Reshape(source.num(), source.channels(), source.height(), source.width()); } else { LOG(FATAL) << "Trying to copy blobs of different sizes."; } } switch (Caffe::mode()) { case Caffe::GPU: if (copy_diff) { caffe_copy(count_, source.gpu_diff(), static_cast(diff_->mutable_gpu_data())); } else { caffe_copy(count_, source.gpu_data(), static_cast(data_->mutable_gpu_data())); } break; case Caffe::CPU: if (copy_diff) { caffe_copy(count_, source.cpu_diff(), static_cast(diff_->mutable_cpu_data())); } else { caffe_copy(count_, source.cpu_data(), static_cast(data_->mutable_cpu_data())); } break; default: LOG(FATAL) << "Unknown caffe mode."; } } template void Blob::FromProto(const BlobProto& proto) { Reshape(proto.num(), proto.channels(), proto.height(), proto.width()); // copy data Dtype* data_vec = mutable_cpu_data(); for (int i = 0; i < count_; ++i) { data_vec[i] = proto.data(i); } if (proto.diff_size() > 0) { Dtype* diff_vec = mutable_cpu_diff(); for (int i = 0; i < count_; ++i) { diff_vec[i] = proto.diff(i); } } } template void Blob::ToProto(BlobProto* proto, bool write_diff) const { proto->set_num(num_); proto->set_channels(channels_); proto->set_height(height_); proto->set_width(width_); proto->clear_data(); proto->clear_diff(); const Dtype* data_vec = cpu_data(); for (int i = 0; i < count_; ++i) { proto->add_data(data_vec[i]); } if (write_diff) { const Dtype* diff_vec = cpu_diff(); for (int i = 0; i < count_; ++i) { proto->add_diff(diff_vec[i]); } } } INSTANTIATE_CLASS(Blob); template class Blob; template class Blob; } // namespace caffe