DistCartesianGraphFactory.hpp 17.6 KB
Newer Older
tonynsyde's avatar
tonynsyde committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
 * DistCartesianGraphFactory.hpp
 *
 *  Created on: Dec 03, 2015
 *      Author: Antonio Leo
 */

#ifndef DISTCARTESIANGRAPHFACTORYOLD_HPP_
#define DISTCARTESIANGRAPHFACTORYOLD_HPP_

#include "VCluster.hpp"
#include "Vector/map_vector.hpp"
#include "Graph/map_graph.hpp"
#include "Grid/grid_sm.hpp"
#include "Space/Shape/Box.hpp"
#include "Space/Shape/HyperCube.hpp"
#include "parmetis.h"

/*! \brief Operator to fill the property 'prp' with the linearization of indexes
 *
 *  \tparam dim Dimension of the space
 *  \tparam G_v Graph
 *  \tparam prp Property to fill
 */
template<unsigned int dim, typename G_v, int prp_l, int prp_g>
struct dist_fill_id
{
	static inline void fill(G_v & g_v, const grid_key_dx<dim> & gk, const grid_sm<dim, void> & gs)
	{
		// Local id
		g_v.template get<prp_l>() = gs.LinId(gk);

		// Global id
		g_v.template get<prp_g>() = gs.LinId(gk);
	}
};
/*! \brief Operator to fill the property in case there are no properties
 *
 *  \tparam dim Dimension of the space
 *  \tparam G_v Graph
 */
template<unsigned int dim, typename G_v>
struct dist_fill_id<dim, G_v, -1, -1>
{
	static inline void fill(G_v & g_v, const grid_key_dx<dim> & gk, const grid_sm<dim, void> & gs)
	{
	}
};

/*! \brief This class work as a functor
 *
 * For each number in the boost::mpl::vector (for example 3 6) set the properties of the vertex at the
 * specified id (3 6) with pos[d] * spacing[d] with d running from 0 to 1, pos[d] the position id of the vertex
 * spacing the grid spacing
 *
 * Example
 *
 * if we give a grid_key of dimension 2 4x4 the expression "pos[d] * spacing[d]"
 * will assume the value
 *
 * (0.0 0.0) (0.25 0.0) ...... (1.0 0.0)
 * (0.0 0.25)................. (1.0 0.25)
 * ....................................
 * (0.0 1.0).................. (1.0 1.0)
 *
 * and the properties 3 6 will be filled with the numbers 0.0 0.0    .......  1.0 1.0
 * progressively
 *
 * \tparam dim Dimensionality of the cartesian grid
 * \tparam dT type of the domain
 * \tparam G_v vertex type object
 * \tparam v boost::mpl::vector containing all the index to fill
 * \tparam is_stub when is true, produce a trivial operator(),
 *         to use when v is an empty vector to avoid compilation error
 *
 */

template<unsigned int dim, int loc_id, int glob_id, typename dT, typename G_v, typename v, int impl>
class dist_fill_prop
{
	//! Reference to an array containing the spacing
	const dT (&szd)[dim];

	//! grid_key_dx Reference containing the actual position
	grid_key_dx<dim> & gk;

	//! Vertex object to fill
	G_v & g_v;

	//! grid info
	const grid_sm<dim, void> & gs;

public:

	//! Fill the object from where to take the properties
	dist_fill_prop(G_v & g_v, const dT (&szd)[dim], grid_key_dx<dim> & gk, const grid_sm<dim, void> & gs) :
			szd(szd), gk(gk), g_v(g_v), gs(gs)
	{
	}

	//! It call the function for each property we want to copy
	template<typename T>
	void operator()(T& t) const
	{
		typedef typename boost::fusion::result_of::at<v, boost::mpl::int_<T::value>>::type t_val;

		g_v.template get<t_val::value>() = gk.get(T::value) * szd[T::value];
		dist_fill_id<dim, G_v, loc_id, glob_id>::fill(g_v, gk, gs);
	}
};

/*! \brief This class work as a functor
 *
 * For each number in the boost::mpl::vector (for example 3 6) set the properties of the vertex at the
 * specified id (3 6) with pos[d] * spacing[d] with d running from 0 to 1, pos[d] the position id of the vertex
 * spacing the grid spacing
 *
 * Example
 *
 * if we give a grid_key of dimension 2 4x4 the expression "pos[d] * spacing[d]"
 * will assume the value
 *
 * (0.0 0.0) (0.25 0.0) ...... (1.0 0.0)
 * (0.0 0.25)................. (1.0 0.25)
 * ....................................
 * (0.0 1.0).................. (1.0 1.0)
 *
 * and the properties 3 6 will be filled with the numbers 0.0 0.0    .......  1.0 1.0
 * progressively
 *
 * \tparam dim Dimensionality of the cartesian grid
 * \tparam dT type of the domain
 * \tparam G_v vertex type object
 * \tparam v boost::mpl::vector containing all the index to fill
 *
 */

template<unsigned int dim, int loc_id, int glob_id, typename dT, typename G_v, typename v>
class dist_fill_prop<dim, loc_id, glob_id, dT, G_v, v, 0>
{

public:

	//! Fill the object from where to take the properties
	dist_fill_prop(G_v & g_v, const dT (&szd)[dim], grid_key_dx<dim> & gk, const grid_sm<dim, void> & gs)
	{
	}

	//! It call the function for each property we want to copy
	template<typename T>
	void operator()(T& t) const
	{
	}
};

/*! \brief This class work as a functor
 *
 * For each number in the boost::mpl::vector (for example 3 6) set the properties of the vertex at the
 * specified id (3 6) with pos[d] * spacing[d] with d running from 0 to 1, pos[d] the position id of the vertex
 * spacing the grid spacing
 *
 * Example
 *
 * if we give a grid_key of dimension 2 4x4 the expression "pos[d] * spacing[d]"
 * will assume the value
 *
 * (0.0 0.0) (0.25 0.0) ...... (1.0 0.0)
 * (0.0 0.25)................. (1.0 0.25)
 * ....................................
 * (0.0 1.0).................. (1.0 1.0)
 *
 * and the properties 3 6 will be filled with the numbers 0.0 0.0    .......  1.0 1.0
 * progressively
 *
 * \tparam dim Dimensionality of the cartesian grid
 * \tparam dT type of the domain
 * \tparam G_v vertex type object
 * \tparam v boost::mpl::vector containing all the index to fill
 *
 */

template<unsigned int dim, int loc_id, int glob_id, typename dT, typename G_v, typename v>
class dist_fill_prop<dim, loc_id, glob_id, dT, G_v, v, 2>
{

	//! Reference to an array containing the spacing
	const dT (&szd)[dim];

	//! grid_key_dx Reference containing the actual position
	grid_key_dx<dim> & gk;

	//! Vertex object to fill
	G_v & g_v;

	//! grid info
	const grid_sm<dim, void> & gs;

public:

	//! Fill the object from where to take the properties
	dist_fill_prop(G_v & g_v, const dT (&szd)[dim], grid_key_dx<dim> & gk, const grid_sm<dim, void> & gs) :
			szd(szd), gk(gk), g_v(g_v), gs(gs)
	{
	}

	//! It call the function for each property we want to copy
	template<typename T>
	void operator()(T& t) const
	{
		typedef typename boost::fusion::result_of::at<v, boost::mpl::int_<0>>::type t_val;

212
		g_v.template get<t_val::value>()[T::value] = gk.get(T::value) * static_cast<float>(szd[T::value]);
tonynsyde's avatar
tonynsyde committed
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
		dist_fill_id<dim, G_v, loc_id, glob_id>::fill(g_v, gk, gs);
	}
};

/*! \brief Operator for vector and scalar property
 *
 * \tparam i Size of the property
 * \tparam p Type of the property
 * \tparam Graph Graph
 * \tparam pos Array of properties
 */
template<int i, typename p, typename Graph, int ... pos>
struct dist_fill_prop_by_type
{

	typedef typename boost::mpl::at<p, boost::mpl::int_<0>>::type v_element;
	typedef typename boost::mpl::at<typename Graph::V_type::type, v_element>::type pos_prop_type;

	enum
	{
		value = ((sizeof...(pos) != 0) * (std::is_array<pos_prop_type>::value + 1))
	};

};

/*! \brief Operator for vector and scalar property in the case there are no properties
 *
 * \tparam i Size of the property
 * \tparam p Type of the property
 * \tparam Graph Graph
 * \tparam pos Array of properties
 */
template<typename p, typename Graph, int ... pos>
struct dist_fill_prop_by_type<0, p, Graph, pos...>
{
	enum
	{
		value = 0
	};

};

/*! \brief Graph constructor function specialization
 *
 * On C++ partial function specialization is not allowed, so we need a class to do it
 *
 * \see CartesianGraphFactory method construct
 *
 */

template<unsigned int dim, int loc_id, int glob_id, int e_sgid, int e_dgid, typename Graph, int se, typename T, unsigned int dim_c, int ... pos>
class DistGraph_constructor_impl
{
public:
	//! Construct Cartesian graph
	static Graph construct(const size_t (&sz)[dim], Box<dim, T> dom, openfpm::vector<idx_t> & vtxdist)
	{
		Vcluster &v_cl = *global_v_cluster;

		// Calculate the size of the hyper-cubes on each dimension
		T szd[dim];

		for (size_t i = 0; i < dim; i++)
		{
			szd[i] = (dom.getHigh(i) - dom.getLow(i)) / sz[i];
		}

		//! Construct an hyper-cube of dimension dim

		HyperCube<dim> hc;

		// Construct a grid info

		grid_sm<dim, void> g(sz);

		//! Get the processor id
		size_t p_id = v_cl.getProcessUnitID();

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

		//! Division of vertices in Np graphs
		//! Put (div+1) vertices in mod graphs
		//! Put div vertices in the rest of the graphs
		size_t mod_v = g.size() % Np;
		size_t div_v = g.size() / Np;

		for (int i = 0; i <= Np; i++)
		{
			if (i < mod_v)
				vtxdist.get(i) = (div_v + 1) * (i);
			else
				vtxdist.get(i) = (div_v) * (i) + mod_v;
		}

		//! Get size of this processor graph
		size_t gp_size = vtxdist.get(p_id + 1) - vtxdist.get(p_id);

		//! Graph to construct

		Graph gp(gp_size);

		/******************
		 *
		 * Create the edges and fill spatial
		 * information properties
		 *
		 ******************/

		//! Construct a key iterator
		grid_key_dx_iterator<dim> k_it(g);

		//! Local iterator of the graph
		size_t local_it = 0;

		//! Iterate through all the elements

		while (k_it.isNext())
		{
			size_t v_id = g.LinId(k_it.get());

			if (v_id < vtxdist.get(p_id + 1) && v_id >= vtxdist.get(p_id))
			{

				grid_key_dx<dim> key = k_it.get();

				// Vertex object

				auto obj = gp.vertex(local_it);

				typedef typename to_boost_vmpl<pos...>::type p;

				// vertex spatial properties functor

				dist_fill_prop<dim, loc_id, glob_id, T, decltype(gp.vertex(local_it)), typename to_boost_vmpl<pos...>::type, dist_fill_prop_by_type<sizeof...(pos), p, Graph, pos...>::value> flp(obj, szd, key, g);

				// fill properties

				boost::mpl::for_each<boost::mpl::range_c<int, 0, sizeof...(pos)> >(flp);

				// set map global to local in the graph, needed because vertex is already created without addVertex method

				gp.setGlobalMap(v_id, local_it, local_it);

				// Get the combinations of dimension d

				for (size_t d = dim - 1; d >= dim_c; d--)
				{
					// create the edges for that dimension

					std::vector<comb<dim>> c = hc.getCombinations_R(d);

					// for each combination calculate a safe linearization and create an edge

					for (size_t j = 0; j < c.size(); j++)
					{
						// Calculate the element size

						T ele_sz = 0;

						// for each dimension multiply and reduce

						for (size_t s = 0; s < dim; s++)
						{
							ele_sz += szd[s] * abs(c[j][s]);
						}

						// Calculate the end point vertex id
						// Calculate the start point id

						size_t start_v = local_it;
						size_t end_v = g.template LinId<CheckExistence>(key, c[j].getComb());

						// check if the end_v is valid globally
						if (end_v < g.size())
						{
							// Add an edge and set the the edge property to the size of the face (communication weight)
							gp.template addEdge<NoCheck>(start_v, end_v).template get<se>() = ele_sz;
						}
					}
				}
				++local_it;
			}
			++k_it;
		}

		return gp;
	}
};

/*! \brief Graph constructor function specialization
 *
 * On C++ partial function specialization is not allowed, so we need a class to do it
 * This specialization handle the case when we have NO_EDGE option active
 *
 * \see CartesianGraphFactory method construct
 *
 */

template<unsigned int dim, int loc_id, int glob_id, int e_sgid, int e_dgid, typename Graph, typename T, unsigned int dim_c, int ... pos>
class DistGraph_constructor_impl<dim, loc_id, glob_id, e_sgid, e_dgid, Graph, NO_EDGE, T, dim_c, pos...>
{
public:
	//! Construct Cartesian graph
	static Graph construct(const size_t (&sz)[dim], Box<dim, T> dom, openfpm::vector<idx_t> & vtxdist)
	{
		Vcluster &v_cl = *global_v_cluster;

		// Calculate the size of the hyper-cubes on each dimension

		T szd[dim];

		for (size_t i = 0; i < dim; i++)
		{
			szd[i] = (dom.getHigh(i) - dom.getLow(i)) / sz[i];
		}

		//! Construct an hyper-cube of dimension dim

		HyperCube<dim> hc;

		// Construct a grid info

		grid_sm<dim, void> g(sz);

		//! Get the processor id
		size_t p_id = v_cl.getProcessUnitID();

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

		//! Division of vertices in Np graphs
		//! Put (div+1) vertices in mod graphs
		//! Put div vertices in the rest of the graphs
		size_t mod_v = g.size() % Np;
		size_t div_v = g.size() / Np;

450
		for (size_t i = 0; i <= Np; i++)
tonynsyde's avatar
tonynsyde committed
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
		{
			if (i < mod_v)
				vtxdist.get(i) = (div_v + 1) * (i);
			else
				vtxdist.get(i) = (div_v) * (i) + mod_v;
		}

		//! Get size of this processor graph
		size_t gp_size = vtxdist.get(p_id + 1) - vtxdist.get(p_id);

		//! Graph to construct
		Graph gp(gp_size);

		/******************
		 *
		 * Create the edges and fill spatial
		 * information properties
		 *
		 ******************/

		//! Construct a key iterator
		grid_key_dx_iterator<dim> k_it(g);

		//! Local iterator of the graph
		size_t local_it = 0;

		//! Iterate through all the elements

		while (k_it.isNext())
		{
			size_t v_id = g.LinId(k_it.get());

483
			if (v_id < (size_t)vtxdist.get(p_id + 1) && v_id >= (size_t)vtxdist.get(p_id))
tonynsyde's avatar
tonynsyde committed
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
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
616
617
618
619
620
621
622
623
624
625
626
627
			{
				grid_key_dx<dim> key = k_it.get();

				// Vertex object
				auto obj = gp.vertex(local_it);

				typedef typename to_boost_vmpl<pos...>::type p;

				// vertex spatial properties functor

				dist_fill_prop<dim, loc_id, glob_id, T, decltype(gp.vertex(local_it)), typename to_boost_vmpl<pos...>::type, dist_fill_prop_by_type<sizeof...(pos), p, Graph, pos...>::value> flp(obj, szd, key, g);

				// fill properties

				boost::mpl::for_each<boost::mpl::range_c<int, 0, sizeof...(pos)> >(flp);

				// set map global to local in the graph, needed because vertex is already created without addVertex method

				gp.setGlobalMap(v_id, local_it, local_it);

				// Get the combinations of dimension d

				for (size_t d = dim - 1; d >= dim_c; d--)
				{
					// create the edges for that dimension

					std::vector<comb<dim>> c = hc.getCombinations_R(d);

					// for each combination calculate a safe linearization and create an edge

					for (size_t j = 0; j < c.size(); j++)
					{
						// Calculate the element size

						T ele_sz = 0;

						// for each dimension multiply and reduce

						for (size_t s = 0; s < dim; s++)
						{
							ele_sz += szd[s] * abs(c[j][s]);
						}

						// Calculate the end point vertex id
						// Calculate the start point id

						size_t start_v = local_it;
						size_t end_v = g.template LinId<CheckExistence>(key, c[j].getComb());

						// check if the end_v is valid globally
						if (end_v < g.size())
						{
							// Add an edge and set the the edge property to the size of the face (communication weight)
							gp.template addEdge<NoCheck, e_sgid, e_dgid>(start_v, end_v, v_id, end_v);
						}
					}
				}
				++local_it;
			}
			++k_it;
		}

		return gp;
	}
};

/*! \brief This class construct a cartesian graph
 *
 * This class construct a cartesian graph
 *
 * \param dim dimensionality of the cartesian grid
 *
 */

template<unsigned int dim, typename Graph>
class DistCartesianGraphFactory
{

public:

	/*!
	 *
	 * \brief Construct a cartesian graph, with V and E edge properties
	 *
	 * Construct a cartesian graph, with V and E edge properties
	 *
	 * Each vertex is a subspace (Hyper-cube) of dimension dim, each vertex is
	 * connected with an edge if two vertex (Hyper-cube) share a element of dimension grater than
	 * dim_c. One property can be used to store the contact size or the d-dimensional
	 * surface in common between two connected hyper-cube.
	 *
	 * \param sz Vector that store the size of the grid on each dimension
	 * \param dom Box enclosing the physical domain
	 *
	 * \tparam se Indicate which properties fill with the contact size. The
	 *           contact size is the point, line , surface, d-dimensional object size
	 *           in contact (in common) between two hyper-cube. NO_EDGE indicate
	 *           no property will store this information
	 * \tparam T type of the domain like (int real complex ... )
	 * \tparam dim_c Connectivity dimension
	 * \tparam pos... (optional)one or more integer indicating the spatial properties
	 *
	 */
	template<int se, typename T, unsigned int dim_c, int ... pos>
	static Graph construct(const size_t (&sz)[dim], Box<dim, T> dom, openfpm::vector<idx_t> & vtxdist)
	{
		return DistGraph_constructor_impl<dim, -1, -1, -1, -1, Graph, se, T, dim_c, pos...>::construct(sz, dom, vtxdist);
	}

	/*!
	 *
	 * \brief Construct a cartesian graph, with V and E edge properties
	 *
	 * Construct a cartesian graph, with V and E edge properties
	 *
	 * Each vertex is a subspace (Hyper-cube) of dimension dim, each vertex is
	 * connected with an edge if two vertex (Hyper-cube) share a element of dimension grater than
	 * dim_c. One property can be used to store the contact size or the d-dimensional
	 * surface in common between two connected hyper-cube.
	 *
	 * \param sz Vector that store the size of the grid on each dimension
	 * \param dom Box enclosing the physical domain
	 *
	 * \tparam se Indicate which properties fill with the contact size. The
	 *           contact size is the point, line , surface, d-dimensional object size
	 *           in contact (in common) between two hyper-cube. NO_EDGE indicate
	 *           no property will store this information
	 * \tparam loc_id property that stores the local vertex id
	 * \tparam glob_id property that stores the local vertex id
	 * \tparam e_sgid property that stores on the edge global id of the source vertex
	 * \tparam e_dgid property that stores on the edge global id of the destination vertex
	 * \tparam T type of the domain like (int real complex ... )
	 * \tparam dim_c Connectivity dimension
	 * \tparam pos... (optional)one or more integer indicating the spatial properties
	 *
	 */
	template<int se, int loc_id, int glob_id, int e_sgid, int e_dgid, typename T, unsigned int dim_c, int ... pos>
	static Graph construct(const size_t (&sz)[dim], Box<dim, T> dom, openfpm::vector<idx_t> & vtxdist)
	{
		return DistGraph_constructor_impl<dim, loc_id, glob_id, e_sgid, e_dgid, Graph, se, T, dim_c, pos...>::construct(sz, dom, vtxdist);
	}
};

#endif /* DISTCARTESIANGRAPHFACTORY_HPP_ */