multi_array_ref_subarray_openfpm.hpp 9.55 KB
Newer Older
incardon's avatar
incardon committed
1
2
3
4
5
6
7
8
9
10
11
12
13
/*
 * multi_array_ref_subarray_openfpm.hpp
 *
 *  Created on: Jul 1, 2018
 *      Author: i-bird
 */

#ifndef MULTI_ARRAY_REF_SUBARRAY_OPENFPM_HPP_
#define MULTI_ARRAY_REF_SUBARRAY_OPENFPM_HPP_

#include "multi_array_view_openfpm.hpp"
#include "multi_array_ref_base_openfpm.hpp"

incardon's avatar
incardon committed
14
15
16
17
18
19
20
21
22
23
/*! \brief return the dimension of the sub_array
 *
 *
 */
template<typename vmpl>
struct subar_dim
{
	typedef typename boost::mpl::at<vmpl,boost::mpl::int_<0>>::type type;
};

incardon's avatar
incardon committed
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
namespace openfpm {
namespace detail {
namespace multi_array {

//
// const_sub_array
//    multi_array's proxy class to allow multiple overloads of
//    operator[] in order to provide a clean multi-dimensional array
//    interface.
template <typename T, std::size_t NumDims, typename vector, typename TPtr>
class const_sub_array_openfpm : public openfpm::detail::multi_array::multi_array_impl_base_openfpm<T,NumDims,vector>
{
  typedef openfpm::detail::multi_array::multi_array_impl_base_openfpm<T,NumDims,vector> super_type;

  typedef typename boost::mpl::accumulate<vector,
								   typename boost::mpl::int_<1>,
								   typename boost::mpl::multiplies<typename boost::mpl::_2,typename boost::mpl::_1>  >::type size_ct;


public:

  typedef typename super_type::value_type value_type;
  typedef typename super_type::const_reference const_reference;
  typedef typename super_type::const_iterator const_iterator;
  typedef typename super_type::const_reverse_iterator const_reverse_iterator;
  typedef typename super_type::element element;
  typedef typename super_type::size_type size_type;
  typedef typename super_type::difference_type difference_type;
  typedef typename super_type::index index;

  // template typedefs
  template <std::size_t NDims>
  struct const_array_view {
incardon's avatar
incardon committed
57
    typedef openfpm::detail::multi_array::const_multi_array_view_openfpm<T,NDims> type;
incardon's avatar
incardon committed
58
59
60
61
  };

  template <std::size_t NDims>
  struct array_view {
incardon's avatar
incardon committed
62
    typedef openfpm::detail::multi_array::multi_array_view_openfpm<T,NDims> type;
incardon's avatar
incardon committed
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
  };

  // Allow default copy constructor as well.

  template <typename Ovector>
  const_sub_array_openfpm (const const_sub_array_openfpm<T,NumDims,Ovector>& rhs)
  :base_(rhs.base_), strides_(rhs.strides_)
  {}

  // const_sub_array always returns const types, regardless of its own
  // constness.
  inline __device__ __host__  const_reference operator[](index idx) const
  {
    return super_type::access(boost::type<const_reference>(),idx,strides(),base_);
  }

  template <typename IndexList>
  const element& operator()(const IndexList& indices) const
  {
    boost::function_requires<boost::CollectionConcept<IndexList> >();
    return super_type::access_element(boost::type<const element&>(),
                                      indices,origin(),strides());
  }

  // see generate_array_view in base.hpp
  template <int NDims>
  __device__ __host__   typename const_array_view<NDims>::type
  operator[](const boost::detail::multi_array::index_gen<NumDims,NDims>& indices) const
  {
    typedef typename const_array_view<NDims>::type return_type;
    return super_type::generate_array_view(boost::type<return_type>(),
                                           indices,
                                           base_);
  }

  template <typename OPtr>
  bool operator!=(const const_sub_array_openfpm<T,NumDims,OPtr>& rhs) const {
    return !(*this == rhs);
  }

  template <typename OPtr>
  bool operator>(const const_sub_array_openfpm<T,NumDims,OPtr>& rhs) const {
    return rhs < *this;
  }

  template <typename OPtr>
  bool operator<=(const const_sub_array_openfpm<T,NumDims,OPtr>& rhs) const {
    return !(*this > rhs);
  }

  template <typename OPtr>
  bool operator>=(const const_sub_array_openfpm<T,NumDims,OPtr>& rhs) const {
    return !(*this < rhs);
  }

  TPtr origin() const { return base_; }
  inline __host__ __device__ size_type size() const { return boost::mpl::at<vector,boost::mpl::int_<0>>::type::value; }
  size_type max_size() const { return num_elements(); }
  bool empty() const { return size() == 0; }
incardon's avatar
incardon committed
122
  inline __device__ __host__ size_type num_dimensions() const { return NumDims; }
incardon's avatar
incardon committed
123
124
125
126
127
128
129
  inline __host__ __device__ const index* strides() const { return strides_; }

  size_type num_elements() const
  {
    return size_ct::type::value;
  }

incardon's avatar
incardon committed
130
131
132
  __device__ __host__ const_sub_array_openfpm (TPtr base, const index* strides)
  :base_(base), strides_(strides)
  {}
incardon's avatar
incardon committed
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216

#ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
protected:
  template <typename,std::size_t,typename> friend class value_accessor_n_openfpm;
  template <typename,std::size_t,typename,typename> friend class const_sub_array_openfpm;
#else
public:  // Should be protected
#endif


  TPtr base_;
  const index* strides_;
private:
  // const_sub_array cannot be assigned to (no deep copies!)
  const_sub_array_openfpm& operator=(const const_sub_array_openfpm&);
};

//
// sub_array
//    multi_array's proxy class to allow multiple overloads of
//    operator[] in order to provide a clean multi-dimensional array
//    interface.
template <typename T, std::size_t NumDims, typename vector>
class sub_array_openfpm : public const_sub_array_openfpm<T,NumDims,vector,T*>
{
  typedef const_sub_array_openfpm<T,NumDims,vector,T*> super_type;
public:
  typedef typename super_type::element element;
  typedef typename super_type::reference reference;
  typedef typename super_type::index index;
  typedef typename super_type::size_type size_type;
  typedef typename super_type::iterator iterator;
  typedef typename super_type::reverse_iterator reverse_iterator;
  typedef typename super_type::const_reference const_reference;
  typedef typename super_type::const_iterator const_iterator;
  typedef typename super_type::const_reverse_iterator const_reverse_iterator;
  typedef int yes_is_multi_array;

  // template typedefs
  template <std::size_t NDims>
  struct const_array_view {
    typedef openfpm::detail::multi_array::const_multi_array_view_openfpm<T,NDims> type;
  };

  template <std::size_t NDims>
  struct array_view {
    typedef openfpm::detail::multi_array::multi_array_view_openfpm<T,NDims> type;
  };

  // Assignment from other ConstMultiArray types.
  template <typename ConstMultiArray>
  __device__ __host__ inline sub_array_openfpm& operator=(const ConstMultiArray& other)
  {
#ifdef SE_CLASS1

    // make sure the dimensions agree
    BOOST_ASSERT(other.num_dimensions() == this->num_dimensions());

#endif
    // iterator-based copy
//    std::copy(other.begin(),other.end(),begin());

	for (int i = 0 ; i < (int)other.size() ; i++)
	{this->operator[](i) = other[i];}
    return *this;
  }

  __device__ __host__ sub_array_openfpm& operator=(const sub_array_openfpm& other)
  {
#ifdef SE_CLASS1
      // make sure the dimensions agree
      BOOST_ASSERT(other.num_dimensions() == this->num_dimensions());
//      BOOST_ASSERT(std::equal(other.shape(),
//                              other.shape()+this->num_dimensions(),
//                              this->shape()));
#endif
      // iterator-based copy
      //std::copy(other.begin(),other.end(),begin());

    	  for (int i = 0 ; i < (int)other.size() ; i++)
    	  {this->operator[](i) = other[i];}
    return *this;
  }

incardon's avatar
incardon committed
217
  __device__ __host__ T* origin_mutable() const { return const_cast<T*>(this->base_); }
incardon's avatar
incardon committed
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
  __device__ __host__ T* origin() { return this->base_; }
  __device__ __host__ const T* origin() const { return this->base_; }

  __device__ __host__ reference operator[](index idx) {
    return super_type::access(boost::type<reference>(),
                              idx,
                              this->strides(),
                              this->base_);
  }

  __device__ __host__ iterator begin()
  {
    return iterator(*this->index_bases(),origin(),
                    this->shape(),this->strides(),this->index_bases());
  }

  __device__ __host__ iterator end()
  {
    return iterator(*this->index_bases()+(index)*this->shape(),origin(),
                    this->shape(),this->strides(),this->index_bases());
  }

  // RG - rbegin() and rend() written naively to thwart MSVC ICE.
  reverse_iterator rbegin() {
    reverse_iterator ri(end());
    return ri;
  }

  reverse_iterator rend() {
    reverse_iterator ri(begin());
    return ri;
  }

  //
  // proxies
  //

  template <class IndexList>
  const element& operator()(const IndexList& indices) const {
    boost::function_requires<
      boost::CollectionConcept<IndexList> >();
    return super_type::operator()(indices);
  }

i-bird's avatar
i-bird committed
262
  const_reference __device__ __host__ operator[](index idx) const {
incardon's avatar
incardon committed
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
    return super_type::operator[](idx);
  }

  const_iterator begin() const {
    return super_type::begin();
  }

  const_iterator end() const {
    return super_type::end();
  }

  const_reverse_iterator rbegin() const {
    return super_type::rbegin();
  }

  const_reverse_iterator rend() const {
    return super_type::rend();
  }

#ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
private:
  template <typename,std::size_t,typename> friend class value_accessor_n_openfpm;
#endif
public: // should be private

  inline __device__ __host__ sub_array_openfpm (T* base,
            									const index* strides)
  :super_type(base,strides)
  {}

};

} // namespace multi_array
} // namespace detail
//
// traits classes to get sub_array types
//
template <typename Array, int N, typename vector>
class subarray_gen_openfpm {
  typedef typename Array::element element;
public:
  typedef openfpm::detail::multi_array::sub_array_openfpm<element,N,vector> type;
};

template <typename Array, int N, typename vector>
class const_subarray_gen_openfpm {
  typedef typename Array::element element;
public:
  typedef openfpm::detail::multi_array::const_sub_array_openfpm<element,N,vector> type;
};
} // namespace boost


#endif /* MULTI_ARRAY_REF_SUBARRAY_OPENFPM_HPP_ */