CartDecomposition.hpp 44.7 KB
Newer Older
incardon's avatar
incardon committed
1 2 3
/*
 * CartDecomposition.hpp
 *
4
 *  Created on: Oct 07, 2015
5
 *      Author: Pietro Incardona, Antonio Leo
incardon's avatar
incardon committed
6 7 8 9 10 11
 */

#ifndef CARTDECOMPOSITION_HPP
#define CARTDECOMPOSITION_HPP

#include "config.h"
12
#include <cmath>
incardon's avatar
incardon committed
13
#include "VCluster/VCluster.hpp"
14
#include "Graph/CartesianGraphFactory.hpp"
incardon's avatar
incardon committed
15
#include "Decomposition.hpp"
incardon's avatar
incardon committed
16
#include "Vector/map_vector.hpp"
incardon's avatar
incardon committed
17 18 19 20 21
#include <vector>
#include <initializer_list>
#include "SubdomainGraphNodes.hpp"
#include "dec_optimizer.hpp"
#include "Space/Shape/Box.hpp"
incardon's avatar
incardon committed
22
#include "Space/Shape/Point.hpp"
incardon's avatar
incardon committed
23
#include "NN/CellList/CellDecomposer.hpp"
incardon's avatar
incardon committed
24 25
#include <unordered_map>
#include "NN/CellList/CellList.hpp"
incardon's avatar
incardon committed
26
#include "Space/Ghost.hpp"
incardon's avatar
incardon committed
27 28
#include "common.hpp"
#include "ie_loc_ghost.hpp"
29 30
#include "ie_ghost.hpp"
#include "nn_processor.hpp"
31
#include "GraphMLWriter/GraphMLWriter.hpp"
32 33 34 35
#include "Distribution/ParMetisDistribution.hpp"
#include "Distribution/DistParMetisDistribution.hpp"
#include "Distribution/MetisDistribution.hpp"
#include "DLB/DLB.hpp"
36
#include "util/se_util.hpp"
37
#include "util/mathutil.hpp"
Pietro Incardona's avatar
Pietro Incardona committed
38
#include "CartDecomposition_ext.hpp"
incardon's avatar
incardon committed
39 40
#include "data_type/aggregate.hpp"
#include "Domain_NN_calculator_cart.hpp"
incardon's avatar
incardon committed
41

42 43
#define CARTDEC_ERROR 2000lu

incardon's avatar
incardon committed
44 45
#define COMPUTE_SKIN_SUB 1

incardon's avatar
incardon committed
46
/**
Pietro Incardona's avatar
Pietro Incardona committed
47
 * \brief This class decompose a space into sub-sub-domains and distribute them across processors
incardon's avatar
incardon committed
48 49 50 51
 *
 * \tparam dim is the dimensionality of the physical domain we are going to decompose.
 * \tparam T type of the space we decompose, Real, Integer, Complex ...
 * \tparam Memory Memory factory used to allocate memory
tonynsyde's avatar
tonynsyde committed
52
 * \tparam Distribution type of distribution, can be ParMetisDistribution or MetisDistribution
incardon's avatar
incardon committed
53
 *
54
 * Given an N-dimensional space, this class decompose the space into a Cartesian grid of small
incardon's avatar
incardon committed
55 56 57 58
 * sub-sub-domain. To each sub-sub-domain is assigned an id that identify at which processor is
 * assigned (in general the union of all the sub-sub-domain assigned to a processor is
 * simply connected space), a second step merge several sub-sub-domain with same id into bigger region
 *  sub-domain. Each sub-domain has an extended space called ghost part
59 60 61 62
 *
 * Assuming that VCluster.getProcessUnitID(), equivalent to the MPI processor rank, return the processor local
 * processor id, we define
 *
63 64
 * * local processor: processor rank
 * * local sub-domain: sub-domain given to the local processor
65 66 67 68 69 70 71 72 73 74 75 76
 * * external ghost box: (or ghost box) are the boxes that compose the ghost space of the processor, or the
 *   boxes produced expanding every local sub-domain by the ghost extension and intersecting with the sub-domain
 *   of the other processors
 * * Near processors are the processors adjacent to the local processor, where with adjacent we mean all the processor
 *   that has a non-zero intersection with the ghost part of the local processor, or all the processors that
 *   produce non-zero external boxes with the local processor, or all the processor that should communicate
 *   in case of ghost data synchronization
 * * internal ghost box: is the part of ghost of the near processor that intersect the space of the
 *       processor, or the boxes produced expanding the sub-domain of the near processors with the local sub-domain
 * * Near processor sub-domain: is a sub-domain that live in the a near (or contiguous) processor
 * * Near processor list: the list of all the near processor of the local processor (each processor has a list
 *                        of the near processor)
Pietro Incardona's avatar
Pietro Incardona committed
77
 * * Local ghosts internal or external are all the ghosts that does not involve inter-processor communications
78 79
 *
 * \see calculateGhostBoxes() for a visualization of internal and external ghost boxes
incardon's avatar
incardon committed
80
 *
81
 * ### Create a Cartesian decomposition object on a Box space, distribute, calculate internal and external ghost boxes
Pietro Incardona's avatar
Pietro Incardona committed
82
 *
83 84
 * \snippet CartDecomposition_unit_test.hpp Create CartDecomposition
 *
incardon's avatar
incardon committed
85 86
 */

Pietro Incardona's avatar
Pietro Incardona committed
87
template<unsigned int dim, typename T, typename Memory, typename Distribution>
incardon's avatar
incardon committed
88
class CartDecomposition: public ie_loc_ghost<dim, T>, public nn_prcs<dim, T>, public ie_ghost<dim, T>, public domain_nn_calculator_cart<dim>
incardon's avatar
incardon committed
89
{
incardon's avatar
incardon committed
90

incardon's avatar
incardon committed
91
public:
incardon's avatar
incardon committed
92

incardon's avatar
incardon committed
93 94 95 96
	//! Type of the domain we are going to decompose
	typedef T domain_type;

	//! It simplify to access the SpaceBox element
97
	typedef SpaceBox<dim, T> Box;
incardon's avatar
incardon committed
98

Pietro Incardona's avatar
Pietro Incardona committed
99 100 101 102 103 104 105
	//! This class is base of itself
	typedef CartDecomposition<dim,T,Memory,Distribution> base_type;

	//! This class admit a class defined on an extended domain
	typedef CartDecomposition_ext<dim,T,Memory,Distribution> extended_type;

protected:
incardon's avatar
incardon committed
106

incardon's avatar
incardon committed
107 108 109
	//! Indicate the communication weight has been set
	bool commCostSet = false;

incardon's avatar
incardon committed
110
	//! This is the key type to access  data_s, for example in the case of vector
incardon's avatar
incardon committed
111
	//! acc_key is size_t
Pietro Incardona's avatar
Pietro Incardona committed
112 113 114 115 116 117
	typedef typename openfpm::vector<SpaceBox<dim, T>,
			Memory,
			typename memory_traits_lin<SpaceBox<dim, T>>::type,
			memory_traits_lin,
			openfpm::vector_grow_policy_default,
			openfpm::vect_isel<SpaceBox<dim, T>>::value>::access_key acc_key;
incardon's avatar
incardon committed
118 119

	//! the set of all local sub-domain as vector
120
	openfpm::vector<SpaceBox<dim, T>> sub_domains;
incardon's avatar
incardon committed
121

incardon's avatar
incardon committed
122
	//! for each sub-domain, contain the list of the neighborhood processors
incardon's avatar
incardon committed
123 124
	openfpm::vector<openfpm::vector<long unsigned int> > box_nn_processor;

incardon's avatar
incardon committed
125
	//! Structure that contain for each sub-sub-domain box the processor id
incardon's avatar
Add ORB  
incardon committed
126
	//! exist for efficient global communication
incardon's avatar
incardon committed
127 128
	openfpm::vector<size_t> fine_s;

incardon's avatar
incardon committed
129
	//! Structure that store the cartesian grid information
130
	grid_sm<dim, void> gr;
incardon's avatar
incardon committed
131

132 133 134
	//! Structure that store the cartesian grid information
	grid_sm<dim, void> gr_dist;

incardon's avatar
incardon committed
135 136
	//! Structure that decompose your structure into cell without creating them
	//! useful to convert positions to CellId or sub-domain id in this case
137
	CellDecomposer_sm<dim, T, shift<dim,T>> cd;
incardon's avatar
incardon committed
138 139

	//! rectangular domain to decompose
Pietro Incardona's avatar
Pietro Incardona committed
140
	::Box<dim,T> domain;
incardon's avatar
incardon committed
141 142 143 144

	//! Box Spacing
	T spacing[dim];

145 146 147 148
	//! Magnification factor between distribution and
	//! decomposition
	size_t magn[dim];

incardon's avatar
incardon committed
149 150 151
	//! Runtime virtual cluster machine
	Vcluster & v_cl;

152
	//! Create distribution
153 154
	Distribution dist;

155
	//! Processor bounding box
incardon's avatar
incardon committed
156
	::Box<dim,T> bbox;
incardon's avatar
incardon committed
157

158
	//! reference counter of the object in case is shared between object
159 160
	long int ref_cnt;

161
	//! ghost info
162 163
	Ghost<dim,T> ghost;

164
	//! Boundary condition info
Pietro Incardona's avatar
Pietro Incardona committed
165
	size_t bc[dim];
incardon's avatar
incardon committed
166

incardon's avatar
incardon committed
167 168 169 170 171 172
	//! Processor domain bounding box
	::Box<dim,size_t> proc_box;

	//! set of Boxes produced by the decomposition optimizer
	openfpm::vector<::Box<dim, size_t>> loc_box;

Pietro Incardona's avatar
Pietro Incardona committed
173 174 175
	/*! \brief It convert the box from the domain decomposition into sub-domain
	 *
	 * The decomposition box from the domain-decomposition contain the box in integer
176 177
	 * coordinates. This box is converted into a continuos box. It also adjust loc_box
	 * if the distribution grid and the decomposition grid are different.
Pietro Incardona's avatar
Pietro Incardona committed
178 179 180
	 *
	 * \param loc_box local box
	 *
181
	 * \return the corresponding sub-domain
Pietro Incardona's avatar
Pietro Incardona committed
182 183
	 *
	 */
184
	template<typename Memory_bx> SpaceBox<dim,T> convertDecBoxIntoSubDomain(encapc<1,::Box<dim,size_t>,Memory_bx> loc_box)
Pietro Incardona's avatar
Pietro Incardona committed
185 186 187 188 189 190 191 192
	{
		// A point with all coordinate to one
		size_t one[dim];
		for (size_t i = 0 ; i < dim ; i++)	{one[i] = 1;}

		SpaceBox<dim, size_t> sub_dc = loc_box;
		SpaceBox<dim, size_t> sub_dce = sub_dc;
		sub_dce.expand(one);
193 194 195 196 197 198 199 200 201
		sub_dce.mul(magn);

		// shrink by one
		for (size_t i = 0 ; i < dim ; i++)
		{
			loc_box.template get<Box::p1>()[i] = sub_dce.getLow(i);
			loc_box.template get<Box::p2>()[i] = sub_dce.getHigh(i) - 1;
		}

Pietro Incardona's avatar
Pietro Incardona committed
202 203 204 205 206 207 208 209 210 211 212 213 214
		SpaceBox<dim, T> sub_d(sub_dce);
		sub_d.mul(spacing);
		sub_d += domain.getP1();

		// we add the

		// Fixing sub-domains to cover all the domain

		// Fixing sub_d
		// if (loc_box) is at the boundary we have to ensure that the box span the full
		// domain (avoiding rounding off error)
		for (size_t i = 0; i < dim; i++)
		{
215
			if (sub_dc.getHigh(i) == gr.size(i) - 1)
Pietro Incardona's avatar
Pietro Incardona committed
216 217 218 219 220 221 222 223 224
				sub_d.setHigh(i, domain.getHigh(i));

			if (sub_dc.getLow(i) == 0)
				sub_d.setLow(i,domain.getLow(i));
		}

		return sub_d;
	}

Pietro Incardona's avatar
Pietro Incardona committed
225 226 227

public:

228
	/*! \brief Constructor, it decompose and distribute the sub-domains across the processors
incardon's avatar
incardon committed
229
	 *
230
	 * \param v_cl Virtual cluster, used internally for communications
Pietro Incardona's avatar
Pietro Incardona committed
231
	 * \param bc boundary conditions
incardon's avatar
incardon committed
232
	 * \param opt option (one option is to construct)
233
	 *
incardon's avatar
incardon committed
234
	 */
incardon's avatar
incardon committed
235
	void createSubdomains(Vcluster & v_cl, const size_t (& bc)[dim], size_t opt = 0)
incardon's avatar
incardon committed
236
	{
237 238
		int p_id = v_cl.getProcessUnitID();

incardon's avatar
incardon committed
239 240 241
		// Calculate the total number of box and and the spacing
		// on each direction
		// Get the box containing the domain
242
		SpaceBox<dim, T> bs = domain.getBox();
incardon's avatar
incardon committed
243

244
		for (unsigned int i = 0; i < dim; i++)
incardon's avatar
incardon committed
245 246
		{
			// Calculate the spacing
incardon's avatar
incardon committed
247
			spacing[i] = (bs.getHigh(i) - bs.getLow(i)) / gr.size(i);
incardon's avatar
incardon committed
248 249
		}

incardon's avatar
Add ORB  
incardon committed
250
		// fill the structure that store the processor id for each sub-domain
incardon's avatar
incardon committed
251
		fine_s.resize(gr.size());
incardon's avatar
Add ORB  
incardon committed
252

incardon's avatar
incardon committed
253 254
		// Optimize the decomposition creating bigger spaces
		// And reducing Ghost over-stress
255
		dec_optimizer<dim, Graph_CSR<nm_v, nm_e>> d_o(dist.getGraph(), gr_dist.getSize());
incardon's avatar
incardon committed
256

257 258 259 260 261 262 263 264 265 266
		// Ghost
		Ghost<dim,long int> ghe;

		// Set the ghost
		for (size_t i = 0 ; i < dim ; i++)
		{
			ghe.setLow(i,static_cast<long int>(ghost.getLow(i)/spacing[i]) - 1);
			ghe.setHigh(i,static_cast<long int>(ghost.getHigh(i)/spacing[i]) + 1);
		}

incardon's avatar
incardon committed
267
		// optimize the decomposition
268
		d_o.template optimize<nm_v::sub_id, nm_v::proc_id>(dist.getGraph(), p_id, loc_box, box_nn_processor,ghe,bc);
incardon's avatar
incardon committed
269

270
		// Initialize
incardon's avatar
incardon committed
271
		if (loc_box.size() >= 0)
incardon's avatar
incardon committed
272
		{
Pietro Incardona's avatar
Pietro Incardona committed
273
			bbox = convertDecBoxIntoSubDomain(loc_box.get(0));
incardon's avatar
incardon committed
274
			proc_box = loc_box.get(0);
275
			sub_domains.add(bbox);
incardon's avatar
incardon committed
276
		}
Pietro Incardona's avatar
Pietro Incardona committed
277

incardon's avatar
incardon committed
278
		// convert into sub-domain
279
		for (size_t s = 1; s < loc_box.size(); s++)
incardon's avatar
incardon committed
280
		{
Pietro Incardona's avatar
Pietro Incardona committed
281
			SpaceBox<dim,T> sub_d = convertDecBoxIntoSubDomain(loc_box.get(s));
282

incardon's avatar
incardon committed
283 284
			// add the sub-domain
			sub_domains.add(sub_d);
incardon's avatar
incardon committed
285 286 287

			// Calculate the bound box
			bbox.enclose(sub_d);
incardon's avatar
incardon committed
288
			proc_box.enclose(loc_box.get(s));
incardon's avatar
incardon committed
289
		}
incardon's avatar
incardon committed
290

291
		nn_prcs<dim,T>::create(box_nn_processor, sub_domains);
Pietro Incardona's avatar
Pietro Incardona committed
292
		nn_prcs<dim,T>::applyBC(domain,ghost,bc);
293

incardon's avatar
incardon committed
294
		// fill fine_s structure
incardon's avatar
incardon committed
295 296 297
		// fine_s structure contain the processor id for each sub-sub-domain
		// with sub-sub-domain we mean the sub-domain decomposition before
		// running dec_optimizer (before merging sub-domains)
298 299
/*		auto it = dist.getGraph().getVertexIterator();

incardon's avatar
incardon committed
300 301 302 303 304 305

		while (it.isNext())
		{
			size_t key = it.get();

			// fill with the fine decomposition
306
			fine_s.get(key) = dist.getGraph().template vertex_p<nm_v::proc_id>(key);
incardon's avatar
incardon committed
307 308

			++it;
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
		}*/

		grid_key_dx_iterator<dim> git(gr);

		while (git.isNext())
		{
			auto key = git.get();
			grid_key_dx<dim> key2;

			for (size_t i = 0 ; i < dim ; i++)
				key2.set_d(i,key.get(i) / magn[i]);

			size_t lin = gr_dist.LinId(key2);
			size_t lin2 = gr.LinId(key);

			fine_s.get(lin2) = dist.getGraph().template vertex_p<nm_v::proc_id>(lin);

			++git;
incardon's avatar
incardon committed
327
		}
328

incardon's avatar
incardon committed
329 330 331 332 333 334 335 336 337 338
		Initialize_geo_cell_lists();
	}

	/*! \brief Initialize geo_cell lists
	 *
	 *
	 *
	 */
	void Initialize_geo_cell_lists()
	{
339 340
		// Get the processor bounding Box
		::Box<dim,T> bound = getProcessorBounds();
Pietro Incardona's avatar
Pietro Incardona committed
341 342
		// Not necessary, but I prefer
		bound.enlarge(ghost);
343

incardon's avatar
incardon committed
344
		// calculate the sub-divisions
345
		size_t div[dim];
346
		for (size_t i = 0; i < dim; i++)
incardon's avatar
incardon committed
347
			div[i] = (size_t) ((bound.getHigh(i) - bound.getLow(i)) / cd.getCellBox().getP2()[i]);
348 349

		// Initialize the geo_cell structure
350
		ie_ghost<dim,T>::Initialize_geo_cell(bound,div);
351

352 353
		// Initialize shift vectors
		ie_ghost<dim,T>::generateShiftVectors(domain);
incardon's avatar
incardon committed
354 355
	}

356 357 358 359 360 361
	/*! \brief Calculate communication and migration costs
	 *
	 * \param ts how many timesteps have passed since last calculation, used to approximate the cost
	 */
	void computeCommunicationAndMigrationCosts(size_t ts)
	{
tonynsyde's avatar
tonynsyde committed
362
		float migration = 0;
363 364

		SpaceBox<dim, T> cellBox = cd.getCellBox();
incardon's avatar
incardon committed
365 366
		float b_s = static_cast<float>(cellBox.getHigh(0));
		float gh_s = static_cast<float>(ghost.getHigh(0));
367 368 369 370 371

		// compute the gh_area for 2 dim case
		float gh_v = (gh_s * b_s);

		// multiply for sub-sub-domain side for each domain
372
		for (size_t i = 2; i < dim; i++)
373 374 375 376 377 378 379 380 381 382
			gh_v *= b_s;

		size_t norm = (size_t) (1.0 / gh_v);

		migration = pow(b_s, dim);

		size_t prev = 0;

		for (size_t i = 0; i < dist.getNSubSubDomains(); i++)
		{
incardon's avatar
incardon committed
383
			dist.setMigrationCost(i, norm * migration /* * dist.getSubSubDomainComputationCost(i)*/ );
384 385 386

			for (size_t s = 0; s < dist.getNSubSubDomainNeighbors(i); s++)
			{
incardon's avatar
incardon committed
387 388 389
				// We have to remove dist.getSubSubDomainComputationCost(i) otherwise the graph is
				// not directed
				dist.setCommunicationCost(i, s, 1 /** dist.getSubSubDomainComputationCost(i)*/  *  ts);
390 391 392
			}
			prev += dist.getNSubSubDomainNeighbors(i);
		}
incardon's avatar
incardon committed
393 394

		commCostSet = true;
395
	}
incardon's avatar
incardon committed
396

incardon's avatar
incardon committed
397
	/*! \brief Create the sub-domain that decompose your domain
incardon's avatar
incardon committed
398 399 400 401 402
	 *
	 */
	void CreateSubspaces()
	{
		// Create a grid where each point is a space
403
		grid_sm<dim, void> g(div);
incardon's avatar
incardon committed
404 405 406 407 408 409 410 411 412 413 414

		// create a grid_key_dx iterator
		grid_key_dx_iterator<dim> gk_it(g);

		// Divide the space into subspaces
		while (gk_it.isNext())
		{
			//! iterate through all subspaces
			grid_key_dx<dim> key = gk_it.get();

			//! Create a new subspace
415
			SpaceBox<dim, T> tmp;
incardon's avatar
incardon committed
416 417

			//! fill with the Margin of the box
418
			for (int i = 0; i < dim; i++)
incardon's avatar
incardon committed
419
			{
420 421
				tmp.setHigh(i, (key.get(i) + 1) * spacing[i]);
				tmp.setLow(i, key.get(i) * spacing[i]);
incardon's avatar
incardon committed
422 423 424 425 426
			}

			//! add the space box
			sub_domains.add(tmp);

incardon's avatar
incardon committed
427
			// Next sub-domain
incardon's avatar
incardon committed
428 429 430 431
			++gk_it;
		}
	}

432

incardon's avatar
incardon committed
433
	/*! \brief It calculate the internal ghost boxes
434 435 436 437 438 439 440 441
	 *
	 * Example: Processor 10 calculate
	 * B8_0 B9_0 B9_1 and B5_0
	 *
	 *
	 *
	 \verbatim

Pietro Incardona's avatar
Pietro Incardona committed
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
	+----------------------------------------------------+
	|                                                    |
	|                 Processor 8                        |
	|                 Sub+domain 0                       +-----------------------------------+
	|                                                    |                                   |
	|                                                    |                                   |
	++--------------+---+---------------------------+----+        Processor 9                |
	 |              |   |     B8_0                  |    |        Subdomain 0                |
	 |              +------------------------------------+                                   |
	 |              |   |                           |    |                                   |
	 |              |   |                           |B9_0|                                   |
	 |              | B |    Local processor        |    |                                   |
	 | Processor 5  | 5 |    Subdomain 0            |    |                                   |
	 | Subdomain 0  | _ |                           +----------------------------------------+
	 |              | 0 |                           |    |                                   |
	 |              |   |                           |    |                                   |
	 |              |   |                           |    |        Processor 9                |
	 |              |   |                           |B9_1|        Subdomain 1                |
	 |              |   |                           |    |                                   |
	 |              |   |                           |    |                                   |
	 |              |   |                           |    |                                   |
	 +--------------+---+---------------------------+----+                                   |
														 |                                   |
														 +-----------------------------------+
466 467 468 469 470 471 472


 \endverbatim

       and also
       G8_0 G9_0 G9_1 G5_0 (External ghost boxes)

Pietro Incardona's avatar
Pietro Incardona committed
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
\verbatim

		  +----------------------------------------------------+
		  |                 Processor 8                        |
		  |                 Subdomain 0                        +-----------------------------------+
		  |                                                    |                                   |
		  |           +---------------------------------------------+                              |
		  |           |         G8_0                           |    |                              |
	+-----+---------------+------------------------------------+    |   Processor 9                |
	|                 |   |                                    |    |   Subdomain 0                |
	|                 |   |                                    |G9_0|                              |
	|                 |   |                                    |    |                              |
	|                 |   |                                    |    |                              |
	|                 |   |        Local processor             |    |                              |
	|  Processor 5    |   |        Sub+domain 0                |    |                              |
	|  Subdomain 0    |   |                                    +-----------------------------------+
	|                 |   |                                    |    |                              |
	|                 | G |                                    |    |                              |
	|                 | 5 |                                    |    |   Processor 9                |
	|                 | | |                                    |    |   Subdomain 1                |
	|                 | 0 |                                    |G9_1|                              |
	|                 |   |                                    |    |                              |
	|                 |   |                                    |    |                              |
	+---------------------+------------------------------------+    |                              |
					  |                                        |    |                              |
					  +----------------------------------------+----+------------------------------+
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

	 \endverbatim

	 *
	 *
	 *
	 * \param ghost margins for each dimensions (p1 negative part) (p2 positive part)
	 *
	 *
	 \verbatim

	 	 	 	 	 ^ p2[1]
	 	 	 	 	 |
	 	 	 	 	 |
	 	 	 	+----+----+
	 	 	 	|         |
	 	 	 	|         |
	 p1[0]<-----+         +----> p2[0]
	 	 	 	|         |
	 	 	 	|         |
	 	 	 	+----+----+
	 	 	 	 	 |
	 	 	 	 	 v  p1[1]

	 \endverbatim

	 *
	 *
	 */
	void calculateGhostBoxes()
	{
		// Intersect all the local sub-domains with the sub-domains of the contiguous processors

		// create the internal structures that store ghost information
		ie_ghost<dim, T>::create_box_nn_processor_ext(v_cl, ghost, sub_domains, box_nn_processor, *this);
		ie_ghost<dim, T>::create_box_nn_processor_int(v_cl, ghost, sub_domains, box_nn_processor, *this);

		ie_loc_ghost<dim,T>::create(sub_domains,domain,ghost,bc);
	}

incardon's avatar
incardon committed
539 540
public:

Pietro Incardona's avatar
Pietro Incardona committed
541
	//! Space dimensions
542 543
	static constexpr int dims = dim;

Pietro Incardona's avatar
Pietro Incardona committed
544
	//! Space type
545 546
	typedef T stype;

incardon's avatar
incardon committed
547 548 549 550 551 552 553 554 555 556 557 558 559 560
	//! Increment the reference counter
	void incRef()
	{ref_cnt++;}

	//! Decrement the reference counter
	void decRef()
	{ref_cnt--;}

	//! Return the reference counter
	long int ref()
	{
		return ref_cnt;
	}

incardon's avatar
incardon committed
561 562
	/*! \brief Cartesian decomposition constructor
	 *
563
	 * \param v_cl Virtual cluster, used internally to handle or pipeline communication
incardon's avatar
incardon committed
564 565
	 *
	 */
incardon's avatar
incardon committed
566 567
	CartDecomposition(Vcluster & v_cl)
	:nn_prcs<dim, T>(v_cl), v_cl(v_cl), dist(v_cl),ref_cnt(0)
incardon's avatar
incardon committed
568 569 570 571
	{
		// Reset the box to zero
		bbox.zero();
	}
incardon's avatar
incardon committed
572

573 574 575 576 577
	/*! \brief Cartesian decomposition copy constructor
	 *
     * \param cart object to copy
	 *
	 */
Pietro Incardona's avatar
Pietro Incardona committed
578
	CartDecomposition(const CartDecomposition<dim,T,Memory> & cart)
579
	:nn_prcs<dim,T>(cart.v_cl),v_cl(cart.v_cl),dist(v_cl),ref_cnt(0)
580 581 582 583 584 585 586 587 588
	{
		this->operator=(cart);
	}

	/*! \brief Cartesian decomposition copy constructor
	 *
     * \param cart object to copy
	 *
	 */
Pietro Incardona's avatar
Pietro Incardona committed
589
	CartDecomposition(CartDecomposition<dim,T,Memory> && cart)
590
	:nn_prcs<dim,T>(cart.v_cl),v_cl(cart.v_cl),dist(v_cl),ref_cnt(0)
591 592 593 594
	{
		this->operator=(cart);
	}

incardon's avatar
incardon committed
595 596
	//! Cartesian decomposition destructor
	~CartDecomposition()
597 598
	{
	}
incardon's avatar
incardon committed
599

600 601 602 603 604 605 606 607 608 609 610 611 612 613
	/*! \brief class to select the returned id by ghost_processorID
	 *
	 */
	class box_id
	{
	public:
		/*! \brief Return the box id
		 *
		 * \param p structure containing the id informations
		 * \param b_id box_id
		 *
		 * \return box id
		 *
		 */
614
		inline static size_t id(p_box<dim, T> & p, size_t b_id)
615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633
		{
			return b_id;
		}
	};

	/*! \brief class to select the returned id by ghost_processorID
	 *
	 */
	class processor_id
	{
	public:
		/*! \brief Return the processor id
		 *
		 * \param p structure containing the id informations
		 * \param b_id box_id
		 *
		 * \return processor id
		 *
		 */
634
		inline static size_t id(p_box<dim, T> & p, size_t b_id)
635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
		{
			return p.proc;
		}
	};

	/*! \brief class to select the returned id by ghost_processorID
	 *
	 */
	class lc_processor_id
	{
	public:
		/*! \brief Return the near processor id
		 *
		 * \param p structure containing the id informations
		 * \param b_id box_id
		 *
		 * \return local processor id
		 *
		 */
654
		inline static size_t id(p_box<dim, T> & p, size_t b_id)
655 656 657 658 659
		{
			return p.lc_proc;
		}
	};

660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679
	/*! \brief class to select the returned id by ghost_processorID
	 *
	 */
	class shift_id
	{
	public:
		/*! \brief Return the shift id
		 *
		 * \param p structure containing the id informations
		 * \param b_id box_id
		 *
		 * \return shift_id id
		 *
		 */
		inline static size_t id(p_box<dim,T> & p, size_t b_id)
		{
			return p.shift_id;
		}
	};

680 681
	/*! \brief Apply boundary condition to the point
	 *
Pietro Incardona's avatar
Pietro Incardona committed
682 683 684 685 686
	 * If the particle go out to the right, bring back the particle on the left
	 * in case of periodic, nothing in case of non periodic
	 *
	 * \param pt Point to apply the boundary condition. (it's coordinated are changed according the
	 *        the explanation before)
687 688 689 690 691 692 693 694 695 696 697 698 699
	 *
	 */
	void applyPointBC(float (& pt)[dim]) const
	{
		for (size_t i = 0 ; i < dim ; i++)
		{
			if (bc[i] == PERIODIC)
				pt[i] = openfpm::math::periodic_l(pt[i],domain.getHigh(i),domain.getLow(i));
		}
	}

	/*! \brief Apply boundary condition to the point
	 *
Pietro Incardona's avatar
Pietro Incardona committed
700 701 702 703 704
	 * If the particle go out to the right, bring back the particle on the left
	 * in case of periodic, nothing in case of non periodic
	 *
	 * \param pt Point to apply the boundary conditions.(it's coordinated are changed according the
	 *        the explanation before)
705 706 707 708 709 710 711 712 713 714 715 716 717
	 *
	 */
	void applyPointBC(Point<dim,T> & pt) const
	{
		for (size_t i = 0 ; i < dim ; i++)
		{
			if (bc[i] == PERIODIC)
				pt.get(i) = openfpm::math::periodic_l(pt.get(i),domain.getHigh(i),domain.getLow(i));
		}
	}

	/*! \brief Apply boundary condition to the point
	 *
Pietro Incardona's avatar
Pietro Incardona committed
718 719 720 721 722
	 * If the particle go out to the right, bring back the particle on the left
	 * in case of periodic, nothing in case of non periodic
	 *
	 * \param pt encapsulated point object (it's coordinated are changed according the
	 *        the explanation before)
723 724 725 726 727 728 729 730 731 732 733
	 *
	 */
	template<typename Mem> void applyPointBC(encapc<1,Point<dim,T>,Mem> && pt) const
	{
		for (size_t i = 0 ; i < dim ; i++)
		{
			if (bc[i] == PERIODIC)
				pt.template get<0>()[i] = openfpm::math::periodic_l(pt.template get<0>()[i],domain.getHigh(i),domain.getLow(i));
		}
	}

incardon's avatar
incardon committed
734 735 736 737 738 739 740
	/*! \brief It create another object that contain the same decomposition information but with different ghost boxes
	 *
	 * \param g ghost
	 *
	 * \return a duplicated decomposition with different ghost boxes
	 *
	 */
Pietro Incardona's avatar
Pietro Incardona committed
741
	CartDecomposition<dim,T,Memory> duplicate(const Ghost<dim,T> & g) const
incardon's avatar
incardon committed
742
	{
Pietro Incardona's avatar
Pietro Incardona committed
743
		CartDecomposition<dim,T,Memory> cart(v_cl);
incardon's avatar
incardon committed
744 745 746 747 748 749 750 751 752 753 754 755 756

		cart.box_nn_processor = box_nn_processor;
		cart.sub_domains = sub_domains;
		cart.fine_s = fine_s;

		cart.gr = gr;
		cart.cd = cd;
		cart.domain = domain;
		std::copy(spacing,spacing+3,cart.spacing);

		cart.bbox = bbox;
		cart.ghost = g;

757 758
		cart.dist = dist;

759 760 761
		for (size_t i = 0 ; i < dim ; i++)
			cart.bc[i] = bc[i];

762 763
		(static_cast<nn_prcs<dim,T> &>(cart)).create(box_nn_processor, sub_domains);
		(static_cast<nn_prcs<dim,T> &>(cart)).applyBC(domain,ghost,bc);
incardon's avatar
incardon committed
764

765 766
		cart.Initialize_geo_cell_lists();
		cart.calculateGhostBoxes();
incardon's avatar
incardon committed
767 768 769 770 771

		return cart;
	}

	/*! \brief It create another object that contain the same information and act in the same way
772
	 *
Pietro Incardona's avatar
Pietro Incardona committed
773
	 * \return a duplicated CartDecomposition object
774 775
	 *
	 */
Pietro Incardona's avatar
Pietro Incardona committed
776
	CartDecomposition<dim,T,Memory> duplicate() const
777
	{
Pietro Incardona's avatar
Pietro Incardona committed
778
		CartDecomposition<dim,T,Memory> cart(v_cl);
779 780 781 782 783 784 785 786 787 788 789 790 791 792 793

		(static_cast<ie_loc_ghost<dim,T>*>(&cart))->operator=(static_cast<ie_loc_ghost<dim,T>>(*this));
		(static_cast<nn_prcs<dim,T>*>(&cart))->operator=(static_cast<nn_prcs<dim,T>>(*this));
		(static_cast<ie_ghost<dim,T>*>(&cart))->operator=(static_cast<ie_ghost<dim,T>>(*this));

		cart.sub_domains = sub_domains;
		cart.box_nn_processor = box_nn_processor;
		cart.fine_s = fine_s;
		cart.gr = gr;
		cart.cd = cd;
		cart.domain = domain;
		std::copy(spacing,spacing+3,cart.spacing);

		cart.ghost = ghost;

incardon's avatar
incardon committed
794 795
		cart.bbox = bbox;

796 797 798
		for (size_t i = 0 ; i < dim ; i++)
			cart.bc[i] = this->bc[i];

799 800 801 802 803 804 805
		return cart;
	}

	/*! \brief Copy the element
	 *
	 * \param cart element to copy
	 *
Pietro Incardona's avatar
Pietro Incardona committed
806 807
	 * \return itself
	 *
808
	 */
Pietro Incardona's avatar
Pietro Incardona committed
809
	CartDecomposition<dim,T,Memory> & operator=(const CartDecomposition & cart)
810 811 812 813 814 815 816 817 818 819 820 821 822 823 824
	{
		static_cast<ie_loc_ghost<dim,T>*>(this)->operator=(static_cast<ie_loc_ghost<dim,T>>(cart));
		static_cast<nn_prcs<dim,T>*>(this)->operator=(static_cast<nn_prcs<dim,T>>(cart));
		static_cast<ie_ghost<dim,T>*>(this)->operator=(static_cast<ie_ghost<dim,T>>(cart));

		sub_domains = cart.sub_domains;
		box_nn_processor = cart.box_nn_processor;
		fine_s = cart.fine_s;
		gr = cart.gr;
		cd = cart.cd;
		domain = cart.domain;
		std::copy(cart.spacing,cart.spacing+3,spacing);

		ghost = cart.ghost;

incardon's avatar
incardon committed
825 826
		bbox = cart.bbox;

827 828 829
		for (size_t i = 0 ; i < dim ; i++)
			bc[i] = cart.bc[i];

830 831 832 833 834 835 836
		return *this;
	}

	/*! \brief Copy the element, move semantic
	 *
	 * \param cart element to copy
	 *
Pietro Incardona's avatar
Pietro Incardona committed
837 838
	 * \return itself
	 *
839
	 */
Pietro Incardona's avatar
Pietro Incardona committed
840
	CartDecomposition<dim,T,Memory> & operator=(CartDecomposition && cart)
841
	{
Pietro Incardona's avatar
Pietro Incardona committed
842 843 844
		static_cast<ie_loc_ghost<dim,T>*>(this)->operator=(static_cast<ie_loc_ghost<dim,T>>(cart));
		static_cast<nn_prcs<dim,T>*>(this)->operator=(static_cast<nn_prcs<dim,T>>(cart));
		static_cast<ie_ghost<dim,T>*>(this)->operator=(static_cast<ie_ghost<dim,T>>(cart));
845 846 847 848 849 850 851 852 853 854 855

		sub_domains.swap(cart.sub_domains);
		box_nn_processor.swap(cart.box_nn_processor);
		fine_s.swap(cart.fine_s);
		gr = cart.gr;
		cd = cart.cd;
		domain = cart.domain;
		std::copy(cart.spacing,cart.spacing+3,spacing);

		ghost = cart.ghost;

856
		bbox = cart.bbox;
incardon's avatar
incardon committed
857

858
		for (size_t i = 0 ; i < dim ; i++)
859 860 861
			bc[i] = cart.bc[i];

		return *this;
862

863 864 865
		return *this;
	}

866 867 868 869 870 871
	/*! \brief The default grid size
	 *
	 *  The default grid is always an isotropic grid that adapt with the number of processors,
	 *  it define in how many cell it will be divided the space for a particular required minimum
	 *  number of sub-domain
	 *
Pietro Incardona's avatar
Pietro Incardona committed
872 873 874 875
	 * \param n_sub number of subdomains per processors
	 *
	 * \return grid dimension (it is one number because on the other dimensions is the same)
	 *
876 877 878 879 880
	 */
	static size_t getDefaultGrid(size_t n_sub)
	{
		// Calculate the number of sub-sub-domain on
		// each dimension
881
		return openfpm::math::round_big_2(pow(n_sub, 1.0 / dim));
882 883
	}

884
	/*! \brief Given a point return in which processor the particle should go
Pietro Incardona's avatar
Pietro Incardona committed
885 886
	 *
	 * \param p point
incardon's avatar
incardon committed
887 888 889 890
	 *
	 * \return processorID
	 *
	 */
incardon's avatar
incardon committed
891
	template<typename Mem> size_t inline processorID(const encapc<1, Point<dim,T>, Mem> & p) const
incardon's avatar
incardon committed
892
	{
incardon's avatar
incardon committed
893
		return fine_s.get(cd.template getCell(p));
incardon's avatar
incardon committed
894 895
	}

896
	/*! \brief Given a point return in which processor the particle should go
Pietro Incardona's avatar
Pietro Incardona committed
897 898
	 *
	 * \param p point
incardon's avatar
incardon committed
899
	 *
900
	 * \return processorID
incardon's avatar
incardon committed
901 902
	 *
	 */
903
	size_t inline processorID(const Point<dim,T> &p) const
incardon's avatar
incardon committed
904
	{
incardon's avatar
incardon committed
905
		return fine_s.get(cd.getCell(p));
incardon's avatar
incardon committed
906 907
	}

908
	/*! \brief Given a point return in which processor the particle should go
Pietro Incardona's avatar
Pietro Incardona committed
909 910
	 *
	 * \param p point
incardon's avatar
incardon committed
911 912 913 914
	 *
	 * \return processorID
	 *
	 */
915
	size_t inline processorID(const T (&p)[dim]) const
incardon's avatar
incardon committed
916
	{
incardon's avatar
incardon committed
917
		return fine_s.get(cd.getCell(p));
incardon's avatar
incardon committed
918
	}
incardon's avatar
incardon committed
919

Pietro Incardona's avatar
Pietro Incardona committed
920
	/*! \brief Given a point return in which processor the point/particle should go
incardon's avatar
incardon committed
921
	 *
922 923
	 * Boundary conditions are considered
	 *
Pietro Incardona's avatar
Pietro Incardona committed
924 925
	 * \param p point
	 *
926
	 * \return processorID
incardon's avatar
incardon committed
927 928
	 *
	 */
929
	template<typename Mem> size_t inline processorIDBC(encapc<1, Point<dim,T>, Mem> p)
incardon's avatar
incardon committed
930
	{
931 932 933 934
		Point<dim,T> pt = p;
		applyPointBC(pt);

		return fine_s.get(cd.getCell(pt));
incardon's avatar
incardon committed
935 936
	}

937
	/*! \brief Given a point return in which processor the particle should go
938 939
	 *
	 * Boundary conditions are considered
incardon's avatar
incardon committed
940
	 *
Pietro Incardona's avatar
Pietro Incardona committed
941 942
	 * \param p point
	 *
incardon's avatar
incardon committed
943 944 945
	 * \return processorID
	 *
	 */
Pietro Incardona's avatar
Pietro Incardona committed
946
	template<typename ofb> size_t inline processorIDBC(const Point<dim,T> &p) const
947 948 949
	{
		Point<dim,T> pt = p;
		applyPointBC(pt);
incardon's avatar
incardon committed
950

951 952 953 954 955 956 957
		return fine_s.get(cd.getCell(p));
	}

	/*! \brief Given a point return in which processor the particle should go
	 *
	 * Boundary consition are considered
	 *
Pietro Incardona's avatar
Pietro Incardona committed
958 959
	 * \param p point position
	 *
960 961 962
	 * \return processorID
	 *
	 */
Pietro Incardona's avatar
Pietro Incardona committed
963
	template<typename ofb> size_t inline processorIDBC(const T (&p)[dim]) const
incardon's avatar
incardon committed
964
	{
965 966 967
		Point<dim,T> pt = p;
		applyPointBC(pt);

incardon's avatar
incardon committed
968
		return fine_s.get(cd.getCell(p));
incardon's avatar
incardon committed
969 970
	}

971 972 973 974 975 976 977
	/*! \brief Get the periodicity on i dimension
	 *
	 * \param i dimension
	 *
	 * \return the periodicity in direction i
	 *
	 */
978
	inline size_t periodicity(size_t i)
979 980 981 982
	{
		return bc[i];
	}

983 984 985 986 987 988 989 990 991 992 993
	/*! \brief Get the periodicity
	 *
	 *
	 * \return the periodicity
	 *
	 */
	inline const size_t (& periodicity() const) [dim]
	{
		return bc;
	}

994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018
	/*! \brief Calculate magnification
	 *
	 * \param gm distribution grid
	 *
	 */
	void calculate_magn(const grid_sm<dim,void> & gm)
	{
		if (gm.size() == 0)
		{
			for (size_t i = 0 ; i < dim ; i++)
				magn[i] = 1;
		}
		else
		{
			for (size_t i = 0 ; i < dim ; i++)
			{
				if (gr.size(i) % gm.size(i) != 0)
					std::cerr << __FILE__ << ":" << __LINE__ << ".Error the distribution grid must be multiple of the decomposition grid" << std::endl;

				magn[i] = gr.size(i) / gm.size(i);
			}
		}
	}


incardon's avatar
incardon committed
1019 1020
	/*! \brief Set the parameter of the decomposition
	 *
1021
	 * \param div_ storing into how many sub-sub-domains to decompose on each dimension
1022
	 * \param domain_ domain to decompose
Pietro Incardona's avatar
Pietro Incardona committed
1023
	 * \param bc boundary conditions
1024
	 * \param ghost Ghost size
1025 1026 1027
	 * \param sec_dist Distribution grid. The distribution grid help in reducing the underlying
	 *                 distribution problem simplifying decomposition problem. This is done in order to
	 *                 reduce the load/balancing dynamic load balancing problem
incardon's avatar
incardon committed
1028 1029
	 *
	 */
1030
	void setParameters(const size_t (& div_)[dim], ::Box<dim,T> domain_, const size_t (& bc)[dim] ,const Ghost<dim,T> & ghost, const grid_sm<dim,void> & sec_dist = grid_sm<dim,void>())
incardon's avatar
incardon committed
1031
	{
Pietro Incardona's avatar
Pietro Incardona committed
1032 1033 1034 1035
		// set the boundary conditions
		for (size_t i = 0 ; i < dim ; i++)
			this->bc[i] = bc[i];

incardon's avatar
incardon committed
1036 1037
		// set the ghost
		this->ghost = ghost;
incardon's avatar
incardon committed
1038

1039
		// Set the decomposition parameters
incardon's avatar
incardon committed
1040
		gr.setDimensions(div_);
incardon's avatar
incardon committed
1041
		domain = domain_;
1042
		cd.setDimensions(domain, div_, 0);
incardon's avatar
incardon committed
1043

1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055
		// We we have a secondary grid costruct a reduced graph
		if (sec_dist.size(0) != 0)
		{
			calculate_magn(sec_dist);
			gr_dist.setDimensions(sec_dist.getSize());
		}
		else
		{
			calculate_magn(sec_dist);
			gr_dist = gr;
		}

1056
		// init distribution
1057
		dist.createCartGraph(gr_dist, domain);
1058 1059 1060

	}

Pietro Incardona's avatar
Pietro Incardona committed
1061 1062 1063 1064
	/*! \brief Delete the decomposition and reset the data-structure
	 *
	 *
	 */
1065 1066 1067 1068 1069
	void reset()
	{
		sub_domains.clear();
		box_nn_processor.clear();
		fine_s.clear();
incardon's avatar
incardon committed
1070
		loc_box.clear();
1071 1072 1073 1074 1075
		nn_prcs<dim, T>::reset();
		ie_ghost<dim, T>::reset();
		ie_loc_ghost<dim, T>::reset();
	}

1076 1077 1078 1079 1080
	/*! \brief Start decomposition
	 *
	 */
	void decompose()
	{
1081 1082
		reset();

incardon's avatar
incardon committed
1083 1084
		if (commCostSet == false)
			computeCommunicationAndMigrationCosts(1);
1085 1086

		dist.decompose();
incardon's avatar
incardon committed
1087

1088
		createSubdomains(v_cl,bc);
1089 1090

		calculateGhostBoxes();
incardon's avatar
incardon committed
1091 1092

		domain_nn_calculator_cart<dim>::reset();
1093
		domain_nn_calculator_cart<dim>::setParameters(proc_box);
incardon's avatar
incardon committed
1094 1095
	}

1096
	/*! \brief Refine the decomposition, available only for ParMetis distribution, for Metis it is a null call
Pietro Incardona's avatar
Pietro Incardona committed
1097 1098
	 *
	 * \param ts number of time step from the previous load balancing
1099 1100
	 *
	 */
incardon's avatar
incardon committed
1101
	void refine(size_t ts)
1102