Caffe2 - C++ API
A deep learning, cross platform ML framework
context.cc
1 #include "caffe2/core/context.h"
2 
3 #include <atomic>
4 #if defined(_MSC_VER)
5 #include <process.h>
6 #endif
7 
8 namespace caffe2 {
9 
10 uint32_t RandomNumberSeed() {
11  // Originally copied from folly::randomNumberSeed (at 418ad4)
12  // modified to use chrono instead of sys/time.h
13  static std::atomic<uint32_t> seedInput(0);
14  auto tv = std::chrono::system_clock::now().time_since_epoch();
15  uint64_t usec = static_cast<uint64_t>(
16  std::chrono::duration_cast<std::chrono::microseconds>(tv).count());
17  uint32_t tv_sec = usec / 1000000;
18  uint32_t tv_usec = usec % 1000000;
19  const uint32_t kPrime0 = 51551;
20  const uint32_t kPrime1 = 61631;
21  const uint32_t kPrime2 = 64997;
22  const uint32_t kPrime3 = 111857;
23  return kPrime0 * (seedInput++) + kPrime1 * static_cast<uint32_t>(getpid()) +
24  kPrime2 * tv_sec + kPrime3 * tv_usec;
25 }
26 
27 namespace {
28 inline void CopyBytesImpl(size_t nbytes, const void* src, void* dst) {
29  if (nbytes == 0) {
30  return;
31  }
32  CAFFE_ENFORCE(src);
33  CAFFE_ENFORCE(dst);
34  memcpy(dst, src, nbytes);
35 }
36 
37 void CopyBytesWrapper(
38  size_t nbytes,
39  const void* src,
40  Device src_device,
41  void* dst,
42  Device dst_device) {
43  CopyBytesImpl(nbytes, src, dst);
44 }
45 } // namespace
46 
47 void CPUContext::CopyBytesSameDevice(
48  size_t nbytes,
49  const void* src,
50  void* dst) {
51  CopyBytesImpl(nbytes, src, dst);
52 }
53 
54 } // namespace caffe2
55 
56 namespace at {
57 
58 REGISTER_CONTEXT(DeviceType::CPU, caffe2::CPUContext);
59 
60 REGISTER_COPY_BYTES_FUNCTION(
61  DeviceType::CPU,
62  DeviceType::CPU,
63  caffe2::CopyBytesWrapper);
64 } // namespace at
The CPU Context, representing the bare minimum of what a Context class in Caffe2 should implement...
Definition: context.h:40
Represents a a compute device on which a tensor is located.
Definition: Device.h:30
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
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
Definition: blob.h:13
Flush-To-Zero and Denormals-Are-Zero mode.