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