1 #ifndef CAFFE2_UTILS_MKL_CONTEXT_H_ 2 #define CAFFE2_UTILS_MKL_CONTEXT_H_ 8 #include "caffe2/core/context.h" 22 MKLContext() : random_seed_(math::randomNumberSeed()) {}
23 explicit MKLContext(
const DeviceOption& option)
25 option.has_random_seed() ? option.random_seed()
26 : math::randomNumberSeed()) {
27 CAFFE_ENFORCE_EQ(option.device_type(), MKLDNN);
32 inline void SwitchToDevice(
int stream_id = 0) {}
33 inline bool FinishDeviceComputation() {
37 inline std::mt19937& RandGenerator() {
38 if (!random_generator_.get()) {
39 random_generator_.reset(
new std::mt19937(random_seed_));
41 return *random_generator_.get();
44 inline static void* New(
size_t nbytes) {
45 return GetCPUAllocator()->New(nbytes);
47 inline static void Delete(
void* data) {
48 GetCPUAllocator()->Delete(data);
52 template <
class SrcContext,
class DstContext>
53 inline void CopyBytes(
size_t nbytes,
const void* src,
void* dst);
55 template <
typename T,
class SrcContext,
class DstContext>
56 inline void Copy(
size_t n,
const T* src, T* dst) {
57 if (std::is_fundamental<T>::value) {
58 CopyBytes<SrcContext, DstContext>(
60 static_cast<const void*>(src),
61 static_cast<void*
>(dst));
63 for (
int i = 0; i < n; ++i) {
69 template <
class SrcContext,
class DstContext>
71 CopyItems(
const TypeMeta& meta,
size_t n,
const void* src,
void* dst) {
73 meta.
copy()(src, dst, n);
75 CopyBytes<SrcContext, DstContext>(n * meta.
itemsize(), src, dst);
81 int random_seed_{1701};
82 std::unique_ptr<std::mt19937> random_generator_;
86 inline void MKLContext::CopyBytes<MKLContext, MKLContext>(
90 memcpy(dst, src, nbytes);
94 inline void MKLContext::CopyBytes<CPUContext, MKLContext>(
98 memcpy(dst, src, nbytes);
102 inline void MKLContext::CopyBytes<MKLContext, CPUContext>(
106 memcpy(dst, src, nbytes);
111 #endif // CAFFE2_UTILS_MKL_CONTEXT_H_
The MKL Context, which is largely the same as the CPUContext.
Simple registry implementation in Caffe2 that uses static variables to register object creators durin...