MetisDistribution.hpp 14.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 * MetisDistribution.hpp
 *
 *  Created on: Nov 19, 2015
 *      Author: Antonio Leo
 */

#ifndef SRC_DECOMPOSITION_METISDISTRIBUTION_HPP_
#define SRC_DECOMPOSITION_METISDISTRIBUTION_HPP_

#include "SubdomainGraphNodes.hpp"
#include "metis_util.hpp"

incardon's avatar
incardon committed
14
#define METIS_DISTRIBUTION_ERROR_OBJECT std::runtime_error("Metis runtime error");
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

/*! \brief Class that distribute sub-sub-domains across processors using Metis Library
 *
 * Given a graph and setting Computational cost, Communication cost (on the edge) and
 * Migration cost or total Communication costs, it produce the optimal distribution
 *
 * ### Initialize a Cartesian graph and decompose
 * \snippet Distribution_unit_tests.hpp Initialize a Metis Cartesian graph and decompose
 *
 * ### Set Computation Communication and Migration cost
 * \snippet Distribution_unit_tests.hpp Decomposition Metis with weights
 *
 */

template<unsigned int dim, typename T>
class MetisDistribution
{
	//! Vcluster
	Vcluster & v_cl;

	//! Structure that store the cartesian grid information
	grid_sm<dim, void> gr;

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

	//! Global sub-sub-domain graph
	Graph_CSR<nm_v, nm_e> gp;

	//! Flag that indicate if we are doing a test (In general it fix the seed)
	bool testing = false;

incardon's avatar
incardon committed
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
	//! Metis decomposer utility
	Metis<Graph_CSR<nm_v, nm_e>> metis_graph;

	/*! \brief sub-domain list and weight
	 *
	 */
	struct met_sub_w
	{
		//! sub-domain id
		size_t id;

		//! sub-domain weight / assignment (it depend in which context is used)
		size_t w;

		static bool noPointers() {return true;}
	};

	//! unordered map that map global sub-sub-domain to owned_cost_sub id
	std::unordered_map<size_t,size_t> owner_scs;

	//! list owned sub-sub-domains set for computation cost
	openfpm::vector<met_sub_w> owner_cost_sub;

	//! received assignment
	openfpm::vector<met_sub_w> recv_ass;

73 74 75 76 77 78 79 80 81 82 83
	/*! \brief Check that the sub-sub-domain id exist
	 *
	 * \param id sub-sub-domain id
	 *
	 */
	inline void check_overflow(size_t id)
	{
#ifdef SE_CLASS1
		if (id >= gp.getNVertex())
		{
			std::cerr << "Error " << __FILE__ ":" << __LINE__ << " such sub-sub-domain doesn't exist (id = " << id << ", " << "total size = " << gp.getNVertex() << ")\n";
incardon's avatar
incardon committed
84
			ACTION_ON_ERROR(METIS_DISTRIBUTION_ERROR_OBJECT)
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
		}
#endif
	}

	/*! \brief Check that the sub-sub-domain id exist
	 *
	 * \param id sub-sub-domain id
	 *
	 */
	inline void check_overflowe(size_t id, size_t e)
	{
#ifdef SE_CLASS1
		if (e >= gp.getNChilds(id))
		{
			std::cerr << "Error " << __FILE__ ":" << __LINE__ << " for the sub-sub-domain " << id << " such neighborhood doesn't exist (e = " << e << ", " << "total size = " << gp.getNChilds(id) << ")\n";
incardon's avatar
incardon committed
100
			ACTION_ON_ERROR(METIS_DISTRIBUTION_ERROR_OBJECT)
101 102 103 104 105 106 107 108
		}
#endif
	}

public:

	static constexpr unsigned int computation = nm_v::computation;

incardon's avatar
incardon committed
109 110 111 112 113 114 115
	/*! \brief constructor
	 *
	 * \param v_cl vcluster
	 *
	 */
	MetisDistribution(Vcluster & v_cl)
	:v_cl(v_cl),metis_graph(gp)
116 117 118 119 120 121 122 123
	{
#ifdef SE_CLASS2
			check_new(this,8,VECTOR_EVENT,1);
#endif
	}

	/*! \brief Copy constructor
	 *
incardon's avatar
incardon committed
124
	 * \param mt distribution to copy
125 126 127 128 129 130 131 132 133 134 135 136 137 138
	 *
	 */
	MetisDistribution(const MetisDistribution & mt)
	:v_cl(mt.v_cl)
	{
#ifdef SE_CLASS2
			check_valid(mt);
			check_new(this,8,VECTOR_EVENT,1);
#endif
		this->operator=(mt);
	}

	/*! \brief Copy constructor
	 *
incardon's avatar
incardon committed
139
	 * \param mt distribution to copy
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
	 *
	 */
	MetisDistribution(MetisDistribution && mt)
	{
#ifdef SE_CLASS2
			check_valid(mt);
			check_new(this,8,VECTOR_EVENT,1);
#endif
		this->operator=(mt);
	}

	/*! \brief Destructor
	 *
	 *
	 */
	~MetisDistribution()
	{
#ifdef SE_CLASS2
		check_delete(this);
#endif
	}


	/*! \brief create a Cartesian distribution graph
	 *
	 * \param grid grid info (sub-sub somains on each dimension)
	 * \param dom domain (domain where the sub-sub-domains are defined)
	 *
	 */
	void createCartGraph(grid_sm<dim, void> & grid, Box<dim, T> dom)
	{
#ifdef SE_CLASS2
			check_valid(this,8);
#endif
		// NON periodic boundary conditions
		size_t bc[dim];

		for (size_t i = 0 ; i < dim ; i++)
			bc[i] = NON_PERIODIC;

		// Set grid and domain
		gr = grid;
		domain = dom;

		// Create a cartesian grid graph
		CartesianGraphFactory<dim, Graph_CSR<nm_v, nm_e>> g_factory_part;
186
		gp = g_factory_part.template construct<NO_EDGE, nm_v::id, T, dim - 1, 0>(gr.getSize(), domain, bc);
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

		// Init to 0.0 axis z (to fix in graphFactory)
		if (dim < 3)
		{
			for (size_t i = 0; i < gp.getNVertex(); i++)
			{
				gp.vertex(i).template get<nm_v::x>()[2] = 0.0;
			}
		}

		for (size_t i = 0; i < gp.getNVertex(); i++)
			gp.vertex(i).template get<nm_v::global_id>() = i;
	}

	/*! \brief Get the current graph (main)
	 *
	 * \return the current sub-sub domain Graph
	 *
	 */
	Graph_CSR<nm_v, nm_e> & getGraph()
	{
#ifdef SE_CLASS2
			check_valid(this,8);
#endif
		return gp;
	}

	/*! \brief Distribute the sub-sub-domains
	 *
	 */
	void decompose()
	{
#ifdef SE_CLASS2
			check_valid(this,8);
#endif

incardon's avatar
incardon committed
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
		// Gather the sub-domain weight in one processor
		recv_ass.clear();
		v_cl.SGather(owner_cost_sub,recv_ass,0);

		if (v_cl.getProcessUnitID() == 0)
		{
			if (recv_ass.size() != 0)
			{
				// we fill the assignment
				for (size_t i = 0 ; i < recv_ass.size() ; i++)
					gp.template vertex_p<nm_v::computation>(recv_ass.get(i).id) = recv_ass.get(i).w;

				metis_graph.initMetisGraph(v_cl.getProcessingUnits(),true);
			}
			else
				metis_graph.initMetisGraph(v_cl.getProcessingUnits(),false);
			metis_graph.onTest(testing);

			// decompose
			metis_graph.decompose<nm_v::proc_id>();

			if (recv_ass.size() != 0)
			{
				// we fill the assignment
				for (size_t i = 0 ; i < recv_ass.size() ; i++)
					recv_ass.get(i).w = gp.template vertex_p<nm_v::proc_id>(recv_ass.get(i).id);
			}
			else
			{
				recv_ass.resize(gp.getNVertex());

				// we fill the assignment
				for (size_t i = 0 ; i < gp.getNVertex() ; i++)
				{
					recv_ass.get(i).id = i;
					recv_ass.get(i).w = gp.template vertex_p<nm_v::proc_id>(i);
				}
			}
		}
incardon's avatar
incardon committed
262 263 264 265
		else
		{
			metis_graph.inc_dec();
		}
incardon's avatar
incardon committed
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

		recv_ass.resize(gp.getNVertex());

		// broad cast the result
		v_cl.Bcast(recv_ass,0);
		v_cl.execute();
		owner_scs.clear();
		owner_cost_sub.clear();

		size_t j = 0;

		// Fill the metis graph
		for (size_t i = 0 ; i < recv_ass.size() ; i++)
		{
			gp.template vertex_p<nm_v::proc_id>(recv_ass.get(i).id) = recv_ass.get(i).w;

			if (recv_ass.get(i).w == v_cl.getProcessUnitID())
			{
				owner_scs[recv_ass.get(i).id] = j;
				j++;
				owner_cost_sub.add();
				owner_cost_sub.last().id = recv_ass.get(i).id;
				owner_cost_sub.last().w = 1;
			}
		}
291 292
	}

incardon's avatar
incardon committed
293
	/*! \brief Refine current decomposition
294
	 *
incardon's avatar
incardon committed
295
	 * In metis case it just re-decompose
296 297 298 299 300 301 302
	 *
	 */
	void refine()
	{
#ifdef SE_CLASS2
			check_valid(this,8);
#endif
incardon's avatar
incardon committed
303 304

		decompose();
305 306
	}

incardon's avatar
incardon committed
307 308 309 310 311 312 313 314 315 316 317 318
	/*! \brief Redecompose current decomposition
	 *
	 */
	void redecompose()
	{
#ifdef SE_CLASS2
			check_valid(this,8);
#endif
		decompose();
	}


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
	/*! \brief Function that return the position (point P1) of the sub-sub domain box in the space
	 *
	 * \param id vertex id
	 * \param pos vector that contain x, y, z
	 *
	 */
	void getSSDomainPos(size_t id, T (&pos)[dim])
	{
#ifdef SE_CLASS2
			check_valid(this,8);
#endif
		check_overflow(id);

		// Copy the geometrical informations inside the pos vector
		pos[0] = gp.vertex(id).template get<nm_v::x>()[0];
		pos[1] = gp.vertex(id).template get<nm_v::x>()[1];
		if (dim == 3)
			pos[2] = gp.vertex(id).template get<nm_v::x>()[2];
	}

	/*! \brief function that get the computational cost of the sub-sub-domain
	 *
	 * \param id sub-sub-domain
	 *
	 * \return the comutational cost
	 *
	 */
	size_t getComputationalCost(size_t id)
	{
#ifdef SE_CLASS2
			check_valid(this,8);
#endif
		check_overflow(id);
		return gp.vertex(id).template get<nm_v::computation>();
	}


	/*! \brief Set computation cost on a sub-sub domain
	 *
	 * \param id sub-sub domain id
	 * \param cost
	 *
	 */
	void setComputationCost(size_t id, size_t cost)
	{
#ifdef SE_CLASS2
			check_valid(this,8);
#endif
incardon's avatar
incardon committed
367
#ifdef SE_CLASS1
368
		check_overflow(id);
incardon's avatar
incardon committed
369
#endif
370

incardon's avatar
incardon committed
371 372 373 374 375 376 377 378 379 380
		auto fnd = owner_scs.find(id);
		if (fnd == owner_scs.end())
		{
			std::cerr << __FILE__ << ":" << __LINE__ << " Error you are setting a sub-sub-domain the processor does not own" << std::endl;
		}
		else
		{
			size_t id = fnd->second;
			owner_cost_sub.get(id).w = cost;
		}
381 382 383 384 385 386 387 388 389 390 391 392
	}

	/*! \brief Set migration cost on a sub-sub domain
	 *
	 * \param id of the sub-sub domain
	 * \param cost
	 */
	void setMigrationCost(size_t id, size_t cost)
	{
#ifdef SE_CLASS2
		check_valid(this,8);
#endif
incardon's avatar
incardon committed
393
#ifdef SE_CLASS1
394
		check_overflow(id);
incardon's avatar
incardon committed
395
#endif
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410

		gp.vertex(id).template get<nm_v::migration>() = cost;
	}

	/*! \brief Set communication cost between neighborhood sub-sub-domains (weight on the edge)
	 *
	 * \param id sub-sub domain
	 * \param e id in the neighborhood list (id in the adjacency list)
	 * \param cost
	 */
	void setCommunicationCost(size_t id, size_t e, size_t cost)
	{
#ifdef SE_CLASS2
		check_valid(this,8);
#endif
incardon's avatar
incardon committed
411
#ifdef SE_CLASS1
412 413
		check_overflow(id);
		check_overflowe(id,e);
incardon's avatar
incardon committed
414
#endif
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

		gp.getChildEdge(id, e).template get<nm_e::communication>() = cost;
	}

	/*! \brief Returns total number of sub-sub-domains
	 *
	 * \return sub-sub domain numbers
	 *
	 */
	size_t getNSubSubDomains()
	{
#ifdef SE_CLASS2
			check_valid(this,8);
#endif
		return gp.getNVertex();
	}

	/*! \brief Returns total number of neighbors of one sub-sub-domain
	 *
	 * \param id of the sub-sub-domain
	 */
	size_t getNSubSubDomainNeighbors(size_t id)
	{
#ifdef SE_CLASS2
			check_valid(this,8);
#endif
incardon's avatar
incardon committed
441
#ifdef SE_CLASS1
442
		check_overflow(id);
incardon's avatar
incardon committed
443
#endif
444 445 446 447 448

		return gp.getNChilds(id);
	}

	/*! \brief Compute the unbalance of the processor compared to the optimal balance
incardon's avatar
incardon committed
449 450
	 *
	 * \warning all processor must call this function
451 452 453 454 455 456 457 458
	 *
	 * \return the unbalance from the optimal one 0.01 mean 1%
	 */
	float getUnbalance()
	{
#ifdef SE_CLASS2
			check_valid(this,8);
#endif
incardon's avatar
incardon committed
459
		size_t load_p = getProcessorLoad();
460

incardon's avatar
incardon committed
461 462 463
		float load_avg = load_p;
		v_cl.sum(load_avg);
		v_cl.execute();
464

incardon's avatar
incardon committed
465
		if (load_avg == 0)
466
		{
incardon's avatar
incardon committed
467 468 469 470 471
			// count the number if sub-sub-domain assigned
			load_avg = owner_cost_sub.size();

			v_cl.sum(load_avg);
			v_cl.execute();
472 473
		}

incardon's avatar
incardon committed
474 475 476 477
		load_avg /= v_cl.getProcessingUnits();

		return ((float)load_p - load_avg) / load_avg;
	}
478

incardon's avatar
incardon committed
479 480 481 482 483 484 485 486 487
	/*! \brief Return the total number of sub-sub-domains in the distribution graph
	 *
	 * \return the total number of sub-sub-domains set
	 *
	 */
	size_t getNOwnerSubSubDomains() const
	{
		return owner_cost_sub.size();
	}
488

incardon's avatar
incardon committed
489 490 491 492 493 494 495 496 497 498
	/*! \brief Return the id of the set sub-sub-domain
	 *
	 * \param id id in the list of the set sub-sub-domains
	 *
	 * \return the id
	 *
	 */
	size_t getOwnerSubSubDomain(size_t id) const
	{
		return owner_cost_sub.get(id).id;
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
	}

	/*! \brief It set the Classs on test mode
	 *
	 * At the moment it fix the seed to have reproducible results
	 *
	 */
	void onTest()
	{
#ifdef SE_CLASS2
			check_valid(this,8);
#endif
		testing = true;
	}

	/*! \brief Write the distribution graph into file
	 *
	 * \param out output filename
	 *
	 */
	void write(std::string out)
	{
#ifdef SE_CLASS2
			check_valid(this,8);
#endif
incardon's avatar
incardon committed
524

525
		VTKWriter<Graph_CSR<nm_v, nm_e>, VTK_GRAPH> gv2(gp);
incardon's avatar
incardon committed
526
		gv2.write(std::to_string(v_cl.getProcessUnitID()) + "_" + out + ".vtk");
527 528 529

	}

incardon's avatar
incardon committed
530 531 532
	/*! \brief Compute the processor load
	 *
	 * \warning all processors must call this function
533 534 535 536 537 538 539 540
	 *
	 * \return the total computation cost
	 */
	size_t getProcessorLoad()
	{
#ifdef SE_CLASS2
			check_valid(this,8);
#endif
incardon's avatar
incardon committed
541 542
		openfpm::vector<size_t> loads(v_cl.getProcessingUnits());

543 544
		size_t load = 0;

incardon's avatar
incardon committed
545
		if (v_cl.getProcessUnitID() == 0)
546
		{
incardon's avatar
incardon committed
547 548 549 550 551 552 553
			for (size_t i = 0; i < gp.getNVertex(); i++)
				loads.get(gp.template vertex_p<nm_v::proc_id>(i)) += gp.template vertex_p<nm_v::computation>(i);

			for (size_t i = 0 ; i < v_cl.getProcessingUnits() ; i++)
			{
				v_cl.send(i,1234,&loads.get(i),sizeof(size_t));
			}
554
		}
incardon's avatar
incardon committed
555 556
		v_cl.recv(0,1234,&load,sizeof(size_t));
		v_cl.execute();
557 558 559 560 561 562

		return load;
	}

	/*! \brief operator=
	 *
incardon's avatar
incardon committed
563 564 565
	 * \param mt object to copy
	 *
	 * \return itself
566 567 568 569 570
	 *
	 */
	MetisDistribution & operator=(const MetisDistribution & mt)
	{
#ifdef SE_CLASS2
571
			check_valid(&mt,8);
572 573 574 575 576
			check_valid(this,8);
#endif
		this->gr = mt.gr;
		this->domain = mt.domain;
		this->gp = mt.gp;
incardon's avatar
incardon committed
577 578
		this->owner_cost_sub = mt.owner_cost_sub;
		this->owner_scs = mt.owner_scs;
579 580 581 582 583
		return *this;
	}

	/*! \brief operator=
	 *
incardon's avatar
incardon committed
584 585 586
	 * \param mt object to copy
	 *
	 * \return itself
587 588 589 590 591 592 593 594 595 596 597
	 *
	 */
	MetisDistribution & operator=(MetisDistribution && mt)
	{
#ifdef SE_CLASS2
			check_valid(mt);
			check_valid(this,8);
#endif
		this->gr = mt.gr;
		this->domain = mt.domain;
		this->gp.swap(mt.gp);
incardon's avatar
incardon committed
598 599
		this->owner_cost_sub.swap(mt.owner_cost_sub);
		this->owner_scs.swap(mt.owner_scs);
600 601 602 603
		return *this;
	}

	/*! \brief operator==
incardon's avatar
incardon committed
604 605
	 *
	 * \param mt Metis distribution to compare with
606 607 608 609 610 611 612
	 *
	 * \return true if the distribution match
	 *
	 */
	inline bool operator==(const MetisDistribution & mt)
	{
#ifdef SE_CLASS2
613
			check_valid(&mt,8);
614 615 616 617 618 619 620
			check_valid(this,8);
#endif
		bool ret = true;

		ret &= (this->gr == mt.gr);
		ret &= (this->domain == mt.domain);
		ret &= (this->gp == mt.gp);
incardon's avatar
incardon committed
621

622 623
		return ret;
	}
incardon's avatar
incardon committed
624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658

	/*! \brief Set the tolerance for each partition
	 *
	 * \param tol tolerance
	 *
	 */
	void setDistTol(double tol)
	{
		metis_graph.setDistTol(tol);
	}



	/*! \brief function that get the weight of the vertex
	 *
	 * \param id vertex id
	 *
	 */
	size_t getSubSubDomainComputationCost(size_t id)
	{
#ifdef SE_CLASS1
		if (id >= gp.getNVertex())
			std::cerr << __FILE__ << ":" << __LINE__ << "Such vertex doesn't exist (id = " << id << ", " << "total size = " << gp.getNVertex() << ")\n";
#endif

		auto fnd = owner_scs.find(id);
		if (fnd == owner_scs.end())
		{
			std::cerr << __FILE__ << ":" << __LINE__ << " Error you are setting a sub-sub-domain that the processor does not own" << std::endl;
			return 0;
		}

		size_t ids = fnd->second;
		return owner_cost_sub.get(ids).w;
	}
incardon's avatar
incardon committed
659 660 661 662 663 664 665 666 667 668

	/*! \brief Get the decomposition counter
	 *
	 * \return the decomposition counter
	 *
	 */
	size_t get_ndec()
	{
		return metis_graph.get_ndec();
	}
incardon's avatar
incardon committed
669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688

	/*! \brief Parmetis distribution is distribute sub-sub-domain on a regular grid
	 *
	 * \return true
	 *
	 */
	constexpr bool isRegularGrid()
	{
		return true;
	}

	/*! \brief Parmetis distribution is not for high scalability
	 *
	 * \return true
	 *
	 */
	constexpr bool isHighScal()
	{
		return false;
	}
689 690 691
};

#endif /* SRC_DECOMPOSITION_METISDISTRIBUTION_HPP_ */