ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
Rand.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 
9 
10 #include <Parallel.h>
11 
12 #include <random>
13 #include <type_traits>
14 #include <vector>
15 
19 
20 namespace cloudViewer {
21 namespace benchmarks {
22 
24  size_t seed,
25  const std::pair<core::Scalar, core::Scalar>& range,
26  core::Dtype dtype,
27  const core::Device& device) {
28  // Initialize on CPU, then copy to device
29  core::Tensor random =
30  core::Tensor::Empty(shape, dtype, core::Device("CPU:0"));
31  core::TensorIterator random_it(random);
32 
34  scalar_t low = range.first.To<scalar_t>();
35  scalar_t high = range.second.To<scalar_t>();
36 
37  using uniform_distribution = std::conditional_t<
38  std::is_same<scalar_t, bool>::value,
39  std::uniform_int_distribution<uint16_t>,
40  std::conditional_t<
41  std::is_same<scalar_t, uint8_t>::value,
42  std::uniform_int_distribution<uint16_t>,
43  std::conditional_t<
44  std::is_same<scalar_t, int8_t>::value,
45  std::uniform_int_distribution<int16_t>,
46  std::conditional_t<
47  std::is_integral<scalar_t>::value,
48  std::uniform_int_distribution<scalar_t>,
49  std::conditional_t<
50  std::is_floating_point<
51  scalar_t>::value,
52  std::uniform_real_distribution<
53  scalar_t>,
54  void>>>>>;
55 
56  int num_threads = utility::EstimateMaxThreads();
57  std::vector<std::default_random_engine> rng;
58  for (int64_t i = 0; i < num_threads; ++i) {
59  rng.emplace_back(seed + i);
60  }
61  uniform_distribution dist(low, high);
62 
63  core::ParallelFor(core::Device("CPU:0"), num_threads, [&](int64_t i) {
64  int64_t start = random.NumElements() * i / num_threads;
65  int64_t end = std::min<int64_t>(
66  random.NumElements() * (i + 1) / num_threads,
67  random.NumElements());
68  for (int64_t idx = start; idx < end; ++idx) {
69  *static_cast<scalar_t*>(random_it.GetPtr(idx)) = dist(rng[i]);
70  }
71  });
72  });
73 
74  return random.To(device);
75 }
76 
77 } // namespace benchmarks
78 } // namespace cloudViewer
#define DISPATCH_DTYPE_TO_TEMPLATE_WITH_BOOL(DTYPE,...)
Definition: Dispatch.h:68
CLOUDVIEWER_HOST_DEVICE void * GetPtr(int64_t workload_idx) const
Definition: Indexer.h:235
int64_t NumElements() const
Definition: Tensor.h:1170
static Tensor Empty(const SizeVector &shape, Dtype dtype, const Device &device=Device("CPU:0"))
Create a tensor with uninitialized values.
Definition: Tensor.cpp:400
Tensor To(Dtype dtype, bool copy=false) const
Definition: Tensor.cpp:739
static double dist(double x1, double y1, double x2, double y2)
Definition: lsd.c:207
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 ParallelFor(const Device &device, int64_t n, const func_t &func)
Definition: ParallelFor.h:111
int EstimateMaxThreads()
Estimate the maximum number of threads to be used in a parallel region.
Definition: Parallel.cpp:31
Generic file read and write utility for python interface.