MNN  1.0
HalideRuntime.h
浏览该文件的文档.
1 #ifndef HALIDE_HALIDERUNTIME_H
2 #define HALIDE_HALIDERUNTIME_H
3 
4 #include <stddef.h>
5 #include <stdint.h>
6 #include <stdbool.h>
7 
8 #ifdef __cplusplus
9 extern "C" {
10 #endif
11 
12 // Note that you should not use "inline" along with HALIDE_ALWAYS_INLINE;
13 // it is not necessary, and may produce warnings for some build configurations.
14 #ifdef _MSC_VER
15 #define HALIDE_ALWAYS_INLINE __forceinline
16 #define HALIDE_NEVER_INLINE __declspec(noinline)
17 #else
18 #define HALIDE_ALWAYS_INLINE __attribute__((always_inline)) inline
19 #define HALIDE_NEVER_INLINE __attribute__((noinline))
20 #endif
21 
51 // Forward-declare to suppress warnings if compiling as C.
52 struct halide_buffer_t;
53 
59 typedef enum halide_type_code_t
60 {
66 
67 // Note that while __attribute__ can go before or after the declaration,
68 // __declspec apparently is only allowed before.
69 #ifndef HALIDE_ATTRIBUTE_ALIGN
70  #ifdef _MSC_VER
71  #define HALIDE_ATTRIBUTE_ALIGN(x) __declspec(align(x))
72  #else
73  #define HALIDE_ATTRIBUTE_ALIGN(x) __attribute__((aligned(x)))
74  #endif
75 #endif
76 
82 struct halide_type_t {
84 #if __cplusplus >= 201103L
85  HALIDE_ATTRIBUTE_ALIGN(1) halide_type_code_t code; // halide_type_code_t
86 #else
87  HALIDE_ATTRIBUTE_ALIGN(1) uint8_t code; // halide_type_code_t
88 #endif
89 
91  HALIDE_ATTRIBUTE_ALIGN(1) uint8_t bits;
92 
94  HALIDE_ATTRIBUTE_ALIGN(2) uint16_t lanes;
95 
96 #ifdef __cplusplus
97 
101  HALIDE_ALWAYS_INLINE halide_type_t(halide_type_code_t code, uint8_t bits, uint16_t lanes = 1)
102  : code(code), bits(bits), lanes(lanes) {
103  }
104 
107  HALIDE_ALWAYS_INLINE halide_type_t() : code((halide_type_code_t)0), bits(0), lanes(0) {}
108 
110  HALIDE_ALWAYS_INLINE bool operator==(const halide_type_t &other) const {
111  return (code == other.code &&
112  bits == other.bits &&
113  lanes == other.lanes);
114  }
115 
116  HALIDE_ALWAYS_INLINE bool operator!=(const halide_type_t &other) const {
117  return !(*this == other);
118  }
119 
121  HALIDE_ALWAYS_INLINE int bytes() const { return (bits + 7) / 8; }
122 #endif
123 };
124 
127 struct halide_device_interface_impl_t;
128 
144  int (*device_malloc)(void *user_context, struct halide_buffer_t *buf,
145  const struct halide_device_interface_t *device_interface);
146  int (*device_free)(void *user_context, struct halide_buffer_t *buf);
147  int (*device_sync)(void *user_context, struct halide_buffer_t *buf);
148  void (*device_release)(void *user_context,
149  const struct halide_device_interface_t *device_interface);
150  int (*copy_to_host)(void *user_context, struct halide_buffer_t *buf);
151  int (*copy_to_device)(void *user_context, struct halide_buffer_t *buf,
152  const struct halide_device_interface_t *device_interface);
153  int (*device_and_host_malloc)(void *user_context, struct halide_buffer_t *buf,
154  const struct halide_device_interface_t *device_interface);
155  int (*device_and_host_free)(void *user_context, struct halide_buffer_t *buf);
156  int (*buffer_copy)(void *user_context, struct halide_buffer_t *src,
157  const struct halide_device_interface_t *dst_device_interface, struct halide_buffer_t *dst);
158  int (*device_crop)(void *user_context, const struct halide_buffer_t *src,
159  struct halide_buffer_t *dst);
160  int (*device_release_crop)(void *user_context, struct halide_buffer_t *buf);
161  int (*wrap_native)(void *user_context, struct halide_buffer_t *buf, uint64_t handle,
162  const struct halide_device_interface_t *device_interface);
163  int (*detach_native)(void *user_context, struct halide_buffer_t *buf);
164  const struct halide_device_interface_impl_t *impl;
165 };
166 
167 typedef struct halide_dimension_t {
168  int32_t min, extent, stride;
169 
170  // Per-dimension flags. None are defined yet (This is reserved for future use).
171  uint32_t flags;
172 
173 #ifdef __cplusplus
175  HALIDE_ALWAYS_INLINE halide_dimension_t(int32_t m, int32_t e, int32_t s, uint32_t f = 0) :
176  min(m), extent(e), stride(s), flags(f) {}
177 
178  HALIDE_ALWAYS_INLINE bool operator==(const halide_dimension_t &other) const {
179  return (min == other.min) &&
180  (extent == other.extent) &&
181  (stride == other.stride) &&
182  (flags == other.flags);
183  }
184 
185  HALIDE_ALWAYS_INLINE bool operator!=(const halide_dimension_t &other) const {
186  return !(*this == other);
187  }
188 #endif
190 
191 #ifdef __cplusplus
192 } // extern "C"
193 #endif
194 
197 
203 typedef struct halide_buffer_t {
205  uint64_t device;
206 
209 
213  uint8_t* host;
214 
216  uint64_t flags;
217 
220 
222  int32_t dimensions;
223 
227 
229  void *padding;
231 
232 
233 #ifdef __cplusplus
234 
235 namespace {
236 template<typename T> struct check_is_pointer;
237 template<typename T> struct check_is_pointer<T *> {};
238 }
239 
241 template<typename T>
242 HALIDE_ALWAYS_INLINE halide_type_t halide_type_of() {
243  // Create a compile-time error if T is not a pointer (without
244  // using any includes - this code goes into the runtime).
245  check_is_pointer<T> check;
246  (void)check;
247  return halide_type_t(halide_type_handle, 64);
248 }
249 
250 template<>
251 HALIDE_ALWAYS_INLINE halide_type_t halide_type_of<float>() {
252  return halide_type_t(halide_type_float, 32);
253 }
254 
255 template<>
256 HALIDE_ALWAYS_INLINE halide_type_t halide_type_of<double>() {
257  return halide_type_t(halide_type_float, 64);
258 }
259 
260 template<>
261 HALIDE_ALWAYS_INLINE halide_type_t halide_type_of<bool>() {
262  return halide_type_t(halide_type_uint, 1);
263 }
264 
265 template<>
266 HALIDE_ALWAYS_INLINE halide_type_t halide_type_of<uint8_t>() {
267  return halide_type_t(halide_type_uint, 8);
268 }
269 
270 template<>
271 HALIDE_ALWAYS_INLINE halide_type_t halide_type_of<uint16_t>() {
272  return halide_type_t(halide_type_uint, 16);
273 }
274 
275 template<>
276 HALIDE_ALWAYS_INLINE halide_type_t halide_type_of<uint32_t>() {
277  return halide_type_t(halide_type_uint, 32);
278 }
279 
280 template<>
281 HALIDE_ALWAYS_INLINE halide_type_t halide_type_of<uint64_t>() {
282  return halide_type_t(halide_type_uint, 64);
283 }
284 
285 template<>
286 HALIDE_ALWAYS_INLINE halide_type_t halide_type_of<int8_t>() {
287  return halide_type_t(halide_type_int, 8);
288 }
289 
290 template<>
291 HALIDE_ALWAYS_INLINE halide_type_t halide_type_of<int16_t>() {
292  return halide_type_t(halide_type_int, 16);
293 }
294 
295 template<>
296 HALIDE_ALWAYS_INLINE halide_type_t halide_type_of<int32_t>() {
297  return halide_type_t(halide_type_int, 32);
298 }
299 
300 template<>
301 HALIDE_ALWAYS_INLINE halide_type_t halide_type_of<int64_t>() {
302  return halide_type_t(halide_type_int, 64);
303 }
304 
305 #endif
306 
307 #endif // HALIDE_HALIDERUNTIME_H
Definition: HalideRuntime.h:167
Definition: HalideRuntime.h:195
int(* device_and_host_malloc)(void *user_context, struct halide_buffer_t *buf, const struct halide_device_interface_t *device_interface)
Definition: HalideRuntime.h:153
struct halide_dimension_t halide_dimension_t
int(* detach_native)(void *user_context, struct halide_buffer_t *buf)
Definition: HalideRuntime.h:163
int(* wrap_native)(void *user_context, struct halide_buffer_t *buf, uint64_t handle, const struct halide_device_interface_t *device_interface)
Definition: HalideRuntime.h:161
struct halide_type_t type
Definition: HalideRuntime.h:219
int(* device_and_host_free)(void *user_context, struct halide_buffer_t *buf)
Definition: HalideRuntime.h:155
floating point numbers
Definition: HalideRuntime.h:63
int(* device_malloc)(void *user_context, struct halide_buffer_t *buf, const struct halide_device_interface_t *device_interface)
Definition: HalideRuntime.h:144
void(* device_release)(void *user_context, const struct halide_device_interface_t *device_interface)
Definition: HalideRuntime.h:148
int32_t extent
Definition: HalideRuntime.h:168
uint64_t flags
Definition: HalideRuntime.h:216
uint32_t flags
Definition: HalideRuntime.h:171
halide_dimension_t * dim
Definition: HalideRuntime.h:226
Definition: HalideRuntime.h:196
unsigned integers
Definition: HalideRuntime.h:62
int(* device_sync)(void *user_context, struct halide_buffer_t *buf)
Definition: HalideRuntime.h:147
int(* copy_to_device)(void *user_context, struct halide_buffer_t *buf, const struct halide_device_interface_t *device_interface)
Definition: HalideRuntime.h:151
int(* device_release_crop)(void *user_context, struct halide_buffer_t *buf)
Definition: HalideRuntime.h:160
struct halide_buffer_t halide_buffer_t
Definition: HalideRuntime.h:143
opaque pointer type (void *)
Definition: HalideRuntime.h:64
int32_t dimensions
Definition: HalideRuntime.h:222
int32_t stride
Definition: HalideRuntime.h:168
#define HALIDE_ALWAYS_INLINE
Definition: HalideRuntime.h:18
Definition: HalideRuntime.h:82
const struct halide_device_interface_t * device_interface
Definition: HalideRuntime.h:208
uint8_t * host
Definition: HalideRuntime.h:213
const struct halide_device_interface_impl_t * impl
Definition: HalideRuntime.h:164
int32_t min
Definition: HalideRuntime.h:168
void * padding
Definition: HalideRuntime.h:229
halide_type_code_t
Definition: HalideRuntime.h:59
int(* copy_to_host)(void *user_context, struct halide_buffer_t *buf)
Definition: HalideRuntime.h:150
uint64_t device
Definition: HalideRuntime.h:205
int(* buffer_copy)(void *user_context, struct halide_buffer_t *src, const struct halide_device_interface_t *dst_device_interface, struct halide_buffer_t *dst)
Definition: HalideRuntime.h:156
Definition: HalideRuntime.h:203
HALIDE_ATTRIBUTE_ALIGN(1) uint8_t code
halide_buffer_flags
Definition: HalideRuntime.h:195
int(* device_crop)(void *user_context, const struct halide_buffer_t *src, struct halide_buffer_t *dst)
Definition: HalideRuntime.h:158
signed integers
Definition: HalideRuntime.h:61
int(* device_free)(void *user_context, struct halide_buffer_t *buf)
Definition: HalideRuntime.h:146