Loading [MathJax]/extensions/TeX/AMSsymbols.js
Open3D (C++ API)  0.14.1
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Dispatch.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - Open3D: www.open3d.org -
3 // ----------------------------------------------------------------------------
4 // The MIT License (MIT)
5 //
6 // Copyright (c) 2018-2021 www.open3d.org
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 // IN THE SOFTWARE.
25 // ----------------------------------------------------------------------------
26 
27 #pragma once
28 
29 #include "open3d/core/Dtype.h"
30 #include "open3d/utility/Logging.h"
31 #include "open3d/utility/MiniVec.h"
32 
33 #define INSTANTIATE_TYPES(DTYPE, DIM) \
34  using key_t = utility::MiniVec<DTYPE, DIM>; \
35  using hash_t = utility::MiniVecHash<DTYPE, DIM>; \
36  using eq_t = utility::MiniVecEq<DTYPE, DIM>;
37 
38 #define DIM_SWITCHER(DTYPE, DIM, ...) \
39  if (DIM == 1) { \
40  INSTANTIATE_TYPES(DTYPE, 1) \
41  return __VA_ARGS__(); \
42  } else if (DIM == 2) { \
43  INSTANTIATE_TYPES(DTYPE, 2) \
44  return __VA_ARGS__(); \
45  } else if (DIM == 3) { \
46  INSTANTIATE_TYPES(DTYPE, 3) \
47  return __VA_ARGS__(); \
48  } else if (DIM == 4) { \
49  INSTANTIATE_TYPES(DTYPE, 4) \
50  return __VA_ARGS__(); \
51  } else if (DIM == 5) { \
52  INSTANTIATE_TYPES(DTYPE, 5) \
53  return __VA_ARGS__(); \
54  } else if (DIM == 6) { \
55  INSTANTIATE_TYPES(DTYPE, 6) \
56  return __VA_ARGS__(); \
57  } else { \
58  utility::LogError( \
59  "Unsupported dim {}, please modify {} and compile from " \
60  "source", \
61  DIM, __FILE__); \
62  }
63 
64 // TODO: dispatch more combinations.
65 #define DISPATCH_DTYPE_AND_DIM_TO_TEMPLATE(DTYPE, DIM, ...) \
66  [&] { \
67  if (DTYPE == open3d::core::Int64) { \
68  DIM_SWITCHER(int64_t, DIM, __VA_ARGS__) \
69  } else if (DTYPE == open3d::core::Int32) { \
70  DIM_SWITCHER(int, DIM, __VA_ARGS__) \
71  } else if (DTYPE == open3d::core::Int16) { \
72  DIM_SWITCHER(short, DIM, __VA_ARGS__) \
73  } else { \
74  utility::LogError( \
75  "Unsupported dtype {}, please use integer types (Int64, " \
76  "Int32, Int16).", \
77  DTYPE.ToString()); \
78  } \
79  }()
80 
81 #ifdef __CUDACC__
82 // Reinterpret hash maps' void* value arrays as CUDA primitive types arrays, to
83 // avoid slow memcpy or byte-by-byte copy in kernels.
84 // Not used in the CPU version since memcpy is relatively fast on CPU.
85 #define DISPATCH_DIVISOR_SIZE_TO_BLOCK_T(DIVISOR, ...) \
86  [&] { \
87  if (DIVISOR == 16) { \
88  using block_t = int4; \
89  return __VA_ARGS__(); \
90  } else if (DIVISOR == 12) { \
91  using block_t = int3; \
92  return __VA_ARGS__(); \
93  } else if (DIVISOR == 8) { \
94  using block_t = int2; \
95  return __VA_ARGS__(); \
96  } else if (DIVISOR == 4) { \
97  using block_t = int; \
98  return __VA_ARGS__(); \
99  } else if (DIVISOR == 2) { \
100  using block_t = int16_t; \
101  return __VA_ARGS__(); \
102  } else { \
103  using block_t = uint8_t; \
104  return __VA_ARGS__(); \
105  } \
106  }()
107 #endif
108 
109 namespace open3d {
110 namespace utility {
111 
112 template <typename T, int N>
113 struct MiniVecHash {
114 public:
116  uint64_t hash = UINT64_C(14695981039346656037);
117 #if defined(__CUDA_ARCH__)
118 #pragma unroll
119 #endif
120  for (int i = 0; i < N; ++i) {
121  hash ^= static_cast<uint64_t>(key[i]);
122  hash *= UINT64_C(1099511628211);
123  }
124  return hash;
125  }
126 };
127 
128 template <typename T, int N>
129 struct MiniVecEq {
130 public:
132  const MiniVec<T, N>& rhs) const {
133  bool is_equal = true;
134 #if defined(__CUDA_ARCH__)
135 #pragma unroll
136 #endif
137  for (int i = 0; i < N; ++i) {
138  is_equal = is_equal && (lhs[i] == rhs[i]);
139  }
140  return is_equal;
141  }
142 };
143 
144 } // namespace utility
145 } // namespace open3d
Definition: Dispatch.h:129
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:352
Definition: Dispatch.h:113
#define OPEN3D_HOST_DEVICE
Definition: CUDAUtils.h:63
OPEN3D_HOST_DEVICE bool operator()(const MiniVec< T, N > &lhs, const MiniVec< T, N > &rhs) const
Definition: Dispatch.h:131
Definition: PinholeCameraIntrinsic.cpp:35
Definition: MiniVec.h:43
OPEN3D_HOST_DEVICE uint64_t operator()(const MiniVec< T, N > &key) const
Definition: Dispatch.h:115