OpenFPM  5.2.0
Project that contain the implementation of distributed structures
NNc_array.hpp
1 /*
2  * NNc_array.hpp
3  *
4  * Created on: Feb 6, 2018
5  * Author: i-bird
6  */
7 
8 #ifndef OPENFPM_DATA_SRC_NN_CELLLIST_NNC_ARRAY_HPP_
9 #define OPENFPM_DATA_SRC_NN_CELLLIST_NNC_ARRAY_HPP_
10 
11 #include "Grid/grid_sm.hpp"
12 #include <boost/mpl/bool.hpp>
13 
19 template<unsigned int dim>
21 {
23  typedef boost::mpl::bool_< dim < 10 > type;
24 };
25 
26 /* \brief NNc_array
27  *
28  * \param size
29  *
30  */
31 template<unsigned int dim, unsigned int size, bool thr = as_array_nnc<dim>::type::value>
32 class NNc_array
33 {
35  long int NNc_arr[size];
36 
39 
40 public:
41 
47  void set_size(const size_t (& sz)[dim])
48  {
49  gs.setDimensions(sz);
50  }
51 
57  inline const long int & operator[](size_t i) const
58  {
59  return NNc_arr[i];
60  }
61 
67  inline long int & operator[](size_t i)
68  {
69  return NNc_arr[i];
70  }
71 
76  void init_full()
77  {
78  typedef typename generate_array<size_t,dim, Fill_zero>::result NNzero;
79  typedef typename generate_array<size_t,dim, Fill_two>::result NNtwo;
80  typedef typename generate_array<size_t,dim, Fill_one>::result NNone;
81 
82  // Generate the sub-grid iterator
83 
84  grid_key_dx_iterator_sub<dim> gr_sub3(gs,NNzero::data,NNtwo::data);
85 
86  // Calculate the NNc array
87 
88  size_t middle = gs.LinId(NNone::data);
89  size_t i = 0;
90  while (gr_sub3.isNext())
91  {
92  NNc_arr[i] = (long int)gs.LinId(gr_sub3.get()) - middle;
93 
94  ++gr_sub3;
95  i++;
96  }
97  }
98 
99 
104  void init_sym()
105  {
106  // compile-time array {0,0,0,....} {2,2,2,...} {1,1,1,...}
107 
108  typedef typename generate_array<size_t,dim, Fill_zero>::result NNzero;
109  typedef typename generate_array<size_t,dim, Fill_two>::result NNtwo;
110  typedef typename generate_array<size_t,dim, Fill_one>::result NNone;
111 
112  size_t middle = gs.LinId(NNone::data);
113 
114  // Generate the sub-grid iterator
115 
116  grid_key_dx_iterator_sub<dim> gr_sub3(gs,NNzero::data,NNtwo::data);
117 
118  // Calculate the NNc_sym array
119 
120  size_t i = 0;
121  while (gr_sub3.isNext())
122  {
123  auto key = gr_sub3.get();
124 
125  size_t lin = gs.LinId(key);
126 
127  // Only the first half is considered
128  if (lin < middle)
129  {
130  ++gr_sub3;
131  continue;
132  }
133 
134  NNc_arr[i] = lin - middle;
135 
136  ++gr_sub3;
137  i++;
138  }
139  }
140 
149  {
150  this->init_full();
151  }
152 
158  const long int * getPointer() const
159  {
160  return NNc_arr;
161  }
162 
169  {
170  std::copy(&nnc.NNc_arr[0],&nnc.NNc_arr[size],&NNc_arr[0]);
171  gs = nnc.gs;
172 
173  return *this;
174  }
175 
182  {
183  gs.swap(nnc.gs);
184 
185  long int NNc_full_tmp[openfpm::math::pow(3,dim)];
186 
187  std::copy(&nnc.NNc_arr[0],&nnc.NNc_arr[size],&NNc_full_tmp[0]);
188  std::copy(&NNc_arr[0],&NNc_arr[size],&nnc.NNc_arr[0]);
189  std::copy(&NNc_full_tmp[0],&NNc_full_tmp[size],&NNc_arr[0]);
190  }
191 };
192 
193 /* \brief NNc_array
194  *
195  * It encapsulate a 3^{dim} array containing the neighborhood cells-id
196  *
197  * \tparam dim dimensionality
198  * \tparam size total number of neighborhood cells
199  *
200  */
201 template<unsigned int dim, unsigned int size>
202 class NNc_array<dim,size,false>
203 {
207 
210 
212  size_t sub_off;
213  size_t sym_mid;
214 
215  bool full_or_sym;
216 
217 public:
218 
224  void set_size(const size_t (& sz)[dim])
225  {
226  typedef typename generate_array<size_t,dim, Fill_three>::result NNthree;
227 
228  gs.setDimensions(sz);
229  gs_base.setDimensions(NNthree::data);
230 
231  typedef typename generate_array<size_t,dim, Fill_one>::result NNone;
232  sub_off = gs.LinId(NNone::data);
233  sym_mid = gs_base.LinId(NNone::data);
234  }
235 
241  long int operator[](size_t i) const
242  {
243  if (full_or_sym == true)
244  {
245  grid_key_dx<dim> key = gs_base.InvLinId(i);
246  return gs.LinId(key) - sub_off;
247  }
248 
249  grid_key_dx<dim> key = gs_base.InvLinId(i + sym_mid);
250  return gs.LinId(key) - sub_off;
251  }
252 
253  void init_full()
254  {
255  full_or_sym = true;
256  }
257 
258  void init_sym()
259  {
260  full_or_sym = false;
261  }
262 
263  void init_sym_local()
264  {
265  full_or_sym = false;
266  }
267 
273  const long int * getPointer() const
274  {
275  std::cerr << __FILE__ << ":" << __LINE__ << " error dimension is too high to use this type of neighborhood" << std::endl;
276  return NULL;
277  }
278 
285  {
286  gs = nnc.gs;
287  gs_base = nnc.gs_base;
288  sub_off = nnc.sub_off;
289  sym_mid = nnc.sym_mid;
290 
291  full_or_sym = nnc.full_or_sym;
292 
293  return *this;
294  }
295 
302  {
303  gs.swap(nnc.gs);
304  gs_base.swap(nnc.gs_base);
305 
306  size_t sub_off_tmp = sub_off;
307  sub_off = nnc.sub_off;
308  nnc.sub_off = sub_off_tmp;
309 
310  size_t sym_mid_tmp = sym_mid;
311  sym_mid = nnc.sym_mid;
312  nnc.sym_mid = sym_mid_tmp;
313 
314  bool full_or_sym_tmp = full_or_sym;
315  full_or_sym = nnc.full_or_sym;
316  nnc.full_or_sym = full_or_sym_tmp;
317  }
318 };
319 
320 
321 #endif /* OPENFPM_DATA_SRC_NN_CELLLIST_NNC_ARRAY_HPP_ */
NNc_array< dim, size, false > & operator=(const NNc_array< dim, size, false > &nnc)
Copy the NNc_array.
Definition: NNc_array.hpp:284
const long int * getPointer() const
return the pointer to the array
Definition: NNc_array.hpp:273
void swap(NNc_array< dim, size, false > &nnc)
swap the NNc_array
Definition: NNc_array.hpp:301
void set_size(const size_t(&sz)[dim])
set the size of the cell grid
Definition: NNc_array.hpp:224
long int operator[](size_t i) const
return the element i
Definition: NNc_array.hpp:241
grid_sm< dim, void > gs
Definition: NNc_array.hpp:206
grid_sm< dim, void > gs_base
Information about the grid in the reduced space.
Definition: NNc_array.hpp:209
const long int * getPointer() const
return the pointer to the array
Definition: NNc_array.hpp:158
grid_sm< dim, void > gs
size of the cell array on each dimension
Definition: NNc_array.hpp:38
void swap(NNc_array< dim, size, thr > &nnc)
swap NNc_array
Definition: NNc_array.hpp:181
NNc_array< dim, size, thr > & operator=(const NNc_array< dim, size, thr > &nnc)
Copy the NNc_array.
Definition: NNc_array.hpp:168
const long int & operator[](size_t i) const
return the element i
Definition: NNc_array.hpp:57
void init_sym()
Initialize the NNc array with symmetric neighborhood cells indexes.
Definition: NNc_array.hpp:104
long int & operator[](size_t i)
return the element i
Definition: NNc_array.hpp:67
void init_full()
Initialize the NNc array with full neighborhood cells indexes.
Definition: NNc_array.hpp:76
long int NNc_arr[size]
NNc_array.
Definition: NNc_array.hpp:35
void init_sym_local()
Initialize the NNc array with symmetric local neighborhood cells indexes.
Definition: NNc_array.hpp:148
void set_size(const size_t(&sz)[dim])
Set the size in each.
Definition: NNc_array.hpp:47
bool isNext()
Check if there is the next element.
grid_key_dx< dim > get() const
Return the actual grid key iterator.
void setDimensions(const size_t(&dims)[N])
Reset the dimension of the grid.
Definition: grid_sm.hpp:326
mem_id LinId(const grid_key_dx< N, ids_type > &gk, const signed char sum_id[N]) const
Linearization of the grid_key_dx with a specified shift.
Definition: grid_sm.hpp:454
void swap(grid_sm< N, T > &g)
swap the grid_sm informations
Definition: grid_sm.hpp:830
__device__ __host__ grid_key_dx< N > InvLinId(mem_id id) const
Construct.
Definition: grid_sm.hpp:609
KeyT const ValueT ValueT OffsetIteratorT OffsetIteratorT int
[in] The number of segments that comprise the sorting data
Set a dimension threshold.
Definition: NNc_array.hpp:21