MPQC  3.0.0-alpha
order.hpp
1 #ifndef MPQC_MATH_TENSOR_ORDER_HPP
2 #define MPQC_MATH_TENSOR_ORDER_HPP
3 
4 #include <boost/tuple/tuple.hpp>
5 #include <boost/fusion/include/boost_tuple.hpp>
6 #include <boost/fusion/include/boost_array.hpp>
7 #include <boost/fusion/include/as_vector.hpp>
8 #include <boost/fusion/include/at_c.hpp>
9 #include <boost/fusion/include/pop_front.hpp>
10 #include <boost/fusion/include/front.hpp>
11 #include <boost/fusion/include/pop_back.hpp>
12 #include <boost/fusion/include/back.hpp>
13 #include <boost/fusion/include/accumulate.hpp>
14 #include <boost/mpl/int.hpp>
15 
16 namespace mpqc {
17 
20 
22  struct TensorRowMajor {
25  template<size_t N>
26  static boost::array<size_t,N> strides(const size_t *ld) {
27  boost::array<size_t,N> strides;
28  size_t stride = 1;
29  for (int i = 0; i < N; ++i) {
30  strides[N-(i+1)] = stride;
31  stride *= ld[N-(i+1)];
32  //printf("strides_[%i]=%i\n", N-(i+1), strides_[N-(i+1)]);
33  }
34  return strides;
35  }
37  template<class Index, class Strides>
38  static ptrdiff_t index(const Index &idx, const Strides &strides) {
39  namespace fusion = boost::fusion;
40  const typename fusion::result_of::as_vector<Index>::type &v =
41  fusion::as_vector(idx);
42  ptrdiff_t diff = fusion::accumulate(fusion::pop_back(v),
43  fusion::back(v),
44  make_index(&strides[0]));
45  return diff;
46  }
47  protected:
48  struct make_index {
49  typedef ptrdiff_t result_type;
50  const size_t *strides;
51  explicit make_index(const size_t *strides) : strides(strides) {}
52  template<typename T>
53  ptrdiff_t operator()(ptrdiff_t idx, const T& t) {
54  return idx + t*(*strides++);
55  }
56  };
57  };
58 
63  template<size_t N>
64  static boost::array<size_t,N> strides(const size_t *ld) {
65  boost::array<size_t,N> strides;
66  size_t stride = 1;
67  for (int i = 0; i < N; ++i) {
68  strides[i] = stride;
69  stride *= ld[i];
70  //printf("strides_[%i]=%i\n", N-(i+1), strides_[N-(i+1)]);
71  }
72  return strides;
73  }
75  template<class Index, class Strides>
76  static ptrdiff_t index(const Index &idx, const Strides &strides) {
77  namespace fusion = boost::fusion;
78  const typename fusion::result_of::as_vector<Index>::type &v =
79  fusion::as_vector(idx);
80  ptrdiff_t diff = fusion::accumulate(fusion::pop_front(v),
81  fusion::front(v),
82  make_index(&strides[1]));
83  return diff;
84  }
85  protected:
86  struct make_index {
87  typedef ptrdiff_t result_type;
88  const size_t *strides;
89  explicit make_index(const size_t *strides) : strides(strides) {}
90  template<typename T>
91  ptrdiff_t operator()(ptrdiff_t idx, const T& t) {
92  return idx + t*(*strides++);
93  }
94  };
95  };
96 
98 
99 }
100 
101 #endif /* MPQC_MATH_TENSOR_ORDER_HPP */
mpqc
Contains new MPQC code since version 3.
Definition: integralenginepool.hpp:37
mpqc::TensorRowMajor
Tensor row major (i.e. last dimension is contiguous) storage order.
Definition: order.hpp:22
mpqc::TensorColumnMajor::strides
static boost::array< size_t, N > strides(const size_t *ld)
Constructs strides array.
Definition: order.hpp:64
mpqc::TensorRowMajor::strides
static boost::array< size_t, N > strides(const size_t *ld)
Constructs strides array.
Definition: order.hpp:26
mpqc::TensorRowMajor::make_index
Definition: order.hpp:48
mpqc::TensorColumnMajor::make_index
Definition: order.hpp:86
mpqc::TensorColumnMajor
Tensor column major (i.e. first dimension is contiguous) storage order.
Definition: order.hpp:60
mpqc::TensorColumnMajor::index
static ptrdiff_t index(const Index &idx, const Strides &strides)
given N-d index tuple and strides, compute 1-d index
Definition: order.hpp:76
mpqc::TensorRowMajor::index
static ptrdiff_t index(const Index &idx, const Strides &strides)
given N-d index tuple and strides, compute 1-d index
Definition: order.hpp:38

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