7 #include <caffe2/core/context.h> 13 typedef std::mt19937 rand_gen_type;
17 option.has_random_seed() ? option.random_seed()
19 CAFFE_ENFORCE_EQ(option.device_type(), PROTO_IDEEP);
22 : IDEEPContext(DeviceToOption(device)) {}
24 ~IDEEPContext() noexcept
override {}
26 inline void SwitchToDevice(
int ) {}
27 using BaseContext::SwitchToDevice;
29 inline void WaitEvent(
const Event& ev) {
33 inline void Record(
Event* ev,
const char* err_msg =
nullptr)
const {
34 CAFFE_ENFORCE(ev,
"Event must not be null.");
35 ev->Record(IDEEP,
this, err_msg);
39 inline void FinishDeviceComputation() {}
41 inline rand_gen_type& RandGenerator() {
42 if (!random_generator_.get()) {
43 random_generator_.reset(
new rand_gen_type(random_seed_));
45 return *random_generator_.get();
49 return GetAllocator(CPU)->allocate(nbytes);
52 void CopyBytesSameDevice(
size_t nbytes,
const void* src,
void* dst)
override {
58 memcpy(dst, src, nbytes);
61 void CopyBytesFromCPU(
size_t nbytes,
const void* src,
void* dst)
override {
62 CopyBytesSameDevice(nbytes, src, dst);
65 void CopyBytesToCPU(
size_t nbytes,
const void* src,
void* dst)
override {
66 CopyBytesSameDevice(nbytes, src, dst);
69 bool SupportsNonFundamentalTypes()
const override {
75 template <
class SrcContext,
class DstContext>
76 inline void CopyBytes(
size_t nbytes,
const void* src,
void* dst);
78 template <
typename T,
class SrcContext,
class DstContext>
79 inline void Copy(
size_t n,
const T* src,
T* dst) {
80 if (std::is_fundamental<T>::value) {
81 CopyBytes<SrcContext, DstContext>(
83 static_cast<const void*>(src),
84 static_cast<void*
>(dst));
86 for (
size_t i = 0; i < n; ++i) {
92 template <
class SrcContext,
class DstContext>
94 CopyItems(
const TypeMeta& meta,
size_t n,
const void* src,
void* dst) {
96 meta.
copy()(src, dst, n);
98 CopyBytes<SrcContext, DstContext>(n * meta.
itemsize(), src, dst);
102 static bool HasAsyncPartDefault() {
106 static bool SupportsAsyncScheduling() {
110 static bool IsStreamFree(
const DeviceOption& ,
int ) {
118 DeviceType device_type()
const override {
122 static constexpr DeviceType GetDeviceType() {
128 int random_seed_{1701};
129 std::unique_ptr<rand_gen_type> random_generator_;
133 inline void IDEEPContext::CopyBytes<IDEEPContext, IDEEPContext>(
142 memcpy(dst, src, nbytes);
146 inline void IDEEPContext::CopyBytes<CPUContext, IDEEPContext>(
155 memcpy(dst, src, nbytes);
159 inline void IDEEPContext::CopyBytes<IDEEPContext, CPUContext>(
168 memcpy(dst, src, nbytes);
Virtual interface for the Context class in Caffe2.
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 ...