Caffe2 - C++ API
A deep learning, cross platform ML framework
context.cc
1 #include "caffe2/core/context.h"
2 #include "caffe2/core/logging.h"
3 #include "caffe2/core/tensor.h"
4 #include "caffe2/core/typeid.h"
5 
6 CAFFE2_DEFINE_bool(
7  caffe2_report_cpu_memory_usage,
8  false,
9  "If set, print out detailed memory usage");
10 
11 namespace caffe2 {
12 
13 static std::unique_ptr<CPUAllocator> g_cpu_allocator(new DefaultCPUAllocator());
14 CPUAllocator* GetCPUAllocator() {
15  return g_cpu_allocator.get();
16 }
17 
18 void SetCPUAllocator(CPUAllocator* alloc) {
19  g_cpu_allocator.reset(alloc);
20 }
21 
22 MemoryAllocationReporter CPUContext::reporter_;
23 
24 void MemoryAllocationReporter::New(void* ptr, size_t nbytes) {
25  std::lock_guard<std::mutex> guard(mutex_);
26  size_table_[ptr] = nbytes;
27  allocated_ += nbytes;
28  LOG(INFO) << "Caffe2 alloc " << nbytes << " bytes, total alloc " << allocated_
29  << " bytes.";
30 }
31 
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);
40 }
41 
42 } // namespace caffe2
Simple registry implementation in Caffe2 that uses static variables to register object creators durin...