Scanner C++ API
common.h
1 /* Copyright 2016 Carnegie Mellon University, NVIDIA Corporation
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 "glog/logging.h"
19 #include "scanner/engine/rpc.pb.h"
20 #include "scanner/metadata.pb.h"
21 #include "scanner/types.pb.h"
22 
23 #include <cstdint>
24 #include <string>
25 #include <vector>
26 
27 namespace scanner {
28 
31 using u8 = uint8_t;
32 using u16 = uint16_t;
33 using u32 = uint32_t;
34 using u64 = uint64_t;
35 using i8 = int8_t;
36 using i16 = int16_t;
37 using i32 = int32_t;
38 using i64 = int64_t;
39 using f32 = float;
40 using f64 = double;
41 
42 using proto::DeviceType;
43 using proto::ImageEncodingType;
44 using proto::ImageColorSpace;
45 using proto::ColumnType;
46 using proto::LoadWorkEntry;
47 using proto::Column;
48 using proto::MemoryPoolConfig;
49 using proto::BoundingBox;
50 using proto::Point;
51 using proto::Result;
52 
53 struct DeviceHandle {
54  DeviceHandle(DeviceType type_, i32 id_) : type(type_), id(id_) {}
55  DeviceHandle() = default;
56  DeviceHandle(const DeviceHandle&) = default;
57  DeviceHandle& operator=(const DeviceHandle&) = default;
58 
59  bool operator==(const DeviceHandle& other) {
60  return type == other.type && id == other.id;
61  }
62 
63  bool operator!=(const DeviceHandle& other) { return !(*this == other); }
64 
65  bool operator<(const DeviceHandle& other) const {
66  return type < other.type && id < other.id;
67  }
68 
69  bool can_copy_to(const DeviceHandle& other) {
70  return !(this->type == DeviceType::GPU && other.type == DeviceType::GPU &&
71  this->id != other.id);
72  }
73 
74  bool is_same_address_space(const DeviceHandle& other) {
75  return this->type == other.type &&
76  ((this->type == DeviceType::CPU) ||
77  (this->type == DeviceType::GPU && this->id == other.id));
78  }
79 
80  DeviceType type;
81  i32 id;
82 };
83 
84 std::ostream& operator<<(std::ostream& os, const DeviceHandle& handle);
85 
86 static const DeviceHandle CPU_DEVICE = {DeviceType::CPU, 0};
87 
88 struct Interval {
89  i32 start;
90  i32 end;
91 };
92 
94  public:
95  StridedInterval() = default;
96  StridedInterval(i32 start, i32 end, i32 stride = 1);
97  StridedInterval(const Interval&);
98 
99  i32 start;
100  i32 end;
101  i32 stride = 1;
102 };
103 
104 bool string_to_image_encoding_type(const std::string& s,
105  proto::ImageEncodingType& t);
106 std::string image_encoding_type_to_string(proto::ImageEncodingType d);
107 
108 #define RESULT_ERROR(result__, str__, ...) \
109  { \
110  char errstr__[1024]; \
111  snprintf(errstr__, 1024, str__, ##__VA_ARGS__); \
112  LOG(ERROR) << errstr__; \
113  (result__)->set_success(false); \
114  (result__)->set_msg(errstr__); \
115  }
116 
119 extern i32 NUM_CUDA_STREAMS; // # of cuda streams for image processing
120 }
Definition: common.h:88
Definition: common.h:53
Definition: database.cpp:36
Definition: common.h:93