grid_dist_id_unit_test.hpp 27.7 KB
Newer Older
incardon's avatar
incardon committed
1
2
3
4
5
6
7
8
#ifndef GRID_DIST_UNIT_TEST_HPP
#define GRID_DIST_UNIT_TEST_HPP

#include "grid_dist_id.hpp"
#include "data_type/scalar.hpp"

BOOST_AUTO_TEST_SUITE( grid_dist_id_test )

incardon's avatar
incardon committed
9
void print_test(std::string test, size_t sz)
incardon's avatar
incardon committed
10
{
incardon's avatar
incardon committed
11
12
	if (global_v_cluster->getProcessUnitID() == 0)
		std::cout << test << " " << sz << "\n";
incardon's avatar
incardon committed
13
14
}

incardon's avatar
incardon committed
15
BOOST_AUTO_TEST_CASE( grid_dist_id_domain_grid_unit_converter_test)
incardon's avatar
incardon committed
16
{
17
18
19
	// Domain
	Box<2,float> domain({0.0,0.0},{1.0,1.0});

incardon's avatar
incardon committed
20
21
22
	// Initialize the global VCluster
	init_global_v_cluster(&boost::unit_test::framework::master_test_suite().argc,&boost::unit_test::framework::master_test_suite().argv);

incardon's avatar
incardon committed
23
	Vcluster & v_cl = *global_v_cluster;
incardon's avatar
incardon committed
24

incardon's avatar
incardon committed
25
26
27
28
	// Skip this test on big scale
	if (v_cl.getProcessingUnits() >= 32)
		return;

incardon's avatar
incardon committed
29
	// Test several grid dimensions
incardon's avatar
incardon committed
30

incardon's avatar
incardon committed
31
	for (size_t k = 1024 ; k >= 2 ; k--)
incardon's avatar
incardon committed
32
	{
incardon's avatar
incardon committed
33
		BOOST_TEST_CHECKPOINT( "Testing grid k=" << k );
incardon's avatar
incardon committed
34

incardon's avatar
incardon committed
35
36
37
38
		// grid size
		size_t sz[2];
		sz[0] = k;
		sz[1] = k;
incardon's avatar
incardon committed
39

incardon's avatar
incardon committed
40
41
		// Ghost
		Ghost<2,float> g(0.01);
incardon's avatar
incardon committed
42

incardon's avatar
incardon committed
43
44
		// Distributed grid with id decomposition
		grid_dist_id<2, float, scalar<float>, CartDecomposition<2,float>> g_dist(sz,domain,g);
incardon's avatar
incardon committed
45

incardon's avatar
incardon committed
46
47
		// get the decomposition
		auto & dec = g_dist.getDecomposition();
incardon's avatar
incardon committed
48

incardon's avatar
incardon committed
49
50
51
52
		// check the consistency of the decomposition
		bool val = dec.check_consistency();
		BOOST_REQUIRE_EQUAL(val,true);

incardon's avatar
incardon committed
53
54
55
		// for each local volume
		// Get the number of local grid needed
		size_t n_grid = dec.getNLocalHyperCube();
incardon's avatar
incardon committed
56

incardon's avatar
incardon committed
57
58
59
60
61
62
63
		size_t vol = 0;

		// Allocate the grids
		for (size_t i = 0 ; i < n_grid ; i++)
		{
			// Get the local hyper-cube
			SpaceBox<2,float> sub = dec.getLocalHyperCube(i);
incardon's avatar
incardon committed
64

incardon's avatar
incardon committed
65
			Box<2,size_t> g_box = g_dist.getCellDecomposer().convertDomainSpaceIntoGridUnits(sub);
incardon's avatar
incardon committed
66

incardon's avatar
incardon committed
67
68
			vol += g_box.getVolumeKey();
		}
incardon's avatar
incardon committed
69

70
		v_cl.sum(vol);
incardon's avatar
incardon committed
71
72
73
74
75
76
		v_cl.execute();

		BOOST_REQUIRE_EQUAL(vol,sz[0]*sz[1]);
	}
}

incardon's avatar
incardon committed
77
78
79
80
void Test2D_sub(const Box<2,float> & domain, long int k)
{
	long int big_step = k / 30;
	big_step = (big_step == 0)?1:big_step;
incardon's avatar
incardon committed
81
	long int small_step = 21;
incardon's avatar
incardon committed
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

	// this test is only performed when the number of processor is <= 32
	if (global_v_cluster->getProcessingUnits() > 32)
		return;

	print_test( "Testing 2D grid sub iterator k<=",k);

	// 2D test
	for ( ; k >= 2 ; k-= (k > 2*big_step)?big_step:small_step )
	{
		BOOST_TEST_CHECKPOINT( "Testing 2D grid k=" << k );

		//! [Create and access a distributed grid]

		// grid size
		size_t sz[2];
		sz[0] = k;
		sz[1] = k;

		float factor = pow(global_v_cluster->getProcessingUnits()/2.0f,1.0f/2.0f);

		// Ghost
		Ghost<2,float> g(0.01 / factor);

		// Distributed grid with id decomposition
		grid_dist_id<2, float, scalar<float>, CartDecomposition<2,float>> g_dist(sz,domain,g);

		// check the consistency of the decomposition
		bool val = g_dist.getDecomposition().check_consistency();
		BOOST_REQUIRE_EQUAL(val,true);

incardon's avatar
incardon committed
113
114
		size_t count;

incardon's avatar
incardon committed
115
116
117
		// Grid sm
		grid_sm<2,void> info(sz);

incardon's avatar
incardon committed
118
		{
incardon's avatar
incardon committed
119
120
121
122
123
124
		//! [Usage of a sub_grid iterator]

		grid_key_dx<2> one(1,1);
		grid_key_dx<2> one_end(k-2,k-2);

		bool check = true;
incardon's avatar
incardon committed
125
126
127
		count = 0;

		// get the sub-domain iterator
incardon's avatar
incardon committed
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
		auto dom = g_dist.getSubDomainIterator(one,one_end);

		while (dom.isNext())
		{
			auto key = dom.get();
			auto key_g = g_dist.getGKey(key);

			// key_g should never be 1 or k-1
			check &= (key_g.get(0) == 0 || key_g.get(0) == k-1)?false:true;
			check &= (key_g.get(1) == 0 || key_g.get(1) == k-1)?false:true;

			g_dist.template get<0>(key) = info.LinId(key_g);

			// Count the point
			count++;

			++dom;
		}

		BOOST_REQUIRE_EQUAL(check,true);

		//! [Usage of a sub_grid iterator]

incardon's avatar
incardon committed
151
152
		}

incardon's avatar
incardon committed
153
154
155
156
157
158
159
160
		// Get the virtual cluster machine
		Vcluster & vcl = g_dist.getVC();

		// reduce
		vcl.sum(count);
		vcl.execute();

		// Check
Pietro Incardona's avatar
Pietro Incardona committed
161
		BOOST_REQUIRE_EQUAL(count,(size_t)(k-2)*(k-2));
incardon's avatar
incardon committed
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183

		// check with a 1x1 square

		{

		grid_key_dx<2> one(k/2,k/2);
		grid_key_dx<2> one_end(k/2,k/2);

		count = 0;

		// get the sub-domain iterator
		auto dom = g_dist.getSubDomainIterator(one,one_end);

		while (dom.isNext())
		{
			auto key = dom.get();
			auto key_g = g_dist.getGKey(key);

			// key_g
			BOOST_REQUIRE_EQUAL(key_g.get(0),k/2);
			BOOST_REQUIRE_EQUAL(key_g.get(1),k/2);

incardon's avatar
incardon committed
184
			auto key_s_it = dom.getGKey(key);
incardon's avatar
incardon committed
185
186
187
188
189
190
191
192
193
194
195
196
197
198

			BOOST_REQUIRE_EQUAL(key_g.get(0),key_s_it.get(0));
			BOOST_REQUIRE_EQUAL(key_g.get(1),key_s_it.get(1));

			// Count the point
			count++;

			++dom;
		}

		// reduce
		vcl.sum(count);
		vcl.execute();

Pietro Incardona's avatar
Pietro Incardona committed
199
		BOOST_REQUIRE_EQUAL(count,1ul);
incardon's avatar
incardon committed
200
		}
incardon's avatar
incardon committed
201
202
203
	}
}

incardon's avatar
incardon committed
204
void Test2D(const Box<2,float> & domain, long int k)
incardon's avatar
incardon committed
205
{
incardon's avatar
incardon committed
206
207
	long int big_step = k / 30;
	big_step = (big_step == 0)?1:big_step;
incardon's avatar
incardon committed
208
	long int small_step = 21;
incardon's avatar
incardon committed
209

incardon's avatar
incardon committed
210
211
	print_test( "Testing 2D grid k<=",k);

incardon's avatar
incardon committed
212
	// 2D test
incardon's avatar
incardon committed
213
	for ( ; k >= 2 ; k-= (k > 2*big_step)?big_step:small_step )
incardon's avatar
incardon committed
214
215
	{
		BOOST_TEST_CHECKPOINT( "Testing 2D grid k=" << k );
216
217
218

		//! [Create and access a distributed grid]

incardon's avatar
incardon committed
219
220
221
222
		// grid size
		size_t sz[2];
		sz[0] = k;
		sz[1] = k;
incardon's avatar
incardon committed
223

incardon's avatar
incardon committed
224
225
		float factor = pow(global_v_cluster->getProcessingUnits()/2.0f,1.0f/2.0f);

incardon's avatar
incardon committed
226
		// Ghost
227
		Ghost<2,float> g(0.01 / factor);
incardon's avatar
incardon committed
228

incardon's avatar
incardon committed
229
230
231
		// Distributed grid with id decomposition
		grid_dist_id<2, float, scalar<float>, CartDecomposition<2,float>> g_dist(sz,domain,g);

incardon's avatar
incardon committed
232
233
234
235
		// check the consistency of the decomposition
		bool val = g_dist.getDecomposition().check_consistency();
		BOOST_REQUIRE_EQUAL(val,true);

incardon's avatar
incardon committed
236
237
238
239
240
241
242
243
244
245
246
247
		// Grid sm
		grid_sm<2,void> info(sz);

		// get the domain iterator
		size_t count = 0;

		auto dom = g_dist.getDomainIterator();

		while (dom.isNext())
		{
			auto key = dom.get();
			auto key_g = g_dist.getGKey(key);
incardon's avatar
incardon committed
248

incardon's avatar
incardon committed
249
250
251
252
253
254
255
256
			g_dist.template get<0>(key) = info.LinId(key_g);

			// Count the point
			count++;

			++dom;
		}

257
258
		//! [Create and access a distributed grid]

incardon's avatar
incardon committed
259
260
261
262
		// Get the virtual cluster machine
		Vcluster & vcl = g_dist.getVC();

		// reduce
263
		vcl.sum(count);
incardon's avatar
incardon committed
264
265
266
		vcl.execute();

		// Check
Pietro Incardona's avatar
Pietro Incardona committed
267
		BOOST_REQUIRE_EQUAL(count,(size_t)k*k);
incardon's avatar
incardon committed
268
269
270

		auto dom2 = g_dist.getDomainIterator();

Pietro Incardona's avatar
Pietro Incardona committed
271
272
273
		grid_key_dx<2> start = dom2.getStart();
		grid_key_dx<2> stop = dom2.getStop();

Pietro Incardona's avatar
Pietro Incardona committed
274
275
		BOOST_REQUIRE_EQUAL((long int)stop.get(0),(long int)g_dist.size(0)-1);
		BOOST_REQUIRE_EQUAL((long int)stop.get(1),(long int)g_dist.size(1)-1);
Pietro Incardona's avatar
Pietro Incardona committed
276
277
278
279

		BOOST_REQUIRE_EQUAL(start.get(0),0);
		BOOST_REQUIRE_EQUAL(start.get(1),0);

incardon's avatar
incardon committed
280
281
		bool match = true;

incardon's avatar
incardon committed
282
283
284
285
286
287
		// check that the grid store the correct information
		while (dom2.isNext())
		{
			auto key = dom2.get();
			auto key_g = g_dist.getGKey(key);

incardon's avatar
incardon committed
288
			match &= (g_dist.template get<0>(key) == info.LinId(key_g))?true:false;
incardon's avatar
incardon committed
289
290
291
292

			++dom2;
		}

incardon's avatar
incardon committed
293
294
		BOOST_REQUIRE_EQUAL(match,true);

incardon's avatar
incardon committed
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
		g_dist.template ghost_get<0>();

		// check that the communication is correctly completed

		auto domg = g_dist.getDomainGhostIterator();

		// check that the grid with the ghost past store the correct information
		while (domg.isNext())
		{
			auto key = domg.get();
			auto key_g = g_dist.getGKey(key);

			// In this case the boundary condition are non periodic
			if (g_dist.isInside(key_g))
			{
incardon's avatar
incardon committed
310
				match &= (g_dist.template get<0>(key),info.LinId(key_g));
incardon's avatar
incardon committed
311
312
313
314
			}

			++domg;
		}
incardon's avatar
incardon committed
315
316
317
	}
}

incardon's avatar
incardon committed
318
319
320
321
void Test3D_sub(const Box<3,float> & domain, long int k)
{
	long int big_step = k / 30;
	big_step = (big_step == 0)?1:big_step;
incardon's avatar
incardon committed
322
	long int small_step = 21;
incardon's avatar
incardon committed
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
355
356
357
358
359

	// this test is only performed when the number of processor is <= 32
	if (global_v_cluster->getProcessingUnits() > 32)
		return;

	print_test( "Testing 3D grid sub k<=",k);

	// 3D test
	for ( ; k >= 2 ; k-= (k > 2*big_step)?big_step:small_step )
	{
		BOOST_TEST_CHECKPOINT( "Testing 3D grid sub k=" << k );

		// grid size
		size_t sz[3];
		sz[0] = k;
		sz[1] = k;
		sz[2] = k;

		// factor
		float factor = pow(global_v_cluster->getProcessingUnits()/2.0f,1.0f/3.0f);

		// Ghost
		Ghost<3,float> g(0.01 / factor);

		// Distributed grid with id decomposition
		grid_dist_id<3, float, scalar<float>, CartDecomposition<3,float>> g_dist(sz,domain,g);

		// check the consistency of the decomposition
		bool val = g_dist.getDecomposition().check_consistency();
		BOOST_REQUIRE_EQUAL(val,true);

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

		// get the domain iterator
		size_t count = 0;

incardon's avatar
incardon committed
360
361
362
363
364
		grid_key_dx<3> one(1,1,1);
		grid_key_dx<3> one_end(k-2,k-2,k-2);

		// Sub-domain iterator
		auto dom = g_dist.getSubDomainIterator(one,one_end);
incardon's avatar
incardon committed
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386

		while (dom.isNext())
		{
			auto key = dom.get();
			auto key_g = g_dist.getGKey(key);

			g_dist.template get<0>(key) = info.LinId(key_g);

			// Count the point
			count++;

			++dom;
		}

		// Get the virtual cluster machine
		Vcluster & vcl = g_dist.getVC();

		// reduce
		vcl.sum(count);
		vcl.execute();

		// Check
Pietro Incardona's avatar
Pietro Incardona committed
387
		BOOST_REQUIRE_EQUAL(count,(size_t)(k-2)*(k-2)*(k-2));
incardon's avatar
incardon committed
388

incardon's avatar
incardon committed
389
		// check with a 1x1x1 square
incardon's avatar
incardon committed
390
391
		{

incardon's avatar
incardon committed
392
393
		grid_key_dx<3> one(k/2,k/2,k/2);
		grid_key_dx<3> one_end(k/2,k/2,k/2);
incardon's avatar
incardon committed
394

incardon's avatar
incardon committed
395
		count = 0;
incardon's avatar
incardon committed
396

incardon's avatar
incardon committed
397
398
		// get the sub-domain iterator
		auto dom = g_dist.getSubDomainIterator(one,one_end);
incardon's avatar
incardon committed
399

incardon's avatar
incardon committed
400
401
402
403
		while (dom.isNext())
		{
			auto key = dom.get();
			auto key_g = g_dist.getGKey(key);
incardon's avatar
incardon committed
404

incardon's avatar
incardon committed
405
406
407
408
			// key_g
			BOOST_REQUIRE_EQUAL(key_g.get(0),k/2);
			BOOST_REQUIRE_EQUAL(key_g.get(1),k/2);
			BOOST_REQUIRE_EQUAL(key_g.get(2),k/2);
incardon's avatar
incardon committed
409

incardon's avatar
incardon committed
410
			auto key_s_it = dom.getGKey(key);
incardon's avatar
incardon committed
411

incardon's avatar
incardon committed
412
413
414
			BOOST_REQUIRE_EQUAL(key_g.get(0),key_s_it.get(0));
			BOOST_REQUIRE_EQUAL(key_g.get(1),key_s_it.get(1));
			BOOST_REQUIRE_EQUAL(key_g.get(2),key_s_it.get(2));
incardon's avatar
incardon committed
415

incardon's avatar
incardon committed
416
417
			// Count the point
			count++;
incardon's avatar
incardon committed
418

incardon's avatar
incardon committed
419
			++dom;
incardon's avatar
incardon committed
420
421
		}

incardon's avatar
incardon committed
422
423
424
		// reduce
		vcl.sum(count);
		vcl.execute();
incardon's avatar
incardon committed
425

Pietro Incardona's avatar
Pietro Incardona committed
426
		BOOST_REQUIRE_EQUAL(count,1ul);
incardon's avatar
incardon committed
427
		}
incardon's avatar
incardon committed
428
429
430
	}
}

incardon's avatar
incardon committed
431
432
void Test3D(const Box<3,float> & domain, long int k)
{
incardon's avatar
incardon committed
433
434
	long int big_step = k / 30;
	big_step = (big_step == 0)?1:big_step;
incardon's avatar
incardon committed
435
	long int small_step = 21;
incardon's avatar
incardon committed
436

incardon's avatar
incardon committed
437
438
	print_test( "Testing 3D grid k<=",k);

incardon's avatar
incardon committed
439
	// 3D test
incardon's avatar
incardon committed
440
	for ( ; k >= 2 ; k-= (k > 2*big_step)?big_step:small_step )
incardon's avatar
incardon committed
441
442
443
444
445
446
447
448
449
	{
		BOOST_TEST_CHECKPOINT( "Testing 3D grid k=" << k );

		// grid size
		size_t sz[3];
		sz[0] = k;
		sz[1] = k;
		sz[2] = k;

incardon's avatar
incardon committed
450
451
452
		// factor
		float factor = pow(global_v_cluster->getProcessingUnits()/2.0f,1.0f/3.0f);

incardon's avatar
incardon committed
453
		// Ghost
incardon's avatar
incardon committed
454
		Ghost<3,float> g(0.01 / factor);
incardon's avatar
incardon committed
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487

		// Distributed grid with id decomposition
		grid_dist_id<3, float, scalar<float>, CartDecomposition<3,float>> g_dist(sz,domain,g);

		// check the consistency of the decomposition
		bool val = g_dist.getDecomposition().check_consistency();
		BOOST_REQUIRE_EQUAL(val,true);

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

		// get the domain iterator
		size_t count = 0;

		auto dom = g_dist.getDomainIterator();

		while (dom.isNext())
		{
			auto key = dom.get();
			auto key_g = g_dist.getGKey(key);

			g_dist.template get<0>(key) = info.LinId(key_g);

			// Count the point
			count++;

			++dom;
		}

		// Get the virtual cluster machine
		Vcluster & vcl = g_dist.getVC();

		// reduce
488
		vcl.sum(count);
incardon's avatar
incardon committed
489
490
491
		vcl.execute();

		// Check
Pietro Incardona's avatar
Pietro Incardona committed
492
		BOOST_REQUIRE_EQUAL(count,(size_t)k*k*k);
incardon's avatar
incardon committed
493

incardon's avatar
incardon committed
494
495
		bool match = true;

incardon's avatar
incardon committed
496
497
498
499
500
501
502
503
		auto dom2 = g_dist.getDomainIterator();

		// check that the grid store the correct information
		while (dom2.isNext())
		{
			auto key = dom2.get();
			auto key_g = g_dist.getGKey(key);

incardon's avatar
incardon committed
504
			match &= (g_dist.template get<0>(key) == info.LinId(key_g))?true:false;
incardon's avatar
incardon committed
505
506
507
508

			++dom2;
		}

incardon's avatar
incardon committed
509
510
		BOOST_REQUIRE_EQUAL(match,true);

511
512
		//! [Synchronize the ghost and check the information]

incardon's avatar
incardon committed
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
		g_dist.template ghost_get<0>();

		// check that the communication is correctly completed

		auto domg = g_dist.getDomainGhostIterator();

		// check that the grid with the ghost past store the correct information
		while (domg.isNext())
		{
			auto key = domg.get();
			auto key_g = g_dist.getGKey(key);

			// In this case the boundary condition are non periodic
			if (g_dist.isInside(key_g))
			{
incardon's avatar
incardon committed
528
				match &= (g_dist.template get<0>(key) == info.LinId(key_g))?true:false;
incardon's avatar
incardon committed
529
530
531
532
			}

			++domg;
		}
incardon's avatar
incardon committed
533
534

		BOOST_REQUIRE_EQUAL(match,true);
535
536

		//! [Synchronize the ghost and check the information]
incardon's avatar
incardon committed
537
	}
incardon's avatar
incardon committed
538
539
}

incardon's avatar
incardon committed
540
541
542
543
544
545

void Test3D_gg(const Box<3,float> & domain, long int k, long int gk)
{
	long int big_step = k / 30;
	big_step = (big_step == 0)?1:big_step;

incardon's avatar
incardon committed
546
547
548
549
	// this test is only performed when the number of processor is <= 32
	if (global_v_cluster->getProcessingUnits() > 32)
		return;

incardon's avatar
incardon committed
550
551
552
	print_test( "Testing 3D grid k<=",k);

	// 3D test
553
	for ( ; k > 64 ; k /= 2 )
incardon's avatar
incardon committed
554
555
556
557
558
559
560
561
562
563
	{
		BOOST_TEST_CHECKPOINT( "Testing 3D grid ghost integer k=" << k );

		// grid size
		size_t sz[3];
		sz[0] = k;
		sz[1] = k;
		sz[2] = k;

		// Ghost
564
		Ghost<3,long int> g(gk);
incardon's avatar
incardon committed
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580

		// Distributed grid with id decomposition
		grid_dist_id<3, float, scalar<float>, CartDecomposition<3,float>> g_dist(sz,domain,g);

		// check the consistency of the decomposition
		bool val = g_dist.getDecomposition().check_consistency();
		BOOST_REQUIRE_EQUAL(val,true);

		auto lg = g_dist.getLocalGridsInfo();

		// for each local grid check that the border is 1 point
		// (Warning this property can only be ensured with k is a multiple of 2)
		// in the other case it will be mostly like that but cannot be ensured

		for (size_t i = 0 ; i < lg.size() ; i++)
		{
incardon's avatar
incardon committed
581
582
583
584
585
			for (size_t j = 0 ; j < 3 ; j++)
			{
				BOOST_REQUIRE(lg.get(i).Dbox.getLow(j) >= gk);
				BOOST_REQUIRE((lg.get(i).GDbox.getHigh(j) - lg.get(i).Dbox.getHigh(j)) >= gk);
			}
incardon's avatar
incardon committed
586
587
588
589
		}
	}
}

incardon's avatar
incardon committed
590
void Test2D_complex(const Box<2,float> & domain, long int k)
incardon's avatar
incardon committed
591
{
incardon's avatar
incardon committed
592
	typedef Point_test<float> p;
incardon's avatar
incardon committed
593

incardon's avatar
incardon committed
594
595
	long int big_step = k / 30;
	big_step = (big_step == 0)?1:big_step;
incardon's avatar
incardon committed
596
	long int small_step = 21;
incardon's avatar
incardon committed
597

incardon's avatar
incardon committed
598
599
	print_test( "Testing 2D complex grid k<=",k);

incardon's avatar
incardon committed
600
601
602
603
	// 2D test
	for ( ; k >= 2 ; k-= (k > 2*big_step)?big_step:small_step )
	{
		BOOST_TEST_CHECKPOINT( "Testing 2D complex grid k=" << k );
incardon's avatar
incardon committed
604

605
606
		//! [Create and access a distributed grid complex]

incardon's avatar
incardon committed
607
608
609
610
		// grid size
		size_t sz[2];
		sz[0] = k;
		sz[1] = k;
incardon's avatar
incardon committed
611

incardon's avatar
incardon committed
612
		float factor = pow(global_v_cluster->getProcessingUnits()/2.0f,1.0f/2.0f);
incardon's avatar
incardon committed
613

incardon's avatar
incardon committed
614
615
		// Ghost
		Ghost<2,float> g(0.01 / factor);
616

incardon's avatar
incardon committed
617
618
		// Distributed grid with id decomposition
		grid_dist_id<2, float, Point_test<float>, CartDecomposition<2,float>> g_dist(sz,domain,g);
619

incardon's avatar
incardon committed
620
621
622
		// check the consistency of the decomposition
		bool val = g_dist.getDecomposition().check_consistency();
		BOOST_REQUIRE_EQUAL(val,true);
incardon's avatar
incardon committed
623

incardon's avatar
incardon committed
624
625
		// Grid sm
		grid_sm<2,void> info(sz);
incardon's avatar
incardon committed
626

incardon's avatar
incardon committed
627
628
		// get the domain iterator
		size_t count = 0;
incardon's avatar
incardon committed
629

incardon's avatar
incardon committed
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
		auto dom = g_dist.getDomainIterator();

		while (dom.isNext())
		{
			auto key = dom.get();
			auto key_g = g_dist.getGKey(key);

			size_t k = info.LinId(key_g);

			g_dist.template get<p::x>(key) = 1 + k;
			g_dist.template get<p::y>(key) = 567 + k;
			g_dist.template get<p::z>(key) = 341 + k;
			g_dist.template get<p::s>(key) = 5670 + k;
			g_dist.template get<p::v>(key)[0] = 921 + k;
			g_dist.template get<p::v>(key)[1] = 5675 + k;
			g_dist.template get<p::v>(key)[2] = 117 + k;
			g_dist.template get<p::t>(key)[0][0] = 1921 + k;
			g_dist.template get<p::t>(key)[0][1] = 25675 + k;
			g_dist.template get<p::t>(key)[0][2] = 3117 + k;
			g_dist.template get<p::t>(key)[1][0] = 4921 + k;
			g_dist.template get<p::t>(key)[1][1] = 55675 + k;
			g_dist.template get<p::t>(key)[1][2] = 6117 + k;
			g_dist.template get<p::t>(key)[2][0] = 7921 + k;
			g_dist.template get<p::t>(key)[2][1] = 85675 + k;
			g_dist.template get<p::t>(key)[2][2] = 9117 + k;

			// Count the point
			count++;

			++dom;
		}
incardon's avatar
incardon committed
661

662
663
		//! [Create and access a distributed grid complex]

incardon's avatar
incardon committed
664
665
		// Get the virtual cluster machine
		Vcluster & vcl = g_dist.getVC();
incardon's avatar
incardon committed
666

incardon's avatar
incardon committed
667
		// reduce
668
		vcl.sum(count);
incardon's avatar
incardon committed
669
		vcl.execute();
incardon's avatar
incardon committed
670

incardon's avatar
incardon committed
671
		// Check
Pietro Incardona's avatar
Pietro Incardona committed
672
		BOOST_REQUIRE_EQUAL(count,(size_t)k*k);
incardon's avatar
incardon committed
673
674

		auto dom2 = g_dist.getDomainIterator();
incardon's avatar
incardon committed
675

incardon's avatar
incardon committed
676
		bool match = true;
incardon's avatar
incardon committed
677

incardon's avatar
incardon committed
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
		// check that the grid store the correct information
		while (dom2.isNext())
		{
			auto key = dom2.get();
			auto key_g = g_dist.getGKey(key);

			size_t k = info.LinId(key_g);

			match &= (g_dist.template get<p::x>(key) == 1 + k)?true:false;
			match &= (g_dist.template get<p::y>(key) == 567 + k)?true:false;
			match &= (g_dist.template get<p::z>(key) == 341 + k)?true:false;
			match &= (g_dist.template get<p::s>(key) == 5670 + k)?true:false;
			match &= (g_dist.template get<p::v>(key)[0] == 921 + k)?true:false;
			match &= (g_dist.template get<p::v>(key)[1] == 5675 + k)?true:false;
			match &= (g_dist.template get<p::v>(key)[2] == 117 + k)?true:false;
			match &= (g_dist.template get<p::t>(key)[0][0] == 1921 + k)?true:false;
			match &= (g_dist.template get<p::t>(key)[0][1] == 25675 + k)?true:false;
			match &= (g_dist.template get<p::t>(key)[0][2] == 3117 + k)?true:false;
			match &= (g_dist.template get<p::t>(key)[1][0] == 4921 + k)?true:false;
			match &= (g_dist.template get<p::t>(key)[1][1] == 55675 + k)?true:false;
			match &= (g_dist.template get<p::t>(key)[1][2] == 6117 + k)?true:false;
			match &= (g_dist.template get<p::t>(key)[2][0] == 7921 + k)?true:false;
			match &= (g_dist.template get<p::t>(key)[2][1] == 85675 + k)?true:false;
			match &= (g_dist.template get<p::t>(key)[2][2] == 9117 + k)?true:false;

			++dom2;
		}

		BOOST_REQUIRE_EQUAL(match,true);

708
709
		//! [Synchronized distributed grid complex]

incardon's avatar
incardon committed
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
		g_dist.template ghost_get<p::x,p::y,p::z,p::s,p::v,p::t>();

		// check that the communication is correctly completed

		auto domg = g_dist.getDomainGhostIterator();

		// check that the grid with the ghost past store the correct information
		while (domg.isNext())
		{
			auto key = domg.get();
			auto key_g = g_dist.getGKey(key);

			// In this case the boundary condition are non periodic
			if (g_dist.isInside(key_g))
			{
				size_t k = info.LinId(key_g);

				match &= (g_dist.template get<p::x>(key) == 1 + k)?true:false;
				match &= (g_dist.template get<p::y>(key) == 567 + k)?true:false;
				match &= (g_dist.template get<p::z>(key) == 341 + k)?true:false;
				match &= (g_dist.template get<p::s>(key) == 5670 + k)?true:false;

				match &= (g_dist.template get<p::v>(key)[0] == 921 + k)?true:false;
				match &= (g_dist.template get<p::v>(key)[1] == 5675 + k)?true:false;
				match &= (g_dist.template get<p::v>(key)[2] == 117 + k)?true:false;

				match &= (g_dist.template get<p::t>(key)[0][0] == 1921 + k)?true:false;
				match &= (g_dist.template get<p::t>(key)[0][1] == 25675 + k)?true:false;
				match &= (g_dist.template get<p::t>(key)[0][2] == 3117 + k)?true:false;
				match &= (g_dist.template get<p::t>(key)[1][0] == 4921 + k)?true:false;
				match &= (g_dist.template get<p::t>(key)[1][1] == 55675 + k)?true:false;
				match &= (g_dist.template get<p::t>(key)[1][2] == 6117 + k)?true:false;
				match &= (g_dist.template get<p::t>(key)[2][0] == 7921 + k)?true:false;
				match &= (g_dist.template get<p::t>(key)[2][1] == 85675 + k)?true:false;
				match &= (g_dist.template get<p::t>(key)[2][2] == 9117 + k)?true:false;
			}

			++domg;
		}
749
750

		//! [Synchronized distributed grid complex]
incardon's avatar
incardon committed
751
	}
incardon's avatar
incardon committed
752
753
}

incardon's avatar
incardon committed
754
void Test3D_complex(const Box<3,float> & domain, long int k)
incardon's avatar
incardon committed
755
{
incardon's avatar
incardon committed
756
	typedef Point_test<float> p;
incardon's avatar
incardon committed
757

incardon's avatar
incardon committed
758
759
	long int big_step = k / 30;
	big_step = (big_step == 0)?1:big_step;
incardon's avatar
incardon committed
760
	long int small_step = 21;
incardon's avatar
incardon committed
761

incardon's avatar
incardon committed
762
763
	print_test( "Testing 3D grid complex k<=",k);

incardon's avatar
incardon committed
764
765
766
767
768
769
770
771
772
773
	// 2D test
	for ( ; k >= 2 ; k-= (k > 2*big_step)?big_step:small_step )
	{
		BOOST_TEST_CHECKPOINT( "Testing 3D complex grid k=" << k );

		// grid size
		size_t sz[3];
		sz[0] = k;
		sz[1] = k;
		sz[2] = k;
incardon's avatar
incardon committed
774

incardon's avatar
incardon committed
775
776
		// factor
		float factor = pow(global_v_cluster->getProcessingUnits()/2.0f,1.0f/3.0f);
incardon's avatar
incardon committed
777

incardon's avatar
incardon committed
778
779
		// Ghost
		Ghost<3,float> g(0.01 / factor);
incardon's avatar
incardon committed
780

incardon's avatar
incardon committed
781
782
		// Distributed grid with id decomposition
		grid_dist_id<3, float, Point_test<float>, CartDecomposition<3,float>> g_dist(sz,domain,g);
incardon's avatar
incardon committed
783

incardon's avatar
incardon committed
784
785
786
		// check the consistency of the decomposition
		bool val = g_dist.getDecomposition().check_consistency();
		BOOST_REQUIRE_EQUAL(val,true);
incardon's avatar
incardon committed
787

incardon's avatar
incardon committed
788
789
		// Grid sm
		grid_sm<3,void> info(sz);
incardon's avatar
incardon committed
790

incardon's avatar
incardon committed
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
		// get the domain iterator
		size_t count = 0;

		auto dom = g_dist.getDomainIterator();

		while (dom.isNext())
		{
			auto key = dom.get();
			auto key_g = g_dist.getGKey(key);

			size_t k = info.LinId(key_g);

			g_dist.template get<p::x>(key) = 1 + k;
			g_dist.template get<p::y>(key) = 567 + k;
			g_dist.template get<p::z>(key) = 341 + k;
			g_dist.template get<p::s>(key) = 5670 + k;
			g_dist.template get<p::v>(key)[0] = 921 + k;
			g_dist.template get<p::v>(key)[1] = 5675 + k;
			g_dist.template get<p::v>(key)[2] = 117 + k;
			g_dist.template get<p::t>(key)[0][0] = 1921 + k;
			g_dist.template get<p::t>(key)[0][1] = 25675 + k;
			g_dist.template get<p::t>(key)[0][2] = 3117 + k;
			g_dist.template get<p::t>(key)[1][0] = 4921 + k;
			g_dist.template get<p::t>(key)[1][1] = 55675 + k;
			g_dist.template get<p::t>(key)[1][2] = 6117 + k;
			g_dist.template get<p::t>(key)[2][0] = 7921 + k;
			g_dist.template get<p::t>(key)[2][1] = 85675 + k;
			g_dist.template get<p::t>(key)[2][2] = 9117 + k;

			// Count the point
			count++;

			++dom;
		}

		// Get the virtual cluster machine
		Vcluster & vcl = g_dist.getVC();

		// reduce
830
		vcl.sum(count);
incardon's avatar
incardon committed
831
832
833
		vcl.execute();

		// Check
Pietro Incardona's avatar
Pietro Incardona committed
834
		BOOST_REQUIRE_EQUAL(count,(size_t)k*k*k);
incardon's avatar
incardon committed
835

incardon's avatar
incardon committed
836
		bool match = true;
incardon's avatar
incardon committed
837

incardon's avatar
incardon committed
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
		auto dom2 = g_dist.getDomainIterator();

		// check that the grid store the correct information
		while (dom2.isNext())
		{
			auto key = dom2.get();
			auto key_g = g_dist.getGKey(key);

			size_t k = info.LinId(key_g);

			match &= (g_dist.template get<p::x>(key) == 1 + k)?true:false;
			match &= (g_dist.template get<p::y>(key) == 567 + k)?true:false;
			match &= (g_dist.template get<p::z>(key) == 341 + k)?true:false;
			match &= (g_dist.template get<p::s>(key) == 5670 + k)?true:false;
			match &= (g_dist.template get<p::v>(key)[0] == 921 + k)?true:false;
			match &= (g_dist.template get<p::v>(key)[1] == 5675 + k)?true:false;
			match &= (g_dist.template get<p::v>(key)[2] == 117 + k)?true:false;
			match &= (g_dist.template get<p::t>(key)[0][0] == 1921 + k)?true:false;
			match &= (g_dist.template get<p::t>(key)[0][1] == 25675 + k)?true:false;
			match &= (g_dist.template get<p::t>(key)[0][2] == 3117 + k)?true:false;
			match &= (g_dist.template get<p::t>(key)[1][0] == 4921 + k)?true:false;
			match &= (g_dist.template get<p::t>(key)[1][1] == 55675 + k)?true:false;
			match &= (g_dist.template get<p::t>(key)[1][2] == 6117 + k)?true:false;
			match &= (g_dist.template get<p::t>(key)[2][0] == 7921 + k)?true:false;
			match &= (g_dist.template get<p::t>(key)[2][1] == 85675 + k)?true:false;
			match &= (g_dist.template get<p::t>(key)[2][2] == 9117 + k)?true:false;

			++dom2;
		}

		BOOST_REQUIRE_EQUAL(match,true);

		g_dist.template ghost_get<p::x,p::y,p::z,p::s,p::v,p::t>();
incardon's avatar
incardon committed
871

incardon's avatar
incardon committed
872
		// check that the communication is correctly completed
incardon's avatar
incardon committed
873

incardon's avatar
incardon committed
874
		auto domg = g_dist.getDomainGhostIterator();
incardon's avatar
incardon committed
875

incardon's avatar
incardon committed
876
877
878
879
880
881
882
883
884
885
886
		// check that the grid with the ghost past store the correct information
		while (domg.isNext())
		{
			auto key = domg.get();
			auto key_g = g_dist.getGKey(key);

			size_t k = info.LinId(key_g);

			// In this case the boundary condition are non periodic
			if (g_dist.isInside(key_g))
			{
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
				match &= (g_dist.template get<p::x>(key) == 1 + k)?true:false;
				match &= (g_dist.template get<p::y>(key) == 567 + k)?true:false;
				match &= (g_dist.template get<p::z>(key) == 341 + k)?true:false;
				match &= (g_dist.template get<p::s>(key) == 5670 + k)?true:false;

				match &= (g_dist.template get<p::v>(key)[0] == 921 + k)?true:false;
				match &= (g_dist.template get<p::v>(key)[1] == 5675 + k)?true:false;
				match &= (g_dist.template get<p::v>(key)[2] == 117 + k)?true:false;

				match &= (g_dist.template get<p::t>(key)[0][0] == 1921 + k)?true:false;
				match &= (g_dist.template get<p::t>(key)[0][1] == 25675 + k)?true:false;
				match &= (g_dist.template get<p::t>(key)[0][2] == 3117 + k)?true:false;
				match &= (g_dist.template get<p::t>(key)[1][0] == 4921 + k)?true:false;
				match &= (g_dist.template get<p::t>(key)[1][1] == 55675 + k)?true:false;
				match &= (g_dist.template get<p::t>(key)[1][2] == 6117 + k)?true:false;
				match &= (g_dist.template get<p::t>(key)[2][0] == 7921 + k)?true:false;
				match &= (g_dist.template get<p::t>(key)[2][1] == 85675 + k)?true:false;
				match &= (g_dist.template get<p::t>(key)[2][2] == 9117 + k)?true:false;
incardon's avatar
incardon committed
905
906
907
908
909
910
911
912
913
			}

			++domg;
		}

		BOOST_REQUIRE_EQUAL(match,true);
	}
}

incardon's avatar
incardon committed
914
915
916
917
918
919
// Test duplicated topology

void Test3D_dup(const Box<3,float> & domain, long int k)
{
	long int big_step = k / 30;
	big_step = (big_step == 0)?1:big_step;
incardon's avatar
incardon committed
920
	long int small_step = 21;
incardon's avatar
incardon committed
921
	long int k_old = k;
incardon's avatar
incardon committed
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946

	Vcluster & v_cl = *global_v_cluster;

	if ( v_cl.getProcessingUnits() > 32 )
		return;

	print_test( "Testing 3D duplicate topology complex k<=",k);

	// 3D test
	for ( ; k >= 2 ; k-= (k > 2*big_step)?big_step:small_step )
	{
		BOOST_TEST_CHECKPOINT( "Testing 3D copy decomposition grid k=" << k );

		// grid size
		size_t sz[3];
		sz[0] = k;
		sz[1] = k;
		sz[2] = k;

		// factor
		float factor = pow(global_v_cluster->getProcessingUnits()/2.0f,1.0f/3.0f);

		// Ghost
		Ghost<3,float> g(0.01 / factor);

incardon's avatar
incardon committed
947
		//! [Construct two grid with the same decomposition]
incardon's avatar
incardon committed
948
949
950
951
952
953
954

		// Distributed grid with id decomposition
		grid_dist_id<3, float, Point_test<float>, CartDecomposition<3,float>> g_dist1(sz,domain,g);

		// another grid with the same decomposition
		grid_dist_id<3, float, Point_test<float>, CartDecomposition<3,float>> g_dist2(g_dist1.getDecomposition(),sz,domain,g);

incardon's avatar
incardon committed
955
956
		//! [Construct two grid with the same decomposition]

957
958
959
960
961
		bool ret = g_dist2.getDecomposition().check_consistency();
		BOOST_REQUIRE_EQUAL(ret,true);
		ret = g_dist2.getDecomposition().is_equal(g_dist2.getDecomposition());
		BOOST_REQUIRE_EQUAL(ret,true);

incardon's avatar
incardon committed
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980

		auto dom_g1 = g_dist1.getDomainIterator();
		auto dom_g2 = g_dist2.getDomainIterator();

		bool check = true;

		while (dom_g1.isNext())
		{
			auto key1 = dom_g1.get();
			auto key2 = dom_g2.get();

			check &= (key1 == key2)?true:false;

			++dom_g1;
			++dom_g2;
		}

		BOOST_REQUIRE_EQUAL(check,true);
	}
incardon's avatar
incardon committed
981

incardon's avatar
incardon committed
982
983
	k = k_old;

incardon's avatar
incardon committed
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
	// 3D test
	for ( ; k >= 2 ; k-= (k > 2*big_step)?big_step:small_step )
	{
		BOOST_TEST_CHECKPOINT( "Testing 3D copy decomposition grid k=" << k );

		// grid size
		size_t sz[3];
		sz[0] = k;
		sz[1] = k;
		sz[2] = k;

		// factor
		float factor = pow(global_v_cluster->getProcessingUnits()/2.0f,1.0f/3.0f);

		// Ghost
		Ghost<3,float> g(0.01 / factor);

		//! [Construct two grid with the same decomposition]

		// Distributed grid with id decomposition
		grid_dist_id<3, float, Point_test<float>, CartDecomposition<3,float>> * g_dist1 = new grid_dist_id<3, float, Point_test<float>, CartDecomposition<3,float>>(sz,domain,g);

		// another grid with the same decomposition
		grid_dist_id<3, float, Point_test<float>, CartDecomposition<3,float>> * g_dist2 = new grid_dist_id<3, float, Point_test<float>, CartDecomposition<3,float>>(g_dist1->getDecomposition(),sz,domain,g);

		//! [Construct two grid with the same decomposition]

		delete g_dist1;

incardon's avatar
incardon committed
1013
1014
		bool ret = g_dist2->getDecomposition().check_consistency();
		BOOST_REQUIRE_EQUAL(ret,true);
1015
1016

		delete g_dist2;
incardon's avatar
incardon committed
1017
	}
incardon's avatar
incardon committed
1018
1019
}

incardon's avatar
incardon committed
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
BOOST_AUTO_TEST_CASE( grid_dist_id_iterator_test_use)
{
	// Domain
	Box<2,float> domain({0.0,0.0},{1.0,1.0});

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

	long int k = 1024*1024*global_v_cluster->getProcessingUnits();
	k = std::pow(k, 1/2.);

	Test2D(domain,k);
	Test2D_complex(domain,k);
	// Domain
	Box<3,float> domain3({0.0,0.0,0.0},{1.0,1.0,1.0});

	k = 128*128*128*global_v_cluster->getProcessingUnits();
	k = std::pow(k, 1/3.);
	Test3D(domain3,k);
	Test3D_complex(domain3,k);
incardon's avatar
incardon committed
1040
1041
1042
1043
1044
1045
1046
1047
1048
}

BOOST_AUTO_TEST_CASE( grid_dist_id_dup)
{
	// Domain
	Box<3,float> domain3({0.0,0.0,0.0},{1.0,1.0,1.0});

	long int k = 128*128*128*global_v_cluster->getProcessingUnits();
	k = std::pow(k, 1/3.);
incardon's avatar
incardon committed
1049
	Test3D_dup(domain3,k);
incardon's avatar
incardon committed
1050
1051
}

incardon's avatar
incardon committed
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
BOOST_AUTO_TEST_CASE( grid_dist_id_sub)
{
	// Domain
	Box<3,float> domain3({0.0,0.0,0.0},{1.0,1.0,1.0});

	long int k = 128*128*128*global_v_cluster->getProcessingUnits();
	k = std::pow(k, 1/3.);
	Test3D_sub(domain3,k);
}

incardon's avatar
incardon committed
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
BOOST_AUTO_TEST_CASE( grid_dist_id_sub_iterator_test_use)
{
	// Domain
	Box<2,float> domain({0.0,0.0},{1.0,1.0});

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

	long int k = 1024*1024*global_v_cluster->getProcessingUnits();
	k = std::pow(k, 1/2.);

	Test2D_sub(domain,k);
}

incardon's avatar
incardon committed
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
BOOST_AUTO_TEST_CASE( grid_dist_id_with_grid_unit_ghost )
{
	// Domain
	Box<2,float> domain({0.0,0.0},{1.0,1.0});

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

	long int k = 1024*1024*global_v_cluster->getProcessingUnits();
	k = std::pow(k, 1/2.);

//	Test2D_gg(domain,k);
	// Domain
	Box<3,float> domain3({0.0,0.0,0.0},{1.0,1.0,1.0});

	k = 128*128*128*global_v_cluster->getProcessingUnits();
	k = std::pow(k, 1/3.);
	Test3D_gg(domain3,k,1);
}

incardon's avatar
incardon committed
1096
1097
1098
BOOST_AUTO_TEST_SUITE_END()

#endif