ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
BinaryEW.cpp
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 #include <benchmark/benchmark.h>
9 
10 #include <type_traits>
11 
18 
19 namespace cloudViewer {
20 namespace core {
21 
22 enum class BinaryOpCode {
23  Add,
24  Sub,
25  Mul,
26  Div,
27  LogicalAnd,
28  LogicalOr,
29  LogicalXor,
30  Gt,
31  Ge,
32  Lt,
33  Le,
34  Eq,
35  Neq,
36 };
37 
38 static std::function<Tensor(const Tensor&, const Tensor&)> MakeOperation(
39  BinaryOpCode op) {
40  switch (op) {
41  case BinaryOpCode::Add:
42  return std::plus<Tensor>();
43 
44  case BinaryOpCode::Sub:
45  return std::minus<Tensor>();
46 
47  case BinaryOpCode::Mul:
48  return std::multiplies<Tensor>();
49 
50  case BinaryOpCode::Div:
51  return std::divides<Tensor>();
52 
54  return [](const Tensor& lhs, const Tensor& rhs) -> Tensor {
55  return lhs && rhs;
56  };
57 
59  return [](const Tensor& lhs, const Tensor& rhs) -> Tensor {
60  return lhs || rhs;
61  };
62 
64  return [](const Tensor& lhs, const Tensor& rhs) -> Tensor {
65  return lhs.LogicalXor(rhs);
66  };
67 
68  case BinaryOpCode::Gt:
69  return [](const Tensor& lhs, const Tensor& rhs) -> Tensor {
70  return lhs > rhs;
71  };
72 
73  case BinaryOpCode::Ge:
74  return [](const Tensor& lhs, const Tensor& rhs) -> Tensor {
75  return lhs >= rhs;
76  };
77 
78  case BinaryOpCode::Lt:
79  return [](const Tensor& lhs, const Tensor& rhs) -> Tensor {
80  return lhs < rhs;
81  };
82 
83  case BinaryOpCode::Le:
84  return [](const Tensor& lhs, const Tensor& rhs) -> Tensor {
85  return lhs <= rhs;
86  };
87 
88  case BinaryOpCode::Eq:
89  return [](const Tensor& lhs, const Tensor& rhs) -> Tensor {
90  return lhs == rhs;
91  };
92 
93  case BinaryOpCode::Neq:
94  return [](const Tensor& lhs, const Tensor& rhs) -> Tensor {
95  return lhs != rhs;
96  };
97 
98  default:
99  utility::LogError("Unknown operation {}", static_cast<int>(op));
100  }
101 }
102 
103 void BinaryEW(benchmark::State& state,
104  int size,
105  BinaryOpCode op_code,
106  const Dtype& dtype,
107  const Device& device) {
108  Tensor lhs = benchmarks::Rand({1, size}, 1, {1, 127}, dtype, device);
109  Tensor rhs = benchmarks::Rand({1, size}, 2, {1, 127}, dtype, device);
110  auto op = MakeOperation(op_code);
111 
112  Tensor result = op(lhs, rhs);
113  benchmark::DoNotOptimize(result);
114 
115  for (auto _ : state) {
116  Tensor result = op(lhs, rhs);
117  benchmark::DoNotOptimize(result);
118 
119  cuda::Synchronize(device);
120  }
121 }
122 
123 #define ENUM_BM_SIZE(FN, OP, DEVICE, DEVICE_NAME, DTYPE) \
124  BENCHMARK_CAPTURE(FN, OP##__##DEVICE_NAME##_##DTYPE##__100, 100, \
125  BinaryOpCode::OP, DTYPE, DEVICE) \
126  ->Unit(benchmark::kMillisecond); \
127  BENCHMARK_CAPTURE(FN, OP##__##DEVICE_NAME##_##DTYPE##__100000, 100000, \
128  BinaryOpCode::OP, DTYPE, DEVICE) \
129  ->Unit(benchmark::kMillisecond); \
130  BENCHMARK_CAPTURE(FN, OP##__##DEVICE_NAME##_##DTYPE##__100000000, \
131  100000000, BinaryOpCode::OP, DTYPE, DEVICE) \
132  ->Unit(benchmark::kMillisecond);
133 
134 #define ENUM_BM_DTYPE(FN, OP, DEVICE, DEVICE_NAME) \
135  ENUM_BM_SIZE(FN, OP, DEVICE, DEVICE_NAME, Int8) \
136  ENUM_BM_SIZE(FN, OP, DEVICE, DEVICE_NAME, UInt8) \
137  ENUM_BM_SIZE(FN, OP, DEVICE, DEVICE_NAME, Int16) \
138  ENUM_BM_SIZE(FN, OP, DEVICE, DEVICE_NAME, UInt16) \
139  ENUM_BM_SIZE(FN, OP, DEVICE, DEVICE_NAME, Int32) \
140  ENUM_BM_SIZE(FN, OP, DEVICE, DEVICE_NAME, UInt32) \
141  ENUM_BM_SIZE(FN, OP, DEVICE, DEVICE_NAME, Int64) \
142  ENUM_BM_SIZE(FN, OP, DEVICE, DEVICE_NAME, UInt64) \
143  ENUM_BM_SIZE(FN, OP, DEVICE, DEVICE_NAME, Float32) \
144  ENUM_BM_SIZE(FN, OP, DEVICE, DEVICE_NAME, Float64)
145 
146 #define ENUM_BM_DTYPE_WITH_BOOL(FN, OP, DEVICE, DEVICE_NAME) \
147  ENUM_BM_SIZE(FN, OP, DEVICE, DEVICE_NAME, Bool) \
148  ENUM_BM_SIZE(FN, OP, DEVICE, DEVICE_NAME, Int8) \
149  ENUM_BM_SIZE(FN, OP, DEVICE, DEVICE_NAME, UInt8) \
150  ENUM_BM_SIZE(FN, OP, DEVICE, DEVICE_NAME, Int16) \
151  ENUM_BM_SIZE(FN, OP, DEVICE, DEVICE_NAME, UInt16) \
152  ENUM_BM_SIZE(FN, OP, DEVICE, DEVICE_NAME, Int32) \
153  ENUM_BM_SIZE(FN, OP, DEVICE, DEVICE_NAME, UInt32) \
154  ENUM_BM_SIZE(FN, OP, DEVICE, DEVICE_NAME, Int64) \
155  ENUM_BM_SIZE(FN, OP, DEVICE, DEVICE_NAME, UInt64) \
156  ENUM_BM_SIZE(FN, OP, DEVICE, DEVICE_NAME, Float32) \
157  ENUM_BM_SIZE(FN, OP, DEVICE, DEVICE_NAME, Float64)
158 
159 // #ifdef BUILD_CUDA_MODULE
160 // #define ENUM_BM_TENSOR(FN, OP)
161 // ENUM_BM_DTYPE(FN, OP, Device("CPU:0"), CPU)
162 // ENUM_BM_DTYPE(FN, OP, Device("CUDA:0"), CUDA)
163 // #else
164 #define ENUM_BM_TENSOR(FN, OP) ENUM_BM_DTYPE(FN, OP, Device("CPU:0"), CPU)
165 // #endif
166 
167 // #ifdef BUILD_CUDA_MODULE
168 // #define ENUM_BM_TENSOR_WTIH_BOOL(FN, OP)
169 // ENUM_BM_DTYPE_WITH_BOOL(FN, OP, Device("CPU:0"), CPU)
170 // ENUM_BM_DTYPE_WITH_BOOL(FN, OP, Device("CUDA:0"), CUDA)
171 // #else
172 #define ENUM_BM_TENSOR_WTIH_BOOL(FN, OP) \
173  ENUM_BM_DTYPE_WITH_BOOL(FN, OP, Device("CPU:0"), CPU)
174 // #endif
175 
189 
190 } // namespace core
191 } // namespace cloudViewer
Common CUDA utilities.
#define ENUM_BM_TENSOR_WTIH_BOOL(FN, OP)
Definition: BinaryEW.cpp:172
#define ENUM_BM_TENSOR(FN, OP)
Definition: BinaryEW.cpp:164
int size
core::Tensor result
Definition: VtkUtils.cpp:76
Tensor LogicalXor(const Tensor &value) const
Definition: Tensor.cpp:1520
#define LogError(...)
Definition: Logging.h:60
core::Tensor Rand(const core::SizeVector &shape, size_t seed, const std::pair< core::Scalar, core::Scalar > &range, core::Dtype dtype, const core::Device &device)
Returns a Tensor with random values within the range range .
Definition: Rand.cpp:23
void BinaryEW(benchmark::State &state, int size, BinaryOpCode op_code, const Dtype &dtype, const Device &device)
Definition: BinaryEW.cpp:103
static std::function< Tensor(const Tensor &, const Tensor &)> MakeOperation(BinaryOpCode op)
Definition: BinaryEW.cpp:38
Generic file read and write utility for python interface.