MPQC  3.0.0-alpha
array.hpp
1 #ifndef MPQC_ARRAY_HPP
2 #define MPQC_ARRAY_HPP
3 
4 #include "mpqc/range.hpp"
5 #include "mpqc/range/operator.hpp"
6 #include "mpqc/mpi.hpp"
7 
8 #include "mpqc/array/core.hpp"
9 #include "mpqc/array/file.hpp"
10 #ifdef MPQC_PARALLEL
11 #include "mpqc/array/parallel.hpp"
12 #endif
13 
14 #include "mpqc/utility/check.hpp"
15 #include <boost/foreach.hpp>
16 #include "mpqc/utility/exception.hpp"
17 
18 namespace mpqc {
19 
22 
25  template<typename T>
26  struct Array {
27 
29  Array() {}
30 
32  template<typename Extent>
33  Array(const std::string &name,
34  const std::vector<Extent> &extents,
35  MPI::Comm comm = MPI::Comm::Self())
36  {
37  initialize(name, extents, ARRAY_CORE, comm);
38  }
39 
41  template<typename Extent, class Driver>
42  Array(const std::string &name,
43  const std::vector<Extent> &extents,
44  Driver driver,
45  MPI::Comm comm = MPI::Comm(MPI::Comm::Self()))
46  {
47  initialize(name, extents, driver, comm);
48  }
49 
52  Array(const Array &a)
53  {
54  range_ = a.range_;
55  dims_ = a.dims_;
56  impl_ = a.impl_;
57  }
58 
61  void put(const T *buffer) {
62  impl_->put(this->range_, buffer);
63  }
64 
67  void get(T *buffer) const {
68  impl_->get(this->range_, buffer);
69  }
70 
71  void sync() {
72  impl_->sync();
73  }
74 
75  const std::vector<size_t>& dims() const {
76  return dims_;
77  }
78 
79  size_t rank() const {
80  return dims().size();
81  }
82 
83  operator vector<T>() const {
84  vector<T> p(range_[0].size());
85  *this >> p;
86  return p;
87  }
88 
89  operator matrix<T>() const {
90  matrix<T> p(range_[0].size(), range_[1].size());
91  *this >> p;
92  return p;
93  }
94 
95  Array operator()(const std::vector<range> &r) {
96  MPQC_CHECK(this->rank() == r.size());
97  return Array(*this, r);
98  }
99 
100  Array operator()(const std::vector<range> &r) const {
101  MPQC_CHECK(this->rank() == r.size());
102  return Array(*this, r);
103  }
104 
105 
106 #ifdef DOXYGEN
107 
114  template<class R, ...>
115  Array operator()(const R &r, ...);
116 #else
117  template<class S>
118  Array operator()(const range::tie<S> &t) {
119  return this->operator()(std::vector<range>(t));
120  }
121  template<class S>
122  Array operator()(const range::tie<S> &t) const {
123  return this->operator()(std::vector<range>(t));
124  }
125  MPQC_RANGE_OPERATORS(4, Array, this->operator())
126  MPQC_RANGE_CONST_OPERATORS(4, Array, this->operator())
127 #endif
128 
129  protected:
130 
131  template<class Extent, class Driver>
132  void initialize(const std::string &name,
133  const std::vector<Extent> &extents,
134  Driver driver, MPI::Comm comm) {
135 
136  BOOST_FOREACH (auto e, extents) {
137  range_.push_back(detail::ArrayBase::extent(e));
138  dims_.push_back(range_.back().size());
139  //std::cout << "extent=" << range_.back() << std::endl;
140  }
141 
142  detail::ArrayBase *impl = NULL;
143  if (comm == MPI::Comm::Self()) {
144  impl = new detail::array_impl<T, Driver>(name, dims_);
145  }
146  else {
147 #ifdef HAVE_MPI
148  impl = new detail::array_parallel_impl<T, Driver>(name, dims_, comm);
149 #else // HAVE_MPI
150  throw MPQC_EXCEPTION("Parallel array not implemented: "
151  "library was compiled without proper MPI support");
152 #endif // HAVE_MPI
153  }
154  MPQC_CHECK(impl);
155  this->impl_.reset(impl);
156  }
157 
158  Array(const Array &A, const std::vector<range> &r)
159  {
160  MPQC_CHECK(A.rank() == r.size());
161  range_ = r;
162  BOOST_FOREACH (range r, range_) {
163  //std::cout << r << ",";
164  dims_.push_back(r.size());
165  }
166  //std::cout << std::endl;
167  impl_ = A.impl_;
168  }
169 
170  size_t block() const {
171  MPQC_ASSERT(this->rank() == 2);
172  range ri = range_[0];
173  return std::max<size_t>((8 << 20)/(sizeof(T)*ri.size()), 1);
174  }
175 
176  private:
177  std::vector<range> range_;
178  std::vector<size_t> dims_;
179  std::shared_ptr<detail::ArrayBase> impl_;
180  };
181 
182 
190  template<typename T, class V>
191  void operator<<(Array<T> A, const V &v) {
192  A.put(v.data());
193  }
194 
195 
203  template<typename T, class V>
204  void operator>>(Array<T> A, V &v) {
205  A.get(v.data());
206  }
207 
208  // template<typename T, class E>
209  // void operator>>(Array<T> A, Eigen::Block<E> b) {
210  // //A >> b;
211  // }
212 
214 
215 }
216 
217 #endif /* MPQC_ARRAY_HPP */
MPQC_RANGE_CONST_OPERATORS
#define MPQC_RANGE_CONST_OPERATORS(N, Type, Function)
Generates const version of MPQC_RANGE_OPERATORS.
Definition: operator.hpp:45
mpqc::Array::put
void put(const T *buffer)
Put buffer data into array.
Definition: array.hpp:61
mpqc::Array::Array
Array()
Create new empty array.
Definition: array.hpp:29
mpqc
Contains new MPQC code since version 3.
Definition: integralenginepool.hpp:37
MPQC_RANGE_OPERATORS
#define MPQC_RANGE_OPERATORS(N, Type, Function)
Generates operators()(const R1 r1, ...) of arity 1 to N.
Definition: operator.hpp:41
mpqc::operator>>
void operator>>(Array< T > A, V &v)
Read from Array to a generic vector V.
Definition: array.hpp:204
mpqc::Array::Array
Array(const std::string &name, const std::vector< Extent > &extents, MPI::Comm comm=MPI::Comm::Self())
Create new array using CORE driver (in-memory)
Definition: array.hpp:33
MPQC_EXCEPTION
#define MPQC_EXCEPTION(...)
Definition: exception.hpp:51
mpqc::Array
Array implementation.
Definition: forward.hpp:10
mpqc::Array::get
void get(T *buffer) const
Get array data into buffer.
Definition: array.hpp:67
mpqc::operator<<
void operator<<(Array< T > A, const V &v)
Write to Array from a generic vector V.
Definition: array.hpp:191
mpqc::Array::Array
Array(const Array &a)
Shallow copy constructor.
Definition: array.hpp:52
mpqc::MPI::Comm
MPI_Comm object wrapper/stub.
Definition: comm.hpp:14
mpqc::Array::Array
Array(const std::string &name, const std::vector< Extent > &extents, Driver driver, MPI::Comm comm=MPI::Comm(MPI::Comm::Self()))
Create new array using an arbitrary driver.
Definition: array.hpp:42

Generated at Sun Jan 26 2020 23:24:01 for MPQC 3.0.0-alpha using the documentation package Doxygen 1.8.16.