Caffe2 - C++ API
A deep learning, cross platform ML framework
context.h
1 
17 #ifndef CAFFE2_CORE_CONTEXT_H_
18 #define CAFFE2_CORE_CONTEXT_H_
19 
20 #include <cstdlib>
21 #include <ctime>
22 #include <random>
23 #include <unordered_map>
24 
25 #include "caffe2/core/allocator.h"
26 #include "caffe2/core/event.h"
27 #include "caffe2/core/logging.h"
28 #include "caffe2/core/typeid.h"
29 #include "caffe2/proto/caffe2.pb.h"
30 
31 CAFFE2_DECLARE_bool(caffe2_report_cpu_memory_usage);
32 
33 namespace caffe2 {
34 
39 uint32_t RandomNumberSeed();
40 
82 class CPUContext final {
83  public:
84  typedef std::mt19937 rand_gen_type;
85  CPUContext() : random_seed_(RandomNumberSeed()) {}
86  explicit CPUContext(const DeviceOption& option)
87  : random_seed_(
88  option.has_random_seed() ? option.random_seed()
89  : RandomNumberSeed()) {
90  CAFFE_ENFORCE_EQ(option.device_type(), CPU);
91  }
92 
93  ~CPUContext() noexcept {}
94 
95  inline void SwitchToDevice(int /*stream_id*/) {}
96  inline void SwitchToDevice() {
97  SwitchToDevice(0);
98  }
99 
100  inline void WaitEvent(const Event& ev) {
101  ev.Wait(CPU, this);
102  }
103 
104  inline void Record(Event* ev, const char* err_msg = nullptr) const {
105  CAFFE_ENFORCE(ev, "Event must not be null.");
106  ev->Record(CPU, this, err_msg);
107  }
108 
109  inline void FinishDeviceComputation() {}
110 
111  inline rand_gen_type& RandGenerator() {
112  if (!random_generator_.get()) {
113  random_generator_.reset(new rand_gen_type(random_seed_));
114  }
115  return *random_generator_.get();
116  }
117 
118  static std::pair<void*, MemoryDeleter> New(size_t nbytes) {
119  auto data_and_deleter = GetCPUAllocator()->New(nbytes);
120  if (FLAGS_caffe2_report_cpu_memory_usage) {
121  reporter_.New(data_and_deleter.first, nbytes);
122  data_and_deleter.second = ReportAndDelete;
123  }
124  return data_and_deleter;
125  }
126 
127  // Two copy functions that deals with cross-device copies.
128  template <class SrcContext, class DstContext>
129  inline void CopyBytes(size_t nbytes, const void* src, void* dst);
130 
131  template <typename T, class SrcContext, class DstContext>
132  inline void Copy(size_t n, const T* src, T* dst) {
133  if (std::is_fundamental<T>::value) {
134  CopyBytes<SrcContext, DstContext>(
135  n * sizeof(T),
136  static_cast<const void*>(src),
137  static_cast<void*>(dst));
138  } else {
139  for (int i = 0; i < n; ++i) {
140  dst[i] = src[i];
141  }
142  }
143  }
144 
145  template <class SrcContext, class DstContext>
146  inline void
147  CopyItems(const TypeMeta& meta, size_t n, const void* src, void* dst) {
148  if (meta.copy()) {
149  meta.copy()(src, dst, n);
150  } else {
151  CopyBytes<SrcContext, DstContext>(n * meta.itemsize(), src, dst);
152  }
153  }
154 
155  // By default CPU operators don't have async device parts
156  static bool HasAsyncPartDefault() {
157  return false;
158  }
159 
160  static bool SupportsAsyncScheduling() {
161  return false;
162  }
163 
164  // CPU streams are not implemented and are silently ignored by CPU ops,
165  // return true to signal executor to schedule a CPU op
166  static bool IsStreamFree(const DeviceOption& /* unused */, int /* unused */) {
167  return true;
168  }
169 
170  protected:
171  // TODO(jiayq): instead of hard-coding a generator, make it more flexible.
172  int random_seed_{1701};
173  std::unique_ptr<rand_gen_type> random_generator_;
174  CAFFE2_API static MemoryAllocationReporter reporter_;
175 
176  private:
177  static void ReportAndDelete(void* ptr) {
178  reporter_.Delete(ptr);
179  GetCPUAllocator()->GetDeleter()(ptr);
180  }
181 };
182 
183 template<>
184 inline void CPUContext::CopyBytes<CPUContext, CPUContext>(
185  size_t nbytes, const void* src, void* dst) {
186  if (nbytes == 0) {
187  return;
188  }
189  CAFFE_ENFORCE(src);
190  CAFFE_ENFORCE(dst);
191  memcpy(dst, src, nbytes);
192 }
193 
194 } // namespace caffe2
195 
196 #endif // CAFFE2_CORE_CONTEXT_H_
The CPU Context, representing the bare minimum of what a Context class in Caffe2 should implement...
Definition: context.h:82
Copyright (c) 2016-present, Facebook, Inc.
TypedCopy copy() const
Returns the typed copy function pointer for individual iterms.
Definition: typeid.h:171
TypeMeta is a thin class that allows us to store the type of a container such as a blob...
Definition: typeid.h:104
const size_t & itemsize() const
Returns the size of the item.
Definition: typeid.h:159
uint32_t RandomNumberSeed()
A function to generate a random number seed that is unique in a best-effort basis, using an ever-incrementing seed and the current time.
Definition: context.cc:26