DistGraphFactory.hpp 15 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
/*
 * DistGraphFactory.hpp
 *
 *  Created on: Dec 03, 2015
 *      Author: Antonio Leo
 */

#ifndef DISTGRAPHFACTORYOLD_HPP_
#define DISTGRAPHFACTORYOLD_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 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
 *
 */

47
template<unsigned int dim, typename dT, typename G_v, typename v, int impl>
tonynsyde's avatar
tonynsyde committed
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
class fill_prop_v
{
	//! 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
	fill_prop_v(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];
	}
};

80

tonynsyde's avatar
tonynsyde committed
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
/*! \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
 *
 */

107 108
template<unsigned int dim, typename dT, typename G_v, typename v>
class fill_prop_v<dim, dT, G_v, v, 0>
tonynsyde's avatar
tonynsyde committed
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
{

public:

	//! Fill the object from where to take the properties
	fill_prop_v(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
 *
 */

151 152
template<unsigned int dim, typename dT, typename G_v, typename v>
class fill_prop_v<dim, dT, G_v, v, 2>
tonynsyde's avatar
tonynsyde committed
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
{

	//! 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
170 171
	fill_prop_v(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)
tonynsyde's avatar
tonynsyde committed
172 173 174 175 176 177 178 179
	{
	}

	//! 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;
180
		typedef typename boost::mpl::at<typename G_v::T_type::type,t_val>::type s_type;
tonynsyde's avatar
tonynsyde committed
181

182 183 184 185 186
		for (size_t i = 0 ; i < std::extent<s_type>::value ; i++)
			g_v.template get<t_val::value>()[i] = 0.0;

		for (size_t i = 0 ; i < dim ; i++)
			g_v.template get<t_val::value>()[i] = gk.get(i) * static_cast<float>(szd[i]);
tonynsyde's avatar
tonynsyde committed
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 226 227 228 229 230 231 232 233 234 235
	}
};

/*! \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 fill_prop_v_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 fill_prop_v_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
 *
 */

236
template<unsigned int dim, typename Graph, int se, typename T, unsigned int dim_c, int ... pos>
tonynsyde's avatar
tonynsyde committed
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
class DistGraph_constr_impl
{
public:
	//! Construct Cartesian graph
	static Graph construct(const size_t (&sz)[dim], Box<dim, T> dom)
	{
		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;

		//! Distribution vector
		openfpm::vector<idx_t> vtxdist(v_cl.getProcessingUnits() + 1);

		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);

		//! Store the decomposition vector inside the distributed graph
		gp.initDistributionVector(vtxdist);

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

325
				fill_prop_v<dim, T, decltype(gp.vertex(local_it)), typename to_boost_vmpl<pos...>::type, fill_prop_v_by_type<sizeof...(pos), p, Graph, pos...>::value> flp(obj, szd, key, g);
tonynsyde's avatar
tonynsyde committed
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

				// 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, v_id);

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

390 391
template<unsigned int dim, typename Graph, typename T, unsigned int dim_c, int ... pos>
class DistGraph_constr_impl<dim, Graph, NO_EDGE, T, dim_c, pos...>
tonynsyde's avatar
tonynsyde committed
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
{
public:
	//! Construct Cartesian graph
	static Graph construct(const size_t (&sz)[dim], Box<dim, T> dom)
	{
		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;

		//! Distribution vector
		openfpm::vector<idx_t> vtxdist(v_cl.getProcessingUnits() + 1);

431
		for (size_t i = 0; i <= Np; i++)
tonynsyde's avatar
tonynsyde committed
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
		{
			if (i < mod_v)
				vtxdist.get(i) = (div_v + 1) * (i);
			else
				vtxdist.get(i) = (div_v) * (i) + mod_v;
		}

		size_t gp_size = vtxdist.get(p_id + 1) - vtxdist.get(p_id);

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

		//! Store the decomposition vector inside the distributed graph
		gp.initDistributionVector(vtxdist);

		/******************
		 *
		 * 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());

466
			if (v_id < (size_t)vtxdist.get(p_id + 1) && v_id >= (size_t)vtxdist.get(p_id))
tonynsyde's avatar
tonynsyde committed
467 468 469 470 471 472 473 474 475 476
			{
				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

477
				fill_prop_v<dim, T, decltype(gp.vertex(local_it)), typename to_boost_vmpl<pos...>::type, fill_prop_v_by_type<sizeof...(pos), p, Graph, pos...>::value> flp(obj, szd, key, g);
tonynsyde's avatar
tonynsyde committed
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

				// 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, v_id);

				// 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)
520
							gp.template addEdge(start_v, end_v, v_id, end_v);
tonynsyde's avatar
tonynsyde committed
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
						}
					}
				}
				++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 DistGraphFactory
{

public:

546
	/*! \brief Construct a cartesian graph, with V and E edge properties
tonynsyde's avatar
tonynsyde committed
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
	 *
	 * 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)
	{
568
		return DistGraph_constr_impl<dim, Graph, se, T, dim_c, pos...>::construct(sz, dom);
tonynsyde's avatar
tonynsyde committed
569 570 571
	}
};

572
#endif /* DISTGRAPHFACTORY_HPP_ */