vector_dist_gpu_unit_tests.cu 38.8 KB
Newer Older
incardon's avatar
incardon committed
1 2 3 4
#define BOOST_TEST_DYN_LINK
#include <boost/test/unit_test.hpp>
#include "VCluster/VCluster.hpp"
#include <Vector/vector_dist.hpp>
5
#include "Vector/tests/vector_dist_util_unit_tests.hpp"
incardon's avatar
incardon committed
6

incardon's avatar
incardon committed
7 8
#define SUB_UNIT_FACTOR 1024

incardon's avatar
incardon committed
9 10 11 12 13 14 15 16 17 18 19 20
template<unsigned int dim , typename vector_dist_type>
__global__ void move_parts_gpu_test(vector_dist_type vd)
{
	auto p = GET_PARTICLE(vd);

#pragma unroll
	for (int i = 0 ; i < dim ; i++)
	{
		vd.getPos(p)[i] += 0.05;
	}
}

incardon's avatar
incardon committed
21 22 23 24 25 26 27 28
BOOST_AUTO_TEST_SUITE( vector_dist_gpu_test )

void print_test(std::string test, size_t sz)
{
	if (create_vcluster().getProcessUnitID() == 0)
		std::cout << test << " " << sz << "\n";
}

incardon's avatar
incardon committed
29 30 31 32 33 34 35 36 37 38 39 40

__global__  void initialize_props(vector_dist_ker<3, float, aggregate<float, float [3], float[3]>> vd)
{
	auto p = GET_PARTICLE(vd);

	vd.template getProp<0>(p) = vd.getPos(p)[0] + vd.getPos(p)[1] + vd.getPos(p)[2];

	vd.template getProp<1>(p)[0] = vd.getPos(p)[0] + vd.getPos(p)[1];
	vd.template getProp<1>(p)[1] = vd.getPos(p)[0] + vd.getPos(p)[2];
	vd.template getProp<1>(p)[2] = vd.getPos(p)[1] + vd.getPos(p)[2];
}

incardon's avatar
Fixing  
incardon committed
41 42 43 44 45
template<typename T,typename CellList_type>
__global__  void calculate_force(vector_dist_ker<3, T, aggregate<T, T[3], T [3]>> vd,
		                         vector_dist_ker<3, T, aggregate<T, T[3], T [3]>> vd_sort,
		                         CellList_type cl,
		                         int rank)
incardon's avatar
incardon committed
46 47 48
{
	auto p = GET_PARTICLE(vd);

incardon's avatar
Fixing  
incardon committed
49
	Point<3,T> xp = vd.getPos(p);
incardon's avatar
incardon committed
50 51 52

    auto it = cl.getNNIterator(cl.getCell(xp));

incardon's avatar
Fixing  
incardon committed
53 54
    Point<3,T> force1({0.0,0.0,0.0});
    Point<3,T> force2({0.0,0.0,0.0});
incardon's avatar
incardon committed
55 56 57

    while (it.isNext())
    {
incardon's avatar
incardon committed
58 59
    	auto q1 = it.get_sort();
    	auto q2 = it.get();
incardon's avatar
incardon committed
60 61 62

    	if (q2 == p) {++it; continue;}

incardon's avatar
Fixing  
incardon committed
63 64
    	Point<3,T> xq_1 = vd_sort.getPos(q1);
    	Point<3,T> xq_2 = vd.getPos(q2);
incardon's avatar
incardon committed
65

incardon's avatar
Fixing  
incardon committed
66 67
    	Point<3,T> r1 = xq_1 - xp;
    	Point<3,T> r2 = xq_2 - xp;
incardon's avatar
incardon committed
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88

    	// Normalize

    	r1 /= r1.norm();
    	r2 /= r2.norm();

    	force1 += vd_sort.template getProp<0>(q1)*r1;
    	force2 += vd.template getProp<0>(q2)*r2;

    	++it;
    }

    vd.template getProp<1>(p)[0] = force1.get(0);
    vd.template getProp<1>(p)[1] = force1.get(1);
    vd.template getProp<1>(p)[2] = force1.get(2);

    vd.template getProp<2>(p)[0] = force2.get(0);
    vd.template getProp<2>(p)[1] = force2.get(1);
    vd.template getProp<2>(p)[2] = force2.get(2);
}

incardon's avatar
Fixing  
incardon committed
89 90
template<typename T, typename CellList_type>
__global__  void calculate_force_full_sort(vector_dist_ker<3, T, aggregate<T, T[3], T [3]>> vd,
incardon's avatar
incardon committed
91
		                         	 	   CellList_type cl, int rank)
92
{
incardon's avatar
incardon committed
93 94
	unsigned int p;
	GET_PARTICLE_SORT(p,cl);
incardon's avatar
incardon committed
95

incardon's avatar
Fixing  
incardon committed
96
	Point<3,T> xp = vd.getPos(p);
97 98 99

    auto it = cl.getNNIterator(cl.getCell(xp));

incardon's avatar
Fixing  
incardon committed
100
    Point<3,T> force1({0.0,0.0,0.0});
101 102 103

    while (it.isNext())
    {
incardon's avatar
incardon committed
104
    	auto q1 = it.get_sort();
105 106 107

    	if (q1 == p) {++it; continue;}

incardon's avatar
Fixing  
incardon committed
108
    	Point<3,T> xq_1 = vd.getPos(q1);
109

incardon's avatar
Fixing  
incardon committed
110
    	Point<3,T> r1 = xq_1 - xp;
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128

    	// Normalize

    	r1 /= r1.norm();

    	force1 += vd.template getProp<0>(q1)*r1;

    	++it;
    }

    vd.template getProp<1>(p)[0] = force1.get(0);
    vd.template getProp<1>(p)[1] = force1.get(1);
    vd.template getProp<1>(p)[2] = force1.get(2);
}

template<typename CellList_type, typename vector_type>
bool check_force(CellList_type & NN_cpu, vector_type & vd)
{
incardon's avatar
Fixing  
incardon committed
129 130
	typedef typename vector_type::stype St;

131 132 133 134 135 136 137 138
	auto it6 = vd.getDomainIterator();

	bool match = true;

	while (it6.isNext())
	{
		auto p = it6.get();

incardon's avatar
Fixing  
incardon committed
139
		Point<3,St> xp = vd.getPos(p);
140 141 142

		// Calculate on CPU

incardon's avatar
Fixing  
incardon committed
143
		Point<3,St> force({0.0,0.0,0.0});
144 145 146 147 148 149 150 151 152

		auto NNc = NN_cpu.getNNIterator(NN_cpu.getCell(xp));

		while (NNc.isNext())
		{
			auto q = NNc.get();

	    	if (q == p.getKey()) {++NNc; continue;}

incardon's avatar
Fixing  
incardon committed
153 154
	    	Point<3,St> xq_2 = vd.getPos(q);
	    	Point<3,St> r2 = xq_2 - xp;
155 156 157 158 159 160 161 162 163

	    	// Normalize

	    	r2 /= r2.norm();
	    	force += vd.template getProp<0>(q)*r2;

			++NNc;
		}

incardon's avatar
incardon committed
164 165 166
		match &= fabs(vd.template getProp<1>(p)[0] - vd.template getProp<2>(p)[0]) < 0.0003;
		match &= fabs(vd.template getProp<1>(p)[1] - vd.template getProp<2>(p)[1]) < 0.0003;
		match &= fabs(vd.template getProp<1>(p)[2] - vd.template getProp<2>(p)[2]) < 0.0003;
167

incardon's avatar
incardon committed
168 169 170 171 172 173 174 175 176 177
		match &= fabs(vd.template getProp<1>(p)[0] - force.get(0)) < 0.0003;
		match &= fabs(vd.template getProp<1>(p)[1] - force.get(1)) < 0.0003;
		match &= fabs(vd.template getProp<1>(p)[2] - force.get(2)) < 0.0003;

		if (match == false)
		{
			std::cout << "ERROR: " << vd.template getProp<1>(p)[0]  << "   " << vd.template getProp<2>(p)[0] << std::endl;
	                std::cout << "ERROR: " << vd.template getProp<1>(p)[1]  << "   " << vd.template getProp<2>(p)[1] << std::endl;
	                std::cout << "ERROR: " << vd.template getProp<1>(p)[2]  << "   " << vd.template getProp<2>(p)[2] << std::endl;

incardon's avatar
incardon committed
178 179 180
	                std::cout << p.getKey() << " ERROR2: " << vd.template getProp<1>(p)[0] << "   " <<  force.get(0) << std::endl;
	                std::cout << p.getKey() << " ERROR2: " << vd.template getProp<1>(p)[1] << "   " <<  force.get(1) << std::endl;
	                std::cout << p.getKey() << " ERROR2: " << vd.template getProp<1>(p)[2] << "   " <<  force.get(2) << std::endl;
incardon's avatar
incardon committed
181 182 183 184


			break;
		}
185 186 187 188 189 190 191 192 193 194 195 196 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

		++it6;
	}

	return match;
}

BOOST_AUTO_TEST_CASE( vector_dist_gpu_ghost_get )
{
	auto & v_cl = create_vcluster();

	if (v_cl.size() > 16)
	{return;}

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

	// set the ghost based on the radius cut off (make just a little bit smaller than the spacing)
	Ghost<3,float> g(0.1);

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

	vector_dist_gpu<3,float,aggregate<float,float[3],float[3]>> vd(1000,domain,bc,g);

	auto it = vd.getDomainIterator();

	while (it.isNext())
	{
		auto p = it.get();

		vd.getPos(p)[0] = (float)rand() / RAND_MAX;
		vd.getPos(p)[1] = (float)rand() / RAND_MAX;
		vd.getPos(p)[2] = (float)rand() / RAND_MAX;

		vd.template getProp<0>(p) = vd.getPos(p)[0] + vd.getPos(p)[1] + vd.getPos(p)[2];

		vd.template getProp<1>(p)[0] = vd.getPos(p)[0] + vd.getPos(p)[1];
		vd.template getProp<1>(p)[1] = vd.getPos(p)[0] + vd.getPos(p)[2];
		vd.template getProp<1>(p)[2] = vd.getPos(p)[1] + vd.getPos(p)[2];

		vd.template getProp<2>(p)[0] = vd.getPos(p)[0] + 3.0*vd.getPos(p)[1];
		vd.template getProp<2>(p)[1] = vd.getPos(p)[0] + 3.0*vd.getPos(p)[2];
		vd.template getProp<2>(p)[2] = vd.getPos(p)[1] + 3.0*vd.getPos(p)[2];


		++it;
	}

	// Ok we redistribute the particles (CPU based)
	vd.map();

	vd.template ghost_get<0,1,2>();

	// Now we check the the ghost contain the correct information

	bool check = true;

	auto itg = vd.getDomainAndGhostIterator();

	while (itg.isNext())
	{
		auto p = itg.get();

		check &= (vd.template getProp<0>(p) == vd.getPos(p)[0] + vd.getPos(p)[1] + vd.getPos(p)[2]);

		check &= (vd.template getProp<1>(p)[0] == vd.getPos(p)[0] + vd.getPos(p)[1]);
		check &= (vd.template getProp<1>(p)[1] == vd.getPos(p)[0] + vd.getPos(p)[2]);
		check &= (vd.template getProp<1>(p)[2] == vd.getPos(p)[1] + vd.getPos(p)[2]);

		check &= (vd.template getProp<2>(p)[0] == vd.getPos(p)[0] + 3.0*vd.getPos(p)[1]);
		check &= (vd.template getProp<2>(p)[1] == vd.getPos(p)[0] + 3.0*vd.getPos(p)[2]);
		check &= (vd.template getProp<2>(p)[2] == vd.getPos(p)[1] + 3.0*vd.getPos(p)[2]);

		++itg;
	}

	size_t tot_s = vd.size_local_with_ghost();

	v_cl.sum(tot_s);
	v_cl.execute();

	// We check that we check something
	BOOST_REQUIRE(tot_s > 1000);
}

incardon's avatar
incardon committed
270 271 272
template<typename vector_type, typename CellList_type, typename CellList_type_cpu>
void check_cell_list_cpu_and_gpu(vector_type & vd, CellList_type & NN, CellList_type_cpu & NN_cpu)
{
incardon's avatar
Fixing  
incardon committed
273
	auto it5 = vd.getDomainIteratorGPU(32);
incardon's avatar
incardon committed
274

incardon's avatar
Fixing  
incardon committed
275
	calculate_force<typename vector_type::stype,decltype(NN.toKernel())><<<it5.wthr,it5.thr>>>(vd.toKernel(),vd.toKernel_sorted(),NN.toKernel(),create_vcluster().rank());
incardon's avatar
incardon committed
276 277 278 279 280 281

	vd.template deviceToHostProp<1,2>();

	bool test = check_force(NN_cpu,vd);
	BOOST_REQUIRE_EQUAL(test,true);

incardon's avatar
incardon committed
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
	// We reset the property 1 on device

	auto rst = vd.getDomainIterator();

	while (rst.isNext())
	{
		auto p = rst.get();

		vd.template getProp<1>(p)[0] = 0.0;
		vd.template getProp<1>(p)[1] = 0.0;
		vd.template getProp<1>(p)[2] = 0.0;

		++rst;
	}

	vd.template hostToDeviceProp<1>();

incardon's avatar
incardon committed
299 300
	// We do exactly the same test as before, but now we completely use the sorted version

incardon's avatar
Fixing  
incardon committed
301
	calculate_force_full_sort<typename vector_type::stype,decltype(NN.toKernel())><<<it5.wthr,it5.thr>>>(vd.toKernel_sorted(),NN.toKernel(),create_vcluster().rank());
incardon's avatar
incardon committed
302

incardon's avatar
incardon committed
303
	vd.template merge_sort<1>(NN);
incardon's avatar
incardon committed
304 305 306 307 308 309
	vd.template deviceToHostProp<1>();

	test = check_force(NN_cpu,vd);
	BOOST_REQUIRE_EQUAL(test,true);
}

incardon's avatar
incardon committed
310 311
template<typename CellList_type>
void vector_dist_gpu_test_impl()
incardon's avatar
incardon committed
312
{
incardon's avatar
incardon committed
313 314 315 316 317
	auto & v_cl = create_vcluster();

	if (v_cl.size() > 16)
	{return;}

incardon's avatar
incardon committed
318 319 320
	Box<3,float> domain({0.0,0.0,0.0},{1.0,1.0,1.0});

	// set the ghost based on the radius cut off (make just a little bit smaller than the spacing)
incardon's avatar
incardon committed
321
	Ghost<3,float> g(0.1);
incardon's avatar
incardon committed
322 323 324 325

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

incardon's avatar
incardon committed
326
	vector_dist_gpu<3,float,aggregate<float,float[3],float[3]>> vd(10000,domain,bc,g);
incardon's avatar
incardon committed
327

incardon's avatar
incardon committed
328 329
	srand(55067*create_vcluster().rank());

incardon's avatar
incardon committed
330 331 332 333 334 335
	auto it = vd.getDomainIterator();

	while (it.isNext())
	{
		auto p = it.get();

incardon's avatar
incardon committed
336 337 338 339 340 341 342 343 344
		int x = rand();
		int y = rand();
		int z = rand();

		vd.getPos(p)[0] = (float)x / RAND_MAX;
		vd.getPos(p)[1] = (float)y / RAND_MAX;
		vd.getPos(p)[2] = (float)z / RAND_MAX;

		Point<3,float> xp = vd.getPos(p);
incardon's avatar
incardon committed
345 346 347 348

		++it;
	}

349
	// Ok we redistribute the particles (CPU based)
incardon's avatar
incardon committed
350
	vd.map();
incardon's avatar
incardon committed
351 352 353 354 355 356

	size_t size_l = vd.size_local();

	v_cl.sum(size_l);
	v_cl.execute();

incardon's avatar
incardon committed
357
	BOOST_REQUIRE_EQUAL(size_l,10000);
incardon's avatar
incardon committed
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381


	auto & ct = vd.getDecomposition();

	bool noOut = true;
	size_t cnt = 0;

	auto it2 = vd.getDomainIterator();

	while (it2.isNext())
	{
		auto p = it2.get();

		noOut &= ct.isLocal(vd.getPos(p));

		cnt++;
		++it2;
	}

	BOOST_REQUIRE_EQUAL(noOut,true);
	BOOST_REQUIRE_EQUAL(cnt,vd.size_local());

	// now we offload all the properties

incardon's avatar
incardon committed
382 383
	auto it3 = vd.getDomainIteratorGPU();

384
	// offload to device
incardon's avatar
incardon committed
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
	vd.hostToDevicePos();

	initialize_props<<<it3.wthr,it3.thr>>>(vd.toKernel());

	// now we check what we initialized

	vd.deviceToHostProp<0,1>();

	auto it4 = vd.getDomainIterator();

	while (it4.isNext())
	{
		auto p = it4.get();

		BOOST_REQUIRE_CLOSE(vd.template getProp<0>(p),vd.getPos(p)[0] + vd.getPos(p)[1] + vd.getPos(p)[2],0.01);

		BOOST_REQUIRE_CLOSE(vd.template getProp<1>(p)[0],vd.getPos(p)[0] + vd.getPos(p)[1],0.01);
		BOOST_REQUIRE_CLOSE(vd.template getProp<1>(p)[1],vd.getPos(p)[0] + vd.getPos(p)[2],0.01);
		BOOST_REQUIRE_CLOSE(vd.template getProp<1>(p)[2],vd.getPos(p)[1] + vd.getPos(p)[2],0.01);

		++it4;
	}

408 409 410
	// here we do a ghost_get
	vd.ghost_get<0>();

incardon's avatar
incardon committed
411
	// Double ghost get to check crashes
incardon's avatar
incardon committed
412 413
	vd.ghost_get<0>();

414 415 416 417
	// we re-offload what we received
	vd.hostToDevicePos();
	vd.template hostToDeviceProp<0>();

incardon's avatar
incardon committed
418
	auto NN = vd.template getCellListGPU<CellList_type>(0.1);
incardon's avatar
incardon committed
419
	auto NN_cpu = vd.getCellList(0.1);
420
	check_cell_list_cpu_and_gpu(vd,NN,NN_cpu);
incardon's avatar
incardon committed
421

incardon's avatar
incardon committed
422 423
	auto NN_up = vd.template getCellListGPU<CellList_type>(0.1);

incardon's avatar
incardon committed
424 425 426
	NN_up.clear();
	vd.updateCellList(NN_up);
	check_cell_list_cpu_and_gpu(vd,NN_up,NN_cpu);
incardon's avatar
incardon committed
427 428
}

incardon's avatar
incardon committed
429 430 431 432 433 434 435 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 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 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
template<typename CellList_type>
void vector_dist_gpu_make_sort_test_impl()
{
	auto & v_cl = create_vcluster();

	if (v_cl.size() > 16)
	{return;}

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

	// set the ghost based on the radius cut off (make just a little bit smaller than the spacing)
	Ghost<3,float> g(0.1);

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

	vector_dist_gpu<3,float,aggregate<float,float[3],float[3]>> vd(10000,domain,bc,g);

	srand(55067*create_vcluster().rank());

	auto it = vd.getDomainIterator();

	while (it.isNext())
	{
		auto p = it.get();

		int x = rand();
		int y = rand();
		int z = rand();

		vd.getPos(p)[0] = (float)x / RAND_MAX;
		vd.getPos(p)[1] = (float)y / RAND_MAX;
		vd.getPos(p)[2] = (float)z / RAND_MAX;

		++it;
	}

	vd.hostToDevicePos();

	// Ok we redistribute the particles
	vd.map(RUN_ON_DEVICE);

	auto it3 = vd.getDomainIteratorGPU();

	initialize_props<<<it3.wthr,it3.thr>>>(vd.toKernel());

	// Here we get do a make sort
	auto NN = vd.template getCellListGPU<CellList_type>(0.1);
	vd.make_sort(NN);

	openfpm::vector_gpu<aggregate<float,float[3],float[3]>> tmp_prp = vd.getPropVector();
	openfpm::vector_gpu<Point<3,float>> tmp_pos = vd.getPosVector();

	vd.deviceToHostPos();
	tmp_pos.template deviceToHost<0>();

	// here we do a ghost_get
	vd.ghost_get<0>(RUN_ON_DEVICE);

	// Here we get do a make sort
	NN = vd.template getCellListGPU<CellList_type>(0.1);

	vd.make_sort_from(NN);

	// Check

	tmp_pos.deviceToHost<0>();
	vd.deviceToHostPos();

	bool match = true;
	for (size_t i = 0 ; i < vd.size_local() ; i++)
	{
		Point<3,float> p1 = vd.getPos(i)[0];
		Point<3,float> p2 = tmp_pos.template get<0>(i);

		// They must be in the same cell
		auto c1 = NN.getCell(p1);
		auto c2 = NN.getCell(p1);

		match &= c1 == c2;
	}

	BOOST_REQUIRE_EQUAL(match,true);
}


BOOST_AUTO_TEST_CASE(vector_dist_gpu_make_sort_sparse)
{
	vector_dist_gpu_make_sort_test_impl<CELLLIST_GPU_SPARSE<3,float>>();
}

BOOST_AUTO_TEST_CASE(vector_dist_gpu_make_sort)
{
	vector_dist_gpu_make_sort_test_impl<CellList_gpu<3,float,CudaMemory,shift_only<3, float>>>();
}

incardon's avatar
incardon committed
525 526 527 528 529 530 531 532 533 534
BOOST_AUTO_TEST_CASE( vector_dist_gpu_test)
{
	vector_dist_gpu_test_impl<CellList_gpu<3,float,CudaMemory,shift_only<3, float>>>();
}

BOOST_AUTO_TEST_CASE( vector_dist_gpu_test_sparse)
{
	vector_dist_gpu_test_impl<CELLLIST_GPU_SPARSE<3,float>>();
}

incardon's avatar
incardon committed
535 536
template<typename St>
void vdist_calc_gpu_test()
incardon's avatar
Latest  
incardon committed
537 538 539 540 541 542
{
	auto & v_cl = create_vcluster();

	if (v_cl.size() > 16)
	{return;}

incardon's avatar
incardon committed
543 544 545 546 547 548 549 550
	Box<3,St> domain({0.0,0.0,0.0},{1.0,1.0,1.0});

	// set the ghost based on the radius cut off (make just a little bit smaller than the spacing)
	Ghost<3,St> g(0.1);

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

incardon's avatar
incardon committed
551 552
	//! [Create a gpu vector]

incardon's avatar
incardon committed
553
	vector_dist_gpu<3,St,aggregate<St,St[3],St[3]>> vd(1000,domain,bc,g);
incardon's avatar
incardon committed
554

incardon's avatar
incardon committed
555 556 557 558
	//! [Create a gpu vector]

	//! [Fill gpu vector and move to GPU]

incardon's avatar
incardon committed
559
	srand(v_cl.rank()*10000);
incardon's avatar
incardon committed
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 589
	auto it = vd.getDomainIterator();

	while (it.isNext())
	{
		auto p = it.get();

		vd.getPos(p)[0] = (St)rand() / RAND_MAX;
		vd.getPos(p)[1] = (St)rand() / RAND_MAX;
		vd.getPos(p)[2] = (St)rand() / RAND_MAX;

		vd.template getProp<0>(p) = vd.getPos(p)[0] + vd.getPos(p)[1] + vd.getPos(p)[2];

		vd.template getProp<1>(p)[0] = vd.getPos(p)[0];
		vd.template getProp<1>(p)[1] = vd.getPos(p)[1];
		vd.template getProp<1>(p)[2] = vd.getPos(p)[2];

		vd.template getProp<2>(p)[0] = vd.getPos(p)[0] + vd.getPos(p)[1];
		vd.template getProp<2>(p)[1] = vd.getPos(p)[0] + vd.getPos(p)[2];
		vd.template getProp<2>(p)[2] = vd.getPos(p)[1] + vd.getPos(p)[2];

		++it;
	}

	// move on device
	vd.hostToDevicePos();
	vd.template hostToDeviceProp<0,1,2>();

	// Ok we redistribute the particles (GPU based)
	vd.map(RUN_ON_DEVICE);

incardon's avatar
incardon committed
590 591
	//! [Fill gpu vector and move to GPU]

incardon's avatar
incardon committed
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 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 661 662 663 664 665 666 667 668 669 670
	vd.deviceToHostPos();
	vd.template deviceToHostProp<0,1,2>();

	// Reset the host part

	auto it3 = vd.getDomainIterator();

	while (it3.isNext())
	{
		auto p = it3.get();

		vd.getPos(p)[0] = 1.0;
		vd.getPos(p)[1] = 1.0;
		vd.getPos(p)[2] = 1.0;

		vd.template getProp<0>(p) = 0.0;

		vd.template getProp<0>(p) = 0.0;
		vd.template getProp<0>(p) = 0.0;
		vd.template getProp<0>(p) = 0.0;

		vd.template getProp<0>(p) = 0.0;
		vd.template getProp<0>(p) = 0.0;
		vd.template getProp<0>(p) = 0.0;

		++it3;
	}

	// we move from Device to CPU

	vd.deviceToHostPos();
	vd.template deviceToHostProp<0,1,2>();

	// Check

	auto it2 = vd.getDomainIterator();

	bool match = true;
	while (it2.isNext())
	{
		auto p = it2.get();

		match &= vd.template getProp<0>(p) == vd.getPos(p)[0] + vd.getPos(p)[1] + vd.getPos(p)[2];

		match &= vd.template getProp<1>(p)[0] == vd.getPos(p)[0];
		match &= vd.template getProp<1>(p)[1] == vd.getPos(p)[1];
		match &= vd.template getProp<1>(p)[2] == vd.getPos(p)[2];

		match &= vd.template getProp<2>(p)[0] == vd.getPos(p)[0] + vd.getPos(p)[1];
		match &= vd.template getProp<2>(p)[1] == vd.getPos(p)[0] + vd.getPos(p)[2];
		match &= vd.template getProp<2>(p)[2] == vd.getPos(p)[1] + vd.getPos(p)[2];

		++it2;
	}

	BOOST_REQUIRE_EQUAL(match,true);

	// count local particles

	size_t l_cnt = 0;
	size_t nl_cnt = 0;
	size_t n_out = 0;

	// Domain + ghost box
	Box<3,St> dom_ext = domain;
	dom_ext.enlarge(g);

	auto it5 = vd.getDomainIterator();
	count_local_n_local<3>(vd,it5,bc,domain,dom_ext,l_cnt,nl_cnt,n_out);

	BOOST_REQUIRE_EQUAL(n_out,0);
	BOOST_REQUIRE_EQUAL(l_cnt,vd.size_local());

	// we do 10 gpu steps (using a cpu vector to check that map and ghost get work as expented)

	for (size_t i = 0 ; i < 10 ; i++)
	{
		vd.map(RUN_ON_DEVICE);

incardon's avatar
incardon committed
671 672
		CUDA_SAFE(cudaGetLastError());

incardon's avatar
incardon committed
673 674 675 676 677
		vd.deviceToHostPos();
		vd.template deviceToHostProp<0,1,2>();

		// To test we copy on a cpu distributed vector and we do a map

incardon's avatar
incardon committed
678
		vector_dist<3,St,aggregate<St,St[3],St[3]>> vd_cpu(vd.getDecomposition().template duplicate_convert<HeapMemory,memory_traits_lin>(),0);
incardon's avatar
incardon committed
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

		auto itc = vd.getDomainIterator();

		while (itc.isNext())
		{
			auto p = itc.get();

			vd_cpu.add();

			vd_cpu.getLastPos()[0] = vd.getPos(p)[0];
			vd_cpu.getLastPos()[1] = vd.getPos(p)[1];
			vd_cpu.getLastPos()[2] = vd.getPos(p)[2];

			vd_cpu.template getLastProp<0>() = vd.template getProp<0>(p);

			vd_cpu.template getLastProp<1>()[0] = vd.template getProp<1>(p)[0];
			vd_cpu.template getLastProp<1>()[1] = vd.template getProp<1>(p)[1];
			vd_cpu.template getLastProp<1>()[2] = vd.template getProp<1>(p)[2];

			vd_cpu.template getLastProp<2>()[0] = vd.template getProp<2>(p)[0];
			vd_cpu.template getLastProp<2>()[1] = vd.template getProp<2>(p)[1];
			vd_cpu.template getLastProp<2>()[2] = vd.template getProp<2>(p)[2];

			++itc;
		}

		vd_cpu.template ghost_get<0,1,2>();
incardon's avatar
incardon committed
706 707 708

		//! [Fill the ghost on GPU]

incardon's avatar
incardon committed
709 710
		vd.template ghost_get<0,1,2>(RUN_ON_DEVICE);

incardon's avatar
incardon committed
711 712
		//! [Fill the ghost on GPU]

incardon's avatar
incardon committed
713 714 715 716 717 718 719 720 721 722 723
		vd.deviceToHostPos();
		vd.template deviceToHostProp<0,1,2>();

		match = true;

		// Particle on the gpu ghost and cpu ghost are not ordered in the same way so we have to reorder

		struct part
		{
			Point<3,St> xp;

incardon's avatar
incardon committed
724 725 726 727

			St prp0;
			St prp1[3];
			St prp2[3];
incardon's avatar
incardon committed
728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755

			bool operator<(const part & tmp) const
			{
				if (xp.get(0) < tmp.xp.get(0))
				{return true;}
				else if (xp.get(0) > tmp.xp.get(0))
				{return false;}

				if (xp.get(1) < tmp.xp.get(1))
				{return true;}
				else if (xp.get(1) > tmp.xp.get(1))
				{return false;}

				if (xp.get(2) < tmp.xp.get(2))
				{return true;}
				else if (xp.get(2) > tmp.xp.get(2))
				{return false;}

				return false;
			}
		};

		openfpm::vector<part> cpu_sort;
		openfpm::vector<part> gpu_sort;

		cpu_sort.resize(vd_cpu.size_local_with_ghost() - vd_cpu.size_local());
		gpu_sort.resize(vd.size_local_with_ghost() - vd.size_local());

incardon's avatar
incardon committed
756 757
		BOOST_REQUIRE_EQUAL(cpu_sort.size(),gpu_sort.size());

incardon's avatar
incardon committed
758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 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
		size_t cnt = 0;

		auto itc2 = vd.getGhostIterator();
		while (itc2.isNext())
		{
			auto p = itc2.get();

			cpu_sort.get(cnt).xp.get(0) = vd_cpu.getPos(p)[0];
			gpu_sort.get(cnt).xp.get(0) = vd.getPos(p)[0];
			cpu_sort.get(cnt).xp.get(1) = vd_cpu.getPos(p)[1];
			gpu_sort.get(cnt).xp.get(1) = vd.getPos(p)[1];
			cpu_sort.get(cnt).xp.get(2) = vd_cpu.getPos(p)[2];
			gpu_sort.get(cnt).xp.get(2) = vd.getPos(p)[2];

			cpu_sort.get(cnt).prp0 = vd_cpu.template getProp<0>(p);
			gpu_sort.get(cnt).prp0 = vd.template getProp<0>(p);

			cpu_sort.get(cnt).prp1[0] = vd_cpu.template getProp<1>(p)[0];
			gpu_sort.get(cnt).prp1[0] = vd.template getProp<1>(p)[0];
			cpu_sort.get(cnt).prp1[1] = vd_cpu.template getProp<1>(p)[1];
			gpu_sort.get(cnt).prp1[1] = vd.template getProp<1>(p)[1];
			cpu_sort.get(cnt).prp1[2] = vd_cpu.template getProp<1>(p)[2];
			gpu_sort.get(cnt).prp1[2] = vd.template getProp<1>(p)[2];

			cpu_sort.get(cnt).prp2[0] = vd_cpu.template getProp<2>(p)[0];
			gpu_sort.get(cnt).prp2[0] = vd.template getProp<2>(p)[0];
			cpu_sort.get(cnt).prp2[1] = vd_cpu.template getProp<2>(p)[1];
			gpu_sort.get(cnt).prp2[1] = vd.template getProp<2>(p)[1];
			cpu_sort.get(cnt).prp2[2] = vd_cpu.template getProp<2>(p)[2];
			gpu_sort.get(cnt).prp2[2] = vd.template getProp<2>(p)[2];

			++cnt;
			++itc2;
		}

		cpu_sort.sort();
		gpu_sort.sort();

		for (size_t i = 0 ; i < cpu_sort.size() ; i++)
		{
			match &= cpu_sort.get(i).xp.get(0) == gpu_sort.get(i).xp.get(0);
			match &= cpu_sort.get(i).xp.get(1) == gpu_sort.get(i).xp.get(1);
			match &= cpu_sort.get(i).xp.get(2) == gpu_sort.get(i).xp.get(2);

			match &= cpu_sort.get(i).prp0 == gpu_sort.get(i).prp0;
			match &= cpu_sort.get(i).prp1[0] == gpu_sort.get(i).prp1[0];
			match &= cpu_sort.get(i).prp1[1] == gpu_sort.get(i).prp1[1];
			match &= cpu_sort.get(i).prp1[2] == gpu_sort.get(i).prp1[2];

			match &= cpu_sort.get(i).prp2[0] == gpu_sort.get(i).prp2[0];
			match &= cpu_sort.get(i).prp2[1] == gpu_sort.get(i).prp2[1];
			match &= cpu_sort.get(i).prp2[2] == gpu_sort.get(i).prp2[2];
		}

		BOOST_REQUIRE_EQUAL(match,true);

		// move particles on gpu

		auto ite = vd.getDomainIteratorGPU();
		move_parts_gpu_test<3,decltype(vd.toKernel())><<<ite.wthr,ite.thr>>>(vd.toKernel());
	}
}

BOOST_AUTO_TEST_CASE( vector_dist_map_on_gpu_test)
{
	vdist_calc_gpu_test<float>();
	vdist_calc_gpu_test<double>();
825
}
incardon's avatar
incardon committed
826

827 828 829
BOOST_AUTO_TEST_CASE(vector_dist_reduce)
{
	auto & v_cl = create_vcluster();
incardon's avatar
incardon committed
830 831 832 833

	if (v_cl.size() > 16)
	{return;}

incardon's avatar
Latest  
incardon committed
834 835 836 837 838 839
	Box<3,float> domain({0.0,0.0,0.0},{1.0,1.0,1.0});

	// set the ghost based on the radius cut off (make just a little bit smaller than the spacing)
	Ghost<3,float> g(0.1);

	// Boundary conditions
incardon's avatar
incardon committed
840
	size_t bc[3]={PERIODIC,PERIODIC,PERIODIC};
incardon's avatar
Latest  
incardon committed
841

842
	vector_dist_gpu<3,float,aggregate<float,double,int,size_t>> vd(5000*v_cl.size(),domain,bc,g);
incardon's avatar
Latest  
incardon committed
843 844 845

	auto it = vd.getDomainIterator();

846 847 848 849 850 851
	float fc = 1.0;
	double dc = 1.0;
	int ic = 1.0;
	size_t sc = 1.0;

	while(it.isNext())
incardon's avatar
Latest  
incardon committed
852 853 854
	{
		auto p = it.get();

855 856 857 858
		vd.template getProp<0>(p) = fc;
		vd.template getProp<1>(p) = dc;
		vd.template getProp<2>(p) = ic;
		vd.template getProp<3>(p) = sc;
incardon's avatar
Latest  
incardon committed
859

860 861 862 863
		fc += 1.0;
		dc += 1.0;
		ic += 1;
		sc += 1;
864

incardon's avatar
Latest  
incardon committed
865 866 867
		++it;
	}

868
	vd.template hostToDeviceProp<0,1,2,3>();
incardon's avatar
incardon committed
869

870 871 872 873
	float redf = reduce_local<0,_add_>(vd);
	double redd = reduce_local<1,_add_>(vd);
	int redi = reduce_local<2,_add_>(vd);
	size_t reds = reduce_local<3,_add_>(vd);
incardon's avatar
incardon committed
874

875 876 877 878
	BOOST_REQUIRE_EQUAL(redf,(vd.size_local()+1.0)*(vd.size_local())/2.0);
	BOOST_REQUIRE_EQUAL(redd,(vd.size_local()+1.0)*(vd.size_local())/2.0);
	BOOST_REQUIRE_EQUAL(redi,(vd.size_local()+1)*(vd.size_local())/2);
	BOOST_REQUIRE_EQUAL(reds,(vd.size_local()+1)*(vd.size_local())/2);
incardon's avatar
incardon committed
879

880 881 882 883
	float redf2 = reduce_local<0,_max_>(vd);
	double redd2 = reduce_local<1,_max_>(vd);
	int redi2 = reduce_local<2,_max_>(vd);
	size_t reds2 = reduce_local<3,_max_>(vd);
incardon's avatar
incardon committed
884 885 886 887 888

	BOOST_REQUIRE_EQUAL(redf2,vd.size_local());
	BOOST_REQUIRE_EQUAL(redd2,vd.size_local());
	BOOST_REQUIRE_EQUAL(redi2,vd.size_local());
	BOOST_REQUIRE_EQUAL(reds2,vd.size_local());
incardon's avatar
Latest  
incardon committed
889 890
}

incardon's avatar
incardon committed
891
template<typename CellList_type>
892
void vector_dist_dlb_on_cuda_impl(size_t k,double r_cut)
incardon's avatar
incardon committed
893
{
incardon's avatar
Fixing  
incardon committed
894
	typedef vector_dist_gpu<3,double,aggregate<double,double[3],double[3]>> vector_type;
incardon's avatar
incardon committed
895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910

	Vcluster<> & v_cl = create_vcluster();

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

	Box<3,double> domain({0.0,0.0,0.0},{1.0,1.0,1.0});
	Ghost<3,double> g(0.1);
	size_t bc[3] = {PERIODIC,PERIODIC,PERIODIC};

	vector_type vd(0,domain,bc,g,DEC_GRAN(2048));

	// Only processor 0 initialy add particles on a corner of a domain

	if (v_cl.getProcessUnitID() == 0)
	{
911
		for(size_t i = 0 ; i < k ; i++)
incardon's avatar
incardon committed
912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934
		{
			vd.add();

			vd.getLastPos()[0] = ((double)rand())/RAND_MAX * 0.3;
			vd.getLastPos()[1] = ((double)rand())/RAND_MAX * 0.3;
			vd.getLastPos()[2] = ((double)rand())/RAND_MAX * 0.3;
		}
	}

	// Move to GPU
	vd.hostToDevicePos();
	vd.template hostToDeviceProp<0>();

	vd.map(RUN_ON_DEVICE);
	vd.template ghost_get<>(RUN_ON_DEVICE);

	// now move to CPU

	vd.deviceToHostPos();
	vd.template deviceToHostProp<0>();

	// Get the neighborhood of each particles

935
	auto VV = vd.getVerlet(r_cut);
incardon's avatar
incardon committed
936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967

	// store the number of neighborhood for each particles

	auto it = vd.getDomainIterator();

	while (it.isNext())
	{
		auto p = it.get();

		vd.template getProp<0>(p) = VV.getNNPart(p.getKey());

		++it;
	}

	// Move to GPU
	vd.template hostToDeviceProp<0>();

	ModelSquare md;
	md.factor = 10;
	vd.addComputationCosts(md);
	vd.getDecomposition().decompose();
	vd.map(RUN_ON_DEVICE);

	vd.deviceToHostPos();
	// Move info to CPU for addComputationcosts

	vd.addComputationCosts(md);

	openfpm::vector<size_t> loads;
	size_t load = vd.getDecomposition().getDistribution().getProcessorLoad();
	v_cl.allGather(load,loads);
	v_cl.execute();
968

incardon's avatar
incardon committed
969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002
	for (size_t i = 0 ; i < loads.size() ; i++)
	{
		double load_f = load;
		double load_fc = loads.get(i);

		BOOST_REQUIRE_CLOSE(load_f,load_fc,7.0);
	}

	BOOST_REQUIRE(vd.size_local() != 0);

	Point<3,double> v({1.0,1.0,1.0});

	for (size_t i = 0 ; i < 25 ; i++)
	{
		// move particles to CPU and move the particles by 0.1

		vd.deviceToHostPos();

		auto it = vd.getDomainIterator();

		while (it.isNext())
		{
			auto p = it.get();

			vd.getPos(p)[0] += v.get(0) * 0.09;
			vd.getPos(p)[1] += v.get(1) * 0.09;
			vd.getPos(p)[2] += v.get(2) * 0.09;

			++it;
		}

		//Back to GPU
		vd.hostToDevicePos();
		vd.map(RUN_ON_DEVICE);
incardon's avatar
Fixing  
incardon committed
1003
		vd.template ghost_get<0>(RUN_ON_DEVICE);
incardon's avatar
incardon committed
1004
		vd.deviceToHostPos();
incardon's avatar
Fixing  
incardon committed
1005 1006 1007
		vd.template deviceToHostProp<0,1,2>();

		// Check calc forces
incardon's avatar
incardon committed
1008
		auto NN_gpu = vd.template getCellListGPU<CellList_type>(r_cut);
incardon's avatar
Fixing  
incardon committed
1009 1010
		auto NN_cpu = vd.getCellList(r_cut);
		check_cell_list_cpu_and_gpu(vd,NN_gpu,NN_cpu);
incardon's avatar
incardon committed
1011

1012
		auto VV2 = vd.getVerlet(r_cut);
incardon's avatar
incardon committed
1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031

		auto it2 = vd.getDomainIterator();

		bool match = true;
		while (it2.isNext())
		{
			auto p = it2.get();

			match &= vd.template getProp<0>(p) == VV2.getNNPart(p.getKey());

			++it2;
		}

		BOOST_REQUIRE_EQUAL(match,true);

		ModelSquare md;
		vd.addComputationCosts(md);
		vd.getDecomposition().redecompose(200);
		vd.map(RUN_ON_DEVICE);
incardon's avatar
incardon committed
1032

incardon's avatar
incardon committed
1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054
		BOOST_REQUIRE(vd.size_local() != 0);

		vd.template ghost_get<0>(RUN_ON_DEVICE);
		vd.deviceToHostPos();
		vd.template deviceToHostProp<0>();

		vd.addComputationCosts(md);

		openfpm::vector<size_t> loads;
		size_t load = vd.getDecomposition().getDistribution().getProcessorLoad();
		v_cl.allGather(load,loads);
		v_cl.execute();

		for (size_t i = 0 ; i < loads.size() ; i++)
		{
			double load_f = load;
			double load_fc = loads.get(i);

			BOOST_REQUIRE_CLOSE(load_f,load_fc,10.0);
		}
	}
}
incardon's avatar
incardon committed
1055

incardon's avatar
incardon committed
1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235
template<typename CellList_type>
void vector_dist_dlb_on_cuda_impl_async(size_t k,double r_cut)
{
	typedef vector_dist_gpu<3,double,aggregate<double,double[3],double[3]>> vector_type;

	Vcluster<> & v_cl = create_vcluster();

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

	Box<3,double> domain({0.0,0.0,0.0},{1.0,1.0,1.0});
	Ghost<3,double> g(0.1);
	size_t bc[3] = {PERIODIC,PERIODIC,PERIODIC};

	vector_type vd(0,domain,bc,g,DEC_GRAN(2048));

	// Only processor 0 initialy add particles on a corner of a domain

	if (v_cl.getProcessUnitID() == 0)
	{
		for(size_t i = 0 ; i < k ; i++)
		{
			vd.add();

			vd.getLastPos()[0] = ((double)rand())/RAND_MAX * 0.3;
			vd.getLastPos()[1] = ((double)rand())/RAND_MAX * 0.3;
			vd.getLastPos()[2] = ((double)rand())/RAND_MAX * 0.3;
		}
	}

	// Move to GPU
	vd.hostToDevicePos();
	vd.template hostToDeviceProp<0>();

	vd.map(RUN_ON_DEVICE);
	vd.template Ighost_get<>(RUN_ON_DEVICE);
	vd.template ghost_wait<>(RUN_ON_DEVICE);

	// now move to CPU

	vd.deviceToHostPos();
	vd.template deviceToHostProp<0>();

	// Get the neighborhood of each particles

	auto VV = vd.getVerlet(r_cut);

	// store the number of neighborhood for each particles

	auto it = vd.getDomainIterator();

	while (it.isNext())
	{
		auto p = it.get();

		vd.template getProp<0>(p) = VV.getNNPart(p.getKey());

		++it;
	}

	// Move to GPU
	vd.template hostToDeviceProp<0>();

	ModelSquare md;
	md.factor = 10;
	vd.addComputationCosts(md);
	vd.getDecomposition().decompose();
	vd.map(RUN_ON_DEVICE);

	vd.deviceToHostPos();
	// Move info to CPU for addComputationcosts

	vd.addComputationCosts(md);

	openfpm::vector<size_t> loads;
	size_t load = vd.getDecomposition().getDistribution().getProcessorLoad();
	v_cl.allGather(load,loads);
	v_cl.execute();

	for (size_t i = 0 ; i < loads.size() ; i++)
	{
		double load_f = load;
		double load_fc = loads.get(i);

		BOOST_REQUIRE_CLOSE(load_f,load_fc,7.0);
	}

	BOOST_REQUIRE(vd.size_local() != 0);

	Point<3,double> v({1.0,1.0,1.0});

	for (size_t i = 0 ; i < 25 ; i++)
	{
		// move particles to CPU and move the particles by 0.1

		vd.deviceToHostPos();

		auto it = vd.getDomainIterator();

		while (it.isNext())
		{
			auto p = it.get();

			vd.getPos(p)[0] += v.get(0) * 0.09;
			vd.getPos(p)[1] += v.get(1) * 0.09;
			vd.getPos(p)[2] += v.get(2) * 0.09;

			++it;
		}

		//Back to GPU
		vd.hostToDevicePos();
		vd.map(RUN_ON_DEVICE);
		vd.template Ighost_get<0>(RUN_ON_DEVICE);
		vd.template ghost_wait<0>(RUN_ON_DEVICE);
		vd.deviceToHostPos();
		vd.template deviceToHostProp<0,1,2>();

		// Check calc forces
		auto NN_gpu = vd.template getCellListGPU<CellList_type>(r_cut);
		auto NN_cpu = vd.getCellList(r_cut);
		check_cell_list_cpu_and_gpu(vd,NN_gpu,NN_cpu);

		auto VV2 = vd.getVerlet(r_cut);

		auto it2 = vd.getDomainIterator();

		bool match = true;
		while (it2.isNext())
		{
			auto p = it2.get();

			match &= vd.template getProp<0>(p) == VV2.getNNPart(p.getKey());

			++it2;
		}

		BOOST_REQUIRE_EQUAL(match,true);

		ModelSquare md;
		vd.addComputationCosts(md);
		vd.getDecomposition().redecompose(200);
		vd.map(RUN_ON_DEVICE);

		BOOST_REQUIRE(vd.size_local() != 0);

//		vd.template ghost_get<0>(RUN_ON_DEVICE);
		if (i == 9)
		{
			int debug = 0;
			debug++;
		}
//		vd.template ghost_get<0>(RUN_ON_DEVICE);
		vd.template Ighost_get<0>(RUN_ON_DEVICE);
		vd.template ghost_wait<0>(RUN_ON_DEVICE);
		vd.deviceToHostPos();
		vd.template deviceToHostProp<0>();

		vd.addComputationCosts(md);

		openfpm::vector<size_t> loads;
		size_t load = vd.getDecomposition().getDistribution().getProcessorLoad();
		v_cl.allGather(load,loads);
		v_cl.execute();

		for (size_t i = 0 ; i < loads.size() ; i++)
		{
			double load_f = load;
			double load_fc = loads.get(i);

			BOOST_REQUIRE_CLOSE(load_f,load_fc,10.0);
		}
	}
}

BOOST_AUTO_TEST_CASE(vector_dist_dlb_on_cuda_async)
{
	vector_dist_dlb_on_cuda_impl_async<CellList_gpu<3,double,CudaMemory,shift_only<3,double>,unsigned int,int,false>>(50000,0.01);
}

1236 1237
BOOST_AUTO_TEST_CASE(vector_dist_dlb_on_cuda)
{
incardon's avatar
incardon committed
1238 1239 1240 1241 1242 1243
	vector_dist_dlb_on_cuda_impl<CellList_gpu<3,double,CudaMemory,shift_only<3,double>,unsigned int,int,false>>(50000,0.01);
}

BOOST_AUTO_TEST_CASE(vector_dist_dlb_on_cuda_sparse)
{
	vector_dist_dlb_on_cuda_impl<CELLLIST_GPU_SPARSE<3,double>>(50000,0.01);
1244 1245 1246 1247
}

BOOST_AUTO_TEST_CASE(vector_dist_dlb_on_cuda2)
{
incardon's avatar
incardon committed
1248
	if (create_vcluster().size() <= 3)
1249 1250
	{return;};

incardon's avatar
incardon committed
1251
	vector_dist_dlb_on_cuda_impl<CellList_gpu<3,double,CudaMemory,shift_only<3,double>,unsigned int,int,false>>(1000000,0.01);
1252 1253 1254 1255 1256 1257 1258
}

BOOST_AUTO_TEST_CASE(vector_dist_dlb_on_cuda3)
{
	if (create_vcluster().size() < 8)
	{return;}

incardon's avatar
incardon committed
1259
	vector_dist_dlb_on_cuda_impl<CellList_gpu<3,double,CudaMemory,shift_only<3,double>,unsigned int,int,false>>(15000000,0.005);
1260 1261 1262
}


incardon's avatar
incardon committed
1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368
BOOST_AUTO_TEST_CASE(vector_dist_keep_prop_on_cuda)
{
	typedef vector_dist_gpu<3,double,aggregate<double,double[3],double[3][3]>> vector_type;

	Vcluster<> & v_cl = create_vcluster();

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

	Box<3,double> domain({0.0,0.0,0.0},{1.0,1.0,1.0});
	Ghost<3,double> g(0.1);
	size_t bc[3] = {PERIODIC,PERIODIC,PERIODIC};

	vector_type vd(0,domain,bc,g,DEC_GRAN(2048));

	// Only processor 0 initialy add particles on a corner of a domain

	if (v_cl.getProcessUnitID() == 0)
	{
		for(size_t i = 0 ; i < 50000 ; i++)
		{
			vd.add();

			vd.getLastPos()[0] = ((double)rand())/RAND_MAX * 0.3;
			vd.getLastPos()[1] = ((double)rand())/RAND_MAX * 0.3;
			vd.getLastPos()[2] = ((double)rand())/RAND_MAX * 0.3;
		}
	}

	// Move to GPU
	vd.hostToDevicePos();
	vd.template hostToDeviceProp<0>();

	vd.map(RUN_ON_DEVICE);
	vd.template ghost_get<>(RUN_ON_DEVICE);

	// now move to CPU

	vd.deviceToHostPos();
	vd.template deviceToHostProp<0>();


	// store the number of neighborhood for each particles

	auto it = vd.getDomainIterator();

	while (it.isNext())
	{
		auto p = it.get();

		vd.template getProp<0>(p) = 0.0;

		vd.template getProp<1>(p)[0] = 1000.0;
		vd.template getProp<1>(p)[1] = 2000.0;
		vd.template getProp<1>(p)[2] = 3000.0;

		vd.template getProp<2>(p)[0][0] = 6000,0;
		vd.template getProp<2>(p)[0][1] = 7000.0;
		vd.template getProp<2>(p)[0][2] = 8000.0;
		vd.template getProp<2>(p)[1][0] = 9000.0;
		vd.template getProp<2>(p)[1][1] = 10000.0;
		vd.template getProp<2>(p)[1][2] = 11000.0;
		vd.template getProp<2>(p)[2][0] = 12000.0;
		vd.template getProp<2>(p)[2][1] = 13000.0;
		vd.template getProp<2>(p)[2][2] = 14000.0;

		++it;
	}

	// Move to GPU
	vd.template hostToDeviceProp<0,1,2>();

	ModelSquare md;
	md.factor = 10;
	vd.addComputationCosts(md);
	vd.getDecomposition().decompose();
	vd.map(RUN_ON_DEVICE);

	vd.deviceToHostPos();
	// Move info to CPU for addComputationcosts

	vd.addComputationCosts(md);

	openfpm::vector<size_t> loads;
	size_t load = vd.getDecomposition().getDistribution().getProcessorLoad();
	v_cl.allGather(load,loads);
	v_cl.execute();

	for (size_t i = 0 ; i < loads.size() ; i++)
	{
		double load_f = load;
		double load_fc = loads.get(i);

		BOOST_REQUIRE_CLOSE(load_f,load_fc,7.0);
	}

	BOOST_REQUIRE(vd.size_local() != 0);

	Point<3,double> v({1.0,1.0,1.0});

	int base = 0;

	for (size_t i = 0 ; i < 25 ; i++)
	{
		if (i % 2 == 0)
		{
incardon's avatar
incardon committed
1369 1370 1371
			// move particles to CPU and move the particles by 0.1

			vd.deviceToHostPos();
incardon's avatar
incardon committed
1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420

			auto it = vd.getDomainIterator();

			while (it.isNext())
			{
				auto p = it.get();

				vd.getPos(p)[0] += v.get(0) * 0.09;
				vd.getPos(p)[1] += v.get(1) * 0.09;
				vd.getPos(p)[2] += v.get(2) * 0.09;

				++it;
			}

			//Back to GPU
			vd.hostToDevicePos();
			vd.map(RUN_ON_DEVICE);
			vd.template ghost_get<>(RUN_ON_DEVICE);
			vd.deviceToHostPos();
			vd.template deviceToHostProp<0,1,2>();

			ModelSquare md;
			vd.addComputationCosts(md);
			vd.getDecomposition().redecompose(200);
			vd.map(RUN_ON_DEVICE);

			BOOST_REQUIRE(vd.size_local() != 0);

			vd.template ghost_get<0>(RUN_ON_DEVICE);
			vd.deviceToHostPos();
			vd.template deviceToHostProp<0,1,2>();

			vd.addComputationCosts(md);

			openfpm::vector<size_t> loads;
			size_t load = vd.getDecomposition().getDistribution().getProcessorLoad();
			v_cl.allGather(load,loads);
			v_cl.execute();

			for (size_t i = 0 ; i < loads.size() ; i++)
			{
				double load_f = load;
				double load_fc = loads.get(i);

				BOOST_REQUIRE_CLOSE(load_f,load_fc,10.0);
			}
		}
		else
		{
incardon's avatar
incardon committed
1421
			vd.template deviceToHostProp<0,1,2>();
incardon's avatar
incardon committed
1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448

			auto it2 = vd.getDomainIterator();

			bool match = true;
			while (it2.isNext())
			{
				auto p = it2.get();

				vd.template getProp<0>(p) += 1;

				vd.template getProp<1>(p)[0] += 1.0;
				vd.template getProp<1>(p)[1] += 1.0;
				vd.template getProp<1>(p)[2] += 1.0;

				vd.template getProp<2>(p)[0][0] += 1.0;
				vd.template getProp<2>(p)[0][1] += 1.0;
				vd.template getProp<2>(p)[0][2] += 1.0;
				vd.template getProp<2>(p)[1][0] += 1.0;
				vd.template getProp<2>(p)[1][1] += 1.0;
				vd.template getProp<2>(p)[1][2] += 1.0;
				vd.template getProp<2>(p)[2][0] += 1.0;
				vd.template getProp<2>(p)[2][1] += 1.0;
				vd.template getProp<2>(p)[2][2] += 1.0;

				++it2;
			}

incardon's avatar
incardon committed
1449 1450
			vd.template hostToDeviceProp<0,1,2>();

incardon's avatar
incardon committed
1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487
			++base;

			vd.template ghost_get<0,1,2>(RUN_ON_DEVICE | KEEP_PROPERTIES);
			vd.template deviceToHostProp<0,1,2>();

			// Check that the ghost contain the correct information

			auto itg = vd.getGhostIterator();

			while (itg.isNext())
			{
				auto p = itg.get();

				match &= vd.template getProp<0>(p) == base;

				match &= vd.template getProp<1>(p)[0] == base + 1000.0;
				match &= vd.template getProp<1>(p)[1] == base + 2000.0;
				match &= vd.template getProp<1>(p)[2] == base + 3000.0;

				match &= vd.template getProp<2>(p)[0][0] == base + 6000.0;
				match &= vd.template getProp<2>(p)[0][1] == base + 7000.0;
				match &= vd.template getProp<2>(p)[0][2] == base + 8000.0;
				match &= vd.template getProp<2>(p)[1][0] == base + 9000.0;
				match &= vd.template getProp<2>(p)[1][1] == base + 10000.0;
				match &= vd.template getProp<2>(p)[1][2] == base + 11000.0;
				match &= vd.template getProp<2>(p)[2][0] == base + 12000.0;
				match &= vd.template getProp<2>(p)[2][1] == base + 13000.0;
				match &= vd.template getProp<2>(p)[2][2] == base + 14000.0;

				++itg;
			}

			BOOST_REQUIRE_EQUAL(match,true);
		}
	}
}

incardon's avatar
incardon committed
1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600

BOOST_AUTO_TEST_CASE(vector_dist_compare_host_device)
{
	Box<3,double> domain({0.0,0.0,0.0},{1.0,1.0,1.0});
	Ghost<3,double> g(0.1);
	size_t bc[3] = {PERIODIC,PERIODIC,PERIODIC};

	if (create_vcluster().size() >= 16)
	{return;}

	vector_dist_gpu<3,double,aggregate<double,double[3],double[3][3]>> vdg(10000,domain,bc,g,DEC_GRAN(128));

	auto it = vdg.getDomainIterator();

	while (it.isNext())
	{
		auto p = it.get();

		vdg.getPos(p)[0] = (double)rand() / RAND_MAX;
		vdg.getPos(p)[1] = (double)rand() / RAND_MAX;
		vdg.getPos(p)[2] = (double)rand() / RAND_MAX;

		vdg.template getProp<0>(p) = (double)rand() / RAND_MAX;

		vdg.template getProp<1>(p)[0] = (double)rand() / RAND_MAX;
		vdg.template getProp<1>(p)[1] = (double)rand() / RAND_MAX;
		vdg.template getProp<1>(p)[2] = (double)rand() / RAND_MAX;

		vdg.template getProp<2>(p)[0][0] = (double)rand() / RAND_MAX;
		vdg.template getProp<2>(p)[0][1] = (double)rand() / RAND_MAX;
		vdg.template getProp<2>(p)[0][2] = (double)rand() / RAND_MAX;
		vdg.template getProp<2>(p)[1][0] = (double)rand() / RAND_MAX;
		vdg.template getProp<2>(p)[1][1] = (double)rand() / RAND_MAX;
		vdg.template getProp<2>(p)[1][2] = (double)rand() / RAND_MAX;
		vdg.template getProp<2>(p)[2][0] = (double)rand() / RAND_MAX;
		vdg.template getProp<2>(p)[2][1] = (double)rand() / RAND_MAX;
		vdg.template getProp<2>(p)[2][2] = (double)rand() / RAND_MAX;

		++it;
	}

	vdg.map();

	vdg.hostToDeviceProp<0,1,2>();
	vdg.hostToDevicePos();

	bool test = vdg.compareHostAndDevicePos(0.00001,0.00000001);
	BOOST_REQUIRE_EQUAL(test,true);

	vdg.getPos(100)[0] = 0.99999999;

	test = vdg.compareHostAndDevicePos(0.00001,0.00000001);
	BOOST_REQUIRE_EQUAL(test,false);

	vdg.hostToDevicePos();
	vdg.getPos(100)[0] = 0.99999999;

	test = vdg.compareHostAndDevicePos(0.00001,0.00000001);
	BOOST_REQUIRE_EQUAL(test,true);

	////////////////////////////////////////////////// PROP VECTOR

	test = vdg.compareHostAndDeviceProp<1>(0.00001,0.00000001);
	BOOST_REQUIRE_EQUAL(test,true);

	vdg.getProp<1>(103)[0] = 0.99999999;

	test = vdg.compareHostAndDeviceProp<1>(0.00001,0.00000001);
	BOOST_REQUIRE_EQUAL(test,false);

	vdg.hostToDeviceProp<1>();
	vdg.getProp<1>(103)[0] = 0.99999999;

	test = vdg.compareHostAndDeviceProp<1>(0.00001,0.00000001);
	BOOST_REQUIRE_EQUAL(test,true);

	////////////////////////////////////////////////// PROP scalar


	test = vdg.compareHostAndDeviceProp<0>(0.00001,0.00000001);
	BOOST_REQUIRE_EQUAL(test,true);

	vdg.getProp<0>(105) = 0.99999999;

	test = vdg.compareHostAndDeviceProp<0>(0.00001,0.00000001);
	BOOST_REQUIRE_EQUAL(test,false);

	vdg.hostToDeviceProp<0>();
	vdg.getProp<0>(105) = 0.99999999;

	test = vdg.compareHostAndDeviceProp<0>(0.00001,0.00000001);
	BOOST_REQUIRE_EQUAL(test,true);


	////////////////////////////////////////////////// PROP scalar


	test = vdg.compareHostAndDeviceProp<2>(0.00001,0.00000001);
	BOOST_REQUIRE_EQUAL(test,true);

	vdg.getProp<2>(108)[1][2] = 0.99999999;

	test = vdg.compareHostAndDeviceProp<2>(0.00001,0.00000001);
	BOOST_REQUIRE_EQUAL(test,false);

	vdg.hostToDeviceProp<2>();
	vdg.getProp<2&