Caffe2 - C++ API
A deep learning, cross platform ML framework
allocator.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 CAFFE2_DEFINE_bool(
12  caffe2_cpu_allocator_do_zero_fill,
13  true,
14  "If set, do memory zerofilling when allocating on CPU");
15 
16 namespace caffe2 {
17 
18 void NoDelete(void*) {}
19 
20 static std::unique_ptr<CPUAllocator> g_cpu_allocator(new DefaultCPUAllocator());
21 CPUAllocator* GetCPUAllocator() {
22  return g_cpu_allocator.get();
23 }
24 
25 void SetCPUAllocator(CPUAllocator* alloc) {
26  g_cpu_allocator.reset(alloc);
27 }
28 
29 MemoryAllocationReporter CPUContext::reporter_;
30 
31 void MemoryAllocationReporter::New(void* ptr, size_t nbytes) {
32  std::lock_guard<std::mutex> guard(mutex_);
33  size_table_[ptr] = nbytes;
34  allocated_ += nbytes;
35  LOG(INFO) << "Caffe2 alloc " << nbytes << " bytes, total alloc " << allocated_
36  << " bytes.";
37 }
38 
39 void MemoryAllocationReporter::Delete(void* ptr) {
40  std::lock_guard<std::mutex> guard(mutex_);
41  auto it = size_table_.find(ptr);
42  CHECK(it != size_table_.end());
43  allocated_ -= it->second;
44  LOG(INFO) << "Caffe2 deleted " << it->second << " bytes, total alloc "
45  << allocated_ << " bytes.";
46  size_table_.erase(it);
47 }
48 
49 } // namespace caffe2
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...