1 #include "caffe2/core/context.h" 2 #include "caffe2/core/logging.h" 3 #include "caffe2/core/tensor.h" 4 #include "caffe2/core/typeid.h" 7 caffe2_report_cpu_memory_usage,
9 "If set, print out detailed memory usage");
13 static std::unique_ptr<CPUAllocator> g_cpu_allocator(
new DefaultCPUAllocator());
14 CPUAllocator* GetCPUAllocator() {
15 return g_cpu_allocator.get();
18 void SetCPUAllocator(CPUAllocator* alloc) {
19 g_cpu_allocator.reset(alloc);
22 MemoryAllocationReporter CPUContext::reporter_;
24 void MemoryAllocationReporter::New(
void* ptr,
size_t nbytes) {
25 std::lock_guard<std::mutex> guard(mutex_);
26 size_table_[ptr] = nbytes;
28 LOG(INFO) <<
"Caffe2 alloc " << nbytes <<
" bytes, total alloc " << allocated_
32 void MemoryAllocationReporter::Delete(
void* ptr) {
33 std::lock_guard<std::mutex> guard(mutex_);
34 auto it = size_table_.find(ptr);
35 CHECK(it != size_table_.end());
36 allocated_ -= it->second;
37 LOG(INFO) <<
"Caffe2 deleted " << it->second <<
" bytes, total alloc " 38 << allocated_ <<
" bytes.";
39 size_table_.erase(it);
Simple registry implementation in Caffe2 that uses static variables to register object creators durin...