DistCartesianGraphFactory_unit_test.hpp 9.97 KB
Newer Older
tonynsyde's avatar
tonynsyde committed
1 2 3 4 5
#ifndef DISTCARTESIAN_GRAPH_UNIT_TEST_HPP
#define DISTCARTESIAN_GRAPH_UNIT_TEST_HPP

#include "Graph/DistCartesianGraphFactory.hpp"
#include "Graph/map_graph.hpp"
6 7
#include "Packer_Unpacker/Packer.hpp"
#include "Packer_Unpacker/Unpacker.hpp"
tonynsyde's avatar
tonynsyde committed
8 9
#include "SubdomainGraphNodes.hpp"

10
#define DGS_SIZE 4
tonynsyde's avatar
tonynsyde committed
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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67

/*!
 *
 * Test node
 *
 */

struct node
{
	//! The node contain 3 unsigned long integer for communication computation and memory
	typedef boost::fusion::vector<size_t, size_t, size_t, size_t, size_t> type;

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

	//! The data
	type data;

	//! communication property id in boost::fusion::vector
	static const unsigned int communication = 0;
	//! computation property id in boost::fusion::vector
	static const unsigned int computation = 1;
	//! memory property id in boost::fusion::vector
	static const unsigned int memory = 2;
	//! id property id in boost::fusion::vector
	static const unsigned int id = 3;
	static const unsigned int global_id = 4;
	//! total number of properties boost::fusion::vector
	static const unsigned int max_prop = 5;

	node(){

	}

	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 node(const encapc<dim, node, Mem> & p)
	{
		this->operator=(p);
	}

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

		return *this;
	}
68 69 70 71 72

	static bool noPointers()
	{
		return true;
	}
tonynsyde's avatar
tonynsyde committed
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
};


const std::string node::attributes::name[] = { "communication", "computation", "memory", "id", "global_id" };

static void * message_receive(size_t msg_i, size_t total_msg, size_t total_p, size_t i, size_t ri, void * ptr)
{
	openfpm::vector<HeapMemory> *v = static_cast<openfpm::vector<HeapMemory> *>(ptr);

	v->get(i).allocate(msg_i);

	return v->get(i).getPointer();

}

BOOST_AUTO_TEST_SUITE (DistCartesianGraphFactory_test)

BOOST_AUTO_TEST_CASE( DistCartesianGraphFactory_3D_use)
{
92 93
	// Boundary conditions, non periodic
	size_t bc[] = {NON_PERIODIC,NON_PERIODIC,NON_PERIODIC};
tonynsyde's avatar
tonynsyde committed
94 95 96 97 98 99 100 101

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

	// Cartesian grid
102
	size_t sz[3] = { DGS_SIZE, DGS_SIZE, DGS_SIZE };
tonynsyde's avatar
tonynsyde committed
103 104 105 106 107 108 109 110

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

	// Graph factory
	CartesianGraphFactory<3,Graph_CSR<Point_test<float>,Point_test<float>>> g_factory;

	// Standard graph
111
	Graph_CSR<Point_test<float>,Point_test<float>> g = g_factory.template construct<NO_EDGE, node::id, float, 3 - 1, 0, 1, 2>(sz, box, bc);
tonynsyde's avatar
tonynsyde committed
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

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

	// Distributed graph factory
	DistCartesianGraphFactory<3, DistGraph_CSR<node, node>> dist_g_factory;

	// Distributed graph, communication and computation are just placeholders, not filled coherently
	DistGraph_CSR<node, node> gd = dist_g_factory.template construct<NO_EDGE, node::id, node::global_id, node::communication, node::computation, float, 3 - 1, 0, 1, 2>(sz, box, vtxdist);

	// Check that sizes of graphs are correct
	size_t t_v = gd.getNVertex();
	vcl.sum(t_v);
	vcl.execute();

	BOOST_REQUIRE_EQUAL(g.getNVertex(), t_v);

	size_t count = 0;
	for(size_t i = 0 ; i< g.getNVertex(); i++){
		if(gd.vertexIsInThisGraph(i))
			count++;
	}

	BOOST_REQUIRE_EQUAL(count, gd.getNVertex());

137
	for(size_t i = (size_t)vtxdist.get(vcl.getProcessUnitID()), local_i = 0; i < (size_t)vtxdist.get(vcl.getProcessUnitID()+1); i++, local_i++)
tonynsyde's avatar
tonynsyde committed
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
	{
		BOOST_REQUIRE_EQUAL(gd.vertex(local_i).template get<node::id>(), g.vertex(i).template get<node::id>());
		BOOST_REQUIRE_EQUAL(gd.getNChilds(local_i), g.getNChilds(i));

		for (size_t s = 0; s < g.getNChilds(i); s++)
		{
			BOOST_REQUIRE_EQUAL(gd.getChild(local_i, s), g.getChild(i, s));
		}
	}


}

BOOST_AUTO_TEST_CASE( DistCartesianGraphFactory_2D_use)
{
153 154
	// Boundary conditions, non periodic
	size_t bc[] = {NON_PERIODIC,NON_PERIODIC};
tonynsyde's avatar
tonynsyde committed
155 156 157 158

	// Vcluster
	Vcluster & vcl = *global_v_cluster;

159 160 161
	if(vcl.getProcessingUnits() != 2)
		return;

tonynsyde's avatar
tonynsyde committed
162 163 164 165
	// 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
166
	size_t sz[2] = { DGS_SIZE, DGS_SIZE };
tonynsyde's avatar
tonynsyde committed
167 168 169 170 171 172 173 174

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

	// Graph factory
	CartesianGraphFactory<2,Graph_CSR<node, node>> g_factory;

	// Standard graph
175
	Graph_CSR<node, node> g = g_factory.template construct<NO_EDGE, node::id, float, 2 - 1, 0, 1, 2>(sz, box, bc);
tonynsyde's avatar
tonynsyde committed
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200

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

	// Distributed graph factory
	DistCartesianGraphFactory<2, DistGraph_CSR<node, node>> dist_g_factory;

	// Distributed graph
	DistGraph_CSR<node, node> gd = dist_g_factory.template construct<NO_EDGE, node::id, node::global_id, node::communication, node::computation, float, 2 - 1, 0, 1, 2>(sz, box, vtxdist);

	// Check that sizes of graphs are correct
	size_t t_v = gd.getNVertex();
	vcl.sum(t_v);
	vcl.execute();

	BOOST_REQUIRE_EQUAL(g.getNVertex(), t_v);

	size_t count = 0;
	for(size_t i = 0 ; i< g.getNVertex(); i++){
		if(gd.vertexIsInThisGraph(i))
			count++;
	}

	BOOST_REQUIRE_EQUAL(count, gd.getNVertex());

201
	for(size_t i = (size_t)vtxdist.get(vcl.getProcessUnitID()), local_i = 0; i < (size_t)vtxdist.get(vcl.getProcessUnitID()+1); i++, local_i++)
tonynsyde's avatar
tonynsyde committed
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 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
	{
		BOOST_REQUIRE_EQUAL(gd.vertex(local_i).template get<node::id>(), g.vertex(i).template get<node::id>());
		BOOST_REQUIRE_EQUAL(gd.getNChilds(local_i), g.getNChilds(i));

		for (size_t s = 0; s < g.getNChilds(i); s++)
		{
			BOOST_REQUIRE_EQUAL(gd.getChild(local_i, s), g.getChild(i, s));
		}
	}

	if(vcl.getProcessUnitID() == 0){
		std::vector<size_t> pap_prp;
		Packer<size_t,HeapMemory>::packRequest(pap_prp);
		Packer<node,HeapMemory>::packRequest(pap_prp);
		Packer<node,HeapMemory>::packRequest(pap_prp);
		Packer<node,HeapMemory>::packRequest(pap_prp);

		// Calculate how much preallocated memory we need to pack all the objects
		size_t req = ExtPreAlloc<HeapMemory>::calculateMem(pap_prp);

		// allocate the memory
		HeapMemory pmem;
		pmem.allocate(req);
		ExtPreAlloc<HeapMemory> & mem = *(new ExtPreAlloc<HeapMemory>(pap_prp,pmem));
		mem.incRef();

		Pack_stat sts;

		// try to pack
		Packer<size_t,HeapMemory>::pack(mem, 3, sts);
		Packer<decltype(gd.vertex(0)),HeapMemory>::pack(mem,gd.vertex(0), sts);
		Packer<decltype(gd.vertex(0)),HeapMemory>::pack(mem,gd.vertex(1), sts);
		Packer<decltype(gd.vertex(0)),HeapMemory>::pack(mem,gd.vertex(2), sts);

		vcl.send(1,0,mem.getPointer(0), pmem.size());
		vcl.execute();
	}

	if(vcl.getProcessUnitID() == 1){

		HeapMemory pmem;
		ExtPreAlloc<HeapMemory> mem(sizeof(node)*3 + sizeof(size_t), pmem);

		Unpack_stat ps;
		node v1,v2,v3;

		vcl.recv(0,0,pmem.getPointer(),sizeof(node)*3 + sizeof(size_t));
		vcl.execute();

		size_t size;
		Unpacker<size_t,HeapMemory>::unpack(mem,size,ps);
253
		BOOST_REQUIRE_EQUAL(size, 3ul);
tonynsyde's avatar
tonynsyde committed
254 255 256 257

		Unpacker<node,HeapMemory>::unpack(mem,v1,ps);
		Unpacker<node,HeapMemory>::unpack(mem,v2,ps);
		Unpacker<node,HeapMemory>::unpack(mem,v3,ps);
258 259 260
		BOOST_REQUIRE_EQUAL(v1.template get<node::id>(), 0ul);
		BOOST_REQUIRE_EQUAL(v2.template get<node::id>(), 1ul);
		BOOST_REQUIRE_EQUAL(v3.template get<node::id>(), 2ul);
tonynsyde's avatar
tonynsyde committed
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
	}

	//! [Exchange n vertices packed]
	std::vector<size_t> pap_prp;
	Packer<size_t,HeapMemory>::packRequest(pap_prp);
	Packer<node,HeapMemory>::packRequest(pap_prp);
	Packer<node,HeapMemory>::packRequest(pap_prp);
	Packer<node,HeapMemory>::packRequest(pap_prp);

	// Calculate how much preallocated memory we need to pack all the objects
	size_t req = ExtPreAlloc<HeapMemory>::calculateMem(pap_prp);

	// allocate the memory
	HeapMemory pmem;
	pmem.allocate(req);
	ExtPreAlloc<HeapMemory> & mem = *(new ExtPreAlloc<HeapMemory>(pap_prp,pmem));
	mem.incRef();

	Pack_stat sts;

	// try to pack
	Packer<size_t,HeapMemory>::pack(mem, 3, sts);
	for(int i = 0; i < 3; i++)
		Packer<decltype(gd.vertex(0)),HeapMemory>::pack(mem,gd.vertex(i), sts);

	openfpm::vector<size_t> prc;
	openfpm::vector<size_t> size;
	openfpm::vector<void *> ptr;
	openfpm::vector<HeapMemory> packs(vcl.getProcessingUnits());

	if(vcl.getProcessUnitID() == 0)
			prc.add(1);
	if(vcl.getProcessUnitID() == 1)
			prc.add(0);
	size.add(pmem.size());
	ptr.add(mem.getPointer(0));

	// Exchange informations through processors
	vcl.sendrecvMultipleMessagesNBX(prc.size(), &size.get(0), &prc.get(0), &ptr.get(0), message_receive, &packs,
	NONE);

	//! [Exchange n vertices packed]

	if(vcl.getProcessUnitID() == 1){

		Unpack_stat ps;
		openfpm::vector<node> v;

		ExtPreAlloc<HeapMemory> mem(packs.get(0).size(),packs.get(0));

		size_t size;
		Unpacker<size_t,HeapMemory>::unpack(mem,size,ps);

314
		BOOST_REQUIRE_EQUAL(size, 3ul);
tonynsyde's avatar
tonynsyde committed
315 316 317

		v.resize(size);

318
		for(size_t i = 0; i < size; i++){
tonynsyde's avatar
tonynsyde committed
319 320 321 322
			node v_n;
			Unpacker<node,HeapMemory>::unpack(mem,v_n,ps);
			v.set(i, v_n);
		}
323 324 325
		BOOST_REQUIRE_EQUAL(v.get(0).template get<node::id>(), 0ul);
		BOOST_REQUIRE_EQUAL(v.get(1).template get<node::id>(), 1ul);
		BOOST_REQUIRE_EQUAL(v.get(2).template get<node::id>(), 2ul);
tonynsyde's avatar
tonynsyde committed
326 327 328 329 330 331 332 333 334 335 336 337
	}

	if(vcl.getProcessUnitID() == 0){

		Unpack_stat ps;
		openfpm::vector<node> v;

		ExtPreAlloc<HeapMemory> mem(packs.get(1).size(),packs.get(1));

		size_t size;
		Unpacker<size_t,HeapMemory>::unpack(mem,size,ps);

338
		BOOST_REQUIRE_EQUAL(size, 3ul);
tonynsyde's avatar
tonynsyde committed
339 340 341

		v.resize(size);

342
		for(size_t i = 0; i < size; i++){
tonynsyde's avatar
tonynsyde committed
343 344 345 346 347
			node v_n;
			Unpacker<node,HeapMemory>::unpack(mem,v_n,ps);
			v.set(i, v_n);
		}

348 349 350
		BOOST_REQUIRE_EQUAL(v.get(0).template get<node::id>(), 8ul);
		BOOST_REQUIRE_EQUAL(v.get(1).template get<node::id>(), 9ul);
		BOOST_REQUIRE_EQUAL(v.get(2).template get<node::id>(), 10ul);
tonynsyde's avatar
tonynsyde committed
351 352 353 354 355 356 357 358
	}


}

BOOST_AUTO_TEST_SUITE_END()

#endif