ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
Arange.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 
12 
13 namespace cloudViewer {
14 namespace core {
15 namespace kernel {
16 
17 Tensor Arange(const Tensor& start, const Tensor& stop, const Tensor& step) {
18  AssertTensorShape(start, {});
19  AssertTensorShape(stop, {});
20  AssertTensorShape(step, {});
21 
22  const Device device = start.GetDevice();
23  AssertTensorDevice(stop, device);
24  AssertTensorDevice(step, device);
25 
26  int64_t num_elements = 0;
27  bool is_arange_valid = true;
28 
29  Dtype dtype = start.GetDtype();
30  DISPATCH_DTYPE_TO_TEMPLATE(dtype, [&]() {
31  scalar_t sstart = start.Item<scalar_t>();
32  scalar_t sstop = stop.Item<scalar_t>();
33  scalar_t sstep = step.Item<scalar_t>();
34 
35  if (sstep == 0) {
36  utility::LogError("Step cannot be 0");
37  }
38  if (sstart == sstop) {
39  is_arange_valid = false;
40  }
41 
42  num_elements = static_cast<int64_t>(
43  std::ceil(static_cast<double>(sstop - sstart) /
44  static_cast<double>(sstep)));
45  if (num_elements <= 0) {
46  is_arange_valid = false;
47  }
48  });
49 
50  // Special case.
51  if (!is_arange_valid) {
52  return Tensor({0}, dtype, device);
53  }
54 
55  // Input parameters.
56  std::unordered_map<std::string, core::Tensor> srcs = {
57  {"start", start},
58  {"step", step},
59  };
60 
61  // Output.
62  Tensor dst = Tensor({num_elements}, dtype, device);
63 
64  if (device.IsCPU()) {
65  ArangeCPU(start, stop, step, dst);
66  } else if (device.IsSYCL()) {
67 #ifdef BUILD_SYCL_MODULE
68  ArangeSYCL(start, stop, step, dst);
69 #else
70  utility::LogError("Not compiled with SYCL, but SYCL device is used.");
71 #endif
72  } else if (device.IsCUDA()) {
73 #ifdef BUILD_CUDA_MODULE
74  ArangeCUDA(start, stop, step, dst);
75 #else
76  utility::LogError("Not compiled with CUDA, but CUDA device is used.");
77 #endif
78  } else {
79  utility::LogError("Arange: Unimplemented device.");
80  }
81 
82  return dst;
83 }
84 
85 } // namespace kernel
86 } // namespace core
87 } // namespace cloudViewer
#define DISPATCH_DTYPE_TO_TEMPLATE(DTYPE,...)
Definition: Dispatch.h:31
#define AssertTensorDevice(tensor,...)
Definition: TensorCheck.h:45
#define AssertTensorShape(tensor,...)
Definition: TensorCheck.h:61
Dtype GetDtype() const
Definition: Tensor.h:1164
Device GetDevice() const override
Definition: Tensor.cpp:1435
#define LogError(...)
Definition: Logging.h:60
void ArangeCPU(const Tensor &start, const Tensor &stop, const Tensor &step, Tensor &dst)
Definition: ArangeCPU.cpp:17
void ArangeSYCL(const Tensor &start, const Tensor &stop, const Tensor &step, Tensor &dst)
Definition: ArangeSYCL.cpp:17
Tensor Arange(const Tensor &start, const Tensor &stop, const Tensor &step)
Definition: Arange.cpp:17
MiniVec< float, N > ceil(const MiniVec< float, N > &a)
Definition: MiniVec.h:89
Generic file read and write utility for python interface.