grid_dist_id.hpp 34.8 KB
Newer Older
incardon's avatar
incardon committed
1
2
3
4
#ifndef COM_UNIT_HPP
#define COM_UNIT_HPP

#include <vector>
5
#include <unordered_map>
incardon's avatar
incardon committed
6
7
8
#include "Grid/map_grid.hpp"
#include "VCluster.hpp"
#include "Space/SpaceBox.hpp"
9
#include "util/mathutil.hpp"
incardon's avatar
incardon committed
10
#include "grid_dist_id_iterator.hpp"
incardon's avatar
incardon committed
11
#include "grid_dist_id_iterator_sub.hpp"
incardon's avatar
incardon committed
12
#include "grid_dist_key.hpp"
13
#include "NN/CellList/CellDecomposer.hpp"
14
15
16
#include "util/object_util.hpp"
#include "memory/ExtPreAlloc.hpp"
#include "VTKWriter.hpp"
17
18
#include "Packer_Unpacker/Packer.hpp"
#include "Packer_Unpacker/Unpacker.hpp"
incardon's avatar
incardon committed
19
20
21
22
23
24

#define SUB_UNIT_FACTOR 64


/*! \brief This is a distributed grid
 *
25
26
 * Implementation of a distributed grid the decomposition is geometrical, grid
 * is splitted across several processor
incardon's avatar
incardon committed
27
28
 *
 * \param dim Dimensionality of the grid
29
30
 * \param St Type of space where the grid is living
 * \param T object the grid is storing
incardon's avatar
incardon committed
31
32
33
34
 * \param Decomposition Class that decompose the grid for example CartDecomposition
 * \param Mem Is the allocator
 * \param device type of base structure is going to store the data
 *
35
36
37
38
39
40
 * ### Create a distributed grid and access it
 * \snippet grid_dist_id_unit_test.hpp Create and access a distributed grid
 * ### Synchronize the ghosts and check the information
 * \snippet grid_dist_id_unit_test.hpp Synchronize the ghost and check the information
 * ### Create and access a distributed grid for complex structures
 * \snippet grid_dist_id_unit_test.hpp Create and access a distributed grid complex
incardon's avatar
incardon committed
41
 * ### Synchronize a distributed grid for complex structures
42
 * \snippet grid_dist_id_unit_test.hpp Synchronized distributed grid complex
incardon's avatar
incardon committed
43
44
 * ### Usage of a grid dist iterator sub
 * \snippet grid_dist_id_unit_test.hpp Usage of a sub_grid iterator
incardon's avatar
incardon committed
45
46
 * ### Construct two grid with the same decomposition
 * \snippet grid_dist_id_unit_test.hpp Construct two grid with the same decomposition
47
 *
incardon's avatar
incardon committed
48
 */
49
template<unsigned int dim, typename St, typename T, typename Decomposition,typename Memory=HeapMemory , typename device_grid=grid_cpu<dim,T> >
incardon's avatar
incardon committed
50
51
class grid_dist_id
{
52
53
54
	// Domain
	Box<dim,St> domain;

incardon's avatar
incardon committed
55
	// Ghost expansion
56
	Ghost<dim,St> ghost;
incardon's avatar
incardon committed
57
58
59
60
61

	//! Local grids
	Vcluster_object_array<device_grid> loc_grid;

	//! Space Decomposition
62
	Decomposition dec;
incardon's avatar
incardon committed
63

incardon's avatar
incardon committed
64
65
66
	//! Extension of each grid: Domain and ghost + domain
	openfpm::vector<GBoxes<device_grid::dims>> gdb_ext;

incardon's avatar
incardon committed
67
68
69
	//! Size of the grid on each dimension
	size_t g_sz[dim];

70
71
	//! Structure that divide the space into cells
	CellDecomposer_sm<dim,St> cd_sm;
incardon's avatar
incardon committed
72

73
	//! Communicator class
incardon's avatar
incardon committed
74
75
	Vcluster & v_cl;

76
77
78
79
80
81
82
83
84
	//! It map a global ghost id (g_id) to the external ghost box information
	std::unordered_map<size_t,size_t> g_id_to_external_ghost_box;

	// Receiving size
	openfpm::vector<size_t> recv_sz;

	// Receiving buffer for particles ghost get
	openfpm::vector<HeapMemory> recv_mem_gg;

incardon's avatar
incardon committed
85
86
87
88
89
90
	// Grid informations object
	grid_sm<dim,T> ginfo;

	// Grid informations object without type
	grid_sm<dim,void> ginfo_v;

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
	/*! \brief Call-back to allocate buffer to receive incoming objects (external ghost boxes)
	 *
	 * \param msg_i message size required to receive from i
	 * \param total_msg message size to receive from all the processors
	 * \param total_p the total number of processor want to communicate with you
	 * \param i processor id
	 * \param ri request id (it is an id that goes from 0 to total_p, and is unique
	 *           every time message_alloc is called)
	 * \param ptr void pointer parameter for additional data to pass to the call-back
	 *
	 */
	static void * msg_alloc_external_box(size_t msg_i ,size_t total_msg, size_t total_p, size_t i, size_t ri, void * ptr)
	{
		grid_dist_id<dim,St,T,Decomposition,Memory,device_grid> * g = static_cast<grid_dist_id<dim,St,T,Decomposition,Memory,device_grid> *>(ptr);

		g->recv_sz.resize(g->dec.getNNProcessors());
		g->recv_mem_gg.resize(g->dec.getNNProcessors());

		// Get the local processor id
		size_t lc_id = g->dec.ProctoID(i);

		// resize the receive buffer
		g->recv_mem_gg.get(lc_id).resize(msg_i);
		g->recv_sz.get(lc_id) = msg_i;

		return g->recv_mem_gg.get(lc_id).getPointer();
	}

119
	/*! \brief Create per-processor internal ghost boxes list in grid units and g_id_to_external_ghost_box
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
	 *
	 */
	void create_ig_box()
	{
		// Get the grid info
		auto g = cd_sm.getGrid();

		if (init_i_g_box == true)	return;

		// Get the number of near processors
		for (size_t i = 0 ; i < dec.getNNProcessors() ; i++)
		{
			ig_box.add();
			auto&& pib = ig_box.last();

			pib.prc = dec.IDtoProc(i);
			for (size_t j = 0 ; j < dec.getProcessorNIGhost(i) ; j++)
			{
				// Get the internal ghost boxes and transform into grid units
incardon's avatar
incardon committed
139
140
141
142
143
144
				::Box<dim,St> ib_dom = dec.getProcessorIGhostBox(i,j);
				::Box<dim,size_t> ib = cd_sm.convertDomainSpaceIntoGridUnits(ib_dom);

				// Check if ib is valid if not it mean that the internal ghost does not contain information so skip it
				if (ib.isValid() == false)
					continue;
145
146
147
148

				// save the box and the sub-domain id (it is calculated as the linearization of P1)
				::Box<dim,size_t> cvt = ib;

149
150
				i_box_id bid_t;
				bid_t.box = cvt;
151
				bid_t.g_id = dec.getProcessorIGhostId(i,j);
152
				bid_t.sub = dec.getProcessorIGhostSub(i,j);
153
154
155
156
157
158
159
				pib.bid.add(bid_t);
			}
		}

		init_i_g_box = true;
	}

160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
	/*! \brief Create per-processor internal ghost box list in grid units
	 *
	 */
	void create_eg_box()
	{
		// Get the grid info
		auto g = cd_sm.getGrid();

		if (init_e_g_box == true)	return;

		// Get the number of near processors
		for (size_t i = 0 ; i < dec.getNNProcessors() ; i++)
		{
			eg_box.add();
			auto&& pib = eg_box.last();

			pib.prc = dec.IDtoProc(i);
			for (size_t j = 0 ; j < dec.getProcessorNEGhost(i) ; j++)
			{
incardon's avatar
incardon committed
179
180
181
182
183
184
185
				// Get the external ghost boxes and transform into grid units
				::Box<dim,St> ib_dom = dec.getProcessorEGhostBox(i,j);
				::Box<dim,size_t> ib = cd_sm.convertDomainSpaceIntoGridUnits(ib_dom);

				// Check if ib is valid if not it mean that the internal ghost does not contain information so skip it
				if (ib.isValid() == false)
					continue;
186
187
188

				// save the box and the unique external ghost box id (linearization of P1)
				// It is (locally) unique because it is ensured that external ghost boxes does not overlap
incardon's avatar
incardon committed
189
				// Carefull it is not unique from the internal ghost box
190
191
192
193
194
195

				// sub domain id at which belong the external ghost box
				size_t sub_id = dec.getProcessorEGhostSub(i,j);

				e_box_id bid_t;
				bid_t.sub = sub_id;
196
197
				bid_t.g_e_box = ib;
				bid_t.l_e_box = ib;
198
				// Translate in local coordinate
199
				Box<dim,long int> tb = ib;
incardon's avatar
incardon committed
200
201
				tb -= gdb_ext.get(sub_id).origin;
				bid_t.l_e_box = tb;
202
203

				pib.bid.add(bid_t);
incardon's avatar
incardon committed
204
205

				// Add the map between the global ghost box id and id of the external box in the vector
206
				size_t g_id = dec.getProcessorEGhostId(i,j);
207
				g_id_to_external_ghost_box[g_id] = pib.bid.size()-1;
208
209
210
211
212
213
			}
		}

		init_e_g_box = true;
	}

incardon's avatar
incardon committed
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
	bool init_local_i_g_box = false;

	/*! \brief Create local internal ghost box in grid units
	 *
	 */
	void create_local_ig_box()
	{
		// Get the grid info
		auto g = cd_sm.getGrid();

		if (init_local_i_g_box == true)	return;

		// Get the number of near processors
		for (size_t i = 0 ; i < dec.getNLocalHyperCube() ; i++)
		{
			loc_ig_box.add();
			auto&& pib = loc_ig_box.last();

			for (size_t j = 0 ; j < dec.getLocalNIGhost(i) ; j++)
			{
				// Get the internal ghost boxes and transform into grid units
				::Box<dim,St> ib_dom = dec.getLocalIGhostBox(i,j);
				::Box<dim,size_t> ib = cd_sm.convertDomainSpaceIntoGridUnits(ib_dom);

				// Check if ib is valid if not it mean that the internal ghost does not contain information so skip it
				if (ib.isValid() == false)
					continue;

incardon's avatar
incardon committed
242
243
244
245
				pib.bid.add();
				pib.bid.last().box = ib;
				pib.bid.last().sub = dec.getLocalIGhostSub(i,j);
				pib.bid.last().k = dec.getLocalIGhostE(i,j);
incardon's avatar
incardon committed
246
247
248
249
250
251
252
253
			}
		}

		init_local_i_g_box = true;
	}

	bool init_local_e_g_box = false;

254
	/*! \brief Create per-processor external ghost boxes list in grid units
incardon's avatar
incardon committed
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
	 *
	 */
	void create_local_eg_box()
	{
		// Get the grid info
		auto g = cd_sm.getGrid();

		if (init_local_e_g_box == true)	return;

		// Get the number of near processors
		for (size_t i = 0 ; i < dec.getNLocalHyperCube() ; i++)
		{
			loc_eg_box.add();
			auto&& pib = loc_eg_box.last();

			for (size_t j = 0 ; j < dec.getLocalNEGhost(i) ; j++)
			{
				// Get the internal ghost boxes and transform into grid units
				::Box<dim,St> ib_dom = dec.getLocalEGhostBox(i,j);
				::Box<dim,size_t> ib = cd_sm.convertDomainSpaceIntoGridUnits(ib_dom);

incardon's avatar
incardon committed
276
277
278
				// Warning even if the ib is not a valid in grid unit we are forced to keep it
				// otherwise the value returned from dec.getLocalEGhostSub(i,j) will point to an
				// invalid or wrong box
incardon's avatar
incardon committed
279

incardon's avatar
incardon committed
280
281
282
				pib.bid.add();
				pib.bid.last().box = ib;
				pib.bid.last().sub = dec.getLocalEGhostSub(i,j);
incardon's avatar
incardon committed
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
			}
		}

		init_local_e_g_box = true;
	}

	/*! \brief Sync the local ghost part
	 *
	 * \tparam prp... properties to sync
	 *
	 */
	template<int... prp> void ghost_get_local()
	{
		//! For all the sub-domains
		for (size_t i = 0 ; i < loc_ig_box.size() ; i++)
		{
			//! For all the internal ghost boxes of each sub-domain
			for (size_t j = 0 ; j < loc_ig_box.get(i).bid.size() ; j++)
			{
incardon's avatar
incardon committed
302
303
304
				Box<dim,size_t> bx_src = loc_ig_box.get(i).bid.get(j).box;
				// convert into local
				bx_src -= gdb_ext.get(i).origin;
incardon's avatar
incardon committed
305

incardon's avatar
incardon committed
306
307
				// sub domain connected with external box
				size_t sub_id_dst = loc_ig_box.get(i).bid.get(j).sub;
incardon's avatar
incardon committed
308
309

				// local external ghost box connected
incardon's avatar
incardon committed
310
				size_t k = loc_ig_box.get(i).bid.get(j).k;
incardon's avatar
incardon committed
311

incardon's avatar
incardon committed
312
313
314
315
				Box<dim,size_t> bx_dst = loc_eg_box.get(sub_id_dst).bid.get(k).box;

				// convert into local
				bx_dst -= gdb_ext.get(sub_id_dst).origin;
incardon's avatar
incardon committed
316
317
318

				// create 2 sub grid iterator
				grid_key_dx_iterator_sub<dim> sub_src(loc_grid.get(i).getGrid(),bx_src.getKP1(),bx_src.getKP2());
incardon's avatar
incardon committed
319
				grid_key_dx_iterator_sub<dim> sub_dst(loc_grid.get(sub_id_dst).getGrid(),bx_dst.getKP1(),bx_dst.getKP2());
incardon's avatar
incardon committed
320
321
322

#ifdef DEBUG

incardon's avatar
incardon committed
323
324
325
				if (loc_eg_box.get(sub_id_dst).bid.get(k).sub != i)
					std::cerr << "Error " << __FILE__ << ":" << __LINE__ << " source and destination are not correctly linked" << "\n";

incardon's avatar
incardon committed
326
327
328
329
330
331
				if (sub_src.getVolume() != sub_dst.getVolume())
					std::cerr << "Error " << __FILE__ << ":" << __LINE__ << " source and destination does not match in size" << "\n";

#endif

				const auto & gs = loc_grid.get(i);
incardon's avatar
incardon committed
332
				auto & gd = loc_grid.get(sub_id_dst);
incardon's avatar
incardon committed
333
334
335
336

				while (sub_src.isNext())
				{
					// Option 1
incardon's avatar
incardon committed
337
					gd.set(sub_dst.get(),gs,sub_src.get());
incardon's avatar
incardon committed
338
339

					// Option 2
incardon's avatar
incardon committed
340
//					gd.get_o(sub_dst.get()) = gs.get_o(sub_src.get());
incardon's avatar
incardon committed
341
342
343
344
345
346
347
348

					++sub_src;
					++sub_dst;
				}
			}
		}
	}

349
	/*! \brief Check the grid has a valid size
incardon's avatar
incardon committed
350
351
352
353
354
355
356
357
358
359
	 *
	 */
	inline void check_size(const size_t (& g_sz)[dim])
	{
		for (size_t i = 0 ; i < dim ; i++)
		{
			if (g_sz[i] < 2)
				std::cerr << "Error: " << __FILE__ << ":" << __LINE__ << " distrobuted grids with size smaller than 2 are not supported\n";
		}
	}
incardon's avatar
incardon committed
360

incardon's avatar
incardon committed
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
392
393
394
395
396
397
398
	/*! \brief Create the grids on memory
	 *
	 */
	void Create()
	{
		Box<dim,St> g_rnd_box;
		for (size_t i = 0 ; i < dim ; i++)	{g_rnd_box.setHigh(i,0.5); g_rnd_box.setLow(i,-0.5);}

		// Get the number of local grid needed
		size_t n_grid = dec.getNLocalHyperCube();

		// create local grids for each hyper-cube
		loc_grid = v_cl.allocate<device_grid>(n_grid);

		// Size of the grid on each dimension
		size_t l_res[dim];

		// Allocate the grids
		for (size_t i = 0 ; i < n_grid ; i++)
		{
			gdb_ext.add();

			// Get the local hyper-cube
			SpaceBox<dim,St> sp = dec.getLocalHyperCube(i);
			SpaceBox<dim,St> sp_g = dec.getSubDomainWithGhost(i);

			// Convert from SpaceBox<dim,St> to SpaceBox<dim,long int>
			SpaceBox<dim,long int> sp_t = cd_sm.convertDomainSpaceIntoGridUnits(sp);
			SpaceBox<dim,long int> sp_tg = cd_sm.convertDomainSpaceIntoGridUnits(sp_g);

			//! Save the origin of the sub-domain of the local grid
			gdb_ext.last().origin = sp_tg.getP1();

			// save information about the local grid: domain box seen inside the domain + ghost box (see GDBoxes for a visual meaning)
			// and where the GDBox start, or the origin of the local grid (+ghost) in global coordinate
			gdb_ext.last().Dbox = sp_t;
			gdb_ext.last().Dbox -= sp_tg.getP1();

399
400
401
			gdb_ext.last().GDbox = sp_tg;
			gdb_ext.last().GDbox -= sp_tg.getP1();

incardon's avatar
incardon committed
402
403
404
405
406
407
408
409
410
411
412
			// center to zero
			sp_tg -= sp_tg.getP1();

			// Get the size of the local grid
			for (size_t i = 0 ; i < dim ; i++) {l_res[i] = (sp_tg.getHigh(i) >= 0)?(sp_tg.getHigh(i)+1):0;}

			// Set the dimensions of the local grid
			loc_grid.get(i).resize(l_res);
		}
	}

incardon's avatar
incardon committed
413
414
415
416
417
	/*! \brief Default Copy constructor on this class make no sense and is unsafe, this definition disable it
	 *
	 */
	grid_dist_id(const grid_dist_id<dim,St,T,Decomposition,Memory,device_grid> & g)
	{
418
419
420
#ifdef SE_CLASS2
		check_new(this,8,GRID_DIST_EVENT,4);
#endif
incardon's avatar
incardon committed
421
	}
incardon's avatar
incardon committed
422

incardon's avatar
incardon committed
423
	void write_ie_boxes(std::string output)
incardon's avatar
incardon committed
424
	{
incardon's avatar
incardon committed
425
426
		// Write internal ghost box
		VTKWriter<openfpm::vector<::Box<dim,size_t>>,VECTOR_BOX> vtk_box1;
incardon's avatar
incardon committed
427

incardon's avatar
incardon committed
428
		openfpm::vector< openfpm::vector< ::Box<dim,size_t> > > boxes;
incardon's avatar
incardon committed
429

incardon's avatar
incardon committed
430
431
		//! Carefully we have to ensure that boxes does not reallocate inside the for loop
		boxes.reserve(ig_box.size());
incardon's avatar
incardon committed
432

incardon's avatar
incardon committed
433
434
435
436
		//! Write internal ghost in grid units (Color encoded)
		for (size_t p = 0 ; p < ig_box.size() ; p++)
		{
			boxes.add();
incardon's avatar
incardon committed
437

incardon's avatar
incardon committed
438
439
440
441
442
			// Create a vector of boxes
			for (size_t j = 0 ; j < ig_box.get(p).bid.size() ; j++)
			{
				boxes.last().add(ig_box.get(p).bid.get(j).box);
			}
443

incardon's avatar
incardon committed
444
445
446
			vtk_box1.add(boxes.last());
		}
		vtk_box1.write(output + std::string("internal_ghost_") + std::to_string(v_cl.getProcessUnitID()) + std::string(".vtk"));
incardon's avatar
incardon committed
447
448
	}

Pietro Incardona's avatar
Pietro Incardona committed
449
    /*! \brief Initialize the Cell decomposer of the grid
incardon's avatar
incardon committed
450
451
452
453
	 *
	 *
	 */
	inline void InitializeCellDecomposer(const size_t (& g_sz)[dim])
incardon's avatar
incardon committed
454
	{
incardon's avatar
incardon committed
455
		// check that the grid has valid size
incardon's avatar
incardon committed
456
457
		check_size(g_sz);

incardon's avatar
incardon committed
458
459
		// For a 5x5 grid you have 4x4 Cell
		size_t c_g[dim];
incardon's avatar
incardon committed
460
		for (size_t i = 0 ; i < dim ; i++)	{c_g[i] = (g_sz[i]-1 > 0)?(g_sz[i]-1):1;}
incardon's avatar
incardon committed
461
462
463

		// Initialize the cell decomposer
		cd_sm.setDimensions(domain,c_g,0);
incardon's avatar
incardon committed
464
	}
incardon's avatar
incardon committed
465

incardon's avatar
incardon committed
466
467
468
469
470
	/*! \brief Initialize the grid
	 *
	 * \param g_sz Global size of the grid
	 *
	 */
incardon's avatar
incardon committed
471
	inline void InitializeDecomposition(const size_t (& g_sz)[dim])
incardon's avatar
incardon committed
472
	{
incardon's avatar
incardon committed
473
		// fill the global size of the grid
incardon's avatar
incardon committed
474
		for (size_t i = 0 ; i < dim ; i++)	{this->g_sz[i] = g_sz[i];}
incardon's avatar
incardon committed
475
476
477
478
479
480
481
482
483

		// Get the number of processor and calculate the number of sub-domain
		// for decomposition
		size_t n_proc = v_cl.getProcessingUnits();
		size_t n_sub = n_proc * SUB_UNIT_FACTOR;

		// Calculate the maximum number (before merging) of sub-domain on
		// each dimension
		size_t div[dim];
incardon's avatar
incardon committed
484
		for (size_t i = 0 ; i < dim ; i++)
485
		{div[i] = openfpm::math::round_big_2(pow(n_sub,1.0/dim));}
incardon's avatar
incardon committed
486

Pietro Incardona's avatar
Pietro Incardona committed
487
488
489
490
491
		// boundary conditions
		size_t bc[dim];
		for (size_t i = 0 ; i < dim ; i++)
			bc[i] = NON_PERIODIC;

incardon's avatar
incardon committed
492
		// Create the sub-domains
Pietro Incardona's avatar
Pietro Incardona committed
493
		dec.setParameters(div,domain,bc,ghost);
incardon's avatar
incardon committed
494

495
		// Calculate ghost boxes
incardon's avatar
incardon committed
496
497
498
		dec.calculateGhostBoxes();
	}

incardon's avatar
incardon committed
499
500
501
502
503
504
505
506
507
508
509
510
511
512
	/*! \brief Initialize the grid
	 *
	 * \param g_sz Global size of the grid
	 *
	 */
	inline void InitializeStructures(const size_t (& g_sz)[dim])
	{
		// fill the global size of the grid
		for (size_t i = 0 ; i < dim ; i++)	{this->g_sz[i] = g_sz[i];}

		// Create local grid
		Create();
	}

Pietro Incardona's avatar
Pietro Incardona committed
513
protected:
incardon's avatar
incardon committed
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534

	/*! \brief Get the point where it start the origin of the grid in the sub-domain i
	 *
	 * \return the point
	 *
	 */
	Point<dim,St> getOffset(size_t i)
	{
		return Point<dim,St>(gdb_ext.get(i).origin) * cd_sm.getCellBox().getP2();
	}

	/*! \brief Given a local sub-domain i with a local grid Domain + ghost return the part of the local grid that is domain
	 *
	 * \return the Box defining the domain in the local grid
	 *
	 */
	Box<dim,size_t> getDomain(size_t i)
	{
		return gdb_ext.get(i).Dbox;
	}

incardon's avatar
incardon committed
535
536
537
538
539
540
541
542
public:

	// Which kind of grid the structure store
	typedef device_grid d_grid;

	// Decomposition used
	typedef Decomposition decomposition;

Pietro Incardona's avatar
Pietro Incardona committed
543
544
545
546
547
548
549
550
551
552
553
554
555
	// value_type
	typedef T value_type;

	/*! \brief Return the total number of points in the grid
	 *
	 * \return number of points
	 *
	 */
	size_t size() const
	{
		return ginfo_v.size();
	}

Pietro Incardona's avatar
Pietro Incardona committed
556
557
558
559
560
561
562
563
564
	/*! \brief Return the total number of points in the grid
	 *
	 * \param i direction
	 *
	 * \return number of points on direction i
	 *
	 */
	size_t size(size_t i) const
	{
565
		return ginfo_v.size(i);
Pietro Incardona's avatar
Pietro Incardona committed
566
567
	}

Pietro Incardona's avatar
Pietro Incardona committed
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
	static inline Ghost<dim,float> convert_ghost(const Ghost<dim,long int> & gd,const CellDecomposer_sm<dim,St> & cd_sm)
	{
		Ghost<dim,float> gc;

		// get the grid spacing
		Box<dim,St> sp = cd_sm.getCellBox();

		// enlarge 0.001 of the spacing
		sp.magnify_fix_P1(1.1);

		// set the ghost
		for (size_t i = 0 ; i < dim ; i++)
		{
			gc.setLow(i,-sp.getHigh(i));
			gc.setHigh(i,sp.getHigh(i));
		}

		return gc;
	}

incardon's avatar
incardon committed
588
589

    //! constructor
590
    grid_dist_id(const Decomposition & dec, const size_t (& g_sz)[dim], const Box<dim,St> & domain, const Ghost<dim,St> & ghost)
Pietro Incardona's avatar
Pietro Incardona committed
591
592
    :domain(domain),ghost(ghost),dec(dec),v_cl(*global_v_cluster)
	{
593
594
595
		// Increment the reference counter of the decomposition
		this->dec.incRef();

Pietro Incardona's avatar
Pietro Incardona committed
596
597
598
599
600
601
		InitializeCellDecomposer(g_sz);
		InitializeStructures(g_sz);
	}

    //! constructor
    grid_dist_id(Decomposition && dec, const size_t (& g_sz)[dim], const Box<dim,St> & domain, const Ghost<dim,St> & ghost)
incardon's avatar
incardon committed
602
    :domain(domain),ghost(ghost),dec(dec),v_cl(*global_v_cluster)
incardon's avatar
incardon committed
603
	{
604
605
606
#ifdef SE_CLASS2
		check_new(this,8,GRID_DIST_EVENT,4);
#endif
607

incardon's avatar
incardon committed
608
609
		InitializeCellDecomposer(g_sz);
		InitializeStructures(g_sz);
incardon's avatar
incardon committed
610
611
	}

Pietro Incardona's avatar
Pietro Incardona committed
612
613
614
615
616
617
618
619
620
621
    /*! \brief Get the spacing of the grid in direction i
     *
     * \return the spacing
     *
     */
    inline St spacing(size_t i) const
    {
    	return cd_sm.getCellBox().getHigh(i);
    }

622
623
624
	/*! \brief Constrcuctor
	 *
	 * \param g_sz array with the grid size on each dimension
625
	 * \param domain domain where this grid live
626
	 * \param g Ghost given in grid units
627
628
	 *
	 */
629
630
	grid_dist_id(const Decomposition & dec, const size_t (& g_sz)[dim],const Box<dim,St> & domain, const Ghost<dim,long int> & g)
	:domain(domain),dec(dec),v_cl(*global_v_cluster),ginfo(g_sz),ginfo_v(g_sz)
incardon's avatar
incardon committed
631
	{
632
633
634
#ifdef SE_CLASS2
		check_new(this,8,GRID_DIST_EVENT,4);
#endif
635

incardon's avatar
incardon committed
636
		InitializeCellDecomposer(g_sz);
637
638
639
640

		ghost = convert_ghost(g,cd_sm);

		// Initialize structures
incardon's avatar
incardon committed
641
642
		InitializeStructures(g_sz);
	}
incardon's avatar
incardon committed
643

incardon's avatar
incardon committed
644
645
646
647
648
649
650
	/*! \brief Constrcuctor
	 *
	 * \param g_sz array with the grid size on each dimension
	 * \param domain domain where this grid live
	 * \param g Ghost given in grid units
	 *
	 */
651
652
	grid_dist_id(Decomposition && dec, const size_t (& g_sz)[dim],const Box<dim,St> & domain, const Ghost<dim,long int> & g)
	:domain(domain),dec(dec),v_cl(*global_v_cluster),ginfo(g_sz),ginfo_v(g_sz)
incardon's avatar
incardon committed
653
	{
654
655
656
#ifdef SE_CLASS2
		check_new(this,8,GRID_DIST_EVENT,4);
#endif
incardon's avatar
incardon committed
657
658
		InitializeCellDecomposer(g_sz);

Pietro Incardona's avatar
Pietro Incardona committed
659
		ghost = convert_ghost(g,cd_sm);
incardon's avatar
incardon committed
660
661
662
663
664
665
666
667
668

		// Initialize structures
		InitializeStructures(g_sz);
	}

	/*! \brief Constrcuctor
	 *
	 * \param g_sz array with the grid size on each dimension
	 * \param domain domain where this grid live
669
	 * \param g Ghost
incardon's avatar
incardon committed
670
671
	 *
	 */
672
673
	grid_dist_id(const size_t (& g_sz)[dim],const Box<dim,St> & domain, const Ghost<dim,St> & g)
	:domain(domain),ghost(g),dec(*global_v_cluster),v_cl(*global_v_cluster),ginfo(g_sz),ginfo_v(g_sz)
incardon's avatar
incardon committed
674
	{
Pietro Incardona's avatar
Pietro Incardona committed
675
676
677
#ifdef SE_CLASS2
		check_new(this,8,GRID_DIST_EVENT,4);
#endif
678
679
		// Increment the reference counter of the decomposition
		this->dec.incRef();
incardon's avatar
incardon committed
680

681
		InitializeCellDecomposer(g_sz);
Pietro Incardona's avatar
Pietro Incardona committed
682
683
684
		InitializeDecomposition(g_sz);
		InitializeStructures(g_sz);
	}
incardon's avatar
incardon committed
685

Pietro Incardona's avatar
Pietro Incardona committed
686
687
688
689
690
691
692
	/*! \brief Constrcuctor
	 *
	 * \param g_sz array with the grid size on each dimension
	 * \param domain domain where this grid live
	 * \param g Ghost given in grid units
	 *
	 */
693
694
	grid_dist_id(const size_t (& g_sz)[dim],const Box<dim,St> & domain, const Ghost<dim,long int> & g)
	:domain(domain),dec(*global_v_cluster),v_cl(*global_v_cluster),ginfo(g_sz),ginfo_v(g_sz)
Pietro Incardona's avatar
Pietro Incardona committed
695
	{
696
697
698
#ifdef SE_CLASS2
		check_new(this,8,GRID_DIST_EVENT,4);
#endif
incardon's avatar
incardon committed
699
		InitializeCellDecomposer(g_sz);
incardon's avatar
incardon committed
700

Pietro Incardona's avatar
Pietro Incardona committed
701
		ghost = convert_ghost(g,cd_sm);
702

incardon's avatar
incardon committed
703
		InitializeDecomposition(g_sz);
incardon's avatar
incardon committed
704
705
		// Initialize structures
		InitializeStructures(g_sz);
incardon's avatar
incardon committed
706
707
	}

708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
	/*! \brief Constrcuctor
	 *
	 * \param g_sz std::vector with the grid size on each dimension
	 * \param domain domain where this grid live
	 * \param g Ghost given in grid units
	 *
	 */
	grid_dist_id(const Decomposition & dec, const std::vector<size_t> & g_sz,const Box<dim,St> & domain, const Ghost<dim,long int> & g)
	:grid_dist_id(dec,*static_cast<const size_t(*) [dim]>(static_cast<const void*>(&g_sz[0])),domain,g)
	{

	}

	/*! \brief Constrcuctor
	 *
	 * \param g_sz std::vector with the grid size on each dimension
	 * \param domain domain where this grid live
	 * \param g Ghost given in grid units
	 *
	 */
	grid_dist_id(Decomposition && dec,const std::vector<size_t> & g_sz,const Box<dim,St> & domain, const Ghost<dim,long int> & g)
	:grid_dist_id(dec, *static_cast<const size_t(*) [dim]>(static_cast<const void*>(&g_sz[0])) , domain, g)
	{
	}

incardon's avatar
incardon committed
733
734
735
736
737
	/*! \brief Get an object containing the grid informations
	 *
	 * \return an information object about this grid
	 *
	 */
Pietro Incardona's avatar
Pietro Incardona committed
738
	const grid_sm<dim,T> & getGridInfo() const
incardon's avatar
incardon committed
739
	{
740
741
742
#ifdef SE_CLASS2
		check_valid(this,8);
#endif
incardon's avatar
incardon committed
743
744
745
746
747
748
749
750
		return ginfo;
	}

	/*! \brief Get an object containing the grid informations without type
	 *
	 * \return an information object about this grid
	 *
	 */
Pietro Incardona's avatar
Pietro Incardona committed
751
	const grid_sm<dim,void> & getGridInfoVoid() const
incardon's avatar
incardon committed
752
	{
753
754
755
#ifdef SE_CLASS2
		check_valid(this,8);
#endif
incardon's avatar
incardon committed
756
757
758
		return ginfo_v;
	}

759
	/*! \brief Get the object that store the information about the decomposition
incardon's avatar
incardon committed
760
761
762
763
764
765
	 *
	 * \return the decomposition object
	 *
	 */
	Decomposition & getDecomposition()
	{
766
767
768
#ifdef SE_CLASS2
		check_valid(this,8);
#endif
incardon's avatar
incardon committed
769
770
771
		return dec;
	}

incardon's avatar
incardon committed
772
773
774
775
776
777
778
	/*! \brief Return the cell decomposer
	 *
	 * \return the cell decomposer
	 *
	 */
	const CellDecomposer_sm<dim,St> & getCellDecomposer()
	{
779
780
781
#ifdef SE_CLASS2
		check_valid(this,8);
#endif
incardon's avatar
incardon committed
782
783
784
785
		return cd_sm;
	}

	/*! \brief Check that the global grid key is inside the grid domain
incardon's avatar
incardon committed
786
	 *
incardon's avatar
incardon committed
787
	 * \return true if is inside
incardon's avatar
incardon committed
788
	 *
incardon's avatar
incardon committed
789
790
791
	 */
	bool isInside(const grid_key_dx<dim> & gk) const
	{
792
793
794
#ifdef SE_CLASS2
		check_valid(this,8);
#endif
incardon's avatar
incardon committed
795
796
797
798
799
800
801
802
803
		for (size_t i = 0 ; i < dim ; i++)
		{
			if (gk.get(i) < 0 || gk.get(i) >= (long int)g_sz[i])
				return false;
		}

		return true;
	}

incardon's avatar
incardon committed
804
805
806
807
808
809
810
	/*! \brief Get the size of local domain grids
	 *
	 * \return The size of the local domain
	 *
	 */
	size_t getLocalDomainSize()
	{
811
812
813
#ifdef SE_CLASS2
		check_valid(this,8);
#endif
incardon's avatar
incardon committed
814
815
816
817
		size_t total = 0;

		for (size_t i = 0 ; i < gdb_ext.size() ; i++)
		{
incardon's avatar
incardon committed
818
			total += gdb_ext.get(i).Dbox.getVolumeKey();
incardon's avatar
incardon committed
819
		}
incardon's avatar
incardon committed
820
821

		return total;
incardon's avatar
incardon committed
822
823
824
825
826
827
828
829
830
	}

	/*! \brief It return the informations about the local grids
	 *
	 * \return The information about the local grids
	 *
	 */
	const openfpm::vector<GBoxes<device_grid::dims>> & getLocalGridsInfo()
	{
831
832
833
#ifdef SE_CLASS2
		check_valid(this,8);
#endif
incardon's avatar
incardon committed
834
835
836
		return gdb_ext;
	}

incardon's avatar
incardon committed
837
838
839
840
841
842
843
	/*! \brief It return an iterator that span the full grid domain (each processor span its local domain)
	 *
	 * \return the iterator
	 *
	 */
	grid_dist_iterator<dim,device_grid,FREE> getDomainIterator()
	{
844
845
846
#ifdef SE_CLASS2
		check_valid(this,8);
#endif
Pietro Incardona's avatar
Pietro Incardona committed
847

Pietro Incardona's avatar
Pietro Incardona committed
848
849
850
851
852
853
		grid_key_dx<dim> stop(ginfo_v.getSize());
		grid_key_dx<dim> one;
		one.one();
		stop = stop - one;

		grid_dist_iterator<dim,device_grid,FREE> it(loc_grid,gdb_ext,stop);
incardon's avatar
incardon committed
854
855
856
857
858

		return it;
	}

	/*! \brief It return an iterator that span the grid domain + ghost part
incardon's avatar
incardon committed
859
860
861
	 *
	 *
	 */
Pietro Incardona's avatar
Pietro Incardona committed
862
	grid_dist_iterator<dim,device_grid,FIXED> getDomainGhostIterator() const
incardon's avatar
incardon committed
863
	{
864
865
866
#ifdef SE_CLASS2
		check_valid(this,8);
#endif
incardon's avatar
incardon committed
867
		grid_dist_iterator<dim,device_grid,FIXED> it(loc_grid,gdb_ext);
incardon's avatar
incardon committed
868
869
870
871

		return it;
	}

incardon's avatar
incardon committed
872
873
874
	/*! \brief It return an iterator that span the grid domain only in the specified
	 * part
	 *
Pietro Incardona's avatar
Pietro Incardona committed
875
876
877
	 * The key spanned are the one inside the box spanned by the start point and the end
	 * point included
	 *
incardon's avatar
incardon committed
878
879
880
881
	 * \param start point
	 * \param stop point
	 *
	 */
Pietro Incardona's avatar
Pietro Incardona committed
882
	grid_dist_iterator_sub<dim,device_grid> getSubDomainIterator(const grid_key_dx<dim> & start, const grid_key_dx<dim> & stop) const
incardon's avatar
incardon committed
883
	{
884
885
886
#ifdef SE_CLASS2
		check_valid(this,8);
#endif
incardon's avatar
incardon committed
887
888
889
890
891
		grid_dist_iterator_sub<dim,device_grid> it(start,stop,loc_grid,gdb_ext);

		return it;
	}

Pietro Incardona's avatar
Pietro Incardona committed
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
	/*! \brief It return an iterator that span the grid domain only in the specified
	 * part
	 *
	 * The key spanned are the one inside the box spanned by the start point and the end
	 * point included
	 *
	 * \param start point
	 * \param stop point
	 *
	 */
	grid_dist_iterator_sub<dim,device_grid> getSubDomainIterator(const long int (& start)[dim], const long int (& stop)[dim]) const
	{
		grid_dist_iterator_sub<dim,device_grid> it(grid_key_dx<dim>(start),grid_key_dx<dim>(stop),loc_grid,gdb_ext);

		return it;
	}

incardon's avatar
incardon committed
909
910
911
	//! Destructor
	~grid_dist_id()
	{
912
913
914
#ifdef SE_CLASS2
		check_delete(this);
#endif
incardon's avatar
incardon committed
915
		dec.decRef();
incardon's avatar
incardon committed
916
	}
incardon's avatar
incardon committed
917
918
919
920
921
922
923
924

	/*! \brief Get the Virtual Cluster machine
	 *
	 * \return the Virtual cluster machine
	 *
	 */
	Vcluster & getVC()
	{
925
926
927
#ifdef SE_CLASS2
		check_valid(this,8);
#endif
incardon's avatar
incardon committed
928
929
		return v_cl;
	}
incardon's avatar
incardon committed
930

Pietro Incardona's avatar
Pietro Incardona committed
931
932
933
934
935
936
937
938
939
940
	/*! \brief Indicate that this grid is not staggered
	 *
	 * \return false
	 *
	 */
	bool is_staggered()
	{
		return false;
	}

incardon's avatar
incardon committed
941
	/*! \brief Get the reference of the selected element
incardon's avatar
incardon committed
942
943
944
945
946
	 *
	 * \param p property to get (is an integer)
	 * \param v1 grid_key that identify the element in the grid
	 *
	 */
incardon's avatar
incardon committed
947
	template <unsigned int p>inline auto get(const grid_dist_key_dx<dim> & v1) const -> typename std::add_lvalue_reference<decltype(loc_grid.get(v1.getSub()).template get<p>(v1.getKey()))>::type
incardon's avatar
incardon committed
948
	{
949
950
951
#ifdef SE_CLASS2
		check_valid(this,8);
#endif
incardon's avatar
incardon committed
952
953
954
955
		return loc_grid.get(v1.getSub()).template get<p>(v1.getKey());
	}

	/*! \brief Get the reference of the selected element
incardon's avatar
incardon committed
956
957
958
959
960
	 *
	 * \param p property to get (is an integer)
	 * \param v1 grid_key that identify the element in the grid
	 *
	 */
961
	template <unsigned int p>inline auto get(const grid_dist_key_dx<dim> & v1) -> typename std::add_lvalue_reference<decltype(loc_grid.get(v1.getSub()).template get<p>(v1.getKey()))>::type
incardon's avatar
incardon committed
962
	{
963
964
965
#ifdef SE_CLASS2
		check_valid(this,8);
#endif
incardon's avatar
incardon committed
966
967
		return loc_grid.get(v1.getSub()).template get<p>(v1.getKey());
	}
968

969
	/*! \brief it store a box, its unique id and the sub-domain from where it come from
970
971
	 *
	 */
972
	struct i_box_id
973
974
975
976
977
	{
		//! Box
		::Box<dim,size_t> box;

		//! id
978
979
980
981
982
983
		size_t g_id;

		//! sub
		size_t sub;
	};

incardon's avatar
incardon committed
984
985
986
987
988
989
990
991
992
993
994
995
996
	/*! \brief it store an internal ghost box, the linked external ghost box and the sub-domain from where
	 *  it come from as internal ghost box
	 *
	 */
	struct i_lbox_id
	{
		//! Box
		::Box<dim,size_t> box;

		//! sub-domain id
		size_t sub;

		//! external box
incardon's avatar
incardon committed
997
		size_t k;
incardon's avatar
incardon committed
998
999
	};

1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
	/*! \brief It store the information about the external ghost box
	 *
	 *
	 */
	struct e_box_id
	{
		//! Box defining the external ghost box in global coordinates
		::Box<dim,size_t> g_e_box;

		//! Box defining the external ghost box in local coordinates
		::Box<dim,size_t> l_e_box;

		//! sub_id in which sub-domain this box live
		size_t sub;
1014
1015
	};

incardon's avatar
incardon committed
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
	/*! \brief It store the information about the external ghost box
	 *
	 *
	 */
	struct e_lbox_id
	{
		//! Box defining the external ghost box in local coordinates
		::Box<dim,size_t> box;

		//! sub_id in which sub-domain this box live
		size_t sub;
	};

1029
	/*! \brief Per-processor Internal ghost box
1030
1031
	 *
	 */
1032
	struct ip_box_grid
1033
	{
1034
1035
		// ghost in grid units
		openfpm::vector<i_box_id> bid;
1036
1037
1038
1039
1040

		//! processor id
		size_t prc;
	};

incardon's avatar
incardon committed
1041
1042
1043
1044
1045
1046
1047
1048
1049
	/*! \brief local Internal ghost box
	 *
	 */
	struct i_lbox_grid
	{
		// ghost in grid units
		openfpm::vector<i_lbox_id> bid;
	};

1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
	/*! \brief Per-processor external ghost box
	 *
	 */
	struct ep_box_grid
	{
		// ghost in grid units
		openfpm::vector<e_box_id> bid;

		//! processor id
		size_t prc;
	};

incardon's avatar
incardon committed
1062
1063
1064
1065
1066
1067
1068
1069
1070
	/*! \brief Per-processor external ghost box
	 *
	 */
	struct e_lbox_grid
	{
		// ghost in grid units
		openfpm::vector<e_lbox_id> bid;
	};

1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
	//! Memory for the ghost sending buffer
	Memory g_send_prp_mem;

	//! Memory for the ghost sending buffer
	Memory g_recv_prp_mem;

	//! Flag that indicate if the external ghost box has been initialized
	bool init_e_g_box = false;

	//! Flag that indicate if the internal ghost box has been initialized
1081
1082
1083
	bool init_i_g_box = false;

	//! Internal ghost boxes in grid units
1084
1085
1086
1087
	openfpm::vector<ip_box_grid> ig_box;

	//! External ghost boxes in grid units
	openfpm::vector<ep_box_grid> eg_box;
1088

incardon's avatar
incardon committed
1089
1090
1091
1092
1093
1094
	//! Local internal ghost boxes in grid units
	openfpm::vector<i_lbox_grid> loc_ig_box;

	//! Local external ghost boxes in grid units
	openfpm::vector<e_lbox_grid> loc_eg_box;

1095
	/*! \brief It synchronize the ghost parts
1096
	 *
1097
	 * \tparam prp... Properties to synchronize
1098
1099
1100
1101
	 *
	 */
	template<int... prp> void ghost_get()
	{
1102
1103
1104
1105
#ifdef SE_CLASS2
		check_valid(this,8);
#endif

1106
1107
1108
		// Sending property object
		typedef object<typename object_creator<typename T::type,prp...>::type> prp_object;

1109
		// Convert the ghost  internal boxes into grid unit boxes
1110
1111
		create_ig_box();

1112
1113
		// Convert the ghost external boxes into grid unit boxes
		create_eg_box();
1114

incardon's avatar
incardon committed
1115
1116
1117
1118
1119
1120
		// Convert the local ghost internal boxes into grid unit boxes
		create_local_ig_box();

		// Convert the local external ghost boxes into grid unit boxes
		create_local_eg_box();

1121
		// total number of sending vector
1122
		std::vector<size_t> pap_prp;
1123

1124
1125
		// Create a packing request vector
		for ( size_t i = 0 ; i < ig_box.size() ; i++ )
1126
1127
		{
			// for each ghost box
1128
			for (size_t j = 0 ; j < ig_box.get(i).bid.size() ; j++)
1129
			{
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
				// And linked sub-domain
				size_t sub_id = ig_box.get(i).bid.get(j).sub;
				// Internal ghost box
				Box<dim,size_t> g_ig_box = ig_box.get(i).bid.get(j).box;
				g_ig_box -= gdb_ext.get(sub_id).origin.template convertPoint<size_t>();

				// Pack a size_t for the internal ghost id
				Packer<size_t,HeapMemory>::packRequest(pap_prp);
				// Create a sub grid iterator spanning the internal ghost layer
				grid_key_dx_iterator_sub<dim> sub_it(loc_grid.get(sub_id).getGrid(),g_ig_box.getKP1(),g_ig_box.getKP2());
				// and pack the internal ghost grid
				Packer<device_grid,HeapMemory>::template packRequest<prp...>(loc_grid.get(sub_id),sub_it,pap_prp);
1142
1143
1144
1145
			}
		}

		// resize the property buffer memory
1146
		g_send_prp_mem.resize(ExtPreAlloc<Memory>::calculateMem(pap_prp));
1147
1148

		// Create an object of preallocated memory for properties
1149
1150
		ExtPreAlloc<Memory> & prAlloc_prp = *(new ExtPreAlloc<Memory>(pap_prp,g_send_prp_mem));
		prAlloc_prp.incRef();
1151

1152
1153
		// Pack information
		Pack_stat sts;
1154

1155
1156
		// Pack the information for each processor and send it
		for ( size_t i = 0 ; i < ig_box.size() ; i++ )
1157
		{
1158
1159
			sts.mark();

1160
			// for each ghost box
1161
			for (size_t j = 0 ; j < ig_box.get(i).bid.size() ; j++)
1162
			{
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
				// And linked sub-domain
				size_t sub_id = ig_box.get(i).bid.get(j).sub;
				// Internal ghost box
				Box<dim,size_t> g_ig_box = ig_box.get(i).bid.get(j).box;
				g_ig_box -= gdb_ext.get(sub_id).origin.template convertPoint<size_t>();
				// Ghost box global id
				size_t g_id = ig_box.get(i).bid.get(j).g_id;

				// Pack a size_t for the internal ghost id
				Packer<size_t,HeapMemory>::pack(prAlloc_prp,g_id,sts);
				// Create a sub grid iterator spanning the internal ghost layer
				grid_key_dx_iterator_sub<dim> sub_it(loc_grid.get(sub_id).getGrid(),g_ig_box.getKP1(),g_ig_box.getKP2());
				// and pack the internal ghost grid
				Packer<device_grid,HeapMemory>::template pack<prp...>(prAlloc_prp,loc_grid.get(sub_id),sub_it,sts);
1177
			}
1178
1179
			// send the request
			v_cl.send(ig_box.get(i).prc,0,sts.getMarkPointer(prAlloc_prp),sts.getMarkSize(prAlloc_prp));
1180
1181
		}

1182
1183
		// Calculate the total information to receive from each processors
		std::vector<size_t> prp_recv;
1184

1185
1186
1187
1188
		//! Receive the information from each processors
		for ( size_t i = 0 ; i < eg_box.size() ; i++ )
		{
			prp_recv.push_back(0);
1189

1190
1191
1192
1193
1194
1195
			// for each external ghost box
			for (size_t j = 0 ; j < eg_box.get(i).bid.size() ; j++)
			{
				// External ghost box
				Box<dim,size_t> g_eg_box = eg_box.get(i).bid.get(j).g_e_box;
				prp_recv[prp_recv.size()-1] += g_eg_box.getVolumeKey() * sizeof(prp_object) + sizeof(size_t);
1196
1197
1198
			}
		}

1199
1200
		//! Resize the receiving buffer
		g_recv_prp_mem.resize(ExtPreAlloc<Memory>::calculateMem(prp_recv));
1201

1202
		// Create an object of preallocated memory for properties
1203
		ExtPreAlloc<Memory> & prRecv_prp = *(new ExtPreAlloc<Memory>(prp_recv,g_recv_prp_mem));
1204
		prRecv_prp.incRef();
1205

incardon's avatar
incardon committed
1206
		// queue the receives
1207
1208
		for ( size_t i = 0 ; i < eg_box.size() ; i++ )
		{
1209
1210
			prRecv_prp.allocate(prp_recv[i]);
			v_cl.recv(eg_box.get(i).prc,0,prRecv_prp.getPointer(),prp_recv[i]);
1211
		}
1212

incardon's avatar
incardon committed
1213
1214
1215
1216
1217
		// Before wait for the communication to complete we sync the local ghost
		// in order to overlap with communication

		ghost_get_local<prp...>();

1218
1219
1220
		// wait to receive communication
		v_cl.execute();

incardon's avatar
incardon committed
1221
		Unpack_stat ps;
1222
1223
1224

		// Unpack the object
		for ( size_t i = 0 ; i < eg_box.size() ; i++ )
1225
		{
1226
1227
			// for each external ghost box
			for (size_t j = 0 ; j < eg_box.get(i).bid.size() ; j++)
1228
			{
1229
				// Unpack the ghost box global-id
1230

1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
				size_t g_id;
				Unpacker<size_t,HeapMemory>::unpack(prRecv_prp,g_id,ps);

				size_t l_id = 0;
				// convert the global id into local id
				auto key = g_id_to_external_ghost_box.find(g_id);
				if (key != g_id_to_external_ghost_box.end()) // FOUND
					l_id = key->second;
				else
				{
					// NOT FOUND
1242

1243
1244
1245
					// It must be always found, if not it mean that the processor has no-idea of
					// what is stored and conseguently do not know how to unpack, print a critical error
					// and return
1246

1247
1248
1249
1250
1251
1252
1253
1254
					std::cerr << "Error: " << __FILE__ << ":" << __LINE__ << " Critical, cannot unpack object, because received data cannot be interpreted\n";

					return;
				}

				// Get the external ghost box associated with the packed information
				Box<dim,size_t> box = eg_box.get(i).bid.get(l_id).l_e_box;
				size_t sub_id = eg_box.get(i).bid.get(l_id).sub;
1255

1256
1257
				// sub-grid where to unpack
				grid_key_dx_iterator_sub<dim> sub2(loc_grid.get(sub_id).getGrid(),box.getKP1(),box.getKP2());
1258

1259
1260
1261
1262
				// Unpack
				Unpacker<device_grid,HeapMemory>::template unpack<prp...>(prRecv_prp,sub2,loc_grid.get(sub_id),ps);
			}
		}
1263
1264
	}

incardon's avatar
incardon committed
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
	/*! \brief Get the spacing on each dimension
	 *
	 * \param get the spacing
	 *
	 */
	Point<dim,St> getSpacing()
	{
		return cd_sm.getCellBox().getP2();
	}

incardon's avatar
incardon committed
1275
1276
1277
	/*! \brief Convert a g_dist_key_dx into a global key
	 *
	 * \see grid_dist_key_dx
1278
	 * \see grid_dist_iterator
incardon's avatar
incardon committed
1279
1280
1281
1282
1283
1284
	 *
	 * \return the global position in the grid
	 *
	 */
	inline grid_key_dx<dim> getGKey(const grid_dist_key_dx<dim> & k)
	{
1285
1286
1287
#ifdef SE_CLASS2
		check_valid(this,8);
#endif
incardon's avatar
incardon committed
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
		// Get the sub-domain id
		size_t sub_id = k.getSub();

		grid_key_dx<dim> k_glob = k.getKey();

		// shift
		k_glob = k_glob + gdb_ext.get(sub_id).origin;

		return k_glob;
	}