Caffe2 - C++ API
A deep learning, cross platform ML framework
context.h
1 #ifndef CAFFE2_CORE_CONTEXT_H_
2 #define CAFFE2_CORE_CONTEXT_H_
3 
4 #include <cstdlib>
5 #include <ctime>
6 #include <random>
7 #include <unordered_map>
8 
9 #include "caffe2/core/allocator.h"
10 #include "caffe2/core/event.h"
11 #include "caffe2/core/logging.h"
12 #include "caffe2/core/typeid.h"
13 #include "caffe2/proto/caffe2.pb.h"
14 
15 CAFFE2_DECLARE_bool(caffe2_report_cpu_memory_usage);
16 
17 namespace caffe2 {
18 
23 uint32_t RandomNumberSeed();
24 
66 class CPUContext final {
67  public:
68  typedef std::mt19937 rand_gen_type;
69  CPUContext() : random_seed_(RandomNumberSeed()) {}
70  explicit CPUContext(const DeviceOption& option)
71  : random_seed_(
72  option.has_random_seed() ? option.random_seed()
73  : RandomNumberSeed()) {
74  CAFFE_ENFORCE_EQ(option.device_type(), CPU);
75  }
76 
77  ~CPUContext() noexcept {}
78 
79  inline void SwitchToDevice(int /*stream_id*/) {}
80  inline void SwitchToDevice() {
81  SwitchToDevice(0);
82  }
83 
84  inline void WaitEvent(const Event& ev) {
85  ev.Wait(CPU, this);
86  }
87 
88  inline void Record(Event* ev, const char* err_msg = nullptr) const {
89  CAFFE_ENFORCE(ev, "Event must not be null.");
90  ev->Record(CPU, this, err_msg);
91  }
92 
93  inline void FinishDeviceComputation() {}
94 
95  inline rand_gen_type& RandGenerator() {
96  if (!random_generator_.get()) {
97  random_generator_.reset(new rand_gen_type(random_seed_));
98  }
99  return *random_generator_.get();
100  }
101 
102  static std::pair<void*, MemoryDeleter> New(size_t nbytes) {
103  auto data_and_deleter = GetCPUAllocator()->New(nbytes);
104  if (FLAGS_caffe2_report_cpu_memory_usage) {
105  reporter_.New(data_and_deleter.first, nbytes);
106  data_and_deleter.second = ReportAndDelete;
107  }
108  return data_and_deleter;
109  }
110 
111  // Two copy functions that deals with cross-device copies.
112  template <class SrcContext, class DstContext>
113  inline void CopyBytes(size_t nbytes, const void* src, void* dst);
114 
115  template <typename T, class SrcContext, class DstContext>
116  inline void Copy(size_t n, const T* src, T* dst) {
117  if (std::is_fundamental<T>::value) {
118  CopyBytes<SrcContext, DstContext>(
119  n * sizeof(T),
120  static_cast<const void*>(src),
121  static_cast<void*>(dst));
122  } else {
123  for (int i = 0; i < n; ++i) {
124  dst[i] = src[i];
125  }
126  }
127  }
128 
129  template <class SrcContext, class DstContext>
130  inline void
131  CopyItems(const TypeMeta& meta, size_t n, const void* src, void* dst) {
132  if (meta.copy()) {
133  meta.copy()(src, dst, n);
134  } else {
135  CopyBytes<SrcContext, DstContext>(n * meta.itemsize(), src, dst);
136  }
137  }
138 
139  // By default CPU operators don't have async device parts
140  static bool HasAsyncPartDefault() {
141  return false;
142  }
143 
144  static bool SupportsAsyncScheduling() {
145  return false;
146  }
147 
148  // CPU streams are not implemented and are silently ignored by CPU ops,
149  // return true to signal executor to schedule a CPU op
150  static bool IsStreamFree(const DeviceOption& /* unused */, int /* unused */) {
151  return true;
152  }
153 
154  protected:
155  // TODO(jiayq): instead of hard-coding a generator, make it more flexible.
156  int random_seed_{1701};
157  std::unique_ptr<rand_gen_type> random_generator_;
158  CAFFE2_API static MemoryAllocationReporter reporter_;
159 
160  private:
161  static void ReportAndDelete(void* ptr) {
162  reporter_.Delete(ptr);
163  GetCPUAllocator()->GetDeleter()(ptr);
164  }
165 };
166 
167 template<>
168 inline void CPUContext::CopyBytes<CPUContext, CPUContext>(
169  size_t nbytes, const void* src, void* dst) {
170  if (nbytes == 0) {
171  return;
172  }
173  CAFFE_ENFORCE(src);
174  CAFFE_ENFORCE(dst);
175  memcpy(dst, src, nbytes);
176 }
177 
178 } // namespace caffe2
179 
180 #endif // CAFFE2_CORE_CONTEXT_H_
The CPU Context, representing the bare minimum of what a Context class in Caffe2 should implement...
Definition: context.h:66
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
TypedCopy copy() const
Returns the typed copy function pointer for individual iterms.
Definition: typeid.h:155
TypeMeta is a thin class that allows us to store the type of a container such as a blob...
Definition: typeid.h:88
const size_t & itemsize() const
Returns the size of the item.
Definition: typeid.h:143
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:10