}
const Dtype* cpu_data() const;
+ void set_cpu_data(Dtype* data);
const Dtype* gpu_data() const;
const Dtype* cpu_diff() const;
const Dtype* gpu_diff() const;
class SyncedMemory {
public:
SyncedMemory()
- : cpu_ptr_(NULL), gpu_ptr_(NULL), size_(0), head_(UNINITIALIZED) {}
+ : cpu_ptr_(NULL), gpu_ptr_(NULL), size_(0), head_(UNINITIALIZED),
+ own_cpu_data_(false) {}
explicit SyncedMemory(size_t size)
- : cpu_ptr_(NULL), gpu_ptr_(NULL), size_(size), head_(UNINITIALIZED) {}
+ : cpu_ptr_(NULL), gpu_ptr_(NULL), size_(size), head_(UNINITIALIZED),
+ own_cpu_data_(false) {}
~SyncedMemory();
const void* cpu_data();
+ void set_cpu_data(void* data);
const void* gpu_data();
void* mutable_cpu_data();
void* mutable_gpu_data();
void* gpu_ptr_;
size_t size_;
SyncedHead head_;
+ bool own_cpu_data_;
DISABLE_COPY_AND_ASSIGN(SyncedMemory);
}; // class SyncedMemory
}
template <typename Dtype>
+void Blob<Dtype>::set_cpu_data(Dtype* data) {
+ CHECK(data);
+ data_->set_cpu_data(data);
+}
+
+template <typename Dtype>
const Dtype* Blob<Dtype>::gpu_data() const {
CHECK(data_);
return (const Dtype*)data_->gpu_data();
namespace caffe {
SyncedMemory::~SyncedMemory() {
- if (cpu_ptr_) {
+ if (cpu_ptr_ && own_cpu_data_) {
CaffeFreeHost(cpu_ptr_);
}
CaffeMallocHost(&cpu_ptr_, size_);
memset(cpu_ptr_, 0, size_);
head_ = HEAD_AT_CPU;
+ own_cpu_data_ = true;
break;
case HEAD_AT_GPU:
if (cpu_ptr_ == NULL) {
CaffeMallocHost(&cpu_ptr_, size_);
+ own_cpu_data_ = true;
}
CUDA_CHECK(cudaMemcpy(cpu_ptr_, gpu_ptr_, size_, cudaMemcpyDeviceToHost));
head_ = SYNCED;
return (const void*)cpu_ptr_;
}
+void SyncedMemory::set_cpu_data(void* data) {
+ CHECK(data);
+ if (own_cpu_data_) {
+ CaffeFreeHost(cpu_ptr_);
+ }
+ cpu_ptr_ = data;
+ head_ = HEAD_AT_CPU;
+ own_cpu_data_ = false;
+}
+
const void* SyncedMemory::gpu_data() {
to_gpu();
return (const void*)gpu_ptr_;