CartDecomposition.hpp 58.9 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
#include "cuda/CartDecomposition_gpu.cuh"
incardon's avatar
incardon committed
42
#include "Domain_icells_cart.hpp"
incardon's avatar
incardon committed
43

44 45
#define CARTDEC_ERROR 2000lu

incardon's avatar
incardon committed
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
/*! \brief It spread the sub-sub-domain on a regular cartesian grid of size dim
 *
 * \warning this function only guarantee that the division on each direction is
 *          2^n with some n and does not guarantee that the number of
 *          sub-sub-domain is preserved
 *
 * \param div number of division on each direction as output
 * \param n_sub number of sub-domain
 * \param dim_r dimension reduction
 *
 */
template<unsigned int dim> static void nsub_to_div2(size_t (& div)[dim], size_t n_sub, size_t dim_r)
{
	for (size_t i = 0; i < dim; i++)
	{
		if (i < dim_r)
		{div[i] = openfpm::math::round_big_2(pow(n_sub, 1.0 / dim_r));}
		else
		{div[i] = 1;}
	}
}

/*! \brief It spread the sub-sub-domain on a regular cartesian grid of size dim
 *
 * \warning this function only guarantee that the division on each direction is
 *          2^n with some n and does not guarantee that the number of
 *          sub-sub-domain is preserved
 *
 * \param div number of division on each direction as output
 * \param n_sub number of sub-domain
 * \param dim_r dimension reduction
 *
 */
template<unsigned int dim> static void nsub_to_div(size_t (& div)[dim], size_t n_sub, size_t dim_r)
{
	for (size_t i = 0; i < dim; i++)
	{
		if (i < dim_r)
		{div[i] = std::floor(pow(n_sub, 1.0 / dim_r));}
		else
		{div[i] = 1;}
	}
}

incardon's avatar
incardon committed
90 91
#define COMPUTE_SKIN_SUB 1

incardon's avatar
incardon committed
92
/**
Pietro Incardona's avatar
Pietro Incardona committed
93
 * \brief This class decompose a space into sub-sub-domains and distribute them across processors
incardon's avatar
incardon committed
94 95 96 97
 *
 * \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
98
 * \tparam Distribution type of distribution, can be ParMetisDistribution or MetisDistribution
incardon's avatar
incardon committed
99
 *
100
 * Given an N-dimensional space, this class decompose the space into a Cartesian grid of small
incardon's avatar
incardon committed
101 102 103 104
 * 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
105 106 107 108
 *
 * Assuming that VCluster.getProcessUnitID(), equivalent to the MPI processor rank, return the processor local
 * processor id, we define
 *
109 110
 * * local processor: processor rank
 * * local sub-domain: sub-domain given to the local processor
111 112 113 114 115 116 117 118 119 120 121 122
 * * 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
123
 * * Local ghosts internal or external are all the ghosts that does not involve inter-processor communications
124 125
 *
 * \see calculateGhostBoxes() for a visualization of internal and external ghost boxes
incardon's avatar
incardon committed
126
 *
127
 * ### Create a Cartesian decomposition object on a Box space, distribute, calculate internal and external ghost boxes
Pietro Incardona's avatar
Pietro Incardona committed
128
 *
129 130
 * \snippet CartDecomposition_unit_test.hpp Create CartDecomposition
 *
incardon's avatar
incardon committed
131 132
 */

incardon's avatar
incardon committed
133
template<unsigned int dim, typename T, typename Memory, template <typename> class layout_base, typename Distribution>
incardon's avatar
incardon committed
134 135 136 137 138
class CartDecomposition: public ie_loc_ghost<dim, T,layout_base, Memory>,
						 public nn_prcs<dim, T,layout_base,Memory>,
						 public ie_ghost<dim,T,Memory,layout_base>,
						 public domain_nn_calculator_cart<dim>,
						 public domain_icell_calculator<dim,T,layout_base,Memory>
incardon's avatar
incardon committed
139 140
{
public:
incardon's avatar
incardon committed
141

incardon's avatar
incardon committed
142 143 144 145
	//! Type of the domain we are going to decompose
	typedef T domain_type;

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

Pietro Incardona's avatar
Pietro Incardona committed
148
	//! This class is base of itself
incardon's avatar
incardon committed
149
	typedef CartDecomposition<dim,T,Memory,layout_base,Distribution> base_type;
Pietro Incardona's avatar
Pietro Incardona committed
150 151

	//! This class admit a class defined on an extended domain
incardon's avatar
incardon committed
152
	typedef CartDecomposition_ext<dim,T,Memory,layout_base,Distribution> extended_type;
Pietro Incardona's avatar
Pietro Incardona committed
153 154

protected:
incardon's avatar
incardon committed
155

incardon's avatar
incardon committed
156 157 158
	//! bool that indicate whenever the buffer has been already transfer to device
	bool host_dev_transfer = false;

incardon's avatar
incardon committed
159 160 161
	//! Indicate the communication weight has been set
	bool commCostSet = false;

incardon's avatar
incardon committed
162
	//! This is the key type to access  data_s, for example in the case of vector
incardon's avatar
incardon committed
163
	//! acc_key is size_t
Pietro Incardona's avatar
Pietro Incardona committed
164 165 166 167 168 169
	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
170 171

	//! the set of all local sub-domain as vector
incardon's avatar
incardon committed
172
	openfpm::vector<SpaceBox<dim, T>,Memory,typename layout_base<SpaceBox<dim, T>>::type,layout_base> sub_domains;
incardon's avatar
incardon committed
173

174
	//! the remote set of all sub-domains as vector of 'sub_domains' vectors
incardon's avatar
incardon committed
175
	mutable openfpm::vector<Box_map<dim, T>,Memory,typename layout_base<Box_map<dim, T>>::type,layout_base> sub_domains_global;
Yaroslav's avatar
Yaroslav committed
176

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

incardon's avatar
incardon committed
180
	//! Structure that contain for each sub-sub-domain box the processor id
incardon's avatar
Add ORB  
incardon committed
181
	//! exist for efficient global communication
incardon's avatar
incardon committed
182
	CellList<dim,T,Mem_fast<Memory,int>,shift<dim,T>> fine_s;
incardon's avatar
incardon committed
183

incardon's avatar
incardon committed
184
	//! Structure that store the cartesian grid information
185
	grid_sm<dim, void> gr;
incardon's avatar
incardon committed
186

187 188 189
	//! Structure that store the cartesian grid information
	grid_sm<dim, void> gr_dist;

incardon's avatar
incardon committed
190
	//! Structure that decompose the space into cells without creating them
incardon's avatar
incardon committed
191
	//! useful to convert positions to CellId or sub-domain id in this case
192
	CellDecomposer_sm<dim, T, shift<dim,T>> cd;
incardon's avatar
incardon committed
193 194

	//! rectangular domain to decompose
Pietro Incardona's avatar
Pietro Incardona committed
195
	::Box<dim,T> domain;
incardon's avatar
incardon committed
196 197 198 199

	//! Box Spacing
	T spacing[dim];

200 201 202 203
	//! Magnification factor between distribution and
	//! decomposition
	size_t magn[dim];

incardon's avatar
incardon committed
204
	//! Runtime virtual cluster machine
205
	Vcluster<> & v_cl;
incardon's avatar
incardon committed
206

207
	//! Create distribution
208 209
	Distribution dist;

210
	//! Processor bounding box
incardon's avatar
incardon committed
211
	::Box<dim,T> bbox;
incardon's avatar
incardon committed
212

213
	//! reference counter of the object in case is shared between object
214 215
	long int ref_cnt;

216
	//! ghost info
217 218
	Ghost<dim,T> ghost;

219
	//! Boundary condition info
Pietro Incardona's avatar
Pietro Incardona committed
220
	size_t bc[dim];
incardon's avatar
incardon committed
221

incardon's avatar
incardon committed
222 223 224 225 226 227
	//! 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
228 229 230
	/*! \brief It convert the box from the domain decomposition into sub-domain
	 *
	 * The decomposition box from the domain-decomposition contain the box in integer
231 232
	 * 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
233 234 235
	 *
	 * \param loc_box local box
	 *
236
	 * \return the corresponding sub-domain
Pietro Incardona's avatar
Pietro Incardona committed
237 238
	 *
	 */
239
	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
240 241 242 243 244 245 246 247
	{
		// 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);
248 249 250 251 252 253 254 255 256
		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
257 258 259 260 261 262 263 264 265 266 267 268 269
		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++)
		{
270
			if (sub_dc.getHigh(i) == gr.size(i) - 1)
incardon's avatar
incardon committed
271
			{sub_d.setHigh(i, domain.getHigh(i));}
Pietro Incardona's avatar
Pietro Incardona committed
272 273

			if (sub_dc.getLow(i) == 0)
incardon's avatar
incardon committed
274
			{sub_d.setLow(i,domain.getLow(i));}
Pietro Incardona's avatar
Pietro Incardona committed
275 276 277 278 279
		}

		return sub_d;
	}

incardon's avatar
incardon committed
280
	void collect_all_sub_domains(openfpm::vector<Box_map<dim,T>,Memory,typename layout_base<Box_map<dim, T>>::type,layout_base> & sub_domains_global)
281 282 283 284 285 286
	{
#ifdef SE_CLASS2
		check_valid(this,8);
#endif

		sub_domains_global.clear();
incardon's avatar
incardon committed
287
		openfpm::vector<Box_map<dim,T>,Memory,typename layout_base<Box_map<dim, T>>::type,layout_base> bm;
288 289 290

		for (size_t i = 0 ; i < sub_domains.size() ; i++)
		{
incardon's avatar
incardon committed
291
			bm.add();
292

incardon's avatar
incardon committed
293 294
			bm.template get<0>(bm.size()-1) = ::SpaceBox<dim,T>(sub_domains.get(i));
			bm.template get<1>(bm.size()-1) = v_cl.rank();
295 296
		}

incardon's avatar
incardon committed
297
		v_cl.SGather<decltype(bm),decltype(sub_domains_global),layout_base>(bm,sub_domains_global,0);
298 299 300 301 302 303 304 305 306 307 308

		size_t size = sub_domains_global.size();

		v_cl.max(size);
		v_cl.execute();

		sub_domains_global.resize(size);

		v_cl.Bcast(sub_domains_global,0);
		v_cl.execute();
	}
Pietro Incardona's avatar
Pietro Incardona committed
309 310 311

public:

312 313 314 315 316 317 318
	void initialize_fine_s(const ::Box<dim,T> & domain)
	{
		fine_s.clear();
		size_t div_g[dim];

		// We reduce the size of the cells by a factor 8 in 3d 4 in 2d
		for (size_t i = 0 ; i < dim ; i++)
incardon's avatar
incardon committed
319
		{div_g[i] = (gr.size(i) == 1)?1:gr.size(i)/2;}
320 321 322 323 324 325 326 327 328 329 330 331 332 333

		fine_s.Initialize(domain,div_g);
	}

	void construct_fine_s()
	{
		collect_all_sub_domains(sub_domains_global);

		// now draw all sub-domains in fine-s

		for (size_t i = 0 ; i < sub_domains_global.size() ; i++)
		{

			// get the cells this box span
334 335
			const grid_key_dx<dim> p1 = fine_s.getCellGrid_me(sub_domains_global.template get<0>(i).getP1());
			const grid_key_dx<dim> p2 = fine_s.getCellGrid_pe(sub_domains_global.template get<0>(i).getP2());
336 337 338 339 340 341 342 343 344 345

			// Get the grid and the sub-iterator
			auto & gi = fine_s.getGrid();
			grid_key_dx_iterator_sub<dim> g_sub(gi,p1,p2);

			// add the box-id to the cell list
			while (g_sub.isNext())
			{
				auto key = g_sub.get();
				fine_s.addCell(gi.LinId(key),i);
incardon's avatar
incardon committed
346

347 348 349
				++g_sub;
			}
		}
incardon's avatar
incardon committed
350 351

		host_dev_transfer = false;
352 353
	}

354
	/*! \brief Constructor, it decompose and distribute the sub-domains across the processors
incardon's avatar
incardon committed
355
	 *
356
	 * \param v_cl Virtual cluster, used internally for communications
Pietro Incardona's avatar
Pietro Incardona committed
357
	 * \param bc boundary conditions
incardon's avatar
incardon committed
358
	 * \param opt option (one option is to construct)
359
	 *
incardon's avatar
incardon committed
360
	 */
361
	void createSubdomains(Vcluster<> & v_cl, const size_t (& bc)[dim], size_t opt = 0)
incardon's avatar
incardon committed
362
	{
363 364
		int p_id = v_cl.getProcessUnitID();

incardon's avatar
incardon committed
365 366 367
		// Calculate the total number of box and and the spacing
		// on each direction
		// Get the box containing the domain
368
		SpaceBox<dim, T> bs = domain.getBox();
incardon's avatar
incardon committed
369

370
		for (unsigned int i = 0; i < dim; i++)
incardon's avatar
incardon committed
371 372
		{
			// Calculate the spacing
incardon's avatar
incardon committed
373
			spacing[i] = (bs.getHigh(i) - bs.getLow(i)) / gr.size(i);
incardon's avatar
incardon committed
374 375
		}

incardon's avatar
Add ORB  
incardon committed
376
		// fill the structure that store the processor id for each sub-domain
377
		initialize_fine_s(domain);
incardon's avatar
Add ORB  
incardon committed
378

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

383 384 385 386 387 388 389 390 391 392
		// 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
393
		// optimize the decomposition
incardon's avatar
incardon committed
394
		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
395

396
		// Initialize
incardon's avatar
incardon committed
397
		if (loc_box.size() > 0)
incardon's avatar
incardon committed
398
		{
Pietro Incardona's avatar
Pietro Incardona committed
399
			bbox = convertDecBoxIntoSubDomain(loc_box.get(0));
incardon's avatar
incardon committed
400
			proc_box = loc_box.get(0);
401
			sub_domains.add(bbox);
incardon's avatar
incardon committed
402
		}
incardon's avatar
incardon committed
403 404 405 406 407 408
		else
		{
			// invalidate all the boxes
			for (size_t i = 0 ; i < dim ; i++)
			{
				proc_box.setLow(i,0.0);
incardon's avatar
incardon committed
409
				proc_box.setHigh(i,0);
incardon's avatar
incardon committed
410 411

				bbox.setLow(i,0.0);
incardon's avatar
incardon committed
412
				bbox.setHigh(i,0);
incardon's avatar
incardon committed
413 414
			}
		}
Pietro Incardona's avatar
Pietro Incardona committed
415

incardon's avatar
incardon committed
416
		// convert into sub-domain
417
		for (size_t s = 1; s < loc_box.size(); s++)
incardon's avatar
incardon committed
418
		{
Pietro Incardona's avatar
Pietro Incardona committed
419
			SpaceBox<dim,T> sub_d = convertDecBoxIntoSubDomain(loc_box.get(s));
420

incardon's avatar
incardon committed
421 422
			// add the sub-domain
			sub_domains.add(sub_d);
incardon's avatar
incardon committed
423 424 425

			// Calculate the bound box
			bbox.enclose(sub_d);
incardon's avatar
incardon committed
426
			proc_box.enclose(loc_box.get(s));
incardon's avatar
incardon committed
427
		}
incardon's avatar
incardon committed
428

incardon's avatar
incardon committed
429 430
		nn_prcs<dim,T,layout_base,Memory>::create(box_nn_processor, sub_domains);
		nn_prcs<dim,T,layout_base,Memory>::applyBC(domain,ghost,bc);
431

incardon's avatar
incardon committed
432
		// fill fine_s structure
incardon's avatar
incardon committed
433 434 435
		// 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)
436

437
		///////////////////////////////// TODO //////////////////////////////////////////
incardon's avatar
incardon committed
438

439 440
		construct_fine_s();

incardon's avatar
incardon committed
441 442 443 444 445 446 447 448 449 450
		Initialize_geo_cell_lists();
	}

	/*! \brief Initialize geo_cell lists
	 *
	 *
	 *
	 */
	void Initialize_geo_cell_lists()
	{
451 452 453
		// Get the processor bounding Box
		::Box<dim,T> bound = getProcessorBounds();

incardon's avatar
incardon committed
454 455
		// Check if the box is valid
		if (bound.isValidN() == true)
incardon's avatar
incardon committed
456 457 458 459
		{
			// calculate the sub-divisions
			size_t div[dim];
			for (size_t i = 0; i < dim; i++)
incardon's avatar
incardon committed
460
			{div[i] = (size_t) ((bound.getHigh(i) - bound.getLow(i)) / cd.getCellBox().getP2()[i]);}
461

incardon's avatar
incardon committed
462
			// Initialize the geo_cell structure
incardon's avatar
incardon committed
463
			ie_ghost<dim,T,Memory,layout_base>::Initialize_geo_cell(bound,div);
incardon's avatar
incardon committed
464 465

			// Initialize shift vectors
incardon's avatar
incardon committed
466
			ie_ghost<dim,T,Memory,layout_base>::generateShiftVectors(domain,bc);
incardon's avatar
incardon committed
467
		}
incardon's avatar
incardon committed
468 469
	}

470 471 472 473 474 475
	/*! \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
476
		float migration = 0;
477 478

		SpaceBox<dim, T> cellBox = cd.getCellBox();
incardon's avatar
incardon committed
479 480
		float b_s = static_cast<float>(cellBox.getHigh(0));
		float gh_s = static_cast<float>(ghost.getHigh(0));
481 482 483 484 485

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

		// multiply for sub-sub-domain side for each domain
486
		for (size_t i = 2; i < dim; i++)
incardon's avatar
incardon committed
487 488
		{
			/* coverity[dead_error_line] */
489
			gh_v *= b_s;
incardon's avatar
incardon committed
490
		}
491 492 493 494 495 496 497 498 499

		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
500
			dist.setMigrationCost(i, norm * migration /* * dist.getSubSubDomainComputationCost(i)*/ );
501 502 503

			for (size_t s = 0; s < dist.getNSubSubDomainNeighbors(i); s++)
			{
incardon's avatar
incardon committed
504 505 506
				// We have to remove dist.getSubSubDomainComputationCost(i) otherwise the graph is
				// not directed
				dist.setCommunicationCost(i, s, 1 /** dist.getSubSubDomainComputationCost(i)*/  *  ts);
507 508 509
			}
			prev += dist.getNSubSubDomainNeighbors(i);
		}
incardon's avatar
incardon committed
510 511

		commCostSet = true;
512
	}
incardon's avatar
incardon committed
513

incardon's avatar
incardon committed
514
	/*! \brief Create the sub-domain that decompose your domain
incardon's avatar
incardon committed
515 516 517 518 519
	 *
	 */
	void CreateSubspaces()
	{
		// Create a grid where each point is a space
520
		grid_sm<dim, void> g(div);
incardon's avatar
incardon committed
521 522 523 524 525 526 527 528 529 530 531

		// 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
532
			SpaceBox<dim, T> tmp;
incardon's avatar
incardon committed
533 534

			//! fill with the Margin of the box
535
			for (int i = 0; i < dim; i++)
incardon's avatar
incardon committed
536
			{
537 538
				tmp.setHigh(i, (key.get(i) + 1) * spacing[i]);
				tmp.setLow(i, key.get(i) * spacing[i]);
incardon's avatar
incardon committed
539 540 541 542 543
			}

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

incardon's avatar
incardon committed
544
			// Next sub-domain
incardon's avatar
incardon committed
545 546 547 548
			++gk_it;
		}
	}

549

incardon's avatar
incardon committed
550
	/*! \brief It calculate the internal ghost boxes
551 552 553 554 555 556 557 558
	 *
	 * Example: Processor 10 calculate
	 * B8_0 B9_0 B9_1 and B5_0
	 *
	 *
	 *
	 \verbatim

Pietro Incardona's avatar
Pietro Incardona committed
559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
	+----------------------------------------------------+
	|                                                    |
	|                 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                |
	 |              |   |                           |    |                                   |
	 |              |   |                           |    |                                   |
	 |              |   |                           |    |                                   |
	 +--------------+---+---------------------------+----+                                   |
														 |                                   |
														 +-----------------------------------+
583 584 585 586 587 588 589


 \endverbatim

       and also
       G8_0 G9_0 G9_1 G5_0 (External ghost boxes)

Pietro Incardona's avatar
Pietro Incardona committed
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615
\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|                              |
	|                 |   |                                    |    |                              |
	|                 |   |                                    |    |                              |
	+---------------------+------------------------------------+    |                              |
					  |                                        |    |                              |
					  +----------------------------------------+----+------------------------------+
616 617 618 619

	 \endverbatim

	 *
incardon's avatar
incardon committed
620
	 * ghost margins for each dimensions (p1 negative part) (p2 positive part)
621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647
	 *
	 *
	 \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
incardon's avatar
incardon committed
648 649
		ie_ghost<dim, T,Memory,layout_base>::create_box_nn_processor_ext(v_cl, ghost, sub_domains, box_nn_processor, *this);
		ie_ghost<dim, T,Memory,layout_base>::create_box_nn_processor_int(v_cl, ghost, sub_domains, box_nn_processor, *this);
650

incardon's avatar
incardon committed
651
		ie_loc_ghost<dim,T,layout_base,Memory>::create(sub_domains,domain,ghost,bc);
incardon's avatar
incardon committed
652 653 654 655

		// Ghost box information must be re-offloaded
		host_dev_transfer = false;
		ie_ghost<dim, T,Memory,layout_base>::reset_host_dev_transfer();
656 657
	}

658

incardon's avatar
incardon committed
659 660
public:

Pietro Incardona's avatar
Pietro Incardona committed
661
	//! Space dimensions
662 663
	static constexpr int dims = dim;

Pietro Incardona's avatar
Pietro Incardona committed
664
	//! Space type
665 666
	typedef T stype;

incardon's avatar
incardon committed
667 668 669 670 671 672 673 674 675 676 677 678 679 680
	//! 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
681 682
	/*! \brief Cartesian decomposition constructor
	 *
683
	 * \param v_cl Virtual cluster, used internally to handle or pipeline communication
incardon's avatar
incardon committed
684 685
	 *
	 */
686
	CartDecomposition(Vcluster<> & v_cl)
incardon's avatar
incardon committed
687
	:nn_prcs<dim, T, layout_base,Memory>(v_cl), v_cl(v_cl), dist(v_cl),ref_cnt(0)
incardon's avatar
incardon committed
688 689 690 691
	{
		// Reset the box to zero
		bbox.zero();
	}
incardon's avatar
incardon committed
692

693 694 695 696 697
	/*! \brief Cartesian decomposition copy constructor
	 *
     * \param cart object to copy
	 *
	 */
incardon's avatar
incardon committed
698
	CartDecomposition(const CartDecomposition<dim,T,Memory,layout_base,Distribution> & cart)
incardon's avatar
incardon committed
699
	:nn_prcs<dim,T,layout_base,Memory>(cart.v_cl),v_cl(cart.v_cl),dist(v_cl),ref_cnt(0)
700 701 702 703 704 705 706 707 708
	{
		this->operator=(cart);
	}

	/*! \brief Cartesian decomposition copy constructor
	 *
     * \param cart object to copy
	 *
	 */
incardon's avatar
incardon committed
709
	CartDecomposition(CartDecomposition<dim,T,Memory,layout_base,Distribution> && cart)
incardon's avatar
incardon committed
710
	:nn_prcs<dim,T,layout_base,Memory>(cart.v_cl),v_cl(cart.v_cl),dist(v_cl),ref_cnt(0)
711 712 713 714
	{
		this->operator=(cart);
	}

incardon's avatar
incardon committed
715 716
	//! Cartesian decomposition destructor
	~CartDecomposition()
717 718
	{
	}
incardon's avatar
incardon committed
719

720 721 722 723 724 725 726 727 728 729 730 731 732 733
	/*! \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
		 *
		 */
734
		inline static size_t id(p_box<dim, T> & p, size_t b_id)
735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753
		{
			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
		 *
		 */
incardon's avatar
incardon committed
754
		template<typename encap_type> inline static size_t id(const encap_type & p, size_t b_id)
755
		{
incardon's avatar
incardon committed
756
			return p.template get<proc_>();
757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773
		}
	};

	/*! \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
		 *
		 */
incardon's avatar
incardon committed
774
		template<typename encap_type> inline static size_t id(const encap_type & p, size_t b_id)
775
		{
incardon's avatar
incardon committed
776
			return p.template get<lc_proc_>();
777 778 779
		}
	};

780 781 782 783 784 785 786 787 788 789 790 791 792 793
	/*! \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
		 *
		 */
incardon's avatar
incardon committed
794
		template<typename encap_type> inline static size_t id(const encap_type & p, size_t b_id)
795
		{
incardon's avatar
incardon committed
796
			return p.template get<shift_id_>();
797 798 799
		}
	};

800 801
	/*! \brief Apply boundary condition to the point
	 *
Pietro Incardona's avatar
Pietro Incardona committed
802 803 804 805 806
	 * 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)
807 808 809 810 811 812 813
	 *
	 */
	void applyPointBC(float (& pt)[dim]) const
	{
		for (size_t i = 0 ; i < dim ; i++)
		{
			if (bc[i] == PERIODIC)
814
			{pt[i] = openfpm::math::periodic_l(pt[i],domain.getHigh(i),domain.getLow(i));}
815 816 817 818 819
		}
	}

	/*! \brief Apply boundary condition to the point
	 *
Pietro Incardona's avatar
Pietro Incardona committed
820 821 822 823 824
	 * 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)
825 826 827 828 829 830 831
	 *
	 */
	void applyPointBC(Point<dim,T> & pt) const
	{
		for (size_t i = 0 ; i < dim ; i++)
		{
			if (bc[i] == PERIODIC)
832
			{pt.get(i) = openfpm::math::periodic_l(pt.get(i),domain.getHigh(i),domain.getLow(i));}
833 834 835 836 837
		}
	}

	/*! \brief Apply boundary condition to the point
	 *
Pietro Incardona's avatar
Pietro Incardona committed
838 839 840 841 842
	 * 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)
843 844 845 846 847 848 849
	 *
	 */
	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)
850
			{pt.template get<0>()[i] = openfpm::math::periodic_l(pt.template get<0>()[i],domain.getHigh(i),domain.getLow(i));}
851 852 853
		}
	}

incardon's avatar
incardon committed
854 855 856 857 858 859 860
	/*! \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
	 *
	 */
incardon's avatar
incardon committed
861
	CartDecomposition<dim,T,Memory,layout_base,Distribution> duplicate(const Ghost<dim,T> & g) const
incardon's avatar
incardon committed
862
	{
incardon's avatar
incardon committed
863
		CartDecomposition<dim,T,Memory,layout_base,Distribution> cart(v_cl);
incardon's avatar
incardon committed
864 865 866 867 868 869 870 871

		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;
incardon's avatar
incardon committed
872
		for (size_t i = 0 ; i < dim ; i++)
incardon's avatar
incardon committed
873 874 875 876
		{
			cart.spacing[i] = spacing[i];
			cart.magn[i] = magn[i];
		};
incardon's avatar
incardon committed
877 878 879 880

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

881 882
		cart.dist = dist;

883 884 885
		for (size_t i = 0 ; i < dim ; i++)
			cart.bc[i] = bc[i];

incardon's avatar
incardon committed
886 887
		(static_cast<nn_prcs<dim,T,layout_base,Memory> &>(cart)).create(box_nn_processor, sub_domains);
		(static_cast<nn_prcs<dim,T,layout_base,Memory> &>(cart)).applyBC(domain,ghost,bc);
incardon's avatar
incardon committed
888

889 890
		cart.Initialize_geo_cell_lists();
		cart.calculateGhostBoxes();
incardon's avatar
incardon committed
891 892 893 894 895

		return cart;
	}

	/*! \brief It create another object that contain the same information and act in the same way
896
	 *
Pietro Incardona's avatar
Pietro Incardona committed
897
	 * \return a duplicated CartDecomposition object
898 899
	 *
	 */
incardon's avatar
incardon committed
900
	CartDecomposition<dim,T,Memory,layout_base,Distribution> duplicate() const
901
	{
incardon's avatar
incardon committed
902
		CartDecomposition<dim,T,Memory,layout_base,Distribution> cart(v_cl);
903

incardon's avatar
incardon committed
904 905
		(static_cast<ie_loc_ghost<dim,T, layout_base,Memory>*>(&cart))->operator=(static_cast<ie_loc_ghost<dim,T,layout_base,Memory>>(*this));
		(static_cast<nn_prcs<dim,T,layout_base,Memory>*>(&cart))->operator=(static_cast<nn_prcs<dim,T,layout_base,Memory>>(*this));
incardon's avatar
incardon committed
906
		(static_cast<ie_ghost<dim,T,Memory,layout_base>*>(&cart))->operator=(static_cast<ie_ghost<dim,T,Memory,layout_base>>(*this));
907 908 909 910 911

		cart.sub_domains = sub_domains;
		cart.box_nn_processor = box_nn_processor;
		cart.fine_s = fine_s;
		cart.gr = gr;
incardon's avatar
incardon committed
912 913 914
		cart.gr_dist = gr_dist;
		cart.dist = dist;
		cart.commCostSet = commCostSet;
915 916
		cart.cd = cd;
		cart.domain = domain;
incardon's avatar
incardon committed
917
		cart.sub_domains_global = sub_domains_global;
incardon's avatar
incardon committed
918
		for (size_t i = 0 ; i < dim ; i++)
incardon's avatar
incardon committed
919 920 921 922
		{
			cart.spacing[i] = spacing[i];
			cart.magn[i] = magn[i];
		};
923 924 925

		cart.ghost = ghost;

incardon's avatar
incardon committed
926 927
		cart.bbox = bbox;

928
		for (size_t i = 0 ; i < dim ; i++)
incardon's avatar
incardon committed
929
		{cart.bc[i] = this->bc[i];}
930

931 932 933
		return cart;
	}

incardon's avatar
incardon committed
934 935 936 937 938 939 940 941 942 943
	/*! \brief It create another object that contain the same information and act in the same way
	 *
	 * \return a duplicated CartDecomposition object
	 *
	 */
	template<typename Memory2, template <typename> class layout_base2>
	CartDecomposition<dim,T,Memory2,layout_base2,Distribution> duplicate_convert() const
	{
		CartDecomposition<dim,T> cart(v_cl);

incardon's avatar
incardon committed
944 945
		(static_cast<ie_loc_ghost<dim,T,layout_base2,Memory2>*>(&cart))->operator=(static_cast<ie_loc_ghost<dim,T,layout_base,Memory>>(*this));
		(static_cast<nn_prcs<dim,T,layout_base2,Memory2>*>(&cart))->operator=(static_cast<nn_prcs<dim,T,layout_base,Memory>>(*this));
incardon's avatar
incardon committed
946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971
		ie_ghost<dim,T,Memory,layout_base> * ptr = static_cast<ie_ghost<dim,T,Memory,layout_base> *>((CartDecomposition<dim,T,Memory,layout_base,Distribution> *)this);
		(static_cast<ie_ghost<dim,T,Memory2,layout_base2>*>(&cart))->operator=(ptr->template duplicate<Memory2,layout_base2>());

		cart.private_get_sub_domains() = sub_domains;
		cart.private_get_box_nn_processor() = box_nn_processor;
		cart.private_get_fine_s() = fine_s;
		cart.private_get_gr() = gr;
		cart.private_get_gr_dist() = gr_dist;
		cart.private_get_dist() = dist;
		cart.private_get_commCostSet() = commCostSet;
		cart.private_get_cd() = cd;
		cart.private_get_domain() = domain;
		cart.private_get_sub_domains_global() = sub_domains_global;
		for (size_t i = 0 ; i < dim ; i++)
		{cart.private_get_spacing(i) = spacing[i];};

		cart.private_get_ghost() = ghost;

		cart.private_get_bbox() = bbox;

		for (size_t i = 0 ; i < dim ; i++)
		{cart.private_get_bc(i) = this->bc[i];}

		return cart;
	}

972 973 974 975
	/*! \brief Copy the element
	 *
	 * \param cart element to copy
	 *
Pietro Incardona's avatar
Pietro Incardona committed
976 977
	 * \return itself
	 *
978
	 */
incardon's avatar
incardon committed
979
	CartDecomposition<dim,T,Memory, layout_base, Distribution> & operator=(const CartDecomposition & cart)
980
	{
incardon's avatar
incardon committed
981 982
		static_cast<ie_loc_ghost<dim,T,layout_base,Memory>*>(this)->operator=(static_cast<ie_loc_ghost<dim,T,layout_base,Memory>>(cart));
		static_cast<nn_prcs<dim,T,layout_base,Memory>*>(this)->operator=(static_cast<nn_prcs<dim,T,layout_base,Memory>>(cart));
incardon's avatar
incardon committed
983
		static_cast<ie_ghost<dim,T,Memory,layout_base>*>(this)->operator=(static_cast<ie_ghost<dim,T,Memory,layout_base>>(cart));
984 985 986 987 988

		sub_domains = cart.sub_domains;
		box_nn_processor = cart.box_nn_processor;
		fine_s = cart.fine_s;
		gr = cart.gr;
incardon's avatar
incardon committed
989 990 991
		gr_dist = cart.gr_dist;
		dist = cart.dist;
		commCostSet = cart.commCostSet;
992 993
		cd = cart.cd;
		domain = cart.domain;
incardon's avatar
incardon committed
994
		sub_domains_global = cart.sub_domains_global;
incardon's avatar
incardon committed
995 996

		for (size_t i = 0 ; i < dim ; i++)
incardon's avatar
incardon committed
997 998 999 1000
		{
			spacing[i] = cart.spacing[i];
			magn[i] = cart.magn[i];
		};
1001 1002 1003

		ghost = cart.ghost;

incardon's avatar
incardon committed
1004 1005
		bbox = cart.bbox;

1006 1007 1008
		for (size_t i = 0 ; i < dim ; i++)
			bc[i] = cart.bc[i];

1009 1010 1011 1012 1013 1014 1015
		return *this;
	}

	/*! \brief Copy the element, move semantic
	 *
	 * \param cart element to copy
	 *
Pietro Incardona's avatar
Pietro Incardona committed
1016 1017
	 * \return itself
	 *
1018
	 */
incardon's avatar
incardon committed
1019
	CartDecomposition<dim,T,Memory,layout_base, Distribution> & operator=(CartDecomposition && cart)
1020
	{
incardon's avatar
incardon committed
1021 1022
		static_cast<ie_loc_ghost<dim,T,layout_base,Memory>*>(this)->operator=(static_cast<ie_loc_ghost<dim,T,layout_base,Memory>>(cart));
		static_cast<nn_prcs<dim,T,layout_base,Memory>*>(this)->operator=(static_cast<nn_prcs<dim,T,layout_base,Memory>>(cart));
incardon's avatar
incardon committed
1023
		static_cast<ie_ghost<dim,T,Memory,layout_base>*>(this)->operator=(static_cast<ie_ghost<dim,T,Memory,layout_base>>(cart));
1024 1025 1026 1027 1028

		sub_domains.swap(cart.sub_domains);
		box_nn_processor.swap(cart.box_nn_processor);
		fine_s.swap(cart.fine_s);
		gr = cart.gr;
incardon's avatar
incardon committed
1029 1030 1031
		gr_dist = cart.gr_dist;
		dist = cart.dist;
		commCostSet = cart.commCostSet;
1032
		cd = cart.cd;
incardon's avatar
incardon committed
1033 1034 1035
		gr_dist = cart.gr_dist;
		dist = cart.dist;

1036
		domain = cart.domain;
incardon's avatar
incardon committed
1037 1038
		sub_domains_global.swap(cart.sub_domains_global);

incardon's avatar
incardon committed
1039
		for (size_t i = 0 ; i < dim ; i++)
incardon's avatar
incardon committed
1040 1041 1042 1043
		{
			spacing[i] = cart.spacing[i];
			magn[i] = cart.magn[i];
		};
1044 1045 1046

		ghost = cart.ghost;

1047
		bbox = cart.bbox;
incardon's avatar
incardon committed
1048

1049
		for (size_t i = 0 ; i < dim ; i++)
1050 1051 1052
			bc[i] = cart.bc[i];

		return *this;
1053 1054
	}

1055 1056 1057 1058 1059 1060
	/*! \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
1061 1062 1063 1064
	 * \param n_sub number of subdomains per processors
	 *
	 * \return grid dimension (it is one number because on the other dimensions is the same)
	 *