SparseGrid_iterator.hpp 12.3 KB
Newer Older
incardon's avatar
incardon committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
/*
 * SparseGrid_iterator.hpp
 *
 *  Created on: Oct 24, 2017
 *      Author: i-bird
 */

#ifndef OPENFPM_DATA_SRC_SPARSEGRID_SPARSEGRID_ITERATOR_HPP_
#define OPENFPM_DATA_SRC_SPARSEGRID_SPARSEGRID_ITERATOR_HPP_

/*! \brief It store the position in space of the sparse grid
 *
 * linearized index
 *
 */
class grid_key_sparse_lin_dx
{
	//! chunk id
	size_t chunk;

	//! linearized id
	size_t lin_id;

public:

	inline grid_key_sparse_lin_dx(size_t chunk, size_t lin_id)
	:chunk(chunk),lin_id(lin_id)
	{

	}

	/*! \brief Return the chunk id
	 *
	 * \return the chunk id
	 *
	 */
	inline size_t getChunk() const
	{
		return chunk;
	}

	/*! \brief Return the linearized position in the chunk
	 *
	 * \return the linearized position in the chunk
	 *
	 */
	inline size_t getPos() const
	{
		return lin_id;
	}
};

incardon's avatar
incardon committed
53
54
55
56
57
58
/*! \brief This function fill the set of all non zero elements
 *
 *
 */
template<unsigned int n_ele>
inline void fill_mask(short unsigned int (& mask_it)[n_ele],
59
		       const size_t (& mask)[n_ele / (sizeof(size_t)*8) + (n_ele % (sizeof(size_t)*8) != 0) + 1],
incardon's avatar
incardon committed
60
61
62
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
		       size_t & mask_nele)
{
	mask_nele = 0;
	size_t index = 0;

	for (size_t i = 0 ; i < n_ele / (sizeof(size_t) * 8) ; i++)
	{
		size_t mi = mask[i];
		size_t tot_idx = 0;

		do
		{
#if defined(__GNUC__) || defined(__clang__)
			index = __builtin_ffsl(mi);
#elif defined(__INTEL_COMPILER)
			_BitScanForward64(&index,mi)
			index += 1;
#else
			index = 0;
			if (mi != 0)
			{
				while (mi >> index & 0x1 == 0)	{index++;}
				index += 1;
			}
#endif
			if (index != 0)
			{
				mask_it[mask_nele] = (index - 1 + tot_idx) + i*sizeof(size_t)*8;
				mask_nele++;

				mi = (index == 64)?0:mi >> index;
				tot_idx += index;
			}
		}
		while (index != 0);
	}
}
incardon's avatar
incardon committed
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
122
123
124
125
126
127
128
129
130
131
132
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
/*! \brief This function fill the set of all non zero elements
 *
 *
 */
template<unsigned int dim, unsigned int n_ele>
inline void fill_mask_box(short unsigned int (& mask_it)[n_ele],
		       const size_t (& mask)[n_ele / (sizeof(size_t)*8) + (n_ele % (sizeof(size_t)*8) != 0) + 1],
		       size_t & mask_nele,
			   Box<dim,size_t> & bx,
			   const grid_key_dx<dim> (& loc_grid)[n_ele])
{
	mask_nele = 0;
	size_t index = 0;

	for (size_t i = 0 ; i < n_ele / (sizeof(size_t) * 8) ; i++)
	{
		size_t mi = mask[i];
		size_t tot_idx = 0;

		do
		{
#if defined(__GNUC__) || defined(__clang__)
			index = __builtin_ffsl(mi);
#elif defined(__INTEL_COMPILER)
			_BitScanForward64(&index,mi)
			index += 1;
#else
			index = 0;
			if (mi != 0)
			{
				while (mi >> index & 0x1 == 0)	{index++;}
				index += 1;
			}
#endif
			if (index != 0)
			{
				size_t id = (index - 1 + tot_idx) + i*sizeof(size_t)*8;

				bool is_inside = true;
				// we check the point is inside inte
				for (size_t i = 0 ; i < dim ; i++)
				{
					if (loc_grid[id].get(i) < (long int)bx.getLow(i) ||
						loc_grid[id].get(i) > (long int)bx.getHigh(i))
					{
						is_inside = false;

						break;
					}
				}

				if (is_inside == true)
				{
					mask_it[mask_nele] = id;
					mask_nele++;
				}

				mi = (index == 64)?0:mi >> index;
				tot_idx += index;
			}
		}
		while (index != 0);
	}
}

incardon's avatar
incardon committed
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*! \brief This structure contain the information of a chunk
 *
 * \tparam dim dimensionality of the chunk
 * \tparam n_ele number of elements in the chunk
 *
 */
template<unsigned int dim, unsigned int n_ele>
struct cheader
{
	//! where is located the chunk
	grid_key_dx<dim> pos;

	//! how many elements in the chunk are set
	size_t nele;

	//! which elements in the chunks are set
	size_t mask[n_ele / (sizeof(size_t)*8) + (n_ele % (sizeof(size_t)*8) != 0) + 1];
};


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
217
218
219
220
221
222
223
224
225
/*! \brief Grid key sparse iterator on a sub-part of the domain
 *
 *
 */
template<unsigned dim, unsigned int n_ele>
class grid_key_sparse_dx_iterator_sub
{
	//! It store the information of each chunk
	const openfpm::vector<cheader<dim,n_ele>> * header;

	//! linearized id to position in coordinates conversion
	const grid_key_dx<dim> (* lin_id_pos)[n_ele];

	//! point to the actual chunk
	size_t chunk_id;

	//! Number of points in mask_it
	size_t mask_nele;

	//! Actual point in mask it
	size_t mask_it_pnt;

	//! Starting point
	grid_key_dx<dim> start;

	//! Stop point
	grid_key_dx<dim> stop;

	//! Sub-grid box
	Box<dim,size_t> bx;

	//! Chunk size
	size_t sz_cnk[dim];

	//! set of index in the chunk on which we have to iterate
	short unsigned int mask_it[n_ele];

	/*! \brief Everytime we move to a new chunk we calculate on which indexes we have to iterate
	 *
	 *
	 */
	void SelectValidAndFill_mask_it()
	{
226
		mask_it_pnt = 0;
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
262
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
		mask_nele = 0;

		while (mask_nele == 0 && chunk_id < header->size())
		{
			auto & mask = header->get(chunk_id).mask;

			Box<dim,size_t> cnk_box;

			for (size_t i = 0 ; i < dim ; i++)
			{
				cnk_box.setLow(i,header->get(chunk_id).pos.get(i));
				cnk_box.setHigh(i,header->get(chunk_id).pos.get(i) + sz_cnk[i] - 1);
			}

			Box<dim,size_t> inte;

			if (bx.Intersect(cnk_box,inte) == true)
			{
				inte -= header->get(chunk_id).pos.toPoint();
				fill_mask_box<dim,n_ele>(mask_it,mask,mask_nele,inte,*lin_id_pos);
			}
			else
			{mask_nele = 0;}

			chunk_id = (mask_nele == 0)?chunk_id + 1:chunk_id;

		}
	}

public:

	/*! \brief Default constructor
	 *
	 * \warning extremely unsafe
	 * If you use this constructor before use the iterator you should call reinitialize first
	 *
	 */
	grid_key_sparse_dx_iterator_sub()	{};

	grid_key_sparse_dx_iterator_sub(const openfpm::vector<cheader<dim,n_ele>> & header,
								const grid_key_dx<dim> (& lin_id_pos)[n_ele],
								const grid_key_dx<dim> & start,
								const grid_key_dx<dim> & stop,
								const size_t (& sz_cnk)[dim])
	:header(&header),lin_id_pos(&lin_id_pos),chunk_id(0),
	 mask_nele(0),mask_it_pnt(0),start(start),stop(stop)
	{
		for (size_t i = 0 ; i < dim ; i++)
		{
			this->sz_cnk[i] = sz_cnk[i];

			bx.setLow(i,start.get(i));
			bx.setHigh(i,stop.get(i));
		}

		SelectValidAndFill_mask_it();
	}

	/*! \brief Reinitialize the iterator
	 *
	 * it re-initialize the iterator with the passed grid_key_dx_iterator_sub
	 * the actual position of the grid_key_dx_iterator_sub is ignored
	 *
	 * \param g_s_it grid_key_dx_iterator_sub
	 *
	 */
	inline void reinitialize(const grid_key_sparse_dx_iterator_sub<dim,n_ele> & g_s_it)
	{
		header = g_s_it.header;
		lin_id_pos = g_s_it.lin_id_pos;
		chunk_id = g_s_it.chunk_id;
		mask_nele = g_s_it.mask_nele;
		mask_it_pnt = g_s_it.mask_it_pnt;
		start = g_s_it.start;
		stop = g_s_it.stop;
		bx = g_s_it.bx;
		for (size_t i = 0 ; i < dim ; i++)
		{sz_cnk[i] = g_s_it.sz_cnk[i];}

		memcpy(mask_it,g_s_it.mask_it,sizeof(short unsigned int)*n_ele);
	}

	inline grid_key_sparse_dx_iterator_sub<dim,n_ele> & operator++()
	{
		mask_it_pnt++;

		if (mask_it_pnt < mask_nele)
		{
			return *this;
		}

		chunk_id++;
		mask_it_pnt = 0;

		if (chunk_id < header->size())
		{
			SelectValidAndFill_mask_it();
		}

		return *this;
	}


	/*! \brief Return the position of the actual point in coordinates
	 *
	 * \return the position
	 *
	 */
	inline grid_key_dx<dim> get() const
	{
		grid_key_dx<dim> k_pos;

		size_t lin_id = mask_it[mask_it_pnt];

		for (size_t i = 0 ; i < dim ; i++)
		{
			k_pos.set_d(i,(*lin_id_pos)[lin_id].get(i) + header->get(chunk_id).pos.get(i));
		}

		return k_pos;
	}

	template<typename> struct Debug;

	/*! \brief Return the actual point linearized
	 *
	 * The disadvantage is that in general you cannot use move in this type of key
	 *
	 * \return the actual point
	 *
	 */
	inline grid_key_sparse_lin_dx getKeyF()
	{
		return grid_key_sparse_lin_dx(chunk_id,mask_it[mask_it_pnt]);
	}

	/*! \brief Return true if there is a next grid point
	 *
	 * \return true if there is the next grid point
	 *
	 */
	bool isNext()
	{
		return chunk_id < header->size();
	}

	/*! \brief Return the starting point for the iteration
	 *
	 * \return the starting point
	 *
	 */
	const grid_key_dx<dim> & getStart() const
	{
		return start;
	}

	/*! \brief Return the stop point for the iteration
	 *
	 * \return the stop point
	 *
	 */
	const grid_key_dx<dim> & getStop() const
	{
		return stop;
	}
incardon's avatar
incardon committed
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476

	/*! \brief Return the private member header
	 *
	 * \return header
	 *
	 */
	const openfpm::vector<cheader<dim,n_ele>> * private_get_header() const
	{return header;}

	/*! \brief Return the private member lin_id_pos
	 *
	 * \return lin_id_pos
	 *
	 */
	const grid_key_dx<dim> (* private_get_lin_id_pos() const)[n_ele]
	{return lin_id_pos;}

	/*! \brief Return the private member chunk_id
	 *
	 * \return chunk_id
	 *
	 */
	size_t private_get_chunk_id() const
	{return chunk_id;}

	/*! \brief Return the private member mask_nele
	 *
	 * \return mask_nele
	 *
	 */
	size_t private_get_mask_nele() const
	{return mask_nele;}


	/*! \brief Return the private member mask_it_pnt
	 *
	 * \return mask_it_pnt
	 *
	 */
	size_t private_get_mask_it_pnt() const
	{return mask_it_pnt;}

	/*! \brief Return the private member mask_it
	 *
	 * \return mask_it
	 *
	 */
	const short unsigned int (& private_get_mask_it() const) [n_ele]
	{
		return mask_it;
	}
};

/*! \brief Grid key sparse iterator
 *
 *
 */
template<unsigned dim, unsigned int n_ele>
class grid_key_sparse_dx_iterator
{
	//! It store the information of each chunk
	const openfpm::vector<cheader<dim,n_ele>> * header;

	//! linearized id to position in coordinates conversion
	const grid_key_dx<dim> (* lin_id_pos)[n_ele];

	//! point to the actual chunk
	size_t chunk_id;

	//! Number of points in mask_it
	size_t mask_nele;

	//! Actual point in mask it
	size_t mask_it_pnt;

	//! set of index in the chunk on which we have to iterate
	short unsigned int mask_it[n_ele];

	/*! \brief Everytime we move to a new chunk we calculate on which indexes we have to iterate
	 *
	 *
	 */
	void SelectValidAndFill_mask_it()
	{
		mask_nele = 0;
477
		mask_it_pnt = 0;
incardon's avatar
incardon committed
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557

		while (mask_nele == 0 && chunk_id < header->size())
		{
			auto & mask = header->get(chunk_id).mask;

			fill_mask<n_ele>(mask_it,mask,mask_nele);

			chunk_id = (mask_nele == 0)?chunk_id + 1:chunk_id;

		}
	}

public:

	/*! \brief Default constructor
	 *
	 * \warning extremely unsafe
	 * If you use this constructor before use the iterator you should call reinitialize first
	 *
	 */
	grid_key_sparse_dx_iterator()	{};

	grid_key_sparse_dx_iterator(const openfpm::vector<cheader<dim,n_ele>> * header,
								const grid_key_dx<dim> (* lin_id_pos)[n_ele])
	:header(header),lin_id_pos(lin_id_pos),chunk_id(0),mask_nele(0),mask_it_pnt(0)
	{
		SelectValidAndFill_mask_it();
	}

	inline grid_key_sparse_dx_iterator<dim,n_ele> & operator++()
	{
		mask_it_pnt++;

		if (mask_it_pnt < mask_nele)
		{
			return *this;
		}

		chunk_id++;
		mask_it_pnt = 0;

		if (chunk_id < header->size())
		{
			SelectValidAndFill_mask_it();
		}

		return *this;
	}

	/*! \brief Reinitialize the iterator
	 *
	 * it re-initialize the iterator with the passed grid_key_dx_iterator_sub
	 * the actual position of the grid_key_dx_iterator_sub is ignored
	 *
	 * \param g_s_it grid_key_dx_iterator
	 *
	 */
	inline void reinitialize(const grid_key_sparse_dx_iterator<dim,n_ele> & g_s_it)
	{
		header = g_s_it.header;
		lin_id_pos = g_s_it.lin_id_pos;
		chunk_id = g_s_it.chunk_id;
		mask_nele = g_s_it.mask_nele;
		mask_it_pnt = g_s_it.mask_it_pnt;

		memcpy(mask_it,g_s_it.mask_it,sizeof(short unsigned int)*n_ele);
	}

	/*! \brief Reinitialize the iterator
	 *
	 * it re-initialize the iterator with the passed grid_key_dx_iterator_sub
	 * the actual position of the grid_key_dx_iterator_sub is ignored
	 *
	 * \param g_s_it grid_key_dx_iterator
	 *
	 */
	inline void reinitialize(const grid_key_sparse_dx_iterator_sub<dim,n_ele> & g_s_it)
	{
		header = g_s_it.private_get_header();
		lin_id_pos = g_s_it.private_get_lin_id_pos();
558
		chunk_id = 0;
incardon's avatar
incardon committed
559

560
		SelectValidAndFill_mask_it();
incardon's avatar
incardon committed
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
	}

	/*! \brief Return the actual point
	 *
	 * \warning the key that this function return cannot be used with function like move
	 *
	 * \return the actual point
	 *
	 */
	inline grid_key_sparse_lin_dx getKeyF()
	{
		return grid_key_sparse_lin_dx(chunk_id,mask_it[mask_it_pnt]);
	}

	/*! \brief Return the position of the actual point in coordinates
	 *
	 * \return the position
	 *
	 */
incardon's avatar
incardon committed
580
	inline grid_key_dx<dim> get() const
incardon's avatar
incardon committed
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
	{
		grid_key_dx<dim> k_pos;

		size_t lin_id = mask_it[mask_it_pnt];

		for (size_t i = 0 ; i < dim ; i++)
		{
			k_pos.set_d(i,(*lin_id_pos)[lin_id].get(i) + header->get(chunk_id).pos.get(i));
		}

		return k_pos;
	}

	/*! \brief Return true if there is a next grid point
	 *
	 * \return true if there is the next grid point
	 *
	 */
	bool isNext()
	{
		return chunk_id < header->size();
	}
603
604
};

incardon's avatar
incardon committed
605
#endif /* OPENFPM_DATA_SRC_SPARSEGRID_SPARSEGRID_ITERATOR_HPP_ */