Caffe2 - C++ API
A deep learning, cross platform ML framework
atomic_ops.cc
1 #include <mutex>
2 #include "caffe2/core/context.h"
3 #include "caffe2/core/operator.h"
4 
5 namespace caffe2 {
6 namespace fb {
7 namespace {
8 
9 class CreateMutexOp final : public Operator<CPUContext> {
10  public:
11  CreateMutexOp(const OperatorDef& operator_def, Workspace* ws)
12  : Operator<CPUContext>(operator_def, ws) {}
13 
14  bool RunOnDevice() override {
15  *OperatorBase::Output<std::unique_ptr<std::mutex>>(0) =
16  std::unique_ptr<std::mutex>(new std::mutex);
17  return true;
18  }
19 };
20 
21 class AtomicFetchAddOp final : public Operator<CPUContext> {
22  public:
23  AtomicFetchAddOp(const OperatorDef& operator_def, Workspace* ws)
24  : Operator<CPUContext>(operator_def, ws) {}
25 
26  bool RunOnDevice() override {
27  auto& mutex = OperatorBase::Input<std::unique_ptr<std::mutex>>(0);
28  auto& a = Input(1);
29  auto& b = Input(2);
30  auto* c = Output(0);
31  auto* d = Output(1);
32  c->Resize(std::vector<TIndex>());
33  d->Resize(std::vector<TIndex>());
34  auto* aPtr = a.data<int32_t>();
35  auto* bPtr = b.data<int32_t>();
36  auto* cPtr = c->mutable_data<int32_t>();
37  auto* dPtr = d->mutable_data<int32_t>();
38  std::lock_guard<std::mutex> lg(*mutex);
39  *dPtr = *aPtr;
40  *cPtr = *aPtr + *bPtr;
41  return true;
42  }
43 };
44 
45 class CreateAtomicBoolOp final : public Operator<CPUContext> {
46  public:
47  using Operator::Operator;
48 
49  bool RunOnDevice() override {
50  *OperatorBase::Output<std::unique_ptr<std::atomic<bool>>>(0) =
51  std::unique_ptr<std::atomic<bool>>(new std::atomic<bool>(false));
52  return true;
53  }
54 };
55 
56 class ConditionalSetAtomicBoolOp final : public Operator<CPUContext> {
57  public:
58  using Operator::Operator;
59 
60  bool RunOnDevice() override {
61  auto& ptr =
62  OperatorBase::Input<std::unique_ptr<std::atomic<bool>>>(ATOMIC_BOOL);
63  if (Input(CONDITION).data<bool>()[0]) {
64  ptr->store(true);
65  }
66  return true;
67  }
68 
69  private:
70  INPUT_TAGS(ATOMIC_BOOL, CONDITION);
71 };
72 
73 class CheckAtomicBoolOp final : public Operator<CPUContext> {
74  public:
75  using Operator::Operator;
76 
77  bool RunOnDevice() override {
78  auto& ptr = OperatorBase::Input<std::unique_ptr<std::atomic<bool>>>(0);
79  Output(0)->Resize(1);
80  *Output(0)->mutable_data<bool>() = ptr->load();
81  return true;
82  }
83 };
84 
85 REGISTER_CPU_OPERATOR(CreateMutex, CreateMutexOp);
86 REGISTER_CPU_OPERATOR(AtomicFetchAdd, AtomicFetchAddOp);
87 
88 REGISTER_CPU_OPERATOR(CreateAtomicBool, CreateAtomicBoolOp);
89 REGISTER_CPU_OPERATOR(ConditionalSetAtomicBool, ConditionalSetAtomicBoolOp);
90 REGISTER_CPU_OPERATOR(CheckAtomicBool, CheckAtomicBoolOp);
91 
92 OPERATOR_SCHEMA(CreateMutex)
93  .NumInputs(0)
94  .NumOutputs(1)
95  .SetDoc("Creates an unlocked mutex and returns it in a unique_ptr blob.")
96  .Output(0, "mutex_ptr", "Blob containing a std::unique_ptr<mutex>.");
97 
98 OPERATOR_SCHEMA(AtomicFetchAdd)
99  .NumInputs(3)
100  .NumOutputs(2)
101  .SetDoc(R"DOC(
102 Given a mutex and two int32 scalar tensors, performs an atomic fetch add
103 by mutating the first argument and adding it to the second input
104 argument. Returns the updated integer and the value prior to the update.
105 )DOC")
106  .Input(0, "mutex_ptr", "Blob containing to a unique_ptr<mutex>")
107  .Input(1, "mut_value", "Value to be mutated after the sum.")
108  .Input(2, "increment", "Value to add to the first operand.")
109  .Output(0, "mut_value", "Mutated value after sum. Usually same as input 1.")
110  .Output(1, "fetched_value", "Value of the first operand before sum.")
111  .AllowInplace({{1, 0}});
112 
113 OPERATOR_SCHEMA(CreateAtomicBool)
114  .NumInputs(0)
115  .NumOutputs(1)
116  .SetDoc("Create an unique_ptr blob to hold an atomic<bool>")
117  .Output(0, "atomic_bool", "Blob containing a unique_ptr<atomic<bool>>");
118 
119 OPERATOR_SCHEMA(ConditionalSetAtomicBool)
120  .NumInputs(2)
121  .NumOutputs(0)
122  .SetDoc(R"DOC(
123 Set an atomic<bool> to true if the given condition bool variable is true
124  )DOC")
125  .Input(0, "atomic_bool", "Blob containing a unique_ptr<atomic<bool>>")
126  .Input(1, "condition", "Blob containing a bool");
127 
128 OPERATOR_SCHEMA(CheckAtomicBool)
129  .NumInputs(1)
130  .NumOutputs(1)
131  .SetDoc("Copy the value of an atomic<bool> to a bool")
132  .Input(0, "atomic_bool", "Blob containing a unique_ptr<atomic<bool>>")
133  .Output(0, "value", "Copy of the value for the atomic<bool>");
134 
135 SHOULD_NOT_DO_GRADIENT(CreateMutex);
136 SHOULD_NOT_DO_GRADIENT(AtomicFetchAdd);
137 SHOULD_NOT_DO_GRADIENT(CreateAtomicBool);
138 SHOULD_NOT_DO_GRADIENT(ConditionalSetAtomicBool);
139 SHOULD_NOT_DO_GRADIENT(CheckAtomicBool);
140 }
141 }
142 }
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...