QMCPACK
container_proxy.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: Jeremy McMinnis, jmcminis@gmail.com, University of Illinois at Urbana-Champaign
8 // Jeongnim Kim, jeongnim.kim@gmail.com, University of Illinois at Urbana-Champaign
9 // Mark A. Berrill, berrillma@ornl.gov, Oak Ridge National Laboratory
10 //
11 // File created by: Jeongnim Kim, jeongnim.kim@gmail.com, University of Illinois at Urbana-Champaign
12 //////////////////////////////////////////////////////////////////////////////////////
13 
14 
15 #ifndef QMCPLUSPLUS_MPI_CONTAINER_PROXY_H
16 #define QMCPLUSPLUS_MPI_CONTAINER_PROXY_H
17 
18 #include <stdexcept>
19 
20 #include "OhmmsPETE/Tensor.h"
21 #include "OhmmsPETE/OhmmsMatrix.h"
22 #include "OhmmsPETE/OhmmsArray.h"
23 #include "Pools/PooledData.h"
24 
25 namespace qmcplusplus
26 {
27 template<class T>
29 {
30  enum
31  {
32  DIM = 1
33  };
34  using real_type = T;
35  using value_type = T;
36  static inline T* get_address(T* a) { return a; }
37 };
38 
39 template<typename T>
40 struct scalar_traits<std::complex<T>>
41 {
42  enum
43  {
44  DIM = 2
45  };
46  using real_type = T;
47  using value_type = std::complex<T>;
48  static inline T* get_address(std::complex<T>* a) { return reinterpret_cast<T*>(a); }
49 };
50 
51 template<typename T>
53 {
54  enum
55  {
57  };
59  T& ref;
60  inline container_proxy(T& a) : ref(a) {}
61  inline size_t size() const { return DIM; }
63 };
64 
65 template<typename T, unsigned D>
67 {
68  enum
69  {
71  };
75  inline size_t size() const { return DIM; }
76  inline pointer data() { return scalar_traits<T>::get_address(ref.data()); }
77 };
78 
79 template<typename T, unsigned D>
80 struct container_proxy<Tensor<T, D>>
81 {
82  enum
83  {
85  };
88  inline container_proxy(Tensor<T, D>& a) : ref(a) {}
89  inline size_t size() const { return DIM; }
90  inline pointer data() { return scalar_traits<T>::get_address(ref.data()); }
91 };
92 
93 template<typename T>
94 struct container_proxy<std::vector<T>>
95 {
96  enum
97  {
99  };
101  std::vector<T>& ref;
102  inline container_proxy(std::vector<T>& a) : ref(a) {}
103  inline size_t size() const { return ref.size() * container_proxy<T>::DIM; }
104  inline pointer data() { return scalar_traits<T>::get_address(&ref[0]); }
105 
106  inline void resize(size_t n) { ref.resize(n); }
107 
108  template<typename I>
109  inline void resize(I* n, int d)
110  {
111  size_t nt = n[0];
112  for (int i = 1; i < d; ++i)
113  nt *= n[i];
114  ref.resize(nt);
115  }
116 };
117 
118 template<>
119 struct container_proxy<std::vector<bool>>
120 {
121  enum
122  {
123  DIM = 1
124  };
125  using pointer = int*;
126  std::vector<bool>& ref;
127  std::vector<int> my_copy;
128  inline container_proxy(std::vector<bool>& a) : ref(a)
129  {
130  my_copy.resize(a.size());
131  copy(a.begin(), a.end(), my_copy.begin());
132  }
133  ~container_proxy() { copy(my_copy.begin(), my_copy.end(), ref.begin()); }
134  inline size_t size() const { return my_copy.size(); }
135  inline pointer data() { return &my_copy[0]; }
136 };
137 
138 template<typename T, unsigned D>
139 struct container_proxy<std::vector<TinyVector<T, D>>>
140 {
141  enum
142  {
144  };
146  using data_type = std::vector<TinyVector<T, D>>;
148  inline container_proxy(data_type& a) : ref(a) {}
149  inline size_t size() const { return ref.size() * DIM; }
150  inline pointer data() { return scalar_traits<T>::get_address(ref[0].data()); }
151 };
152 
153 
154 template<typename T>
156 {
157  enum
158  {
159  DIM = 1
160  };
163  inline container_proxy(PooledData<T>& a) : ref(a) {}
164  inline size_t size() const { return ref.size() * container_proxy<T>::DIM; }
165  inline pointer data() { return ref.data(); }
166  template<typename I>
167  inline void resize(I* n)
168  {
169  ref.resize(static_cast<size_t>(n[0]));
170  }
171 };
172 
173 template<typename T>
175 {
176  enum
177  {
179  };
182  inline container_proxy(Vector<T>& a) : ref(a) {}
183  inline size_t size() const { return ref.size() * container_proxy<T>::DIM; }
184  inline pointer data() { return scalar_traits<T>::get_address(ref.data()); }
185  template<typename I>
186  inline void resize(I* n)
187  {
188  ref.resize(static_cast<size_t>(n[0]));
189  }
190 };
191 
192 template<typename T>
194 {
195  enum
196  {
198  };
201  inline container_proxy(Matrix<T>& a) : ref(a) {}
202  inline size_t size() const { return ref.size(); }
203  inline pointer data() { return scalar_traits<T>::get_address(ref.data()); }
204  template<typename I>
205  inline void resize(I* n, int d)
206  {
207  if (d != 2)
208  throw std::runtime_error("OhmmsMatrix can only be resized with int[2].");
209  ref.resize(n[0], n[1]);
210  }
211 };
212 
213 template<typename T, unsigned D>
215 {
216  enum
217  {
219  };
223  inline container_proxy(data_type& a) : ref(a) {}
224  inline size_t size() const { return ref.size() * DIM; }
225  inline pointer data() { return scalar_traits<T>::get_address(ref[0].data()); }
226 };
227 
228 template<typename T, unsigned D>
229 struct container_proxy<Array<T, D>>
230 {
233  inline container_proxy(Array<T, D>& a) : ref(a) {}
234  inline size_t size() const { return ref.size() * container_proxy<T>::DIM; }
235  inline pointer data() { return scalar_traits<T>::get_address(ref.data()); }
236 };
237 /*
238 template<typename T, class Alloc>
239 struct container_proxy<boost::multi::array<T,2,Alloc> >
240 {
241  enum {DIM=scalar_traits<T>::DIM};
242  using pointer = typename container_proxy<T>::pointer;
243  boost::multi::array<T,2,Alloc>& ref;
244  inline container_proxy(boost::multi::array<T,2,Alloc>& a):ref(a) {}
245  inline size_t size() const
246  {
247  return ref.num_elements()*DIM;
248  }
249  inline pointer data()
250  {
251  //using detail::to_address;
252  //return scalar_traits<T>::get_address(to_address(ref.origin()));
253  return scalar_traits<T>::get_address(std::addressof(*ref.origin()));
254  }
255  inline void resize(size_t n)
256  {
257  APP_ABORT(" Error: Can not resize container_proxy<boost::multi::array<T,D,Alloc> >. \n");
258  }
259  template<typename I>
260  inline void resize(I* n, int d)
261  {
262  if(d < 2)
263  APP_ABORT(" Error: Inconsistent dimension in container_proxy<boost::multi::array<T,D,Alloc> >::resize(I*,int). \n");
264  ref.reextent({n[0],n[1]});
265  }
266 };
267 
268 template<typename T>
269 struct container_proxy<boost::multi::array_ref<T,2> >
270 {
271  enum {DIM=scalar_traits<T>::DIM};
272  using pointer = typename container_proxy<T>::pointer;
273  boost::multi::array_ref<T,2>& ref;
274  inline container_proxy(boost::multi::array_ref<T,2>& a):ref(a) {}
275  inline size_t size() const
276  {
277  return ref.num_elements()*DIM;
278  }
279  inline pointer data()
280  {
281  //using detail::to_address;
282  //return scalar_traits<T>::get_address(to_address(ref.origin()));
283  return scalar_traits<T>::get_address(std::addressof(*ref.origin()));
284  }
285  inline void resize(size_t n)
286  {
287  APP_ABORT(" Error: Can not resize container_proxy<boost::multi::array_ref<T,D> >. \n");
288  }
289 
290  template<typename I>
291  inline void resize(I* n, int d)
292  {
293  APP_ABORT(" Error: Can not resize container_proxy<boost::multi::array_ref<T,D> >. \n");
294  }
295 
296 };
297 */
298 } // namespace qmcplusplus
299 #endif
Fixed-size array.
Definition: OhmmsTinyMeta.h:30
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
typename container_proxy< T >::pointer pointer
typename scalar_traits< T >::real_type * pointer
static T * get_address(T *a)
void copy(const Array< T1, 3 > &src, Array< T2, 3 > &dest)
Definition: Blitz.h:639
Tensor<T,D> class for D by D tensor.
Definition: OhmmsTinyMeta.h:32
typename container_proxy< T >::pointer pointer
typename container_proxy< T >::pointer pointer
std::complex< double > I
typename container_proxy< T >::pointer pointer
A D-dimensional Array class based on PETE.
Definition: OhmmsArray.h:25
static T * get_address(std::complex< T > *a)
typename scalar_traits< T >::real_type * pointer
Define a serialized buffer to store anonymous data.
typename scalar_traits< CT >::real_type * pointer
typename container_proxy< T >::pointer pointer