dist_map_graph_unit_test.hpp 11.3 KB
Newer Older
tonynsyde's avatar
tonynsyde committed
1 2 3 4 5
#ifndef DIST_MAP_GRAPH_UNIT_TEST_HPP
#define DIST_MAP_GRAPH_UNIT_TEST_HPP

#include "Graph/DistGraphFactory.hpp"
#include "Graph/dist_map_graph.hpp"
6 7
#include "Packer_Unpacker/Packer.hpp"
#include "Packer_Unpacker/Unpacker.hpp"
tonynsyde's avatar
tonynsyde committed
8

9
#define DGRAPH_GS_SIZE 4
tonynsyde's avatar
tonynsyde committed
10 11 12

struct vx
{
13
	typedef boost::fusion::vector<float[3]> type;
tonynsyde's avatar
tonynsyde committed
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29

	//! Attributes name
	struct attributes
	{
		static const std::string name[];
	};

	//! type of the positional field
	typedef float s_type;

	//! The data
	type data;

	//! x property id in boost::fusion::vector
	static const unsigned int x = 0;
	//! total number of properties boost::fusion::vector
30
	static const unsigned int max_prop = 1;
tonynsyde's avatar
tonynsyde committed
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

	vx()
	{

	}

	inline vx(const vx & p)
	{
		boost::fusion::at_c<0>(data)[0] = boost::fusion::at_c<0>(p.data)[0];
		boost::fusion::at_c<0>(data)[1] = boost::fusion::at_c<0>(p.data)[1];
		boost::fusion::at_c<0>(data)[2] = boost::fusion::at_c<0>(p.data)[2];
	}

	template<unsigned int id> inline auto get() -> decltype(boost::fusion::at_c < id > (data))
	{
		return boost::fusion::at_c<id>(data);
	}

	template<unsigned int id> inline auto get() const -> const decltype(boost::fusion::at_c < id > (data))
	{
		return boost::fusion::at_c<id>(data);
	}

	template<unsigned int dim, typename Mem> inline vx(const encapc<dim, vx, Mem> & p)
	{
		this->operator=(p);
	}

	template<unsigned int dim, typename Mem> inline vx & operator=(const encapc<dim, vx, Mem> & p)
	{
		boost::fusion::at_c<0>(data)[0] = p.template get<0>()[0];
		boost::fusion::at_c<0>(data)[1] = p.template get<0>()[1];
		boost::fusion::at_c<0>(data)[2] = p.template get<0>()[2];

		return *this;
	}

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

74
const std::string vx::attributes::name[] = { "x" };
tonynsyde's avatar
tonynsyde committed
75 76 77

struct ed
{
78
	typedef boost::fusion::vector<size_t> type;
tonynsyde's avatar
tonynsyde committed
79 80 81 82 83 84 85 86 87 88 89

	//! Attributes name
	struct attributes
	{
		static const std::string name[];
	};

	//! The data
	type data;

	//! srcgid property id in boost::fusion::vector
90
	static const unsigned int prop = 0;
tonynsyde's avatar
tonynsyde committed
91
	//! total number of properties boost::fusion::vector
92
	static const unsigned int max_prop = 1;
tonynsyde's avatar
tonynsyde committed
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

	ed()
	{

	}

	template<unsigned int id> inline auto get() -> decltype(boost::fusion::at_c < id > (data))
	{
		return boost::fusion::at_c<id>(data);
	}

	template<unsigned int dim, typename Mem> inline ed(const encapc<dim, ed, Mem> & p)
	{
		this->operator=(p);
	}

	template<unsigned int dim, typename Mem> inline ed & operator=(const encapc<dim, ed, Mem> & p)
	{
		boost::fusion::at_c<0>(data) = p.template get<0>();

		return *this;
	}

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

122
const std::string ed::attributes::name[] = { "prop" };
tonynsyde's avatar
tonynsyde committed
123 124 125

BOOST_AUTO_TEST_SUITE (dist_map_graph_test)

126
BOOST_AUTO_TEST_CASE( dist_map_graph_use)
tonynsyde's avatar
tonynsyde committed
127 128 129 130 131 132 133 134
{

	//! Vcluster
	Vcluster & vcl = *global_v_cluster;

	//! Initialize the global VCluster
	init_global_v_cluster(&boost::unit_test::framework::master_test_suite().argc,&boost::unit_test::framework::master_test_suite().argv);

135
	if(vcl.getProcessingUnits() != 4)
136
		return;
137

tonynsyde's avatar
tonynsyde committed
138
	//! Cartesian grid
139
	size_t sz[2] = { DGRAPH_GS_SIZE, DGRAPH_GS_SIZE };
tonynsyde's avatar
tonynsyde committed
140 141 142 143 144 145 146 147

	//! Box
	Box<2, float> box( { 0.0, 0.0 }, { 1.0, 1.0 });

	//! Distributed graph factory
	DistGraphFactory<2, DistGraph_CSR<vx, ed>> g_factory;

	//! Distributed graph
148
	DistGraph_CSR<vx, ed> gd = g_factory.template construct<NO_EDGE, float, 2 - 1, 0, 1, 2>(sz, box);
tonynsyde's avatar
tonynsyde committed
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

	//! [Request some vertices given global ids]

	gd.reqVertex(13);
	gd.reqVertex(1);
	gd.reqVertex(14);
	gd.sync();

	gd.reqVertex(15);
	gd.reqVertex(2);
	gd.reqVertex(10);
	gd.sync();

	gd.deleteGhosts();

	//! [Exchange n vertices and edges packed]

	if(vcl.getProcessUnitID() == 0)
	{
		for(size_t i = 0; i < 4; i++)
		gd.q_move(i, 1);
	}

	if(vcl.getProcessUnitID() == 1)
	{
		for(size_t i = 0; i < 4; i++)
		gd.q_move(i, 0);
	}

	if(vcl.getProcessUnitID() == 2)
	{
		for(size_t i = 0; i < 2; i++)
		gd.q_move(i, 3);
	}

	if(vcl.getProcessUnitID() == 3)
	{
		for(size_t i = 0; i < 4; i++)
		gd.q_move(i, 2);
	}

	//! Redistribute
	gd.redistribute();

	if(vcl.getProcessUnitID() == 0)
	{
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
		BOOST_REQUIRE_EQUAL(gd.getVertexId(0), 0ul);
		BOOST_REQUIRE_EQUAL(gd.getVertexId(1), 1ul);
		BOOST_REQUIRE_EQUAL(gd.getVertexId(2), 2ul);
		BOOST_REQUIRE_EQUAL(gd.getVertexId(3), 3ul);
		BOOST_REQUIRE_EQUAL(gd.getNChilds(0), 3ul);
		BOOST_REQUIRE_EQUAL(gd.getChild(0,0), 1ul);
		BOOST_REQUIRE_EQUAL(gd.getChild(0,1), 14ul);
		BOOST_REQUIRE_EQUAL(gd.getChild(0,2), 4ul);
		BOOST_REQUIRE_EQUAL(gd.getNChilds(1), 4ul);
		BOOST_REQUIRE_EQUAL(gd.getChild(1,0), 2ul);
		BOOST_REQUIRE_EQUAL(gd.getChild(1,1), 0ul);
		BOOST_REQUIRE_EQUAL(gd.getChild(1,2), 15ul);
		BOOST_REQUIRE_EQUAL(gd.getChild(1,3), 5ul);
		BOOST_REQUIRE_EQUAL(gd.getNChilds(2), 4ul);
		BOOST_REQUIRE_EQUAL(gd.getChild(2,0), 3ul);
		BOOST_REQUIRE_EQUAL(gd.getChild(2,1), 1ul);
		BOOST_REQUIRE_EQUAL(gd.getChild(2,2), 8ul);
		BOOST_REQUIRE_EQUAL(gd.getChild(2,3), 6ul);
		BOOST_REQUIRE_EQUAL(gd.getNChilds(3), 3ul);
		BOOST_REQUIRE_EQUAL(gd.getChild(3,0), 2ul);
		BOOST_REQUIRE_EQUAL(gd.getChild(3,1), 9ul);
		BOOST_REQUIRE_EQUAL(gd.getChild(3,2), 7ul);
tonynsyde's avatar
tonynsyde committed
217 218 219 220
	}

	if(vcl.getProcessUnitID() == 1)
	{
221 222 223 224
		BOOST_REQUIRE_EQUAL(gd.getVertexId(0), 4ul);
		BOOST_REQUIRE_EQUAL(gd.getVertexId(1), 5ul);
		BOOST_REQUIRE_EQUAL(gd.getVertexId(2), 6ul);
		BOOST_REQUIRE_EQUAL(gd.getVertexId(3), 7ul);
tonynsyde's avatar
tonynsyde committed
225 226 227 228
	}

	if(vcl.getProcessUnitID() == 2)
	{
229 230 231 232 233 234
		BOOST_REQUIRE_EQUAL(gd.getVertexId(0), 8ul);
		BOOST_REQUIRE_EQUAL(gd.getVertexId(1), 9ul);
		BOOST_REQUIRE_EQUAL(gd.getVertexId(2), 10ul);
		BOOST_REQUIRE_EQUAL(gd.getVertexId(3), 11ul);
		BOOST_REQUIRE_EQUAL(gd.getVertexId(4), 12ul);
		BOOST_REQUIRE_EQUAL(gd.getVertexId(5), 13ul);
tonynsyde's avatar
tonynsyde committed
235 236 237 238
	}

	if(vcl.getProcessUnitID() == 3)
	{
239 240
		BOOST_REQUIRE_EQUAL(gd.getVertexId(0), 14ul);
		BOOST_REQUIRE_EQUAL(gd.getVertexId(1), 15ul);
tonynsyde's avatar
tonynsyde committed
241 242 243
	}
}

244
BOOST_AUTO_TEST_CASE( dist_map_graph_use_redistribution)
tonynsyde's avatar
tonynsyde committed
245 246 247 248
{
	//! Vcluster
	Vcluster & vcl = *global_v_cluster;

249
	if(vcl.getProcessingUnits() != 4)
250
		return;
251

tonynsyde's avatar
tonynsyde committed
252 253 254 255 256 257 258 259 260 261 262 263 264
	//! Initialize the global VCluster
	init_global_v_cluster(&boost::unit_test::framework::master_test_suite().argc,&boost::unit_test::framework::master_test_suite().argv);

	//! Cartesian grid
	size_t sz[2] = { 4, 4 };

	//! Box
	Box<2, float> box( { 0.0, 0.0 }, { 1.0, 1.0 });

	//! Distributed graph factory
	DistGraphFactory<2, DistGraph_CSR<vx, ed>> g_factory;

	//! Distributed graph
265
	DistGraph_CSR<vx, ed> gd = g_factory.template construct<NO_EDGE, float, 2 - 1, 0, 1, 2>(sz, box);
tonynsyde's avatar
tonynsyde 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 291 292 293 294 295

	for(size_t i=0; i< gd.getNVertex(); i++)
	gd.vertex(i).template get<vx::x>()[2] = 0;

	if (vcl.getProcessUnitID() == 0)
	{
		gd.q_move(0,1);
		gd.q_move(1,1);
	}

	if (vcl.getProcessUnitID() == 1)
	{
		gd.q_move(2,0);
		gd.q_move(3,0);
	}

	if (vcl.getProcessUnitID() == 2)
	{
		gd.q_move(0,3);
		gd.q_move(1,3);
	}

	if (vcl.getProcessUnitID() == 3)
	{
		gd.q_move(2,2);
		gd.q_move(3,2);
	}

	gd.redistribute();

296 297
	VTKWriter<DistGraph_CSR<vx, ed>, DIST_GRAPH> gv2(gd);
	gv2.write("dist_graph_redistribution_0.vtk");
tonynsyde's avatar
tonynsyde committed
298

299
	if(vcl.getProcessUnitID() == 0)
tonynsyde's avatar
tonynsyde committed
300
	{
301 302
		bool test = compare("dist_graph_redistribution_0.vtk", "dist_graph_redistribution_0_test.vtk");
		BOOST_REQUIRE_EQUAL(true,test);
tonynsyde's avatar
tonynsyde committed
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
	}

	if (vcl.getProcessUnitID() == 2)
	{
		gd.q_move(0,0);
		gd.q_move(1,0);
	}

	if (vcl.getProcessUnitID() == 3)
	{
		gd.q_move(0,0);
		gd.q_move(2,0);
	}

	gd.redistribute();

319
	gv2.write("dist_graph_redistribution_1.vtk");
tonynsyde's avatar
tonynsyde committed
320

321
	if(vcl.getProcessUnitID() == 0)
tonynsyde's avatar
tonynsyde committed
322
	{
323 324
		bool test = compare("dist_graph_redistribution_1.vtk","dist_graph_redistribution_1_test.vtk");
		BOOST_REQUIRE_EQUAL(true,test);
tonynsyde's avatar
tonynsyde committed
325 326 327 328
	}

}

Pietro Incardona's avatar
Pietro Incardona committed
329

330
BOOST_AUTO_TEST_CASE( dist_map_graph_use_free_add)
tonynsyde's avatar
tonynsyde committed
331
{
332
	// Vcluster
tonynsyde's avatar
tonynsyde committed
333 334
	Vcluster & vcl = *global_v_cluster;

335
	if(vcl.getProcessingUnits() != 4)
336
		return;
337

338
	// Initialize the global VCluster
tonynsyde's avatar
tonynsyde committed
339 340
	init_global_v_cluster(&boost::unit_test::framework::master_test_suite().argc,&boost::unit_test::framework::master_test_suite().argv);

341
	// [create graph adding freely the vertices and the edges ]
tonynsyde's avatar
tonynsyde committed
342

343
	// Distributed graph
tonynsyde's avatar
tonynsyde committed
344 345
	DistGraph_CSR<vx, ed> gd;

346
	// Add vertices
tonynsyde's avatar
tonynsyde committed
347 348 349
	for (size_t i = 0; i < 4; ++i)
	{
		vx v;
350 351 352
		v.template get<vx::x>()[0] = vcl.getProcessUnitID();
		v.template get<vx::x>()[1] = i;
		v.template get<vx::x>()[2] = 0;
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
		size_t gid = vcl.getProcessUnitID()*4 + i;
		gd.add_vertex(v, gid);
	}

	// This method must be called after adding vertices
	gd.init();

	// Add edges
	if(vcl.getProcessUnitID()==0)
	{
		gd.add_edge(0,1);
		gd.add_edge(1,2);
		gd.add_edge(2,3);
		gd.add_edge(0,4);
		gd.add_edge(1,5);
		gd.add_edge(2,6);
		gd.add_edge(3,7);
	}

	if(vcl.getProcessUnitID()==1)
	{
		gd.add_edge(4,5);
		gd.add_edge(5,6);
		gd.add_edge(6,7);
		gd.add_edge(4,8);
		gd.add_edge(5,9);
		gd.add_edge(6,10);
		gd.add_edge(7,11);
	}

	if(vcl.getProcessUnitID()==2)
	{
		gd.add_edge(8,9);
		gd.add_edge(9,10);
		gd.add_edge(10,11);
		gd.add_edge(8,12);
		gd.add_edge(9,13);
		gd.add_edge(10,14);
		gd.add_edge(11,15);
	}

	if(vcl.getProcessUnitID()==3)
	{
		gd.add_edge(12,13);
		gd.add_edge(13,14);
		gd.add_edge(14,15);
	}
400 401 402

	gd.syncEdge();

403 404
	//! [create graph adding freely the vertices and the edges ]

tonynsyde's avatar
tonynsyde committed
405 406 407 408
	if(vcl.getProcessUnitID() == 0)
	{
		for (size_t i = 0; i < 4; ++i)
		{
409
			BOOST_REQUIRE_EQUAL(gd.getVertexId(i), i);
tonynsyde's avatar
tonynsyde committed
410 411 412 413 414 415 416
		}
	}

	if(vcl.getProcessUnitID() == 2)
	{
		for (size_t i = 8, j = 0; i < 12 && j < gd.getNVertex(); ++i, ++j)
		{
417
			BOOST_REQUIRE_EQUAL(gd.getVertexId(j), i);
tonynsyde's avatar
tonynsyde committed
418 419 420
		}
	}

421
	if(vcl.getProcessUnitID() == 0)
422
		gd.reqVertex(5);
423 424 425

	gd.sync();

426
	if(vcl.getProcessUnitID() == 0)
427
		BOOST_REQUIRE_EQUAL(gd.getVertexId(5), 5ul);
428

429
	gd.deleteGhosts();
tonynsyde's avatar
tonynsyde committed
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

	if (vcl.getProcessUnitID() == 0)
	{
		gd.q_move(0,1);
		gd.q_move(1,1);
	}

	if (vcl.getProcessUnitID() == 1)
	{
		gd.q_move(2,0);
		gd.q_move(3,0);
	}

	if (vcl.getProcessUnitID() == 2)
	{
		gd.q_move(0,3);
		gd.q_move(1,3);
	}

	if (vcl.getProcessUnitID() == 3)
	{
		gd.q_move(2,2);
		gd.q_move(3,2);
	}

	gd.redistribute();

457 458
	VTKWriter<DistGraph_CSR<vx, ed>, DIST_GRAPH> gv2(gd);
	gv2.write("dist_graph_free_0.vtk");
tonynsyde's avatar
tonynsyde committed
459

460
	if(vcl.getProcessUnitID() == 0)
tonynsyde's avatar
tonynsyde committed
461
	{
462 463
		bool test = compare("dist_graph_free_0.vtk", "dist_graph_free_0_test.vtk");
		BOOST_REQUIRE_EQUAL(true,test);
tonynsyde's avatar
tonynsyde committed
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
	}

	if (vcl.getProcessUnitID() == 2)
	{
		gd.q_move(0,0);
		gd.q_move(1,0);
	}

	if (vcl.getProcessUnitID() == 3)
	{
		gd.q_move(0,0);
		gd.q_move(2,0);
	}

	gd.redistribute();

480
	gv2.write("dist_graph_free_1.vtk");
tonynsyde's avatar
tonynsyde committed
481

482
	if(vcl.getProcessUnitID() == 0)
tonynsyde's avatar
tonynsyde committed
483
	{
484 485
		bool test = compare("dist_graph_free_1.vtk", "dist_graph_free_1_test.vtk");
		BOOST_REQUIRE_EQUAL(true,test);
tonynsyde's avatar
tonynsyde committed
486 487 488
	}
}

489 490 491 492 493 494
BOOST_AUTO_TEST_CASE( dist_map_graph_use_multi_free_add)
{
	// Vcluster
	Vcluster & vcl = *global_v_cluster;

	if(vcl.getProcessingUnits() != 4)
495
		return;
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

	// Initialize the global VCluster
	init_global_v_cluster(&boost::unit_test::framework::master_test_suite().argc, &boost::unit_test::framework::master_test_suite().argv);

	// Distributed graph
	DistGraph_CSR<vx, ed> gd;

	// Add vertices
	if(vcl.getProcessUnitID()==0)
	{
		vx v;
		gd.add_vertex(v, 0);
		gd.add_vertex(v, 1);
		gd.add_vertex(v, 2);
		gd.add_vertex(v, 3);
	}

	if(vcl.getProcessUnitID()==1)
	{
		vx v;
		gd.add_vertex(v, 4);
		gd.add_vertex(v, 5);
		gd.add_vertex(v, 6);
		gd.add_vertex(v, 7);
	}

	// This method must be called ALWAYS after adding vertices
	gd.init();

	if(vcl.getProcessUnitID()==2)
	{
		vx v;
		gd.add_vertex(v, 8);
		gd.add_vertex(v, 9);
		gd.add_vertex(v, 10);
		gd.add_vertex(v, 11);
	}

	if(vcl.getProcessUnitID()==3)
	{
		vx v;
		gd.add_vertex(v, 12);
		gd.add_vertex(v, 13);
		gd.add_vertex(v, 14);
		gd.add_vertex(v, 15);
	}

	// This method must be called ALWAYS after adding vertices
	gd.init();

	gd.reqVertex(15);

	gd.sync();

550
	BOOST_REQUIRE_EQUAL(gd.getVertexId(gd.getNVertex()-1), 15ul);
551 552
}

tonynsyde's avatar
tonynsyde committed
553 554 555
BOOST_AUTO_TEST_SUITE_END()

#endif