1 #ifndef CAFFE2_CORE_CONTEXT_H_ 2 #define CAFFE2_CORE_CONTEXT_H_ 7 #include <unordered_map> 9 #include "caffe2/core/logging.h" 10 #include "caffe2/core/typeid.h" 11 #include "caffe2/proto/caffe2.pb.h" 12 #include "caffe2/utils/math.h" 14 CAFFE2_DECLARE_bool(caffe2_report_cpu_memory_usage);
19 constexpr
size_t gCaffe2Alignment = 32;
25 virtual void* New(
size_t nbytes) = 0;
26 virtual void Delete(
void* data) = 0;
34 void New(
void* ptr,
size_t nbytes);
35 void Delete(
void* ptr);
39 std::unordered_map<void*, size_t> size_table_;
46 void* New(
size_t nbytes)
override {
49 data = memalign(gCaffe2Alignment, nbytes);
50 #elif defined(_MSC_VER) 51 data = _aligned_malloc(nbytes, gCaffe2Alignment);
53 CAFFE_ENFORCE_EQ(posix_memalign(&data, gCaffe2Alignment, nbytes), 0);
55 CHECK(data) <<
"Failed to allocate " << nbytes <<
" bytes.";
56 memset(data, 0, nbytes);
60 void Delete(
void* data)
override { _aligned_free(data); }
62 void Delete(
void* data)
override { free(data); }
107 CPUContext() : random_seed_(math::randomNumberSeed()) {}
108 explicit CPUContext(
const DeviceOption& option)
110 option.has_random_seed() ? option.random_seed()
111 : math::randomNumberSeed()) {
112 CAFFE_ENFORCE_EQ(option.device_type(), CPU);
117 inline void SwitchToDevice(
int stream_id) {}
118 inline void SwitchToDevice() {
122 inline bool FinishDeviceComputation() {
return true; }
124 inline std::mt19937& RandGenerator() {
125 if (!random_generator_.get()) {
126 random_generator_.reset(
new std::mt19937(random_seed_));
128 return *random_generator_.get();
131 static void* New(
size_t nbytes) {
132 void* data = GetCPUAllocator()->New(nbytes);
133 if (FLAGS_caffe2_report_cpu_memory_usage) {
134 reporter_.New(data, nbytes);
139 static void Delete(
void* data) {
140 if (FLAGS_caffe2_report_cpu_memory_usage) {
141 reporter_.Delete(data);
143 GetCPUAllocator()->Delete(data);
147 template <
class SrcContext,
class DstContext>
148 inline void CopyBytes(
size_t nbytes,
const void* src,
void* dst);
150 template <
typename T,
class SrcContext,
class DstContext>
151 inline void Copy(
size_t n,
const T* src, T* dst) {
152 if (std::is_fundamental<T>::value) {
153 CopyBytes<SrcContext, DstContext>(n *
sizeof(T),
154 static_cast<const void*>(src),
155 static_cast<void*
>(dst));
157 for (
int i = 0; i < n; ++i) {
163 template <
class SrcContext,
class DstContext>
165 CopyItems(
const TypeMeta& meta,
size_t n,
const void* src,
void* dst) {
167 meta.
copy()(src, dst, n);
169 CopyBytes<SrcContext, DstContext>(n * meta.
itemsize(), src, dst);
175 int random_seed_{1701};
176 std::unique_ptr<std::mt19937> random_generator_;
181 inline void CPUContext::CopyBytes<CPUContext, CPUContext>(
182 size_t nbytes,
const void* src,
void* dst) {
188 memcpy(dst, src, nbytes);
193 #endif // CAFFE2_CORE_CONTEXT_H_
Simple registry implementation in Caffe2 that uses static variables to register object creators durin...
The CPU Context, representing the bare minimum of what a Context class in Caffe2 should implement...