Distribution_unit_tests.hpp 10.7 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*
 * Distribution_unit_tests.hpp
 *
 *  Created on: Feb 27, 2016
 *      Author: i-bird
 */

#ifndef SRC_DECOMPOSITION_DISTRIBUTION_DISTRIBUTION_UNIT_TESTS_HPP_
#define SRC_DECOMPOSITION_DISTRIBUTION_DISTRIBUTION_UNIT_TESTS_HPP_

/*! \brief Set a sphere as high computation cost
 *
 * \param dist Distribution structure
 * \param gr grid info
 * \param center of the sphere
 * \param radius radius of the sphere
 * \param max_l maximum load of the processor
 * \param min_l minimum load of the processor
 *
 */
21
template<unsigned int dim, typename Distribution> void setSphereComputationCosts(Distribution & dist, grid_sm<dim, void> & gr, Point<3, float> center, float radius, size_t max_l, size_t min_l)
22
23
24
25
26
{
	float radius2 = radius * radius;
	float eq;

	// Position structure for the single vertex
27
	float pos[dim];
28

29
	for (size_t i = 0; i < dist.getNSubSubDomains(); i++)
30
31
32
33
	{
		dist.getSubSubDomainPosition(i, pos);

		eq = 0;
34
		for (size_t j = 0; j < dim; j++)
35
36
37
38
39
			eq += (pos[j] - center.get(j)) * (pos[j] - center.get(j));

		if (eq <= radius2)
		{
			dist.setComputationCost(i, max_l);
40
			dist.setMigrationCost(i, max_l * 2);
41
42
43
44
		}
		else
		{
			dist.setComputationCost(i, min_l);
45
			dist.setMigrationCost(i, min_l * 2);
46
47
48
		}

		// set Migration cost and communication cost
49
50
		for (size_t j = 0; j < dist.getNSubSubDomainNeighbors(i); j++)
			dist.setCommunicationCost(i, j, 1);
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
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
struct animal
{
	typedef boost::fusion::vector<float[2], size_t, size_t, size_t> type;

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

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

	//! The data
	type data;

	//! position property id in boost::fusion::vector
	static const unsigned int pos = 0;
	//! genre of animal property id in boost::fusion::vector
	static const unsigned int genre = 1;
	//! state property id in boost::fusion::vector
	static const unsigned int status = 2;
	//! alive time property id in boost::fusion::vector
	static const unsigned int time_a = 3;

	//! total number of properties boost::fusion::vector
	static const unsigned int max_prop = 4;

	animal()
	{
	}

	inline animal(const animal & 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];
		boost::fusion::at_c<1>(data) = boost::fusion::at_c<1>(p.data);
		boost::fusion::at_c<2>(data) = boost::fusion::at_c<2>(p.data);
		boost::fusion::at_c<3>(data) = boost::fusion::at_c<3>(p.data);
	}

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

	template<unsigned int dim, typename Mem> inline animal & operator=(const encapc<dim, animal, 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];
		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>();

		return *this;
	}

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

tonynsyde's avatar
tonynsyde committed
129
const std::string animal::attributes::name[] = { "pos", "genre", "status", "time_a", "j_repr" };
130

131
BOOST_AUTO_TEST_SUITE (Distribution_test)
132
133
134
135
136
137

BOOST_AUTO_TEST_CASE( Metis_distribution_test)
{
	Vcluster & v_cl = *global_v_cluster;

	if (v_cl.getProcessingUnits() != 3)
138
	return;
139
140

	if (v_cl.getProcessUnitID() != 0)
141
	return;
142
143
144

	//! [Initialize a Metis Cartesian graph and decompose]

145
	MetisDistribution<3, float> met_dist(v_cl);
146
147

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

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

	// Grid info
154
	grid_sm<3, void> info(sz);
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169

	// Set metis on test, It fix the seed (not required if we are not testing)
	met_dist.onTest();

	// Initialize Cart graph and decompose

	met_dist.createCartGraph(info,box);
	met_dist.decompose();

	//! [Initialize a Metis Cartesian graph and decompose]

	BOOST_REQUIRE(met_dist.getUnbalance() < 0.03);

	met_dist.write("vtk_metis_distribution.vtk");

170
	size_t b = GS_SIZE * GS_SIZE * GS_SIZE / 5;
171
172
173
174
175
176
177
178
179

	//! [Decomposition Metis with weights]

	// Initialize the weights to 1.0
	// not required, if we set ALL Computation,Migration,Communication cost
	met_dist.initWeights();

	// Change set some weight on the graph and re-decompose

180
	for (size_t i = 0; i < met_dist.getNSubSubDomains(); i++)
181
182
	{
		if (i == 0 || i == b || i == 2*b || i == 3*b || i == 4*b)
183
		met_dist.setComputationCost(i,10);
184
		else
185
		met_dist.setComputationCost(i,1);
186
187
188
189
190

		// We also show how to set some Communication and Migration cost

		met_dist.setMigrationCost(i,1);

191
192
		for (size_t j = 0; j < met_dist.getNSubSubDomainNeighbors(i); j++)
		met_dist.setCommunicationCost(i,j,1);
193
194
195
196
197
198
199
200
201
202
203
204
	}

	met_dist.decompose();

	//! [Decomposition Metis with weights]

	BOOST_REQUIRE(met_dist.getUnbalance() < 0.03);

	met_dist.write("vtk_metis_distribution_red.vtk");

	// check that match

205
	bool test = compare("vtk_metis_distribution.vtk", "src/Decomposition/Distribution/test_data/vtk_metis_distribution_test.vtk");
206
207
208
209
210
211
212
	BOOST_REQUIRE_EQUAL(true,test);

	test = compare("vtk_metis_distribution_red.vtk","src/Decomposition/Distribution/test_data/vtk_metis_distribution_red_test.vtk");
	BOOST_REQUIRE_EQUAL(true,test);

	// Copy the Metis distribution

213
	MetisDistribution<3, float> met_dist2(v_cl);
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228

	met_dist2 = met_dist;

	test = (met_dist2 == met_dist);

	BOOST_REQUIRE_EQUAL(test,true);

	// We fix the size of MetisDistribution if you are gointg to change this number
	// please check the following
	// duplicate functions
	// swap functions
	// Copy constructors
	// operator= functions
	// operator== functions

Pietro Incardona's avatar
Pietro Incardona committed
229
	BOOST_REQUIRE_EQUAL(sizeof(MetisDistribution<3,float>),568ul);
230
231
232
233
234
235
236
}

BOOST_AUTO_TEST_CASE( Parmetis_distribution_test)
{
	Vcluster & v_cl = *global_v_cluster;

	if (v_cl.getProcessingUnits() != 3)
237
	return;
238
239
240

	//! [Initialize a ParMetis Cartesian graph and decompose]

241
	ParMetisDistribution<3, float> pmet_dist(v_cl);
242
243

	// Physical domain
244
	Box<3, float> box( { 0.0, 0.0, 0.0 }, { 10.0, 10.0, 10.0 });
245
246

	// Grid info
247
	grid_sm<3, void> info( { GS_SIZE, GS_SIZE, GS_SIZE });
248
249
250
251
252

	// Initialize Cart graph and decompose
	pmet_dist.createCartGraph(info,box);

	// First create the center of the weights distribution, check it is coherent to the size of the domain
253
	Point<3, float> center( { 2.0, 2.0, 2.0 });
254
255
256
257
258
259
260
261
262
263

	// It produces a sphere of radius 2.0
	// with high computation cost (5) inside the sphere and (1) outside
	setSphereComputationCosts(pmet_dist, info, center, 2.0f, 5ul, 1ul);

	// first decomposition
	pmet_dist.decompose();

	//! [Initialize a ParMetis Cartesian graph and decompose]

Pietro Incardona's avatar
Pietro Incardona committed
264
	if (v_cl.getProcessUnitID() == 0)
265
266
	{
		// write the first decomposition
Pietro Incardona's avatar
Pietro Incardona committed
267
		pmet_dist.write("vtk_parmetis_distribution_0");
268

Pietro Incardona's avatar
Pietro Incardona committed
269
		bool test = compare(std::to_string(v_cl.getProcessUnitID()) + "_vtk_parmetis_distribution_0.vtk","src/Decomposition/Distribution/test_data/" + std::to_string(v_cl.getProcessUnitID()) + "_vtk_parmetis_distribution_0_test.vtk");
270
271
272
273
274
275
276
277
		BOOST_REQUIRE_EQUAL(true,test);
	}

	//! [refine with parmetis the decomposition]

	float stime = 0.0, etime = 10.0, tstep = 0.1;

	// Shift of the sphere at each iteration
278
	Point<3, float> shift( { tstep, tstep, tstep });
279
280
281
282
283
284

	size_t iter = 1;

	for(float t = stime; t < etime; t = t + tstep, iter++)
	{
		if(t < etime/2)
285
		center += shift;
286
		else
287
		center -= shift;
288
289
290
291
292
293
294
295
296
297
298
299

		setSphereComputationCosts(pmet_dist, info, center, 2.0f, 5, 1);

		// With some regularity refine and write the parmetis distribution
		if ((size_t)iter % 10 == 0)
		{
			pmet_dist.refine();

			if (v_cl.getProcessUnitID() == 0)
			{
				std::stringstream str;
				str << "vtk_parmetis_distribution_" << iter;
Pietro Incardona's avatar
Pietro Incardona committed
300
				pmet_dist.write(str.str());
301
302

				// Check
Pietro Incardona's avatar
Pietro Incardona committed
303
				bool test = compare(std::to_string(v_cl.getProcessUnitID()) + "_" + str.str() + ".vtk", "src/Decomposition/Distribution/test_data/" + std::to_string(v_cl.getProcessUnitID()) + "_" + str.str() + "_test.vtk");
304
305
306
307
308
309
				BOOST_REQUIRE_EQUAL(true,test);
			}
		}
	}

	//! [refine with parmetis the decomposition]
Pietro Incardona's avatar
Pietro Incardona committed
310
311

	BOOST_REQUIRE_EQUAL(sizeof(MetisDistribution<3,float>),568ul);
312
313
}

314
BOOST_AUTO_TEST_CASE( DistParmetis_distribution_test)
315
{
316
317
318
	Vcluster & v_cl = *global_v_cluster;

	if (v_cl.getProcessingUnits() != 3)
319
		return;
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

	//! [Initialize a ParMetis Cartesian graph and decompose]

	DistParMetisDistribution<3, float> pmet_dist(v_cl);

	// Physical domain
	Box<3, float> box( { 0.0, 0.0, 0.0 }, { 10.0, 10.0, 10.0 });

	// Grid info
	grid_sm<3, void> info( { GS_SIZE, GS_SIZE, GS_SIZE });

	// Initialize Cart graph and decompose
	pmet_dist.createCartGraph(info,box);

	// First create the center of the weights distribution, check it is coherent to the size of the domain
	Point<3, float> center( { 2.0, 2.0, 2.0 });

	// It produces a sphere of radius 2.0
	// with high computation cost (5) inside the sphere and (1) outside
	setSphereComputationCosts(pmet_dist, info, center, 2.0f, 5ul, 1ul);

	// first decomposition
	pmet_dist.decompose();

	//! [Initialize a ParMetis Cartesian graph and decompose]

	// write the first decomposition
Pietro Incardona's avatar
Pietro Incardona committed
347
	pmet_dist.write("vtk_dist_parmetis_distribution_0");
348
349
350
351
352
353

	if (v_cl.getProcessingUnits() == 0)
	{
		bool test = compare("vtk_dist_parmetis_distribution_0.vtk","src/Decomposition/Distribution/test_data/vtk_dist_parmetis_distribution_0_test.vtk");
		BOOST_REQUIRE_EQUAL(true,test);
	}
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
	//! [refine with dist_parmetis the decomposition]

	float stime = 0.0, etime = 10.0, tstep = 0.1;

	// Shift of the sphere at each iteration
	Point<3, float> shift( { tstep, tstep, tstep });

	size_t iter = 1;

	for(float t = stime; t < etime; t = t + tstep, iter++)
	{
		if(t < etime/2)
		center += shift;
		else
		center -= shift;

		setSphereComputationCosts(pmet_dist, info, center, 2.0f, 5, 1);

		// With some regularity refine and write the parmetis distribution
		if ((size_t)iter % 10 == 0)
		{
			pmet_dist.refine();

			std::stringstream str;
			str << "vtk_dist_parmetis_distribution_" << iter;
Pietro Incardona's avatar
Pietro Incardona committed
380
			pmet_dist.write(str.str());
381
382
383
384
385
386
387
388
389
390
391
392

			// Check
			if (v_cl.getProcessUnitID() == 0)
			{
				bool test = compare(str.str() + ".vtk",std::string("src/Decomposition/Distribution/test_data/") + str.str() + "_test.vtk");
				BOOST_REQUIRE_EQUAL(true,test);
			}
		}
	}

	//! [refine with dist_parmetis the decomposition]

393
	BOOST_REQUIRE_EQUAL(sizeof(DistParMetisDistribution<3,float>),1520ul);
394
395
}

396
397
398
399
400
401
void print_test_v(std::string test, size_t sz)
{
	if (global_v_cluster->getProcessUnitID() == 0)
		std::cout << test << " " << sz << "\n";
}

402
403
404
BOOST_AUTO_TEST_SUITE_END()

#endif /* SRC_DECOMPOSITION_DISTRIBUTION_DISTRIBUTION_UNIT_TESTS_HPP_ */