Open3D (C++ API)  0.13.0
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 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/Console.h"
31 
32 // TODO: dispatch more combinations.
33 #define DISPATCH_DTYPE_AND_DIM_TO_TEMPLATE(DTYPE, DIM, ...) \
34  [&] { \
35  if (DTYPE == open3d::core::Dtype::Int32) { \
36  if (DIM == 1) { \
37  using key_t = Block<int, 1>; \
38  using hash_t = BlockHash<int, 1>; \
39  return __VA_ARGS__(); \
40  } else if (DIM == 2) { \
41  using key_t = Block<int, 2>; \
42  using hash_t = BlockHash<int, 2>; \
43  return __VA_ARGS__(); \
44  } else if (DIM == 3) { \
45  using key_t = Block<int, 3>; \
46  using hash_t = BlockHash<int, 3>; \
47  return __VA_ARGS__(); \
48  } \
49  } else if (DTYPE == open3d::core::Dtype::Int64) { \
50  if (DIM == 1) { \
51  using key_t = Block<int64_t, 1>; \
52  using hash_t = BlockHash<int64_t, 1>; \
53  return __VA_ARGS__(); \
54  } else if (DIM == 2) { \
55  using key_t = Block<int64_t, 2>; \
56  using hash_t = BlockHash<int64_t, 2>; \
57  return __VA_ARGS__(); \
58  } else if (DIM == 3) { \
59  using key_t = Block<int64_t, 3>; \
60  using hash_t = BlockHash<int64_t, 3>; \
61  return __VA_ARGS__(); \
62  } \
63  } else { \
64  utility::LogError("Unsupported dtype {} and dim {} combination", \
65  DTYPE.ToString(), DIM); \
66  } \
67  }()
68 
69 namespace open3d {
70 namespace core {
71 template <typename T, size_t N>
72 class Block {
73 public:
74  Block() = default;
75 
77 #if defined(__CUDA_ARCH__)
78 #pragma unroll
79 #endif
80  for (size_t i = 0; i < N; ++i) {
81  data_[i] = other.data_[i];
82  }
83  }
84 
86 #if defined(__CUDA_ARCH__)
87 #pragma unroll
88 #endif
89  for (size_t i = 0; i < N; ++i) {
90  data_[i] = other.data_[i];
91  }
92 
93  return *this;
94  }
95 
96  OPEN3D_HOST_DEVICE bool operator==(const Block<T, N>& other) const {
97  bool is_eq = true;
98 #if defined(__CUDA_ARCH__)
99 #pragma unroll
100 #endif
101  for (size_t i = 0; i < N; ++i) {
102  is_eq = is_eq && (data_[i] == other.data_[i]);
103  }
104  return is_eq;
105  }
106 
107  OPEN3D_HOST_DEVICE T Get(size_t i) const { return data_[i]; }
108  OPEN3D_HOST_DEVICE void Set(size_t i, const T& value) { data_[i] = value; }
109 
110 private:
111  T data_[N];
112 };
113 
114 template <typename T, size_t N>
115 struct BlockHash {
116 public:
118  uint64_t hash = UINT64_C(14695981039346656037);
119 #if defined(__CUDA_ARCH__)
120 #pragma unroll
121 #endif
122  for (size_t i = 0; i < N; ++i) {
123  hash ^= static_cast<uint64_t>(key.Get(i));
124  hash *= UINT64_C(1099511628211);
125  }
126  return hash;
127  }
128 };
129 
130 } // namespace core
131 } // namespace open3d
OPEN3D_HOST_DEVICE Block< T, N > & operator=(const Block< T, N > &other)
Definition: Dispatch.h:85
OPEN3D_HOST_DEVICE T Get(size_t i) const
Definition: Dispatch.h:107
Definition: Dispatch.h:115
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
#define OPEN3D_HOST_DEVICE
Definition: CUDAUtils.h:56
Definition: Dispatch.h:72
OPEN3D_HOST_DEVICE void Set(size_t i, const T &value)
Definition: Dispatch.h:108
OPEN3D_HOST_DEVICE bool operator==(const Block< T, N > &other) const
Definition: Dispatch.h:96
OPEN3D_HOST_DEVICE uint64_t operator()(const Block< T, N > &key) const
Definition: Dispatch.h:117
Definition: PinholeCameraIntrinsic.cpp:35
OPEN3D_HOST_DEVICE Block(const Block< T, N > &other)
Definition: Dispatch.h:76