Scanner C++ API
op.h
1 /* Copyright 2016 Carnegie Mellon University
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #pragma once
17 
18 #include "scanner/util/common.h"
19 #include "scanner/util/profiler.h"
20 
21 #include <vector>
22 
23 namespace scanner {
24 
27 namespace internal {
28 
29 class OpBuilder;
30 
32  public:
33  OpRegistration(const OpBuilder& builder);
34 };
35 
36 class OpBuilder {
37  public:
38  friend class OpRegistration;
39 
40  OpBuilder(const std::string& name)
41  : name_(name), variadic_inputs_(false), can_stencil_(false),
42  has_bounded_state_(false), warmup_(0), has_unbounded_state_(false) {}
43 
44  OpBuilder& variadic_inputs() {
45  if (input_columns_.size() > 0) {
46  LOG(FATAL) << "Op " << name_ << " cannot have both fixed and variadic "
47  << "inputs";
48  }
49  variadic_inputs_ = true;
50  return *this;
51  }
52 
53  OpBuilder& input(const std::string& name,
54  ColumnType type = ColumnType::Bytes) {
55  if (variadic_inputs_) {
56  LOG(FATAL) << "Op " << name_ << " cannot have both fixed and variadic "
57  << "inputs";
58  }
59  input_columns_.push_back(std::make_tuple(name, type));
60  return *this;
61  }
62 
63  OpBuilder& frame_input(const std::string& name) {
64  return input(name, ColumnType::Video);
65  }
66 
67  OpBuilder& output(const std::string& name,
68  ColumnType type = ColumnType::Bytes,
69  std::string type_name = "") {
70  output_columns_.push_back(std::make_tuple(name, type, type_name));
71  return *this;
72  }
73 
74  OpBuilder& frame_output(const std::string& name) {
75  return output(name, ColumnType::Video);
76  }
77 
78  OpBuilder& stencil(const std::vector<int>& stencil = {0}) {
79  can_stencil_ = true;
80  preferred_stencil_ = stencil;
81  return *this;
82  }
83 
84  OpBuilder& bounded_state(i32 warmup = 0) {
85  if (has_unbounded_state_) {
86  LOG(FATAL) << "Attempted to specify Op " << name_
87  << " has bounded state but Op was already declared to have "
88  "unbounded state.";
89  }
90  has_bounded_state_ = true;
91  warmup_ = warmup;
92  return *this;
93  }
94 
95  OpBuilder& unbounded_state() {
96  if (has_bounded_state_) {
97  LOG(FATAL) << "Attempted to specify Op " << name_
98  << " has unbounded state but Op was already declared to have "
99  "bounded state.";
100  }
101  has_unbounded_state_ = true;
102  return *this;
103  }
104 
105  OpBuilder& protobuf_name(std::string protobuf_name) {
106  protobuf_name_ = protobuf_name;
107  return *this;
108  }
109 
110  OpBuilder& stream_protobuf_name(std::string protobuf_name) {
111  stream_protobuf_name_ = protobuf_name;
112  return *this;
113  }
114 
115  private:
116  std::string name_;
117  bool variadic_inputs_;
118  std::vector<std::tuple<std::string, ColumnType>> input_columns_;
119  std::vector<std::tuple<std::string, ColumnType, std::string>> output_columns_;
120  bool can_stencil_;
121  std::vector<int> preferred_stencil_ = {0};
122  bool has_bounded_state_;
123  i32 warmup_;
124  bool has_unbounded_state_;
125  std::string protobuf_name_;
126  std::string stream_protobuf_name_;
127 };
128 }
129 
130 #define REGISTER_OP(name__) REGISTER_OP_HELPER(__COUNTER__, name__)
131 
132 #define REGISTER_OP_HELPER(uid__, name__) REGISTER_OP_UID(uid__, name__)
133 
134 #define REGISTER_OP_UID(uid__, name__) \
135  static ::scanner::internal::OpRegistration op_registration_##uid__ \
136  __attribute__((unused)) = ::scanner::internal::OpBuilder(#name__)
137 }
Definition: op.h:36
Definition: database.cpp:36