|
| 1 | +#include <c10/core/CPUAllocator.h> |
| 2 | +#include <c10/util/typeid.h> |
| 3 | +#include <c10/core/DeviceType.h> |
| 4 | + |
| 5 | +// TODO: rename flags to C10 |
| 6 | +C10_DEFINE_bool( |
| 7 | + caffe2_report_cpu_memory_usage, |
| 8 | + false, |
| 9 | + "If set, print out detailed memory usage"); |
| 10 | + |
| 11 | +C10_DEFINE_bool( |
| 12 | + caffe2_cpu_allocator_do_zero_fill, |
| 13 | + false, |
| 14 | + "If set, do memory zerofilling when allocating on CPU"); |
| 15 | + |
| 16 | +C10_DEFINE_bool( |
| 17 | + caffe2_cpu_allocator_do_junk_fill, |
| 18 | + false, |
| 19 | + "If set, fill memory with deterministic junk when allocating on CPU"); |
| 20 | + |
| 21 | +namespace c10 { |
| 22 | + |
| 23 | +void memset_junk(void* data, size_t num) { |
| 24 | + // This garbage pattern is NaN when interpreted as floating point values, |
| 25 | + // or as very large integer values. |
| 26 | + static constexpr int32_t kJunkPattern = 0x7fedbeef; |
| 27 | + static constexpr int64_t kJunkPattern64 = |
| 28 | + static_cast<int64_t>(kJunkPattern) << 32 | kJunkPattern; |
| 29 | + int32_t int64_count = num / sizeof(kJunkPattern64); |
| 30 | + int32_t remaining_bytes = num % sizeof(kJunkPattern64); |
| 31 | + int64_t* data_i64 = reinterpret_cast<int64_t*>(data); |
| 32 | + for (int i = 0; i < int64_count; i++) { |
| 33 | + data_i64[i] = kJunkPattern64; |
| 34 | + } |
| 35 | + if (remaining_bytes > 0) { |
| 36 | + memcpy(data_i64 + int64_count, &kJunkPattern64, remaining_bytes); |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +void* alloc_cpu(size_t nbytes) { |
| 41 | + if (nbytes == 0) { |
| 42 | + return nullptr; |
| 43 | + } |
| 44 | + |
| 45 | + void* data; |
| 46 | +#ifdef __ANDROID__ |
| 47 | + data = memalign(gAlignment, nbytes); |
| 48 | +#elif defined(_MSC_VER) |
| 49 | + data = _aligned_malloc(nbytes, gAlignment); |
| 50 | +#else |
| 51 | + CAFFE_ENFORCE_EQ(posix_memalign(&data, gAlignment, nbytes), 0); |
| 52 | +#endif |
| 53 | + |
| 54 | + CAFFE_ENFORCE( |
| 55 | + data, |
| 56 | + "DefaultCPUAllocator: not enough memory: you tried to allocate %dGB. Buy new RAM!", |
| 57 | + nbytes / 1073741824); |
| 58 | + |
| 59 | + // move data to a thread's NUMA node |
| 60 | + NUMAMove(data, nbytes, GetCurrentNUMANode()); |
| 61 | + CHECK( |
| 62 | + !FLAGS_caffe2_cpu_allocator_do_zero_fill || |
| 63 | + !FLAGS_caffe2_cpu_allocator_do_junk_fill) |
| 64 | + << "Cannot request both zero-fill and junk-fill at the same time"; |
| 65 | + if (FLAGS_caffe2_cpu_allocator_do_zero_fill) { |
| 66 | + memset(data, 0, nbytes); |
| 67 | + } else if (FLAGS_caffe2_cpu_allocator_do_junk_fill) { |
| 68 | + memset_junk(data, nbytes); |
| 69 | + } |
| 70 | + |
| 71 | + return data; |
| 72 | +} |
| 73 | + |
| 74 | +// A virtual struct that is used to report C10's memory allocation and |
| 75 | +// deallocation status |
| 76 | +class C10_API MemoryAllocationReporter { |
| 77 | + public: |
| 78 | + MemoryAllocationReporter() : allocated_(0) {} |
| 79 | + void New(void* ptr, size_t nbytes); |
| 80 | + void Delete(void* ptr); |
| 81 | + |
| 82 | + private: |
| 83 | + std::mutex mutex_; |
| 84 | + std::unordered_map<void*, size_t> size_table_; |
| 85 | + size_t allocated_; |
| 86 | +}; |
| 87 | + |
| 88 | +struct C10_API DefaultCPUAllocator final : at::Allocator { |
| 89 | + DefaultCPUAllocator() {} |
| 90 | + ~DefaultCPUAllocator() override {} |
| 91 | + at::DataPtr allocate(size_t nbytes) const override { |
| 92 | + void* data = alloc_cpu(nbytes); |
| 93 | + if (FLAGS_caffe2_report_cpu_memory_usage && nbytes > 0) { |
| 94 | + getMemoryAllocationReporter().New(data, nbytes); |
| 95 | + return {data, data, &ReportAndDelete, at::Device(at::DeviceType::CPU)}; |
| 96 | + } |
| 97 | + return {data, data, &Delete, at::Device(at::DeviceType::CPU)}; |
| 98 | + } |
| 99 | + |
| 100 | +#ifdef _MSC_VER |
| 101 | + static void Delete(void* data) { |
| 102 | + _aligned_free(data); |
| 103 | + } |
| 104 | +#else |
| 105 | + static void Delete(void* data) { |
| 106 | + free(data); |
| 107 | + } |
| 108 | +#endif |
| 109 | + |
| 110 | + static void ReportAndDelete(void* ptr) { |
| 111 | + if (!ptr) { |
| 112 | + return; |
| 113 | + } |
| 114 | + getMemoryAllocationReporter().Delete(ptr); |
| 115 | + Delete(ptr); |
| 116 | + } |
| 117 | + |
| 118 | + at::DeleterFnPtr raw_deleter() const override { |
| 119 | + if (FLAGS_caffe2_report_cpu_memory_usage) { |
| 120 | + return &ReportAndDelete; |
| 121 | + } |
| 122 | + return &Delete; |
| 123 | + } |
| 124 | + |
| 125 | + protected: |
| 126 | + static MemoryAllocationReporter& getMemoryAllocationReporter() { |
| 127 | + static MemoryAllocationReporter reporter_; |
| 128 | + return reporter_; |
| 129 | + } |
| 130 | + |
| 131 | +}; |
| 132 | + |
| 133 | +void NoDelete(void*) {} |
| 134 | + |
| 135 | +at::Allocator* GetCPUAllocator() { |
| 136 | + return GetAllocator(DeviceType::CPU); |
| 137 | +} |
| 138 | + |
| 139 | +void SetCPUAllocator(at::Allocator* alloc) { |
| 140 | + SetAllocator(DeviceType::CPU, alloc); |
| 141 | +} |
| 142 | + |
| 143 | +// Global default CPU Allocator |
| 144 | +static DefaultCPUAllocator g_cpu_alloc; |
| 145 | + |
| 146 | +at::Allocator* GetDefaultCPUAllocator() { |
| 147 | + return &g_cpu_alloc; |
| 148 | +} |
| 149 | + |
| 150 | +REGISTER_ALLOCATOR(DeviceType::CPU, &g_cpu_alloc); |
| 151 | + |
| 152 | +void MemoryAllocationReporter::New(void* ptr, size_t nbytes) { |
| 153 | + std::lock_guard<std::mutex> guard(mutex_); |
| 154 | + size_table_[ptr] = nbytes; |
| 155 | + allocated_ += nbytes; |
| 156 | + LOG(INFO) << "C10 alloc " << nbytes << " bytes, total alloc " << allocated_ |
| 157 | + << " bytes."; |
| 158 | +} |
| 159 | + |
| 160 | +void MemoryAllocationReporter::Delete(void* ptr) { |
| 161 | + std::lock_guard<std::mutex> guard(mutex_); |
| 162 | + auto it = size_table_.find(ptr); |
| 163 | + CHECK(it != size_table_.end()); |
| 164 | + allocated_ -= it->second; |
| 165 | + LOG(INFO) << "C10 deleted " << it->second << " bytes, total alloc " |
| 166 | + << allocated_ << " bytes."; |
| 167 | + size_table_.erase(it); |
| 168 | +} |
| 169 | + |
| 170 | +} // namespace c10 |
0 commit comments