ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
IndexGetSetCPU.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 <Logging.h>
9 
15 
16 namespace cloudViewer {
17 namespace core {
18 namespace kernel {
19 
20 template <typename func_t>
22  const func_t& func) {
23  ParallelFor(Device("CPU:0"), indexer.NumWorkloads(),
24  [&indexer, &func](int64_t i) {
25  func(indexer.GetInputPtr(i), indexer.GetOutputPtr(i));
26  });
27 }
28 
29 template <typename scalar_t>
30 static void CPUCopyElementKernel(const void* src, void* dst) {
31  *static_cast<scalar_t*>(dst) = *static_cast<const scalar_t*>(src);
32 }
33 
34 static void CPUCopyObjectElementKernel(const void* src,
35  void* dst,
36  int64_t object_byte_size) {
37  const char* src_bytes = static_cast<const char*>(src);
38  char* dst_bytes = static_cast<char*>(dst);
39  memcpy(dst_bytes, src_bytes, object_byte_size);
40 }
41 
42 void IndexGetCPU(const Tensor& src,
43  Tensor& dst,
44  const std::vector<Tensor>& index_tensors,
45  const SizeVector& indexed_shape,
46  const SizeVector& indexed_strides) {
47  Dtype dtype = src.GetDtype();
48  AdvancedIndexer ai(src, dst, index_tensors, indexed_shape, indexed_strides,
50  if (dtype.IsObject()) {
51  int64_t object_byte_size = dtype.ByteSize();
52  LaunchAdvancedIndexerKernel(ai, [&](const void* src, void* dst) {
53  CPUCopyObjectElementKernel(src, dst, object_byte_size);
54  });
55  } else {
57  LaunchAdvancedIndexerKernel(ai, CPUCopyElementKernel<scalar_t>);
58  });
59  }
60 }
61 
62 void IndexSetCPU(const Tensor& src,
63  Tensor& dst,
64  const std::vector<Tensor>& index_tensors,
65  const SizeVector& indexed_shape,
66  const SizeVector& indexed_strides) {
67  Dtype dtype = src.GetDtype();
68  AdvancedIndexer ai(src, dst, index_tensors, indexed_shape, indexed_strides,
70  if (dtype.IsObject()) {
71  int64_t object_byte_size = dtype.ByteSize();
72  LaunchAdvancedIndexerKernel(ai, [&](const void* src, void* dst) {
73  CPUCopyObjectElementKernel(src, dst, object_byte_size);
74  });
75  } else {
77  LaunchAdvancedIndexerKernel(ai, CPUCopyElementKernel<scalar_t>);
78  });
79  }
80 }
81 
82 } // namespace kernel
83 } // namespace core
84 } // namespace cloudViewer
Indexer indexer
#define DISPATCH_DTYPE_TO_TEMPLATE_WITH_BOOL(DTYPE,...)
Definition: Dispatch.h:68
bool IsObject() const
Definition: Dtype.h:63
int64_t ByteSize() const
Definition: Dtype.h:59
Dtype GetDtype() const
Definition: Tensor.h:1164
static void LaunchAdvancedIndexerKernel(const AdvancedIndexer &indexer, const func_t &func)
void IndexGetCPU(const Tensor &src, Tensor &dst, const std::vector< Tensor > &index_tensors, const SizeVector &indexed_shape, const SizeVector &indexed_strides)
void IndexSetCPU(const Tensor &src, Tensor &dst, const std::vector< Tensor > &index_tensors, const SizeVector &indexed_shape, const SizeVector &indexed_strides)
static void CPUCopyElementKernel(const void *src, void *dst)
static void CPUCopyObjectElementKernel(const void *src, void *dst, int64_t object_byte_size)
void ParallelFor(const Device &device, int64_t n, const func_t &func)
Definition: ParallelFor.h:111
Generic file read and write utility for python interface.