dec_optimizer_unit_test.hpp 5.48 KB
Newer Older
incardon's avatar
incardon committed
1 2 3 4
/*
 * dec_optimize.hpp
 *
 *  Created on: Jan 16, 2015
incardon's avatar
incardon committed
5
 *      Author: Pietro Incardona
incardon's avatar
incardon committed
6 7 8 9 10 11 12
 */

#ifndef DEC_OPTIMIZE_HPP_
#define DEC_OPTIMIZE_HPP_


#include "Graph/CartesianGraphFactory.hpp"
incardon's avatar
incardon committed
13
#include "Graph/map_graph.hpp"
14
#include "Decomposition/Distribution/metis_util.hpp"
incardon's avatar
incardon committed
15
#include "dec_optimizer.hpp"
16
#include "util/SimpleRNG.hpp"
incardon's avatar
incardon committed
17

incardon's avatar
incardon committed
18 19 20 21 22
#undef GS_SIZE
#define GS_SIZE 8

BOOST_AUTO_TEST_SUITE( dec_optimizer_test )

23
BOOST_AUTO_TEST_CASE( dec_optimizer_test_use_np)
incardon's avatar
incardon committed
24 25 26 27 28
{
	CartesianGraphFactory<3,Graph_CSR<nm_v,nm_e>> g_factory;
	CartesianGraphFactory<3,Graph_CSR<nm_part_v,nm_part_e>> g_factory_part;

	// Cartesian grid
incardon's avatar
incardon committed
29
	size_t sz[3] = {GS_SIZE,GS_SIZE,GS_SIZE};
incardon's avatar
incardon committed
30 31 32 33

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

34 35 36
	// Boundary conditions, non periodic
	size_t bc[] = {NON_PERIODIC,NON_PERIODIC,NON_PERIODIC};

incardon's avatar
incardon committed
37
	// Graph to decompose
38
	Graph_CSR<nm_v,nm_e> g = g_factory.construct<nm_e::communication,NO_VERTEX_ID,float,2,0>(sz,box,bc);
incardon's avatar
incardon committed
39 40

	// Processor graph
41
	Graph_CSR<nm_part_v,nm_part_e> gp = g_factory_part.construct<NO_EDGE,NO_VERTEX_ID,float,2>(sz,box,bc);
incardon's avatar
incardon committed
42 43 44 45 46 47 48 49 50 51 52

	// Convert the graph to metis
	Metis<Graph_CSR<nm_v,nm_e>> met(g,16);

	// decompose
	met.decompose<nm_part_v::id>(gp);
	met.decompose<nm_v::id>();

	// optimize
	dec_optimizer<3,Graph_CSR<nm_v,nm_e>> d_o(g,sz);

53 54
	Ghost<3,size_t> ghe(1);

incardon's avatar
incardon committed
55
	grid_key_dx<3> keyZero(0,0,0);
56
	d_o.optimize<nm_v::sub_id,nm_v::id>(keyZero,g,ghe,bc);
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
}

BOOST_AUTO_TEST_CASE( dec_optimizer_test_use_p)
{
	CartesianGraphFactory<3,Graph_CSR<nm_v,nm_e>> g_factory;
	CartesianGraphFactory<3,Graph_CSR<nm_part_v,nm_part_e>> g_factory_part;

	// Cartesian grid
	size_t sz[3] = {GS_SIZE,GS_SIZE,GS_SIZE};

	//! Grid info
	grid_sm<3,void> gs(sz);

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

	// Boundary conditions, non periodic
	size_t bc[] = {PERIODIC,PERIODIC,PERIODIC};

	// Graph to decompose
77
	Graph_CSR<nm_v,nm_e> g = g_factory.construct<nm_e::communication,NO_VERTEX_ID,float,2,0>(sz,box,bc);
78 79

	// Processor graph
80
	Graph_CSR<nm_part_v,nm_part_e> gp = g_factory_part.construct<NO_EDGE,NO_VERTEX_ID,float,2>(sz,box,bc);
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98

	bool p[3];

	// Divide in 8 parts your graph

	// decompose
	for (size_t i = 0 ; i < GS_SIZE ; i++)
	{
		p[0] = (i < GS_SIZE/2)?false:true;
		for (size_t j = 0 ; j < GS_SIZE ; j++)
		{
			p[1] = (j < GS_SIZE/2)?false:true;
			for (size_t k = 0 ; k < GS_SIZE ; k++)
			{
				p[2] = (k < GS_SIZE/2)?false:true;
				size_t id = 4*p[2] + 2*p[1] + p[0];

				grid_key_dx<3> key(i,j,k);
99 100
				gp.vertex(gs.LinId(key)).get<nm_part_v::id>() = id;
				g.vertex(gs.LinId(key)).get<nm_v::id>() = id;
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
			}
		}
	}

	// optimize
	dec_optimizer<3,Graph_CSR<nm_v,nm_e>> d_o(g,sz);

	grid_key_dx<3> keyZero(0,0,0);

	// Set of sub-domain produced by dec-optimizer
	openfpm::vector<Box<3,size_t>> dec_o;

	// For each sub-domain check the neighborhood processors
	openfpm::vector< openfpm::vector<size_t> > box_nn_processor;

116 117
	Ghost<3,size_t> ghe(1);

118
	// gp,p_id,loc_box,box_nn_processor,bc
119
	d_o.optimize<nm_v::sub_id,nm_v::id>(g,-1,dec_o,box_nn_processor,ghe,bc);
120 121 122 123 124

	BOOST_REQUIRE_EQUAL(box_nn_processor.size(),8ul);

	for(size_t i = 0 ; i < box_nn_processor.size() ; i++)
	{
Pietro Incardona's avatar
Pietro Incardona committed
125
		BOOST_REQUIRE_EQUAL(box_nn_processor.get(i).size(),8ul);
126 127 128 129 130
		for (size_t j = 0 ; j < box_nn_processor.get(i).size(); j++)
		{
			BOOST_REQUIRE(box_nn_processor.get(i).get(j) < 8);
		}
	}
incardon's avatar
incardon committed
131

132
	// check
incardon's avatar
incardon committed
133 134
}

135 136 137
BOOST_AUTO_TEST_CASE( dec_optimizer_disconnected_subdomains_np)
{
	// Vcluster
138
	Vcluster & vcl = create_vcluster();
139

140
	// Test for only 3 processors
incardon's avatar
incardon committed
141
	if (vcl.getProcessingUnits() != 3)
142 143
		return;

144 145 146 147 148 149 150 151 152 153 154 155
	CartesianGraphFactory<2,Graph_CSR<nm_v,nm_e>> g_factory;

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

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

	// Boundary conditions, non periodic
	size_t bc[] = {NON_PERIODIC,NON_PERIODIC};

	// Graph to decompose
156
	Graph_CSR<nm_v,nm_e> g = g_factory.construct<nm_e::communication,NO_VERTEX_ID,float,1,0>(sz,box,bc);
157 158 159 160 161 162 163 164 165

	SimpleRNG rng;

	auto vit = g.getVertexIterator();

	while (vit.isNext())
	{
		auto vk = vit.get();

166 167
		g.vertex_p<nm_v::proc_id>(vk) = rng.GetUniform() * 2.9999;
		g.vertex_p<nm_v::sub_id>(vk) = 100;
168 169 170 171 172 173 174 175 176 177 178 179 180

		++vit;
	}

	// optimize
	dec_optimizer<2,Graph_CSR<nm_v,nm_e>> d_o(g,sz);

	// set of Boxes produced by the decomposition optimizer
	openfpm::vector<::Box<2, size_t>> loc_box;

	//! for each sub-domain, contain the list of the neighborhood processors
	openfpm::vector<openfpm::vector<long unsigned int> > box_nn_processor;

181 182
	Ghost<2,size_t> ghe(1);
	d_o.optimize<nm_v::sub_id, nm_v::proc_id>(g, vcl.getProcessUnitID(), loc_box, box_nn_processor,ghe,bc);
183 184 185 186

	std::stringstream str_g;
	str_g << "dec_optimizer_disc_graph" << vcl.getProcessUnitID() << ".vtk";
	std::stringstream str_gt;
Pietro Incardona's avatar
Pietro Incardona committed
187
	str_gt << "src/Decomposition/Distribution/test_data/dec_optimizer_disc_graph" << vcl.getProcessUnitID() << "_test.vtk";
188 189 190 191

	std::stringstream str_s;
	str_s << "dec_optimizer_disc_sub" << vcl.getProcessUnitID() << ".vtk";
	std::stringstream str_st;
Pietro Incardona's avatar
Pietro Incardona committed
192
	str_st << "src/Decomposition/Distribution/test_data/dec_optimizer_disc_sub" << vcl.getProcessUnitID() << "_test.vtk";
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207

	VTKWriter<Graph_CSR<nm_v,nm_e>,VTK_GRAPH> wrt(g);
	wrt.write("dec_optimizer_disc_graph" + std::to_string(vcl.getProcessUnitID()) + ".vtk");

	VTKWriter<openfpm::vector<::Box<2, size_t>>, VECTOR_BOX> vtk_box1;
	vtk_box1.add(loc_box);
	vtk_box1.write("dec_optimizer_disc_sub" + std::to_string(vcl.getProcessUnitID()) + std::string(".vtk"));

	bool test = compare(str_g.str(), str_gt.str());
	BOOST_REQUIRE_EQUAL(true,test);

	test = compare(str_s.str(),str_st.str());
	BOOST_REQUIRE_EQUAL(true,test);
}

incardon's avatar
incardon committed
208 209 210 211
BOOST_AUTO_TEST_SUITE_END()


#endif /* DEC_OPTIMIZE_HPP_ */