1 #ifndef CAFFE2_OPERATORS_COUNTER_OPS_H 2 #define CAFFE2_OPERATORS_COUNTER_OPS_H 6 #include "caffe2/core/context.h" 7 #include "caffe2/core/logging.h" 8 #include "caffe2/core/operator.h" 14 explicit Counter(
T count) : count_(count) {}
30 T checkIfDone()
const {
31 return (count_.load() <= 0);
34 T reset(
T init_count) {
35 return count_.exchange(init_count);
39 std::atomic<T> count_;
44 template <
typename T,
class Context>
47 USE_OPERATOR_CONTEXT_FUNCTIONS;
48 template <
class... Args>
51 init_count_(this->
template GetSingleArgument<T>(
"init_count", 0)) {
52 CAFFE_ENFORCE_LE(0, init_count_,
"negative init_count is not permitted.");
55 bool RunOnDevice()
override {
56 *this->
template Output<std::unique_ptr<Counter<T>>>(0) =
65 template <
typename T,
class Context>
68 USE_OPERATOR_CONTEXT_FUNCTIONS;
69 template <
class... Args>
72 init_count_(this->
template GetSingleArgument<T>(
"init_count", 0)) {
73 CAFFE_ENFORCE_LE(0, init_count_,
"negative init_count is not permitted.");
76 bool RunOnDevice()
override {
77 auto& counterPtr = this->
template Input<std::unique_ptr<Counter<T>>>(0);
78 auto previous = counterPtr->reset(init_count_);
79 if (OutputSize() == 1) {
80 auto* output = Output(0);
82 *output->template mutable_data<T>() = previous;
92 template <
typename T,
class Context>
95 USE_OPERATOR_CONTEXT_FUNCTIONS;
96 template <
class... Args>
100 bool RunOnDevice()
override {
101 auto& counterPtr = this->
template Input<std::unique_ptr<Counter<T>>>(0);
102 auto* output = Output(0);
103 output->Resize(std::vector<int>{});
104 *output->template mutable_data<bool>() = counterPtr->countDown();
110 template <
typename T,
class Context>
113 USE_OPERATOR_CONTEXT_FUNCTIONS;
114 template <
class... Args>
118 bool RunOnDevice()
override {
119 auto& counterPtr = this->
template Input<std::unique_ptr<Counter<T>>>(0);
120 auto* output = Output(0);
121 output->Resize(std::vector<int>{});
122 *output->template mutable_data<bool>() = counterPtr->checkIfDone();
128 template <
typename T,
class Context>
131 USE_OPERATOR_CONTEXT_FUNCTIONS;
132 template <
class... Args>
136 bool RunOnDevice()
override {
137 auto& counterPtr = this->
template Input<std::unique_ptr<Counter<T>>>(0);
138 auto* output = Output(0);
139 output->Resize(std::vector<int>{});
140 *output->template mutable_data<T>() = counterPtr->countUp();
146 template <
typename T,
class Context>
149 USE_OPERATOR_CONTEXT_FUNCTIONS;
150 template <
class... Args>
154 bool RunOnDevice()
override {
155 auto& counterPtr = this->
template Input<std::unique_ptr<Counter<T>>>(0);
156 auto* output = Output(0);
157 output->Resize(std::vector<int>{});
158 *output->template mutable_data<T>() = counterPtr->retrieve();
164 #endif // CAFFE2_OPERATORS_COUNTER_OPS_H_
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...