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

#ifndef METIS_UTIL_HPP
#define METIS_UTIL_HPP

#include <iostream>
#include "metis.h"
13
#include "SubdomainGraphNodes.hpp"
14
#include "VTKWriter/VTKWriter.hpp"
incardon's avatar
incardon committed
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

/*! \brief Metis graph structure
 *
 * Metis graph structure
 *
 */
struct Metis_graph
{
	//! The number of vertices in the graph
	idx_t * nvtxs;

	//! number of balancing constrains
	//! more practical, are the number of weights for each vertex
	//! PS even we you specify vwgt == NULL ncon must be set at leat to
	//! one
	idx_t * ncon;

	//! For each vertex it store the adjacency lost start for the vertex i
	idx_t * xadj;

	//! For each vertex it store a list of all neighborhood vertex
	idx_t * adjncy;

	//! Array that store the weight for each vertex
	idx_t * vwgt;

	//! Array of the vertex size, basically is the total communication amount
	idx_t * vsize;
	//! The weight of the edge
	idx_t * adjwgt;
	//! number of part to partition the graph
	idx_t * nparts;
	//! Desired weight for each partition (one for each constrain)
	real_t * tpwgts;
	//! For each partition load imbalance tollerated
	real_t * ubvec;
	//! Additional option for the graph partitioning
	idx_t * options;
	//! return the total comunication cost for each partition
	idx_t * objval;
	//! Is a output vector containing the partition for each vertex
	idx_t * part;
};

//! Balance communication and computation
#define BALANCE_CC 1
//! Balance communication computation and memory
#define BALANCE_CCM 2
//! Balance computation and comunication and others
#define BALANCE_CC_O(c) c+1

/*! \brief Helper class to define Metis graph
 *
 * \tparam graph structure that store the graph
 *
 */

template<typename Graph>
class Metis
{
	// Graph in metis reppresentation
	Metis_graph Mg;

	// Original graph
	Graph & g;

81
	//Check if weights are available
82
	bool useWeights = false;
83

incardon's avatar
incardon committed
84 85 86 87 88 89 90 91 92
	/*! \brief Construct Adjacency list
	 *
	 * \param g Graph
	 *
	 */
	void constructAdjList(Graph & g)
	{
		// create xadj and adjlist

93 94
		// create xadj, adjlist, vwgt, adjwgt and vsize
		Mg.xadj = new idx_t[g.getNVertex() + 1];
incardon's avatar
incardon committed
95 96
		Mg.adjncy = new idx_t[g.getNEdge()];

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
		//! starting point in the adjacency list
		size_t prev = 0;

		// actual position
		size_t id = 0;

		// for each vertex calculate the position of the starting point in the adjacency list
		for (size_t i = 0; i < g.getNVertex(); i++)
		{
			// Calculate the starting point in the adjacency list
			Mg.xadj[id] = prev;

			// Create the adjacency list
			for (size_t s = 0; s < g.getNChilds(i); s++)
			{
				Mg.adjncy[prev + s] = g.getChild(i, s);
			}

			// update the position for the next vertex
			prev += g.getNChilds(i);

			id++;
		}

		// Fill the last point
		Mg.xadj[id] = prev;
	}

	/*! \brief Construct Adjacency list
	 *
	 * \param g Graph
	 *
	 */
	void constructAdjListWithWeights(Graph & g)
	{
		// create xadj, adjlist, vwgt, adjwgt and vsize
		Mg.xadj = new idx_t[g.getNVertex() + 1];
		Mg.adjncy = new idx_t[g.getNEdge()];
		Mg.vwgt = new idx_t[g.getNVertex()];
		Mg.adjwgt = new idx_t[g.getNEdge()];
		Mg.vsize = new idx_t[g.getNVertex()];
incardon's avatar
incardon committed
138 139 140 141 142 143 144 145

		//! starting point in the adjacency list
		size_t prev = 0;

		// actual position
		size_t id = 0;

		// for each vertex calculate the position of the starting point in the adjacency list
146
		for (size_t i = 0; i < g.getNVertex(); i++)
incardon's avatar
incardon committed
147
		{
148 149
			// Add weight to vertex and migration cost
			Mg.vwgt[i] = g.vertex(i).template get<nm_v::computation>();
150
			Mg.vwgt[i] = (Mg.adjwgt[i] == 0)?1:Mg.vwgt[i];
151
			Mg.vsize[i] = g.vertex(i).template get<nm_v::migration>();
152
			Mg.vsize[i] = (Mg.vsize[i] == 0)?1:Mg.vsize[i];
153

incardon's avatar
incardon committed
154 155 156 157
			// Calculate the starting point in the adjacency list
			Mg.xadj[id] = prev;

			// Create the adjacency list
158 159 160 161
			for (size_t s = 0; s < g.getNChilds(i); s++)
			{
				Mg.adjncy[prev + s] = g.getChild(i, s);

162
				// zero values on Metis are dangerous
tonynsyde's avatar
tonynsyde committed
163
				Mg.adjwgt[prev + s] = g.getChildEdge(i, s).template get<nm_e::communication>();
164
				Mg.adjwgt[prev + s] = (Mg.adjwgt[prev + s] == 0)?1:Mg.adjwgt[prev + s];
165
			}
incardon's avatar
incardon committed
166 167

			// update the position for the next vertex
168
			prev += g.getNChilds(i);
incardon's avatar
incardon committed
169 170 171 172 173 174 175 176 177 178

			id++;
		}

		// Fill the last point
		Mg.xadj[id] = prev;
	}

public:

179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
	/*! \brief Constructor
	 *
	 * Construct a metis graph from Graph_CSR
	 *
	 * \param g Graph we want to convert to decompose
	 * \param nc number of partitions
	 * \param useWeights tells if weights are used or not
	 *
	 */
	Metis(Graph & g, size_t nc, bool useWeights) :
			g(g), useWeights(useWeights)
	{
		initMetisGraph(nc);
	}

incardon's avatar
incardon committed
194 195 196 197 198 199 200 201
	/*! \brief Constructor
	 *
	 * Construct a metis graph from Graph_CSR
	 *
	 * \param g Graph we want to convert to decompose
	 * \param nc number of partitions
	 *
	 */
202 203
	Metis(Graph & g, size_t nc) :
			g(g)
incardon's avatar
incardon committed
204
	{
205 206 207 208 209 210
		initMetisGraph(nc);
	}

	void initMetisGraph(int nc)
	{

incardon's avatar
incardon committed
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
		// Get the number of vertex

		Mg.nvtxs = new idx_t[1];
		Mg.nvtxs[0] = g.getNVertex();

		// Set the number of constrains

		Mg.ncon = new idx_t[1];
		Mg.ncon[0] = 1;

		// Set to null the weight of the vertex

		Mg.vwgt = NULL;

		// Put the total communication size to NULL

		Mg.vsize = NULL;

		// Set to null the weight of the edge

		Mg.adjwgt = NULL;

233 234 235 236 237 238
		// construct the adjacency list
		if (useWeights)
			constructAdjListWithWeights(g);
		else
			constructAdjList(g);

incardon's avatar
incardon committed
239 240 241 242 243 244 245 246 247
		// Set the total number of partitions

		Mg.nparts = new idx_t[1];
		Mg.nparts[0] = nc;

		// Set to null the desired weight for each partition (one for each constrain)

		Mg.tpwgts = NULL;

248
		//! Set to null the partition load imbalance tolerance
incardon's avatar
incardon committed
249 250 251 252 253 254 255 256 257 258 259 260

		Mg.ubvec = NULL;

		//! Set tp null additional option for the graph partitioning

		Mg.options = NULL;

		//! set the objective value
		Mg.objval = new idx_t[1];

		//! Is an output vector containing the partition for each vertex
		Mg.part = new idx_t[g.getNVertex()];
tonynsyde's avatar
tonynsyde committed
261

262
		for (size_t i = 0; i < g.getNVertex(); i++)
tonynsyde's avatar
tonynsyde committed
263
			Mg.part[i] = 0;
incardon's avatar
incardon committed
264 265 266 267 268 269 270 271 272 273 274 275
	}

	/*! \brief destructor
	 *
	 * Destructor, It destroy all the memory allocated
	 *
	 */
	~Metis()
	{
		// Deallocate the Mg structure
		if (Mg.nvtxs != NULL)
		{
276
			delete[] Mg.nvtxs;
incardon's avatar
incardon committed
277 278 279 280
		}

		if (Mg.ncon != NULL)
		{
281
			delete[] Mg.ncon;
incardon's avatar
incardon committed
282 283 284 285
		}

		if (Mg.xadj != NULL)
		{
286
			delete[] Mg.xadj;
incardon's avatar
incardon committed
287 288 289 290
		}

		if (Mg.adjncy != NULL)
		{
291
			delete[] Mg.adjncy;
incardon's avatar
incardon committed
292 293 294
		}
		if (Mg.vwgt != NULL)
		{
295
			delete[] Mg.vwgt;
incardon's avatar
incardon committed
296 297 298
		}
		if (Mg.adjwgt != NULL)
		{
299
			delete[] Mg.adjwgt;
incardon's avatar
incardon committed
300 301 302
		}
		if (Mg.nparts != NULL)
		{
303
			delete[] Mg.nparts;
incardon's avatar
incardon committed
304 305 306
		}
		if (Mg.tpwgts != NULL)
		{
307
			delete[] Mg.tpwgts;
incardon's avatar
incardon committed
308 309 310
		}
		if (Mg.ubvec != NULL)
		{
311
			delete[] Mg.ubvec;
incardon's avatar
incardon committed
312 313 314
		}
		if (Mg.options != NULL)
		{
315
			delete[] Mg.options;
incardon's avatar
incardon committed
316 317 318
		}
		if (Mg.objval != NULL)
		{
319
			delete[] Mg.objval;
incardon's avatar
incardon committed
320 321 322
		}
		if (Mg.part != NULL)
		{
323
			delete[] Mg.part;
incardon's avatar
incardon committed
324 325 326 327 328 329 330 331 332 333 334 335
		}
	}

	/*! \brief Decompose the graph
	 *
	 * \tparam i which property store the decomposition
	 *
	 */

	template<unsigned int i>
	void decompose()
	{
incardon's avatar
incardon committed
336 337 338
		if (Mg.nparts[0] != 1)
		{
			// Decompose
339
			METIS_PartGraphRecursive(Mg.nvtxs, Mg.ncon, Mg.xadj, Mg.adjncy, Mg.vwgt, Mg.vsize, Mg.adjwgt, Mg.nparts, Mg.tpwgts, Mg.ubvec, Mg.options, Mg.objval, Mg.part);
incardon's avatar
incardon committed
340

incardon's avatar
incardon committed
341
			// vertex id
incardon's avatar
incardon committed
342

incardon's avatar
incardon committed
343
			size_t id = 0;
incardon's avatar
incardon committed
344

incardon's avatar
incardon committed
345
			// For each vertex store the processor that contain the data
incardon's avatar
incardon committed
346

incardon's avatar
incardon committed
347
			auto it = g.getVertexIterator();
incardon's avatar
incardon committed
348

incardon's avatar
incardon committed
349 350 351 352 353 354 355 356 357
			while (it.isNext())
			{
				g.vertex(it).template get<i>() = Mg.part[id];

				++id;
				++it;
			}
		}
		else
incardon's avatar
incardon committed
358
		{
incardon's avatar
incardon committed
359
			// Trivially assign all the domains to the processor 0
incardon's avatar
incardon committed
360

incardon's avatar
incardon committed
361 362 363 364 365 366 367 368
			auto it = g.getVertexIterator();

			while (it.isNext())
			{
				g.vertex(it).template get<i>() = 0;

				++it;
			}
incardon's avatar
incardon committed
369 370 371 372 373 374 375 376 377 378 379 380 381
		}
	}

	/*! \brief Decompose the graph
	 *
	 * \tparam i which property store the decomposition
	 *
	 */

	template<unsigned int i, typename Graph_part>
	void decompose(Graph_part & gp)
	{
		// Decompose
382
		METIS_PartGraphRecursive(Mg.nvtxs, Mg.ncon, Mg.xadj, Mg.adjncy, Mg.vwgt, Mg.vsize, Mg.adjwgt, Mg.nparts, Mg.tpwgts, Mg.ubvec, Mg.options, Mg.objval, Mg.part);
incardon's avatar
incardon committed
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399

		// vertex id

		size_t id = 0;

		// For each vertex store the processor that contain the data

		auto it = gp.getVertexIterator();

		while (it.isNext())
		{
			gp.vertex(it).template get<i>() = Mg.part[id];

			++id;
			++it;
		}
	}
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

	/*! \brief It set Metis on test
	 *
	 * \param testing set to true to disable the testing
	 *
	 * At the moment disable the seed randomness to keep the result
	 * reproducible
	 *
	 */
	void onTest(bool testing)
	{
		if (testing == false)
			return;

		if (Mg.options == NULL)
		{
			// allocate
			Mg.options = new idx_t[METIS_NOPTIONS];

			// set default options
			METIS_SetDefaultOptions(Mg.options);
		}

		Mg.options[METIS_OPTION_SEED] = 0;
	}
incardon's avatar
incardon committed
425 426 427
};

#endif