VCluster_unit_test_util.hpp 21 KB
Newer Older
incardon's avatar
incardon committed
1 2 3 4 5 6 7 8 9 10 11
/*
 * VCluster_unit_test_util.hpp
 *
 *  Created on: May 30, 2015
 *      Author: i-bird
 */

#ifndef VCLUSTER_UNIT_TEST_UTIL_HPP_
#define VCLUSTER_UNIT_TEST_UTIL_HPP_

#include "VCluster.hpp"
12 13
#include "Point_test.hpp"
#include "Vector/vector_test_util.hpp"
incardon's avatar
incardon committed
14 15 16 17 18 19

#define NBX 1

#define N_TRY 2
#define N_LOOP 67108864
#define BUFF_STEP 524288
20
#define P_STRIDE 17
incardon's avatar
incardon committed
21 22 23 24 25

bool totp_check;
size_t global_step = 0;
size_t global_rank;

26 27 28 29 30 31 32 33 34 35
/*! \brief calculate the x mob m
 *
 * \param x
 * \param m
 *
 */
int mod(int x, int m) {
    return (x%m + m)%m;
}

incardon's avatar
incardon committed
36 37 38 39 40 41
// Alloc the buffer to receive the messages

void * msg_alloc(size_t msg_i ,size_t total_msg, size_t total_p, size_t i,size_t ri, void * ptr)
{
	openfpm::vector<openfpm::vector<unsigned char>> * v = static_cast<openfpm::vector<openfpm::vector<unsigned char>> *>(ptr);

42 43
	if (create_vcluster().getProcessingUnits() <= 8)
	{if (totp_check) BOOST_REQUIRE_EQUAL(total_p,create_vcluster().getProcessingUnits()-1);}
incardon's avatar
incardon committed
44
	else
45
	{if (totp_check) BOOST_REQUIRE_EQUAL(total_p,(size_t)8);}
incardon's avatar
incardon committed
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 74 75 76 77

	BOOST_REQUIRE_EQUAL(msg_i, global_step);
	v->get(i).resize(msg_i);

	return &(v->get(i).get(0));
}

// Alloc the buffer to receive the messages

size_t id = 0;
openfpm::vector<size_t> prc_recv;

void * msg_alloc2(size_t msg_i ,size_t total_msg, size_t total_p, size_t i, size_t ri, void * ptr)
{
	openfpm::vector<openfpm::vector<unsigned char>> * v = static_cast<openfpm::vector<openfpm::vector<unsigned char>> *>(ptr);

	v->resize(total_p);
	prc_recv.resize(total_p);

	BOOST_REQUIRE_EQUAL(msg_i, global_step);

	id++;
	v->get(id-1).resize(msg_i);
	prc_recv.get(id-1) = i;
	return &(v->get(id-1).get(0));
}

void * msg_alloc3(size_t msg_i ,size_t total_msg, size_t total_p, size_t i, size_t ri, void * ptr)
{
	openfpm::vector<openfpm::vector<unsigned char>> * v = static_cast<openfpm::vector<openfpm::vector<unsigned char>> *>(ptr);

	v->add();
incardon's avatar
incardon committed
78

incardon's avatar
incardon committed
79 80 81 82 83 84 85 86 87 88 89
	prc_recv.add();

	BOOST_REQUIRE_EQUAL(msg_i, global_step);

	v->last().resize(msg_i);
	prc_recv.last() = i;
	return &(v->last().get(0));
}

template<unsigned int ip, typename T> void commFunc(Vcluster & vcl,openfpm::vector< size_t > & prc, openfpm::vector< T > & data, void * (* msg_alloc)(size_t,size_t,size_t,size_t,size_t,void *), void * ptr_arg)
{
90
	vcl.sendrecvMultipleMessagesNBX(prc,data,msg_alloc,ptr_arg);
incardon's avatar
incardon committed
91 92
}

93 94
template<unsigned int ip, typename T> void commFunc_null_odd(Vcluster & vcl,openfpm::vector< size_t > & prc, openfpm::vector< T > & data, void * (* msg_alloc)(size_t,size_t,size_t,size_t,size_t,void *), void * ptr_arg)
{
95 96 97
	if (vcl.getProcessUnitID() % 2 == 0)
		vcl.sendrecvMultipleMessagesNBX(prc,data,msg_alloc,ptr_arg);
	else
98
	{
99 100
		// No send check if passing null to sendrecv  work
		vcl.sendrecvMultipleMessagesNBX(prc.size(),(size_t *)NULL,(size_t *)NULL,(void **)NULL,msg_alloc,ptr_arg,NONE);
101 102 103
	}
}

incardon's avatar
incardon committed
104 105
template <unsigned int ip> std::string method()
{
106
	return std::string("NBX");
incardon's avatar
incardon committed
107 108
}

109 110
template<unsigned int ip> void test_no_send_some_peer()
{
111
	Vcluster & vcl = create_vcluster();
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 137 138 139 140 141 142 143 144 145 146 147 148 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 195

	size_t n_proc = vcl.getProcessingUnits();

	// Check long communication with some peer not comunication

	size_t j = 4567;

	global_step = j;
	// Processor step
	long int ps = n_proc / (8 + 1);

	// send message
	openfpm::vector<openfpm::vector<unsigned char>> message;
	// recv message
	openfpm::vector<openfpm::vector<unsigned char>> recv_message(n_proc);

	openfpm::vector<size_t> prc;

	// only even communicate

	if (vcl.getProcessUnitID() % 2 == 0)
	{
		for (size_t i = 0 ; i < 8  && i < n_proc ; i++)
		{
			size_t p_id = ((i+1) * ps + vcl.getProcessUnitID()) % n_proc;
			if (p_id != vcl.getProcessUnitID())
			{
				prc.add(p_id);
				message.add();
				std::ostringstream msg;
				msg << "Hello from " << vcl.getProcessUnitID() << " to " << p_id;
				std::string str(msg.str());
				message.last().resize(j);
				memset(message.last().getPointer(),0,j);
				std::copy(str.c_str(),&(str.c_str())[msg.str().size()],&(message.last().get(0)));
			}
		}
	}

	recv_message.resize(n_proc);

#ifdef VERBOSE_TEST
	timer t;
	t.start();
#endif

	commFunc_null_odd<ip>(vcl,prc,message,msg_alloc,&recv_message);

#ifdef VERBOSE_TEST
	t.stop();
	double clk = t.getwct();
	double clk_max = clk;

	size_t size_send_recv = 2 * j * (prc.size());
	vcl.sum(size_send_recv);
	vcl.max(clk_max);
	vcl.execute();

	if (vcl.getProcessUnitID() == 0)
		std::cout << "(Long pattern: " << method<ip>() << ")Buffer size: " << j << "    Bandwidth (Average): " << size_send_recv / vcl.getProcessingUnits() / clk / 1e6 << " MB/s  " << "    Bandwidth (Total): " << size_send_recv / clk / 1e6 << " MB/s    Clock: " << clk << "   Clock MAX: " << clk_max <<"\n";
#endif

	// Check the message
	for (long int i = 0 ; i < 8  && i < (long int)n_proc ; i++)
	{
		long int p_id = (- (i+1) * ps + (long int)vcl.getProcessUnitID());
		if (p_id < 0)
			p_id += n_proc;
		else
			p_id = p_id % n_proc;

		if (p_id != (long int)vcl.getProcessUnitID())
		{
			// only even processor communicate
			if (p_id % 2 == 1)
				continue;

			std::ostringstream msg;
			msg << "Hello from " << p_id << " to " << vcl.getProcessUnitID();
			std::string str(msg.str());
			BOOST_REQUIRE_EQUAL(std::equal(str.c_str(),str.c_str() + str.size() ,&(recv_message.get(p_id).get(0))),true);
		}
		else
		{
196
			BOOST_REQUIRE_EQUAL((size_t)0,recv_message.get(p_id).size());
197 198 199 200
		}
	}
}

201 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 253 254 255 256 257 258 259 260 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
template<unsigned int ip> void test_known()
{
	Vcluster & vcl = create_vcluster();

	// send/recv messages

	global_rank = vcl.getProcessUnitID();
	size_t n_proc = vcl.getProcessingUnits();

	// Checking short communication pattern

	for (size_t s = 0 ; s < N_TRY ; s++)
	{
		for (size_t j = 32 ; j < N_LOOP ; j*=2)
		{
			global_step = j;
			// send message
			openfpm::vector<openfpm::vector<unsigned char>> message;
			// recv message
			openfpm::vector<openfpm::vector<unsigned char>> recv_message(n_proc);
			recv_message.reserve(n_proc);

			openfpm::vector<size_t> prc_recv;
			openfpm::vector<size_t> recv_sz;

			openfpm::vector<size_t> prc;

			for (size_t i = 0 ; i < 8  && i < n_proc ; i++)
			{
				size_t p_id = (i + 1 + vcl.getProcessUnitID()) % n_proc;
				if (p_id != vcl.getProcessUnitID())
				{
					prc.add(p_id);
					message.add();
					std::ostringstream msg;
					msg << "Hello from " << vcl.getProcessUnitID() << " to " << p_id;
					std::string str(msg.str());
					message.last().resize(j);
					memset(message.last().getPointer(),0,j);
					std::copy(str.c_str(),&(str.c_str())[msg.str().size()],&(message.last().get(0)));
				}
			}

			recv_message.resize(n_proc);
			// The pattern is not really random preallocate the receive buffer
			for (size_t i = 0 ; i < 8  && i < n_proc ; i++)
			{
				long int p_id = vcl.getProcessUnitID() - i - 1;
				if (p_id < 0)
					p_id += n_proc;
				else
					p_id = p_id % n_proc;

				if (p_id != (long int)vcl.getProcessUnitID())
				{
					prc_recv.add(p_id);
					recv_message.get(p_id).resize(j);
					recv_sz.add(j);
				}
			}

#ifdef VERBOSE_TEST
			timer t;
			t.start();
#endif

			vcl.sendrecvMultipleMessagesNBX(prc,message,prc_recv,recv_sz,msg_alloc,&recv_message);

#ifdef VERBOSE_TEST
			t.stop();

			double clk = t.getwct();
			double clk_max = clk;

			size_t size_send_recv = 2 * j * (prc.size());
			vcl.sum(size_send_recv);
			vcl.max(clk_max);
			vcl.execute();

			if (vcl.getProcessUnitID() == 0)
				std::cout << "(Short pattern: " << method<ip>() << ")Buffer size: " << j << "    Bandwidth (Average): " << size_send_recv / vcl.getProcessingUnits() / clk / 1e6 << " MB/s  " << "    Bandwidth (Total): " << size_send_recv / clk / 1e6 << " MB/s    Clock: " << clk << "   Clock MAX: " << clk_max <<"\n";
#endif

			// Check the message
			for (size_t i = 0 ; i < 8  && i < n_proc ; i++)
			{
				long int p_id = vcl.getProcessUnitID() - i - 1;
				if (p_id < 0)
					p_id += n_proc;
				else
					p_id = p_id % n_proc;

				if (p_id != (long int)vcl.getProcessUnitID())
				{
					std::ostringstream msg;
					msg << "Hello from " << p_id << " to " << vcl.getProcessUnitID();
					std::string str(msg.str());
					BOOST_REQUIRE_EQUAL(std::equal(str.c_str(),str.c_str() + str.size() ,&(recv_message.get(p_id).get(0))),true);
				}
				else
				{
					BOOST_REQUIRE_EQUAL((size_t)0,recv_message.get(p_id).size());
				}
			}
		}
	}
}

incardon's avatar
incardon committed
309 310
template<unsigned int ip> void test()
{
311
	Vcluster & vcl = create_vcluster();
incardon's avatar
incardon committed
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328

	// send/recv messages

	global_rank = vcl.getProcessUnitID();
	size_t n_proc = vcl.getProcessingUnits();

	// Checking short communication pattern

	for (size_t s = 0 ; s < N_TRY ; s++)
	{
		for (size_t j = 32 ; j < N_LOOP ; j*=2)
		{
			global_step = j;
			// send message
			openfpm::vector<openfpm::vector<unsigned char>> message;
			// recv message
			openfpm::vector<openfpm::vector<unsigned char>> recv_message(n_proc);
incardon's avatar
incardon committed
329
			recv_message.reserve(n_proc);
incardon's avatar
incardon committed
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 360 361 362 363 364 365 366 367 368 369 370 371

			openfpm::vector<size_t> prc;

			for (size_t i = 0 ; i < 8  && i < n_proc ; i++)
			{
				size_t p_id = (i + 1 + vcl.getProcessUnitID()) % n_proc;
				if (p_id != vcl.getProcessUnitID())
				{
					prc.add(p_id);
					message.add();
					std::ostringstream msg;
					msg << "Hello from " << vcl.getProcessUnitID() << " to " << p_id;
					std::string str(msg.str());
					message.last().resize(j);
					memset(message.last().getPointer(),0,j);
					std::copy(str.c_str(),&(str.c_str())[msg.str().size()],&(message.last().get(0)));
				}
			}

			recv_message.resize(n_proc);
			// The pattern is not really random preallocate the receive buffer
			for (size_t i = 0 ; i < 8  && i < n_proc ; i++)
			{
				long int p_id = vcl.getProcessUnitID() - i - 1;
				if (p_id < 0)
					p_id += n_proc;
				else
					p_id = p_id % n_proc;

				if (p_id != (long int)vcl.getProcessUnitID())
					recv_message.get(p_id).resize(j);
			}

#ifdef VERBOSE_TEST
			timer t;
			t.start();
#endif

			commFunc<ip>(vcl,prc,message,msg_alloc,&recv_message);

#ifdef VERBOSE_TEST
			t.stop();
incardon's avatar
incardon committed
372

incardon's avatar
incardon committed
373 374 375 376
			double clk = t.getwct();
			double clk_max = clk;

			size_t size_send_recv = 2 * j * (prc.size());
377
			vcl.sum(size_send_recv);
incardon's avatar
incardon committed
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
			vcl.max(clk_max);
			vcl.execute();

			if (vcl.getProcessUnitID() == 0)
				std::cout << "(Short pattern: " << method<ip>() << ")Buffer size: " << j << "    Bandwidth (Average): " << size_send_recv / vcl.getProcessingUnits() / clk / 1e6 << " MB/s  " << "    Bandwidth (Total): " << size_send_recv / clk / 1e6 << " MB/s    Clock: " << clk << "   Clock MAX: " << clk_max <<"\n";
#endif

			// Check the message
			for (size_t i = 0 ; i < 8  && i < n_proc ; i++)
			{
				long int p_id = vcl.getProcessUnitID() - i - 1;
				if (p_id < 0)
					p_id += n_proc;
				else
					p_id = p_id % n_proc;

				if (p_id != (long int)vcl.getProcessUnitID())
				{
					std::ostringstream msg;
					msg << "Hello from " << p_id << " to " << vcl.getProcessUnitID();
					std::string str(msg.str());
					BOOST_REQUIRE_EQUAL(std::equal(str.c_str(),str.c_str() + str.size() ,&(recv_message.get(p_id).get(0))),true);
				}
				else
				{
403
					BOOST_REQUIRE_EQUAL((size_t)0,recv_message.get(p_id).size());
incardon's avatar
incardon committed
404 405 406 407
				}
			}
		}

408
		std::srand(create_vcluster().getProcessUnitID());
incardon's avatar
incardon committed
409 410 411 412 413 414 415 416 417 418 419 420 421 422
		std::default_random_engine eg;
		std::uniform_int_distribution<int> d(0,n_proc/8);

		// Check random pattern (maximum 16 processors)

		for (size_t j = 32 ; j < N_LOOP && n_proc < 16 ; j*=2)
		{
			global_step = j;
			// original send
			openfpm::vector<size_t> o_send;
			// send message
			openfpm::vector<openfpm::vector<unsigned char>> message;
			// recv message
			openfpm::vector<openfpm::vector<unsigned char>> recv_message;
incardon's avatar
incardon committed
423
//			recv_message.reserve(n_proc);
incardon's avatar
incardon committed
424 425 426 427 428

			openfpm::vector<size_t> prc;

			for (size_t i = 0 ; i < n_proc ; i++)
			{
429
				// randomly with which processor communicate
incardon's avatar
incardon committed
430 431 432 433 434
				if (d(eg) == 0)
				{
					prc.add(i);
					o_send.add(i);
					message.add();
435
					message.last().fill(0);
incardon's avatar
incardon committed
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
					std::ostringstream msg;
					msg << "Hello from " << vcl.getProcessUnitID() << " to " << i;
					std::string str(msg.str());
					message.last().resize(str.size());
					std::copy(str.c_str(),&(str.c_str())[msg.str().size()],&(message.last().get(0)));
					message.last().resize(j);
				}
			}

			id = 0;
			prc_recv.clear();


#ifdef VERBOSE_TEST
			timer t;
			t.start();
#endif

			commFunc<ip>(vcl,prc,message,msg_alloc3,&recv_message);

#ifdef VERBOSE_TEST
			t.stop();
			double clk = t.getwct();
			double clk_max = clk;

			size_t size_send_recv = (prc.size() + recv_message.size()) * j;
462 463
			vcl.sum(size_send_recv);
			vcl.sum(clk);
incardon's avatar
incardon committed
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 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 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588
			vcl.max(clk_max);
			vcl.execute();
			clk /= vcl.getProcessingUnits();

			if (vcl.getProcessUnitID() == 0)
				std::cout << "(Random Pattern: " << method<ip>() << ") Buffer size: " << j << "    Bandwidth (Average): " << size_send_recv / vcl.getProcessingUnits() / clk / 1e6 << " MB/s  " << "    Bandwidth (Total): " << size_send_recv / clk / 1e6 <<  " MB/s    Clock: " << clk << "   Clock MAX: " << clk_max << "\n";
#endif

			// Check the message

			for (size_t i = 0 ; i < recv_message.size() ; i++)
			{
				std::ostringstream msg;
				msg << "Hello from " << prc_recv.get(i) << " to " << vcl.getProcessUnitID();
				std::string str(msg.str());
				BOOST_REQUIRE_EQUAL(std::equal(str.c_str(),str.c_str() + str.size() ,&(recv_message.get(i).get(0))),true);
			}

			// Reply back

			// Create the message

			prc.clear();
			message.clear();
			for (size_t i = 0 ; i < prc_recv.size() ; i++)
			{
				prc.add(prc_recv.get(i));
				message.add();
				std::ostringstream msg;
				msg << "Hey from " << vcl.getProcessUnitID() << " to " << prc_recv.get(i);
				std::string str(msg.str());
				message.last().resize(str.size());
				std::copy(str.c_str(),&(str.c_str())[msg.str().size()],&(message.last().get(0)));
				message.last().resize(j);
			}

			id = 0;
			prc_recv.clear();
			recv_message.clear();

			commFunc<ip>(vcl,prc,message,msg_alloc3,&recv_message);

			// Check if the received hey message match the original send

			BOOST_REQUIRE_EQUAL(o_send.size(),prc_recv.size());

			for (size_t i = 0 ; i < o_send.size() ; i++)
			{
				size_t j = 0;
				for ( ; j < prc_recv.size() ; j++)
				{
					if (o_send.get(i) == prc_recv.get(j))
					{
						// found the message check it

						std::ostringstream msg;
						msg << "Hey from " << prc_recv.get(i) << " to " << vcl.getProcessUnitID();
						std::string str(msg.str());
						BOOST_REQUIRE_EQUAL(std::equal(str.c_str(),str.c_str() + str.size() ,&(recv_message.get(i).get(0))),true);
						break;
					}
				}
				// Check that we find always a match
				BOOST_REQUIRE_EQUAL(j != prc_recv.size(),true);
			}
		}

		// Check long communication pattern

		for (size_t j = 32 ; j < N_LOOP ; j*=2)
		{
			global_step = j;
			// Processor step
			long int ps = n_proc / (8 + 1);

			// send message
			openfpm::vector<openfpm::vector<unsigned char>> message;
			// recv message
			openfpm::vector<openfpm::vector<unsigned char>> recv_message(n_proc);

			openfpm::vector<size_t> prc;

			for (size_t i = 0 ; i < 8  && i < n_proc ; i++)
			{
				size_t p_id = ((i+1) * ps + vcl.getProcessUnitID()) % n_proc;
				if (p_id != vcl.getProcessUnitID())
				{
					prc.add(p_id);
					message.add();
					std::ostringstream msg;
					msg << "Hello from " << vcl.getProcessUnitID() << " to " << p_id;
					std::string str(msg.str());
					message.last().resize(j);
					memset(message.last().getPointer(),0,j);
					std::copy(str.c_str(),&(str.c_str())[msg.str().size()],&(message.last().get(0)));
				}
			}

			recv_message.resize(n_proc);
			// The pattern is not really random preallocate the receive buffer
			for (size_t i = 0 ; i < 8  && i < n_proc ; i++)
			{
				long int p_id = (- (i+1) * ps + (long int)vcl.getProcessUnitID());
				if (p_id < 0)
					p_id += n_proc;
				else
					p_id = p_id % n_proc;

				if (p_id != (long int)vcl.getProcessUnitID())
					recv_message.get(p_id).resize(j);
			}

#ifdef VERBOSE_TEST
			timer t;
			t.start();
#endif

			commFunc<ip>(vcl,prc,message,msg_alloc,&recv_message);

#ifdef VERBOSE_TEST
			t.stop();
			double clk = t.getwct();
			double clk_max = clk;

			size_t size_send_recv = 2 * j * (prc.size());
589
			vcl.sum(size_send_recv);
incardon's avatar
incardon committed
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614
			vcl.max(clk_max);
			vcl.execute();

			if (vcl.getProcessUnitID() == 0)
				std::cout << "(Long pattern: " << method<ip>() << ")Buffer size: " << j << "    Bandwidth (Average): " << size_send_recv / vcl.getProcessingUnits() / clk / 1e6 << " MB/s  " << "    Bandwidth (Total): " << size_send_recv / clk / 1e6 << " MB/s    Clock: " << clk << "   Clock MAX: " << clk_max <<"\n";
#endif

			// Check the message
			for (long int i = 0 ; i < 8  && i < (long int)n_proc ; i++)
			{
				long int p_id = (- (i+1) * ps + (long int)vcl.getProcessUnitID());
				if (p_id < 0)
					p_id += n_proc;
				else
					p_id = p_id % n_proc;

				if (p_id != (long int)vcl.getProcessUnitID())
				{
					std::ostringstream msg;
					msg << "Hello from " << p_id << " to " << vcl.getProcessUnitID();
					std::string str(msg.str());
					BOOST_REQUIRE_EQUAL(std::equal(str.c_str(),str.c_str() + str.size() ,&(recv_message.get(p_id).get(0))),true);
				}
				else
				{
615
					BOOST_REQUIRE_EQUAL((size_t)0,recv_message.get(p_id).size());
incardon's avatar
incardon committed
616 617 618 619 620 621
				}
			}
		}
	}
}

622 623 624 625 626 627 628 629
/*! \brief Test vectors send for complex structures
 *
 * \param n test size
 * \param vcl VCluster
 *
 */
void test_send_recv_complex(const size_t n, Vcluster & vcl)
{
630 631
	//! [Send and receive vectors of complex]

632 633 634 635 636 637 638 639
	// Point test typedef
	typedef Point_test<float> p;

	openfpm::vector<Point_test<float>> v_send = allocate_openfpm_fill(n,vcl.getProcessUnitID());

	// Send to 8 processors
	for (size_t i = 0 ; i < 8 ; i++)
		vcl.send( mod(vcl.getProcessUnitID() + i * P_STRIDE, vcl.getProcessingUnits()) ,i,v_send);
incardon's avatar
incardon committed
640

641 642 643 644 645 646 647 648 649 650 651 652
	openfpm::vector<openfpm::vector<Point_test<float>> > pt_buf;
	pt_buf.resize(8);

	// Recv from 8 processors
	for (size_t i = 0 ; i < 8 ; i++)
	{
		pt_buf.get(i).resize(n);
		vcl.recv( mod( (vcl.getProcessUnitID() - i * P_STRIDE), vcl.getProcessingUnits()) ,i,pt_buf.get(i));
	}

	vcl.execute();

653 654
	//! [Send and receive vectors of complex]

655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694
	// Check the received buffers (careful at negative modulo)
	for (size_t i = 0 ; i < 8 ; i++)
	{
		for (size_t j = 0 ; j < n ; j++)
		{
			Point_test<float> pt = pt_buf.get(i).get(j);

			size_t p_recv = mod( (vcl.getProcessUnitID() - i * P_STRIDE), vcl.getProcessingUnits());

			BOOST_REQUIRE_EQUAL(pt.template get<p::x>(),p_recv);
			BOOST_REQUIRE_EQUAL(pt.template get<p::y>(),p_recv);
			BOOST_REQUIRE_EQUAL(pt.template get<p::z>(),p_recv);
			BOOST_REQUIRE_EQUAL(pt.template get<p::s>(),p_recv);
			BOOST_REQUIRE_EQUAL(pt.template get<p::v>()[0],p_recv);
			BOOST_REQUIRE_EQUAL(pt.template get<p::v>()[1],p_recv);
			BOOST_REQUIRE_EQUAL(pt.template get<p::v>()[2],p_recv);
			BOOST_REQUIRE_EQUAL(pt.template get<p::t>()[0][0],p_recv);
			BOOST_REQUIRE_EQUAL(pt.template get<p::t>()[0][1],p_recv);
			BOOST_REQUIRE_EQUAL(pt.template get<p::t>()[0][2],p_recv);
			BOOST_REQUIRE_EQUAL(pt.template get<p::t>()[1][0],p_recv);
			BOOST_REQUIRE_EQUAL(pt.template get<p::t>()[1][1],p_recv);
			BOOST_REQUIRE_EQUAL(pt.template get<p::t>()[1][2],p_recv);
			BOOST_REQUIRE_EQUAL(pt.template get<p::t>()[2][0],p_recv);
			BOOST_REQUIRE_EQUAL(pt.template get<p::t>()[2][1],p_recv);
			BOOST_REQUIRE_EQUAL(pt.template get<p::t>()[2][2],p_recv);
		}
	}
}

/*! \brief Test vectors send for complex structures
 *
 * \tparam T primitives
 *
 * \param n size
 *
 */
template<typename T> void test_send_recv_primitives(size_t n, Vcluster & vcl)
{
	openfpm::vector<T> v_send = allocate_openfpm_primitive<T>(n,vcl.getProcessUnitID());

incardon's avatar
incardon committed
695
	{
696
	//! [Sending and receiving primitives]
incardon's avatar
incardon committed
697

698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
	// Send to 8 processors
	for (size_t i = 0 ; i < 8 ; i++)
		vcl.send( mod(vcl.getProcessUnitID() + i * P_STRIDE, vcl.getProcessingUnits()) ,i,v_send);

	openfpm::vector<openfpm::vector<T> > pt_buf;
	pt_buf.resize(8);

	// Recv from 8 processors
	for (size_t i = 0 ; i < 8 ; i++)
	{
		pt_buf.get(i).resize(n);
		vcl.recv( mod( (vcl.getProcessUnitID() - i * P_STRIDE), vcl.getProcessingUnits()) ,i,pt_buf.get(i));
	}

	vcl.execute();

714 715
	//! [Sending and receiving primitives]

716 717 718 719 720 721 722
	// Check the received buffers (careful at negative modulo)
	for (size_t i = 0 ; i < 8 ; i++)
	{
		for (size_t j = 0 ; j < n ; j++)
		{
			T pt = pt_buf.get(i).get(j);

incardon's avatar
incardon committed
723
			T p_recv = mod( (vcl.getProcessUnitID() - i * P_STRIDE), vcl.getProcessingUnits());
724 725 726 727

			BOOST_REQUIRE_EQUAL(pt,p_recv);
		}
	}
incardon's avatar
incardon committed
728 729 730 731

	}

	{
732
	//! [Send and receive plain buffer data]
incardon's avatar
incardon committed
733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749

	// Send to 8 processors
	for (size_t i = 0 ; i < 8 ; i++)
		vcl.send( mod(vcl.getProcessUnitID() + i * P_STRIDE, vcl.getProcessingUnits()) ,i,v_send.getPointer(),v_send.size()*sizeof(T));

	openfpm::vector<openfpm::vector<T> > pt_buf;
	pt_buf.resize(8);

	// Recv from 8 processors
	for (size_t i = 0 ; i < 8 ; i++)
	{
		pt_buf.get(i).resize(n);
		vcl.recv( mod( (vcl.getProcessUnitID() - i * P_STRIDE), vcl.getProcessingUnits()) ,i,pt_buf.get(i).getPointer(),pt_buf.get(i).size()*sizeof(T));
	}

	vcl.execute();

750 751
	//! [Send and receive plain buffer data]

incardon's avatar
incardon committed
752 753 754 755 756 757 758 759 760 761 762 763 764 765
	// Check the received buffers (careful at negative modulo)
	for (size_t i = 0 ; i < 8 ; i++)
	{
		for (size_t j = 0 ; j < n ; j++)
		{
			T pt = pt_buf.get(i).get(j);

			T p_recv = mod( (vcl.getProcessUnitID() - i * P_STRIDE), vcl.getProcessingUnits());

			BOOST_REQUIRE_EQUAL(pt,p_recv);
		}
	}

	}
766
}
incardon's avatar
incardon committed
767

incardon's avatar
incardon committed
768 769 770 771 772 773 774 775 776 777 778
template<typename T>  void test_single_all_gather_primitives(Vcluster & vcl)
{
	//! [allGather numbers]

	openfpm::vector<T> clt;
	T data = vcl.getProcessUnitID();

	vcl.allGather(data,clt);
	vcl.execute();

	for (size_t i = 0 ; i < vcl.getProcessingUnits() ; i++)
779
		BOOST_REQUIRE_EQUAL(i,(size_t)clt.get(i));
incardon's avatar
incardon committed
780 781 782 783 784

	//! [allGather numbers]

}

incardon's avatar
incardon committed
785
#endif /* VCLUSTER_UNIT_TEST_UTIL_HPP_ */