QMCPACK
TensorSoaContainer.h
Go to the documentation of this file.
1 //////////////////////////////////////////////////////////////////////////////////////
2 // This file is distributed under the University of Illinois/NCSA Open Source License.
3 // See LICENSE file in top directory for details.
4 //
5 // Copyright (c) 2016 Jeongnim Kim and QMCPACK developers.
6 //
7 // File developed by:
8 //
9 // File created by: Jeongnim Kim, jeongnim.kim@intel.com, Intel Corp.
10 //////////////////////////////////////////////////////////////////////////////////////
11 // -*- C++ -*-
12 /** @file TensorSoaContainer.h
13  *
14  * Alternative to Container<Tensor<T,D>> to support SoA algorithms
15  * In addition to AoS->SoA transformation, it exploits the symmetric
16  * properties of Hessian, i.e., H(x,y)==H(y,x)
17  */
18 #ifndef QMCPLUSPLUS_TENSOR_SOA_CONTAINER_H
19 #define QMCPLUSPLUS_TENSOR_SOA_CONTAINER_H
20 namespace qmcplusplus
21 {
22 template<typename T, unsigned D>
24 {};
25 
26 /** SoA adaptor class for ParticleAttrib<TinyVector<T,3> >
27  * @tparam T data type, float, double, complex<float>, complex<double>
28  */
29 template<typename T>
30 struct TensorSoaContainer<T, 3>
31 {
32  using Element_t = T;
33 
34  ///number of elements
35  int nLocal;
36  ///number of elements + padded
37  int nGhosts;
38  ///container
40 
41  ///default constructor
42  TensorSoaContainer() : nLocal(0), nGhosts(0) {}
43  ///default copy constructor
44  TensorSoaContainer(const TensorSoaContainer& in) = default;
45  ///default copy operator
46  TensorSoaContainer& operator=(const TensorSoaContainer& in) = default;
47  /** constructor with size n without initialization
48  */
49  explicit TensorSoaContainer(int n) { resize(n); }
50 
51  /** need A=0.0;
52  */
53  template<typename T1>
55  {
56  std::fill(m_data.begin(), m_data.end(), static_cast<T>(in));
57  return *this;
58  }
59 
60  ~TensorSoaContainer() = default;
61 
62  inline void resize(int n)
63  {
64  nLocal = n;
65  nGhosts = getAlignedSize<T>(n);
66  m_data.resize(nGhosts * 6);
67  }
68 
69  /** return TinyVector<T,3>
70  */
71  inline Tensor<T, 3> operator[](int i) const
72  {
73  const T* restrict b = m_data + i;
74  T xx = *(b);
75  T xy = *(b + 1 * nGhosts);
76  T xz = *(b + 2 * nGhosts);
77  T yy = *(b + 3 * nGhosts);
78  T yz = *(b + 4 * nGhosts);
79  T zz = *(b + 5 * nGhosts);
80  return Tensor<T, 3>(xx, xy, xz, xy, yy, yz, xz, yz, zz);
81  }
82 
83  ///helper class for operator ()(int i) to assign a value
84  struct Accessor
85  {
86  int M;
87  T* _base;
88  Accessor() = delete;
89  Accessor(const Accessor&) = delete;
90  inline Accessor(T* a, int ng) : _base(a), M(ng) {}
91 
92  template<unsigned D>
93  inline Accessor& operator=(const Tensor<T, D>& rhs)
94  {
95  *_base = rhs(0);
96  *(_base + M) = rhs(1);
97  *(_base + 2 * M) = rhs(2);
98  *(_base + 3 * M) = rhs(4);
99  *(_base + 4 * M) = rhs(5);
100  *(_base + 5 * M) = rhs(8);
101  return *this;
102  }
103 
104  /** assign value */
105  template<typename T1>
106  inline Accessor& operator=(T1 rhs)
107  {
108  *_base = rhs;
109  *(_base + M) = rhs;
110  *(_base + 2 * M) = rhs;
111  *(_base + 3 * M) = rhs;
112  *(_base + 4 * M) = rhs;
113  *(_base + 5 * M) = rhs;
114  return *this;
115  }
116  };
117 
118  /** access operator for assignment of the i-th value
119  *
120  * Use for (*this)[i]=Tensor<T,3>;
121  */
122  inline Accessor operator()(int i) { return Accessor(m_data.data() + i, nGhosts); }
123 
124  ///return the base
125  inline T* data() { return m_data.data(); }
126  ///return the base
127  inline const T* data() const { return m_data.data(); }
128  ///return the base of XX components
129  inline T* restrict data(int i, int j)
130  {
131  const int n = (i < j) ? i * 3 + j : j * 3 + i;
132  return m_data().data() + n * nGhosts;
133  }
134  ///return the base of XX components
135  inline const T* restrict data(int i, int j) const
136  {
137  const int n = (i < j) ? i * 3 + j : j * 3 + i;
138  return m_data().data() + n * nGhosts;
139  }
140 
141  /** serialization function */
142  template<class Archive>
143  void serialize(Archive& ar, const unsigned int version)
144  {
145  //ar & m_data;
146  ar& nLocal& nGhosts& m_data;
147  }
148 };
149 
150 } // namespace qmcplusplus
151 
152 #endif
TensorSoaContainer(int n)
constructor with size n without initialization
Tensor< T, 3 > operator[](int i) const
return TinyVector<T,3>
std::vector< T, aligned_allocator< T > > aligned_vector
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
T *restrict data(int i, int j)
return the base of XX components
int nGhosts
number of elements + padded
Accessor & operator=(const Tensor< T, D > &rhs)
aligned_vector< T > m_data
container
Accessor operator()(int i)
access operator for assignment of the i-th value
Tensor<T,D> class for D by D tensor.
Definition: OhmmsTinyMeta.h:32
const T *restrict data(int i, int j) const
return the base of XX components
TensorSoaContainer & operator=(T1 in)
need A=0.0;
const T * data() const
return the base
const char version[]
Definition: HDFVersion.h:30
void serialize(Archive &ar, const unsigned int version)
serialization function