parmetis_dist_util.hpp 10.3 KB
Newer Older
tonynsyde's avatar
tonynsyde committed
1 2 3 4 5 6 7 8 9 10 11 12
/*
 * parmetis_util.hpp
 *
 *  Created on: Oct 07, 2015
 *      Author: Antonio Leo
 */

#ifndef DISTPARMETIS_UTIL_HPP
#define DISTPARMETIS_UTIL_HPP

#include <iostream>
#include "parmetis.h"
13
#include "VTKWriter/VTKWriter.hpp"
incardon's avatar
incardon committed
14
#include "VCluster/VCluster.hpp"
tonynsyde's avatar
tonynsyde 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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98

/*! \brief Metis graph structure
 *
 * Metis graph structure
 *
 */
struct Parmetis_dist_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;

	//! Upon successful completion, the number of edges that are cut by the partitioning is written to this parameter.
	idx_t * edgecut;

	//! This parameter describes the ratio of inter-processor communication time compared to data redistri- bution time. It should be set between 0.000001 and 1000000.0. If ITR is set high, a repartitioning with a low edge-cut will be computed. If it is set low, a repartitioning that requires little data redistri- bution will be computed. Good values for this parameter can be obtained by dividing inter-processor communication time by data redistribution time. Otherwise, a value of 1000.0 is recommended.
	real_t * itr;

	//! This is used to indicate the numbering scheme that is used for the vtxdist, xadj, adjncy, and part arrays. (0 for C-style, start from 0 index)
	idx_t * numflag;

	//! This is used to indicate if the graph is weighted. wgtflag can take one of four values:
	//! 0 No weights (vwgt and adjwgt are both NULL).
	//! 1 Weights on the edges only (vwgt is NULL).
	//! 2 Weights on the vertices only (adjwgt is NULL).
	//! 3 Weights on both the vertices and edges.
	idx_t * wgtflag;
};

//! 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
 *
 *  TODO Transform pointer to openfpm vector
 *
 * \tparam graph structure that store the graph
 *
 */
template<typename Graph>
class DistParmetis
{
incardon's avatar
incardon committed
99
	//! Graph in parmetis reppresentation
tonynsyde's avatar
tonynsyde committed
100 101
	Parmetis_dist_graph Mg;

incardon's avatar
incardon committed
102
	//! Communticator for OpenMPI
Pietro Incardona's avatar
Pietro Incardona committed
103
	MPI_Comm comm = (MPI_Comm)NULL;
tonynsyde's avatar
tonynsyde committed
104

incardon's avatar
incardon committed
105
	//! VCluster
tonynsyde's avatar
tonynsyde committed
106 107
	Vcluster & v_cl;

incardon's avatar
incardon committed
108
	//! Process rank information
tonynsyde's avatar
tonynsyde committed
109 110
	int p_id = 0;

incardon's avatar
incardon committed
111
	//! nc Number of partition
tonynsyde's avatar
tonynsyde committed
112 113 114 115
	size_t nc = 0;

	/*! \brief Construct Adjacency list
	 *
incardon's avatar
incardon committed
116
	 * \param sub_g graph in which we construct the adjacency list
tonynsyde's avatar
tonynsyde committed
117 118 119 120 121 122 123 124
	 *
	 */
	void constructAdjList(Graph & sub_g)
	{

		// init basic graph informations and part vector
		Mg.nvtxs[0] = sub_g.getNVertex();
		Mg.part = new idx_t[sub_g.getNVertex()];
125
		for (size_t i = 0; i < sub_g.getNVertex(); i++)
tonynsyde's avatar
tonynsyde committed
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
			Mg.part[i] = p_id;

		// create xadj, adjlist, vwgt, adjwgt and vsize
		Mg.xadj = new idx_t[sub_g.getNVertex() + 1];
		Mg.adjncy = new idx_t[sub_g.getNEdge()];
		Mg.vwgt = new idx_t[sub_g.getNVertex()];
		Mg.adjwgt = new idx_t[sub_g.getNEdge()];
		Mg.vsize = new idx_t[sub_g.getNVertex()];

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

		// actual position
		size_t id = 0;

		for (size_t i = 0, j = sub_g.firstId(); i < sub_g.getNVertex() && j <= sub_g.lastId(); i++, j++)
		{
143 144
			size_t idx = sub_g.nodeById(j);

tonynsyde's avatar
tonynsyde committed
145
			// Add weight to vertex and migration cost
146 147
			Mg.vwgt[i] = sub_g.vertex(idx).template get<nm_v::computation>();
			Mg.vsize[i] = sub_g.vertex(idx).template get<nm_v::migration>();
tonynsyde's avatar
tonynsyde committed
148 149 150 151 152

			// Calculate the starting point in the adjacency list
			Mg.xadj[id] = prev;

			// Create the adjacency list and the weights for edges
153
			for (size_t s = 0; s < sub_g.getNChilds(idx); s++)
tonynsyde's avatar
tonynsyde committed
154
			{
155
				Mg.adjncy[prev + s] = sub_g.getChild(idx, s);
tonynsyde's avatar
tonynsyde committed
156

157
				Mg.adjwgt[prev + s] = sub_g.getChildEdge(idx, s).template get<nm_e::communication>();
tonynsyde's avatar
tonynsyde committed
158 159 160
			}

			// update the position for the next vertex
161
			prev += sub_g.getNChilds(idx);
tonynsyde's avatar
tonynsyde committed
162 163 164 165 166 167

			id++;
		}

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

tonynsyde's avatar
tonynsyde committed
169
	}
Pietro Incardona's avatar
Pietro Incardona committed
170

tonynsyde's avatar
tonynsyde committed
171 172 173 174
public:

	/*! \brief Constructor
	 *
incardon's avatar
incardon committed
175
	 * Construct a metis graph from a Graph_CSR
tonynsyde's avatar
tonynsyde committed
176
	 *
incardon's avatar
incardon committed
177
	 * \param v_cl Vcluster
tonynsyde's avatar
tonynsyde committed
178 179 180 181 182 183 184 185
	 * \param nc number of partitions
	 *
	 */
	DistParmetis(Vcluster & v_cl, size_t nc) :
			v_cl(v_cl), nc(nc)
	{
		// TODO Move into VCluster
		MPI_Comm_dup(MPI_COMM_WORLD, &comm);
Pietro Incardona's avatar
Pietro Incardona committed
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204

		// Nullify Mg
		Mg.nvtxs = NULL;
		Mg.ncon = NULL;
		Mg.xadj = NULL;
		Mg.adjncy = NULL;
		Mg.vwgt = NULL;
		Mg.vsize = NULL;
		Mg.adjwgt = NULL;
		Mg.nparts = NULL;
		Mg.tpwgts = NULL;
		Mg.ubvec = NULL;
		Mg.options = NULL;
		Mg.objval = NULL;
		Mg.part = NULL;
		Mg.edgecut = NULL;
		Mg.itr = NULL;
		Mg.numflag = NULL;
		Mg.wgtflag = NULL;
tonynsyde's avatar
tonynsyde committed
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 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
	}

	//TODO deconstruct new variables
	/*! \brief destructor
	 *
	 * Destructor, It destroy all the memory allocated
	 *
	 */
	~DistParmetis()
	{
		// Deallocate the Mg structure
		if (Mg.nvtxs != NULL)
		{
			delete[] Mg.nvtxs;
		}

		if (Mg.ncon != NULL)
		{
			delete[] Mg.ncon;
		}

		if (Mg.xadj != NULL)
		{
			delete[] Mg.xadj;
		}

		if (Mg.adjncy != NULL)
		{
			delete[] Mg.adjncy;
		}

		if (Mg.vwgt != NULL)
		{
			delete[] Mg.vwgt;
		}

		if (Mg.adjwgt != NULL)
		{
			delete[] Mg.adjwgt;
		}

		if (Mg.nparts != NULL)
		{
			delete[] Mg.nparts;
		}

		if (Mg.tpwgts != NULL)
		{
			delete[] Mg.tpwgts;
		}

		if (Mg.ubvec != NULL)
		{
			delete[] Mg.ubvec;
		}

		if (Mg.options != NULL)
		{
			delete[] Mg.options;
		}

		if (Mg.part != NULL)
		{
			delete[] Mg.part;
		}

		if (Mg.edgecut != NULL)
		{
			delete[] Mg.edgecut;
		}

		if (Mg.numflag != NULL)
		{
			delete[] Mg.numflag;
		}

		if (Mg.wgtflag != NULL)
		{
			delete[] Mg.wgtflag;
		}
	}

	/*! \brief Set the Sub-graph
	 *
	 * \param sub_g Sub-graph to set
	 *
	 */
	void initSubGraph(Graph & sub_g)
	{
		p_id = v_cl.getProcessUnitID();

		// Get the number of vertex

		Mg.nvtxs = new idx_t[1];
		Mg.nvtxs[0] = sub_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 (init after in constructAdjList) (can be removed)

		Mg.vwgt = NULL;

		// Set to null the weight of the edge (init after in constructAdjList) (can be removed)

		Mg.adjwgt = NULL;

		// construct the adjacency list

		constructAdjList(sub_g);

		// Set the total number of partitions

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

		//! Set option for the graph partitioning (set as default)

		Mg.options = new idx_t[4];
		Mg.options[0] = 0;
		Mg.options[1] = 0;
		Mg.options[2] = 0;
		Mg.options[3] = 0;

		//! adaptiveRepart itr value
		Mg.itr = new real_t[1];
		Mg.itr[0] = 1000.0;

		//! init tpwgts to have balanced vertices and ubvec

		Mg.tpwgts = new real_t[Mg.nparts[0]];
		Mg.ubvec = new real_t[Mg.nparts[0]];

		for (int s = 0; s < Mg.nparts[0]; s++)
		{
			Mg.tpwgts[s] = 1.0 / Mg.nparts[0];
			Mg.ubvec[s] = 1.05;
		}

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

		//! This is used to indicate the numbering scheme that is used for the vtxdist, xadj, adjncy, and part arrays. (0 for C-style, start from 0 index)
		Mg.numflag = new idx_t[1];
		Mg.numflag[0] = 0;

		//! This is used to indicate if the graph is weighted.
		Mg.wgtflag = new idx_t[1];
355
		Mg.wgtflag[0] = 3;
tonynsyde's avatar
tonynsyde committed
356 357 358 359 360 361
	}

	/*! \brief Decompose the graph
	 *
	 * \tparam i which property store the decomposition
	 *
incardon's avatar
incardon committed
362
	 * \param sub_g graph to decompose
tonynsyde's avatar
tonynsyde committed
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
	 *
	 */
	template<unsigned int i>
	void decompose(Graph & sub_g)
	{

		// Decompose

		ParMETIS_V3_PartKway((idx_t *) sub_g.getVtxdist()->getPointer(), Mg.xadj, Mg.adjncy, Mg.vwgt, Mg.adjwgt, Mg.wgtflag, Mg.numflag, Mg.ncon, Mg.nparts, Mg.tpwgts, Mg.ubvec, Mg.options, Mg.edgecut, Mg.part, &comm);
		/*
		 ParMETIS_V3_AdaptiveRepart( (idx_t *) vtxdist.getPointer(), Mg.xadj,Mg.adjncy,Mg.vwgt,Mg.vsize,Mg.adjwgt, Mg.wgtflag, Mg.numflag,
		 Mg.ncon, Mg.nparts, Mg.tpwgts, Mg.ubvec, Mg.itr, Mg.options, Mg.edgecut,
		 Mg.part, &comm );
		 */

		// For each vertex store the processor that contain the data
379
		for (size_t id = 0, j = sub_g.firstId(); id < sub_g.getNVertex() && j <= sub_g.lastId(); id++, j++)
tonynsyde's avatar
tonynsyde committed
380
		{
381
			sub_g.vertex(sub_g.nodeById(j)).template get<i>() = Mg.part[id];
tonynsyde's avatar
tonynsyde committed
382 383 384 385 386 387 388
		}
	}

	/*! \brief Refine the graph
	 *
	 * \tparam i which property store the refined decomposition
	 *
incardon's avatar
incardon committed
389 390
	 * \param sub_g graph to decompose
	 *
tonynsyde's avatar
tonynsyde committed
391 392 393 394 395 396
	 */

	template<unsigned int i>
	void refine(Graph & sub_g)
	{
		// Refine
397 398
		//ParMETIS_V3_PartKway((idx_t *) sub_g.getVtxdist()->getPointer(), Mg.xadj, Mg.adjncy, Mg.vwgt, Mg.adjwgt, Mg.wgtflag, Mg.numflag, Mg.ncon, Mg.nparts, Mg.tpwgts, Mg.ubvec, Mg.options, Mg.edgecut, Mg.part, &comm);
		ParMETIS_V3_AdaptiveRepart((idx_t *) sub_g.getVtxdist()->getPointer(), Mg.xadj, Mg.adjncy, Mg.vwgt, Mg.vsize, Mg.adjwgt, Mg.wgtflag, Mg.numflag, Mg.ncon, Mg.nparts, Mg.tpwgts, Mg.ubvec, Mg.itr, Mg.options, Mg.edgecut, Mg.part, &comm);
tonynsyde's avatar
tonynsyde committed
399 400

		// For each vertex store the processor that contain the data
401
		for (size_t id = 0, j = sub_g.firstId(); id < sub_g.getNVertex() && j <= sub_g.lastId(); id++, j++)
tonynsyde's avatar
tonynsyde committed
402
		{
403
			sub_g.vertex(sub_g.nodeById(j)).template get<i>() = Mg.part[id];
tonynsyde's avatar
tonynsyde committed
404 405 406 407
		}
	}

	/*! \brief Get graph partition vector
incardon's avatar
incardon committed
408 409
	 *
	 * \return the partition or the assignment of each sub-sub-domain
tonynsyde's avatar
tonynsyde committed
410 411 412 413 414 415 416 417
	 *
	 */
	idx_t * getPartition()
	{
		return Mg.part;
	}

	/*! \brief Reset graph and reconstruct it
incardon's avatar
incardon committed
418 419
	 *
	 * \param sub_g graph to decompose
tonynsyde's avatar
tonynsyde committed
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 450 451 452 453 454 455 456 457 458 459 460 461 462 463
	 *
	 */
	void reset(Graph & sub_g)
	{
		// Deallocate the graph structures

		if (Mg.xadj != NULL)
		{
			delete[] Mg.xadj;
		}

		if (Mg.adjncy != NULL)
		{
			delete[] Mg.adjncy;
		}

		if (Mg.vwgt != NULL)
		{
			delete[] Mg.vwgt;
		}

		if (Mg.adjwgt != NULL)
		{
			delete[] Mg.adjwgt;
		}

		if (Mg.part != NULL)
		{
			delete[] Mg.part;
		}

		if (Mg.vsize != NULL)
		{
			delete[] Mg.vsize;
		}

		sub_g.deleteGhosts();

		constructAdjList(sub_g);
	}
}
;

#endif