Open3D (C++ API)  0.18.0
Dispatch.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - Open3D: www.open3d.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2023 www.open3d.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 #include "open3d/core/Dtype.h"
11 #include "open3d/utility/Logging.h"
12 #include "open3d/utility/MiniVec.h"
13 
14 #define INSTANTIATE_TYPES(DTYPE, DIM) \
15  using key_t = utility::MiniVec<DTYPE, DIM>; \
16  using hash_t = utility::MiniVecHash<DTYPE, DIM>; \
17  using eq_t = utility::MiniVecEq<DTYPE, DIM>;
18 
19 #define DIM_SWITCHER(DTYPE, DIM, ...) \
20  if (DIM == 1) { \
21  INSTANTIATE_TYPES(DTYPE, 1) \
22  return __VA_ARGS__(); \
23  } else if (DIM == 2) { \
24  INSTANTIATE_TYPES(DTYPE, 2) \
25  return __VA_ARGS__(); \
26  } else if (DIM == 3) { \
27  INSTANTIATE_TYPES(DTYPE, 3) \
28  return __VA_ARGS__(); \
29  } else if (DIM == 4) { \
30  INSTANTIATE_TYPES(DTYPE, 4) \
31  return __VA_ARGS__(); \
32  } else if (DIM == 5) { \
33  INSTANTIATE_TYPES(DTYPE, 5) \
34  return __VA_ARGS__(); \
35  } else if (DIM == 6) { \
36  INSTANTIATE_TYPES(DTYPE, 6) \
37  return __VA_ARGS__(); \
38  } else { \
39  utility::LogError( \
40  "Unsupported dim {}, please modify {} and compile from " \
41  "source", \
42  DIM, __FILE__); \
43  }
44 
45 // TODO: dispatch more combinations.
46 #define DISPATCH_DTYPE_AND_DIM_TO_TEMPLATE(DTYPE, DIM, ...) \
47  [&] { \
48  if (DTYPE == open3d::core::Int64) { \
49  DIM_SWITCHER(int64_t, DIM, __VA_ARGS__) \
50  } else if (DTYPE == open3d::core::Int32) { \
51  DIM_SWITCHER(int, DIM, __VA_ARGS__) \
52  } else if (DTYPE == open3d::core::Int16) { \
53  DIM_SWITCHER(short, DIM, __VA_ARGS__) \
54  } else { \
55  utility::LogError( \
56  "Unsupported dtype {}, please use integer types (Int64, " \
57  "Int32, Int16).", \
58  DTYPE.ToString()); \
59  } \
60  }()
61 
62 #ifdef __CUDACC__
63 // Reinterpret hash maps' void* value arrays as CUDA primitive types arrays, to
64 // avoid slow memcpy or byte-by-byte copy in kernels.
65 // Not used in the CPU version since memcpy is relatively fast on CPU.
66 #define DISPATCH_DIVISOR_SIZE_TO_BLOCK_T(DIVISOR, ...) \
67  [&] { \
68  if (DIVISOR == 16) { \
69  using block_t = int4; \
70  return __VA_ARGS__(); \
71  } else if (DIVISOR == 12) { \
72  using block_t = int3; \
73  return __VA_ARGS__(); \
74  } else if (DIVISOR == 8) { \
75  using block_t = int2; \
76  return __VA_ARGS__(); \
77  } else if (DIVISOR == 4) { \
78  using block_t = int; \
79  return __VA_ARGS__(); \
80  } else if (DIVISOR == 2) { \
81  using block_t = int16_t; \
82  return __VA_ARGS__(); \
83  } else { \
84  using block_t = uint8_t; \
85  return __VA_ARGS__(); \
86  } \
87  }()
88 #endif
89 
90 namespace open3d {
91 namespace utility {
92 
93 template <typename T, int N>
94 struct MiniVecHash {
95 public:
97  uint64_t hash = UINT64_C(14695981039346656037);
98 #if defined(__CUDA_ARCH__)
99 #pragma unroll
100 #endif
101  for (int i = 0; i < N; ++i) {
102  hash ^= static_cast<uint64_t>(key[i]);
103  hash *= UINT64_C(1099511628211);
104  }
105  return hash;
106  }
107 };
108 
109 template <typename T, int N>
110 struct MiniVecEq {
111 public:
113  const MiniVec<T, N>& rhs) const {
114  bool is_equal = true;
115 #if defined(__CUDA_ARCH__)
116 #pragma unroll
117 #endif
118  for (int i = 0; i < N; ++i) {
119  is_equal = is_equal && (lhs[i] == rhs[i]);
120  }
121  return is_equal;
122  }
123 };
124 
125 } // namespace utility
126 } // namespace open3d
#define OPEN3D_HOST_DEVICE
Definition: CUDAUtils.h:44
const char const char value recording_handle imu_sample recording_handle uint8_t size_t data_size k4a_record_configuration_t config target_format k4a_capture_t capture_handle k4a_imu_sample_t imu_sample uint64_t
Definition: K4aPlugin.cpp:343
Definition: PinholeCameraIntrinsic.cpp:16
Definition: Dispatch.h:110
OPEN3D_HOST_DEVICE bool operator()(const MiniVec< T, N > &lhs, const MiniVec< T, N > &rhs) const
Definition: Dispatch.h:112
Definition: Dispatch.h:94
OPEN3D_HOST_DEVICE uint64_t operator()(const MiniVec< T, N > &key) const
Definition: Dispatch.h:96
Definition: MiniVec.h:24