CartDecomposition.hpp 12.7 KB
Newer Older
incardon's avatar
incardon committed
1
2
3
4
5
6
7
8
9
10
11
12
/*
 * CartDecomposition.hpp
 *
 *  Created on: Aug 15, 2014
 *      Author: Pietro Incardona
 */

#ifndef CARTDECOMPOSITION_HPP
#define CARTDECOMPOSITION_HPP

#include "config.h"
#include "Decomposition.hpp"
incardon's avatar
incardon committed
13
#include "Vector/map_vector.hpp"
incardon's avatar
incardon committed
14
15
16
17
18
19
20
#include <vector>
#include "global_const.hpp"
#include <initializer_list>
#include "SubdomainGraphNodes.hpp"
#include "metis_util.hpp"
#include "dec_optimizer.hpp"
#include "Space/Shape/Box.hpp"
incardon's avatar
incardon committed
21
#include "Space/Shape/Point.hpp"
incardon's avatar
incardon committed
22
#include "NN/CellList/CellDecomposer.hpp"
incardon's avatar
incardon committed
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42

/**
 * \brief This class decompose a space into subspaces
 *
 * This class decompose a space into regular hyper-cube subspaces, and give the possibilities to
 * select one subspace
 *
 * \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 layout to use
 * \tparam Memory Memory factory used to allocate memory
 * \tparam Domain Structure that contain the information of your physical domain
 * \tparam data type of structure that store the sub-domain decomposition can be an openfpm structure like
 *        vector, ...
 *
 * \note if PARALLEL_DECOMPOSITION macro is defined a parallel decomposition algorithm is used, basically
 *       each processor does not recompute the same decomposition
 *
 */

incardon's avatar
incardon committed
43
template<unsigned int dim, typename T, template<typename> class device_l=openfpm::device_cpu, typename Memory=HeapMemory, template<unsigned int, typename> class Domain=Box, template<typename, typename, typename, typename, unsigned int> class data_s = openfpm::vector>
incardon's avatar
incardon committed
44
45
46
class CartDecomposition
{
public:
incardon's avatar
incardon committed
47

incardon's avatar
incardon committed
48
49
50
51
52
53
54
55
	//! Type of the domain we are going to decompose
	typedef T domain_type;

	//! It simplify to access the SpaceBox element
	typedef SpaceBox<dim,T> Box;

private:

incardon's avatar
incardon committed
56
	//! This is the key type to access  data_s, for example in the case of vector
incardon's avatar
incardon committed
57
	//! acc_key is size_t
incardon's avatar
incardon committed
58
	typedef typename data_s<SpaceBox<dim,T>,device_l<SpaceBox<dim,T>>,Memory,openfpm::vector_grow_policy_default,openfpm::vect_isel<SpaceBox<dim,T>>::value >::access_key acc_key;
incardon's avatar
incardon committed
59
60
61
62
63
64
65
66
67

	//! Subspace selected
	//! access_key in case of grid is just the set of the index to access the grid
	std::vector<acc_key> id_sub;

	//! the margin of the sub-domain selected
	SpaceBox<dim,T> sub_domain;

	//! the set of all local sub-domain as vector
incardon's avatar
incardon committed
68
	data_s<SpaceBox<dim,T>,device_l<SpaceBox<dim,T>>,Memory,openfpm::vector_grow_policy_default, openfpm::vect_isel<SpaceBox<dim,T>>::value > sub_domains;
incardon's avatar
incardon committed
69

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

incardon's avatar
incardon committed
74
75
	//! Structure that store the cartesian grid information
	grid_sm<dim,void> gr;
incardon's avatar
incardon committed
76

incardon's avatar
incardon committed
77
78
79
	//! Structure that decompose your structure into cell without creating them
	//! useful to convert positions to CellId or sub-domain id in this case
	CellDecomposer_sm<dim,T> cd;
incardon's avatar
incardon committed
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106

	//! rectangular domain to decompose
	Domain<dim,T> domain;

	//! Box Spacing
	T spacing[dim];

	//! Runtime virtual cluster machine
	Vcluster & v_cl;

	/*! \brief Create internally the decomposition
	 *
     * \param v_cl Virtual cluster, used internally to handle or pipeline communication
	 *
	 */
	void CreateDecomposition(Vcluster & v_cl)
	{
		// Calculate the total number of box and and the spacing
		// on each direction

		// Get the box containing the domain
		SpaceBox<dim,T> bs = domain.getBox();

		for (unsigned int i = 0; i < dim ; i++)
		{
			// Calculate the spacing

incardon's avatar
incardon committed
107
			spacing[i] = (bs.getHigh(i) - bs.getLow(i)) / gr.size(i);
incardon's avatar
incardon committed
108
109
110
111
112
		}

		// Here we use METIS

		// Create a cartesian grid graph
incardon's avatar
incardon committed
113
		CartesianGraphFactory<dim,Graph_CSR<nm_part_v,nm_part_e>> g_factory_part;
incardon's avatar
incardon committed
114
115
116

		// Processor graph

incardon's avatar
incardon committed
117
		Graph_CSR<nm_part_v,nm_part_e> gp = g_factory_part.template construct<NO_EDGE,T,dim-1>(gr.getSize(),domain);
incardon's avatar
incardon committed
118
119
120
121
122
123
124
125

		// Get the number of processing units
		size_t Np = v_cl.getProcessingUnits();

		// Get the processor id
		long int p_id = v_cl.getProcessUnitID();

		// Convert the graph to metis
incardon's avatar
incardon committed
126
		Metis<Graph_CSR<nm_part_v,nm_part_e>> met(gp,Np);
incardon's avatar
incardon committed
127
128
129

		// decompose

incardon's avatar
incardon committed
130
		met.decompose<nm_part_v::id>();
incardon's avatar
incardon committed
131

incardon's avatar
Add ORB    
incardon committed
132
133
		// fill the structure that store the processor id for each sub-domain

incardon's avatar
incardon committed
134
		fine_s.resize(gr.size());
incardon's avatar
Add ORB    
incardon committed
135

incardon's avatar
incardon committed
136
137
138
		// Optimize the decomposition creating bigger spaces
		// And reducing Ghost over-stress

incardon's avatar
incardon committed
139
		dec_optimizer<dim,Graph_CSR<nm_part_v,nm_part_e>> d_o(gp,gr.getSize());
incardon's avatar
incardon committed
140
141
142
143

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

incardon's avatar
incardon committed
144
		// optimize the decomposition
incardon's avatar
incardon committed
145
		d_o.template optimize<nm_part_v::sub_id,nm_part_v::id>(gp,p_id,loc_box);
incardon's avatar
incardon committed
146
147
148
149
150
151

		// convert into sub-domain
		for (size_t s = 0 ; s < loc_box.size() ; s++)
		{
			SpaceBox<dim,T> sub_d(loc_box.get(s));

incardon's avatar
incardon committed
152
153
			// re-scale with spacing
			sub_d.spacing(spacing);
incardon's avatar
incardon committed
154
155
156
157

			// add the sub-domain
			sub_domains.add(sub_d);
		}
incardon's avatar
incardon committed
158
159
160
161
162
163
164
165
166
167
168
169
170

		// fill fine_s structure
		auto it = gp.getVertexIterator();

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

			// fill with the fine decomposition
			fine_s.get(key) = gp.template vertex_p<nm_part_v::id>(key);

			++it;
		}
incardon's avatar
incardon committed
171
172
173
174
175
176
177
178
179
180
181
182
	}

	/*! \brief Create the subspaces that decompose your domain
	 *
	 * Create the subspaces that decompose your domain
	 *
	 */

	void CreateSubspaces()
	{
		// Create a grid where each point is a space

183
		grid_sm<dim,void> g(div);
incardon's avatar
incardon committed
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225

		// 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

			SpaceBox<dim,T> tmp;

			//! fill with the Margin of the box

			for (int i = 0 ; i < dim ; i++)
			{
				tmp.setHigh(i,(key.get(i)+1)*spacing[i]);
				tmp.setLow(i,key.get(i)*spacing[i]);
			}

			//! add the space box

			sub_domains.add(tmp);

			// add the iterator

			++gk_it;
		}
	}

public:

	/*! \brief Cartesian decomposition copy constructor
	 *
     * \param v_cl Virtual cluster, used internally to handle or pipeline communication
	 *
	 */
	CartDecomposition(CartDecomposition<dim,T,device_l,Memory,Domain,data_s> && cd)
incardon's avatar
incardon committed
226
	:sub_domain(cd.sub_domain),gr(cd.gr),cd(cd.cd),domain(cd.domain),v_cl(cd.v_cl)
incardon's avatar
incardon committed
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
	{
		//! Subspace selected
		//! access_key in case of grid is just the set of the index to access the grid
		id_sub.swap(cd.id_sub);

		//! the set of all local sub-domain as vector
		sub_domains.swap(cd.sub_domains);

		for (int i = 0 ; i < dim ; i++)
		{
			//! Box Spacing
			this->spacing[i] = spacing[i];
		}
	}

	/*! \brief Cartesian decomposition constructor
	 *
     * \param v_cl Virtual cluster, used internally to handle or pipeline communication
	 *
	 */
	CartDecomposition(Vcluster & v_cl)
incardon's avatar
incardon committed
248
	:id_sub(0),v_cl(v_cl)
incardon's avatar
incardon committed
249
250
251
252
253
254
255
256
257
258
	{}

	/*! \brief Cartesian decomposition constructor, it divide the space in boxes
	 *
	 * \param dec is a vector that store how to divide on each dimension
	 * \param domain is the domain to divide
	 * \param v_cl are information of the cluster runtime machine
	 *
	 */
	CartDecomposition(std::vector<size_t> dec, Domain<dim,T> domain, Vcluster & v_cl)
incardon's avatar
incardon committed
259
	:id_sub(0),gr(dec),cd(domain,dec),domain(domain),v_cl(v_cl)
incardon's avatar
incardon committed
260
261
262
263
264
265
266
267
268
269
	{
		// Create the decomposition

		CreateDecomposition(v_cl);
	}

	//! Cartesian decomposition destructor
	~CartDecomposition()
	{}

incardon's avatar
incardon committed
270
271
272
273
274
275
	/*! \brief processorID return in which processor the particle should go
	 *
	 * \return processorID
	 *
	 */

incardon's avatar
incardon committed
276
	template<typename Mem> size_t inline processorID(encapc<1, Point<dim,T>, Mem> p)
incardon's avatar
incardon committed
277
	{
incardon's avatar
incardon committed
278
		return fine_s.get(cd.getCell(p));
incardon's avatar
incardon committed
279
280
281
282
283
284
285
286
287
288
	}

	/*! \brief processorID return in which processor the particle should go
	 *
	 * \return processorID
	 *
	 */

	size_t inline processorID(T (&p)[dim])
	{
incardon's avatar
incardon committed
289
		return fine_s.get(cd.getCell(p));
incardon's avatar
incardon committed
290
291
	}

incardon's avatar
incardon committed
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
	/*! \brief Set the parameter of the decomposition
	 *
     * \param div_ std::vector storing into how many domain to decompose on each dimension
     * \param domain_ domain to decompose
	 *
	 */
	void setParameters(std::vector<size_t> div_, Domain<dim,T> domain_)
	{
		// Set the decomposition parameters

		div = div_;
		domain = domain_;

		//! Create the decomposition

		CreateDecomposition(v_cl);
	}

	/*! \brief Set the parameter of the decomposition
	 *
     * \param div_ std::vector storing into how many domain to decompose on each dimension
     * \param domain_ domain to decompose
	 *
	 */
incardon's avatar
incardon committed
316
	void setParameters(const size_t (& div_)[dim], Domain<dim,T> domain_)
incardon's avatar
incardon committed
317
318
319
	{
		// Set the decomposition parameters

incardon's avatar
incardon committed
320
		gr.setDimensions(div_);
incardon's avatar
incardon committed
321
		domain = domain_;
incardon's avatar
incardon committed
322
		cd.setDimensions(domain,div_);
incardon's avatar
incardon committed
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427

		//! Create the decomposition

		CreateDecomposition(v_cl);
	}

	/*! \brief Get the number of local local hyper-cubes or sub-domains
	 *
	 * \return the number of sub-domains
	 *
	 */
	size_t getNLocalHyperCube()
	{
		return sub_domains.size();
	}

	/*! The the bulk part of the data set, or the data that
	 * does not depend from the ghosts layers
	 *
	 * \return the bulk of your data
	 *
	 */
	T getBulk()
	{

	}

	/*! \brief This function divide the data set into bulk, border, external and internal part
	 *
	 * \tparam dim dimensionality of the structure storing your data
	 *         (example if they are in 3D grid, has to be 3)
	 * \tparam T type of object we are dividing
	 * \tparam device type of layout selected
	 * \param data 1-dimensional grid of point
	 * \param nb define the neighborhood of all the points
	 * \return a structure with the set of objects divided
	 *
	 */

//	dataDiv<T> CartDecomposition<dim,T,layout>::divide(layout::grid<1,Point<dim,T>> & data, neighborhood & nb);

	/*! The the internal part of the data set, or the data that
	 * are inside the local space
	 *
	 * \return the internal part of your data
	 *
	 */
	T getInternal()
	{

	}

	/*! Get the internal part of the dataset, or the data that
	 * depend from the ghost layers
	 *
	 * \return the ghost part of your data
	 *
	 */

	T getBorder()
	{

	}

	/*! Get the external part of the dataset, or the data that
	 * are outside localSpace including ghost
	 *
	 * \return the external part of your data
	 *
	 */
	T getExternal()
	{

	}

	/*! \brief Get the number of one set of hyper-cube enclosing one particular
	 *         subspace, the hyper-cube enclose your space, even if one box is enough
	 *         can be more that one to increase occupancy
	 *
     * In case of Cartesian decomposition it just return 1, each subspace
	 * has one hyper-cube, and occupancy 1
	 *
	 * \param id of the subspace
	 * \return the number of hyper-cube enclosing your space
	 *
	 */
	size_t getNHyperCube(size_t id)
	{
		return 1;
	}

	/*! \brief Get the hyper-cube margins id_c has to be 0
	 *
	 * Get the hyper-cube margins id_c has to be 0, each subspace
	 * has one hyper-cube
	 *
	 * \param id of the subspace
	 * \param id_c
	 * \return The specified hyper-cube space
	 *
	 */
	SpaceBox<dim,T> & getHyperCubeMargins(size_t id, size_t id_c)
	{
#ifdef DEBUG
		// Check if this subspace exist
incardon's avatar
incardon committed
428
		if (id >= gr.size())
incardon's avatar
incardon committed
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
		{
			std::cerr << "Error CartDecomposition: id > N_tot";
		}
		else if (id_c > 0)
		{
			// Each subspace is an hyper-cube so return error if id_c > 0
			std::cerr << "Error CartDecomposition: id_c > 0";
		}
#endif

		return sub_domains.get<Object>(id);
	}

	/*! \brief Get the total number of Hyper-cube
	 *
	 * Get the total number of Hyper-cube
	 *
	 * \return The total number of hyper-cube
	 *
	 */

	size_t getNHyperCube()
	{
incardon's avatar
incardon committed
452
		return gr.size();
incardon's avatar
incardon committed
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
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
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
	}

	/*! \brief produce an hyper-cube approximation of the space decomposition
	 *
	 */

	void hyperCube()
	{
	}

	/*! \brief Select the local space
	 *
	 * Select the local space
	 *
	 * \param sub select the sub-space
	 *
	 */
	void setSpace(size_t sub)
	{
		id_sub.push_back(sub);
	}


	/*! \brief Get the local grids
	 *
	 * Get the local grids
	 *
	 * \return the local grids
	 *
	 */

	auto getLocalHyperCubes() -> decltype(sub_domains) &
	{
		return sub_domains;
	}

	/*! \brief Get the local hyper-cubes
	 *
	 * Get the local hyper-cubes
	 *
	 * \param lc is the id of the space
	 * \return the local hyper-cube
	 *
	 */

	SpaceBox<dim,T> getLocalHyperCube(size_t lc)
	{
		// Create a space box
		SpaceBox<dim,T> sp;

		// fill the space box

		for (size_t k = 0 ; k < dim ; k++)
		{
			// create the SpaceBox Low and High
			sp.setLow(k,sub_domains.template get<Box::p1>(lc)[k]);
			sp.setHigh(k,sub_domains.template get<Box::p2>(lc)[k]);
		}

		return sp;
	}

	/*! \brief Return the structure that store the physical domain
	 *
	 * Return the structure that store the physical domain
	 *
	 * \return The physical domain
	 *
	 */

	Domain<dim,T> & getDomain()
	{
		return domain;
	}

incardon's avatar
incardon committed
528
529
530
	/*! \brief Check if the particle is local
	 *
	 * \param p object position
incardon's avatar
incardon committed
531
	 *
incardon's avatar
incardon committed
532
	 * \return true if it is local
incardon's avatar
incardon committed
533
534
	 *
	 */
incardon's avatar
incardon committed
535
	template<typename Mem> bool isLocal(encapc<1, Point<dim,T>, Mem> p)
incardon's avatar
incardon committed
536
	{
incardon's avatar
incardon committed
537
538
		return processorID<Mem>() == v_cl.getProcessUnitID();
	}
incardon's avatar
incardon committed
539

incardon's avatar
incardon committed
540
	/*! \brief Check if the particle is local
incardon's avatar
incardon committed
541
	 *
incardon's avatar
incardon committed
542
	 * \param p object position
incardon's avatar
incardon committed
543
	 *
incardon's avatar
incardon committed
544
	 * \return true if it is local
incardon's avatar
incardon committed
545
546
	 *
	 */
incardon's avatar
incardon committed
547
	bool isLocal(T (&pos)[dim])
incardon's avatar
incardon committed
548
	{
incardon's avatar
incardon committed
549
550
		return processorID(pos) == v_cl.getProcessUnitID();
	}
incardon's avatar
incardon committed
551
552
553
554
};


#endif