1 #ifndef CAFFE2_CORE_CONTEXT_H_     2 #define CAFFE2_CORE_CONTEXT_H_     7 #include <unordered_map>     9 #include "caffe2/core/allocator.h"    10 #include "caffe2/core/context_base.h"    11 #include "caffe2/core/event.h"    12 #include "caffe2/core/logging.h"    13 #include <c10/util/typeid.h>    14 #include "caffe2/proto/caffe2_pb.h"    16 #include <c10/util/ArrayRef.h>    18 C10_DECLARE_bool(caffe2_report_cpu_memory_usage);
    42   typedef std::mt19937 rand_gen_type;
    44   explicit CPUContext(
const DeviceOption& option)
    46             option.has_random_seed() ? option.random_seed()
    48     CAFFE_ENFORCE_EQ(option.device_type(), PROTO_CPU);
    51       : CPUContext(DeviceToOption(device)) {}
    53   ~CPUContext() noexcept 
override {}
    55   inline void SwitchToDevice(
int )
 override {}
    57   using BaseContext::SwitchToDevice;
    59   inline void WaitEvent(
const Event& ev)
 override {
    63   inline void Record(
Event* ev, 
const char* err_msg = 
nullptr)
 const override {
    64     CAFFE_ENFORCE(ev, 
"Event must not be null.");
    65     ev->Record(CPU, 
this, err_msg);
    68   inline void FinishDeviceComputation()
 override {}
    70   inline rand_gen_type& RandGenerator() {
    71     if (!random_generator_.get()) {
    72       random_generator_.reset(
new rand_gen_type(random_seed_));
    74     return *random_generator_.get();
    78     return GetCPUAllocator()->allocate(nbytes);
    81   void CopyBytesSameDevice(
size_t nbytes, 
const void* src, 
void* dst) 
override;
    83   void CopyBytesFromCPU(
size_t nbytes, 
const void* src, 
void* dst)
 override {
    84     CopyBytesSameDevice(nbytes, src, dst);
    87   void CopyBytesToCPU(
size_t nbytes, 
const void* src, 
void* dst)
 override {
    88     CopyBytesSameDevice(nbytes, src, dst);
    91   bool SupportsNonFundamentalTypes()
 const override {
    96   template <
class SrcContext, 
class DstContext>
    97   inline void CopyBytes(
size_t nbytes, 
const void* src, 
void* dst);
    99   template <
typename T, 
class SrcContext, 
class DstContext>
   100   inline void Copy(
size_t n, 
const T* src, 
T* dst) {
   101     if (std::is_fundamental<T>::value) {
   102       CopyBytes<SrcContext, DstContext>(
   104           static_cast<const void*>(src),
   105           static_cast<void*
>(dst));
   107       for (
size_t i = 0; i < n; ++i) {
   113   template <
class SrcContext, 
class DstContext>
   115   CopyItems(
const TypeMeta& meta, 
size_t n, 
const void* src, 
void* dst) {
   117       meta.
copy()(src, dst, n);
   119       CopyBytes<SrcContext, DstContext>(n * meta.
itemsize(), src, dst);
   124   static bool HasAsyncPartDefault() {
   128   static bool SupportsAsyncScheduling() {
   134   static bool IsStreamFree(
   135       const DeviceOption& ,
   145   DeviceType device_type()
 const override {
   149   static constexpr DeviceType GetDeviceType() {
   155   int random_seed_{1701};
   156   std::unique_ptr<rand_gen_type> random_generator_;
   160 inline void CPUContext::CopyBytes<CPUContext, CPUContext>(
   169   memcpy(dst, src, nbytes);
   174 #endif  // CAFFE2_CORE_CONTEXT_H_ 
The CPU Context, representing the bare minimum of what a Context class in Caffe2 should implement...
 
Represents a a compute device on which a tensor is located. 
 
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. 
 
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...