ABACUS develop
Atomic-orbital Based Ab-initio Computation at UStc
Loading...
Searching...
No Matches
tensor_accessor.h
Go to the documentation of this file.
1#ifndef ATEN_CORE_TENSOR_ACCESSOR_H_
2#define ATEN_CORE_TENSOR_ACCESSOR_H_
3
4#include <cstddef>
5#include <cstdint>
7
8namespace container {
9
10template <typename T>
12 using PtrType = T*;
13};
14
15#if defined(__CUDACC__) || defined(__HIPCC__)
16template <typename T>
17struct RestrictPtrTraits {
18 using PtrType = T* __restrict__;
19};
20#endif
21
22template <typename T, size_t N, typename index_t = int64_t,
23 template <typename U> class PtrTraits = DefaultPtrTraits>
25 public:
26
27 using PtrType = typename PtrTraits<T>::PtrType;
28
31 const index_t* sizes,
32 const index_t* strides)
34
35 // NOTE: Previously returned int_array_ref (from <base/utils/array_ref.h>),
36 // which supported .size(), iteration, etc. Now returns raw const pointer.
37 // If you need array_ref functionality, include <base/utils/array_ref.h> directly
38 // and wrap the result: int_array_ref(accessor.sizes(), N).
39 // mohan add 20260605
40 AT_HOST_DEVICE const index_t* sizes() const {
41 return sizes_;
42 }
43
44 AT_HOST_DEVICE const index_t* strides() const {
45 return strides_;
46 }
47
48 AT_HOST_DEVICE index_t stride(index_t idx) const {
49 return strides_[idx];
50 }
51
52 AT_HOST_DEVICE index_t size(index_t idx) const {
53 return sizes_[idx];
54 }
55
57 return data_;
58 }
59
60 AT_HOST_DEVICE const PtrType data() const {
61 return data_;
62 }
63
64 protected:
65 T* data_ = nullptr;
66 const index_t* sizes_ = nullptr;
67 const index_t* strides_ = nullptr;
68};
69
70template <typename T, size_t N, typename index_t = int64_t,
71 template <typename U> class PtrTraits = DefaultPtrTraits>
72class TensorAccessor : public TensorAccessorBase<T, N, index_t, PtrTraits> {
73 public:
74 using PtrType = typename PtrTraits<T>::PtrType;
75
76 AT_HOST_DEVICE TensorAccessor(PtrType data, const index_t* sizes, const index_t* strides)
77 : TensorAccessorBase<T, N, index_t, PtrTraits>(data, sizes, strides) {}
78
79 AT_HOST_DEVICE TensorAccessor<T, N - 1, index_t, PtrTraits> operator[](index_t idx) {
80 return TensorAccessor<T, N - 1, index_t, PtrTraits>(this->data_ + idx * this->strides_[0], this->sizes_ + 1, this->strides_ + 1);
81 }
82
83 AT_HOST_DEVICE const TensorAccessor<T, N - 1, index_t, PtrTraits> operator[](index_t idx) const {
84 return TensorAccessor<T, N - 1, index_t, PtrTraits>(this->data_ + idx * this->strides_[0], this->sizes_ + 1, this->strides_ + 1);
85 }
86};
87
88template <typename T, typename index_t,
89 template <typename U> class PtrTraits>
90class TensorAccessor<T, 1, index_t, PtrTraits> : public TensorAccessorBase<T, 1, index_t, PtrTraits> {
91 public:
92 using PtrType = typename PtrTraits<T>::PtrType;
93 AT_HOST_DEVICE TensorAccessor(T* data, const index_t* sizes, const index_t* strides)
94 : TensorAccessorBase<T, 1, index_t, PtrTraits>(data, sizes, strides) {}
95
96 AT_HOST_DEVICE T& operator[](index_t idx) {
97 return this->data_[idx * this->strides_[0]];
98 }
99
100 AT_HOST_DEVICE const T& operator[](index_t idx) const {
101 return this->data_[idx * this->strides_[0]];
102 }
103};
104
105
106} // namespace container
107
108#endif // ATEN_CORE_TENSOR_ACCESSOR_H_
AT_HOST_DEVICE PtrType data()
Definition tensor_accessor.h:56
AT_HOST_DEVICE const index_t * strides() const
Definition tensor_accessor.h:44
T * data_
Definition tensor_accessor.h:65
AT_HOST_DEVICE const PtrType data() const
Definition tensor_accessor.h:60
AT_HOST_DEVICE index_t size(index_t idx) const
Definition tensor_accessor.h:52
AT_HOST_DEVICE const index_t * sizes() const
Definition tensor_accessor.h:40
const index_t * strides_
Definition tensor_accessor.h:67
typename PtrTraits< T >::PtrType PtrType
Definition tensor_accessor.h:27
AT_HOST_DEVICE index_t stride(index_t idx) const
Definition tensor_accessor.h:48
AT_HOST_DEVICE TensorAccessorBase(PtrType data, const index_t *sizes, const index_t *strides)
Definition tensor_accessor.h:29
const index_t * sizes_
Definition tensor_accessor.h:66
typename PtrTraits< T >::PtrType PtrType
Definition tensor_accessor.h:92
AT_HOST_DEVICE const T & operator[](index_t idx) const
Definition tensor_accessor.h:100
AT_HOST_DEVICE TensorAccessor(T *data, const index_t *sizes, const index_t *strides)
Definition tensor_accessor.h:93
AT_HOST_DEVICE T & operator[](index_t idx)
Definition tensor_accessor.h:96
AT_HOST_DEVICE TensorAccessor< T, N - 1, index_t, PtrTraits > operator[](index_t idx)
Definition tensor_accessor.h:79
typename PtrTraits< T >::PtrType PtrType
Definition tensor_accessor.h:74
AT_HOST_DEVICE TensorAccessor(PtrType data, const index_t *sizes, const index_t *strides)
Definition tensor_accessor.h:76
AT_HOST_DEVICE const TensorAccessor< T, N - 1, index_t, PtrTraits > operator[](index_t idx) const
Definition tensor_accessor.h:83
#define N
Definition exp.cpp:24
#define T
Definition exp.cpp:237
#define AT_HOST_DEVICE
Definition macros.h:47
Definition tensor.cpp:8
Definition tensor_accessor.h:11
T * PtrType
Definition tensor_accessor.h:12