VCluster_meta_function.hpp 21.8 KB
Newer Older
incardon's avatar
incardon committed
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 * VCluster_meta_function.hpp
 *
 *  Created on: Dec 8, 2016
 *      Author: i-bird
 */

#ifndef OPENFPM_VCLUSTER_SRC_VCLUSTER_VCLUSTER_META_FUNCTION_HPP_
#define OPENFPM_VCLUSTER_SRC_VCLUSTER_VCLUSTER_META_FUNCTION_HPP_

#include "memory/BHeapMemory.hpp"
#include "Packer_Unpacker/has_max_prop.hpp"

incardon's avatar
incardon committed
14
template<bool result, typename T, typename S, template<typename> class layout_base>
incardon's avatar
incardon committed
15 16
struct unpack_selector_with_prp
{
incardon's avatar
incardon committed
17 18 19
	template<typename op,
			 int ... prp>
	static void call_unpack(S & recv,
20
			                openfpm::vector<BMemory<HeapMemory>> & recv_buf,
incardon's avatar
incardon committed
21 22
							openfpm::vector<size_t> * sz,
							openfpm::vector<size_t> * sz_byte,
incardon's avatar
incardon committed
23 24
							op & op_param,
							size_t opt)
incardon's avatar
incardon committed
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
	{
		if (sz_byte != NULL)
			sz_byte->resize(recv_buf.size());

		for (size_t i = 0 ; i < recv_buf.size() ; i++)
		{
			T unp;

			ExtPreAlloc<HeapMemory> & mem = *(new ExtPreAlloc<HeapMemory>(recv_buf.get(i).size(),recv_buf.get(i)));
			mem.incRef();

			Unpack_stat ps;

			Unpacker<T,HeapMemory>::template unpack<>(mem, unp, ps);

			size_t recv_size_old = recv.size();
			// Merge the information

incardon's avatar
incardon committed
43
			op_param.template execute<true,T,decltype(recv),decltype(unp),layout_base,prp...>(recv,unp,i,opt);
incardon's avatar
incardon committed
44 45 46 47 48 49 50 51 52 53 54 55 56 57

			size_t recv_size_new = recv.size();

			if (sz_byte != NULL)
				sz_byte->get(i) = recv_buf.get(i).size();
			if (sz != NULL)
				sz->get(i) = recv_size_new - recv_size_old;

			mem.decRef();
			delete &mem;
		}
	}
};

incardon's avatar
incardon committed
58 59
template<typename op, typename Vt, typename S, template<typename> class layout_base, typename v_mpl>
struct unpack_each_prop_buffer
incardon's avatar
incardon committed
60
{
incardon's avatar
incardon committed
61 62
	S & recv;

63
	openfpm::vector<BMemory<HeapMemory>> & recv_buf;
incardon's avatar
incardon committed
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78

	size_t i;

	op & op_param;

    openfpm::vector<size_t> * sz;

    openfpm::vector<size_t> * sz_byte;

	/*! \brief constructor
	 *
	 * \param v set of pointer buffers to set
	 *
	 */
	inline unpack_each_prop_buffer(S & recv,
79
			                       openfpm::vector<BMemory<HeapMemory>> & recv_buf,
incardon's avatar
incardon committed
80 81 82 83 84 85 86 87 88 89
			                       op & op_param,
			                       size_t i,
			                       openfpm::vector<size_t> * sz,
			                       openfpm::vector<size_t> * sz_byte)
	:recv(recv),recv_buf(recv_buf),op_param(op_param),i(i),sz(sz),sz_byte(sz_byte)
	{};

	//! It call the copy function for each property
	template<typename T>
	inline void operator()(T& t) const
incardon's avatar
incardon committed
90
	{
incardon's avatar
incardon committed
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
		// here we get the the type of the property at position T::value
		typedef typename boost::mpl::at<typename T::value_type::type,boost::mpl::int_<T::value> >::type prp_type;

		// here we get the the type of the property at position T::value
		typedef typename boost::mpl::at<v_mpl,boost::mpl::int_<T::value>>::type prp_num;

		// calculate the number of received elements
		size_t n_ele = recv_buf.get(i).size() / sizeof(prp_type);

		// add the received particles to the vector
		PtrMemory * ptr1 = new PtrMemory(recv_buf.get(i).getPointer(),recv_buf.get(i).size());

		// create vector representation to a piece of memory already allocated
		openfpm::vector<typename Vt::value_type,PtrMemory,typename layout_base<typename Vt::value_type>::type,layout_base,openfpm::grow_policy_identity> v2;

		v2.template setMemory<prp_num::value>(*ptr1);

		// resize with the number of elements
		v2.resize(n_ele);

		// Merge the information

		size_t recv_size_old = recv.size();

		op_param.template execute<false,T,decltype(recv),decltype(v2),layout_base,prp_num::value>(recv,v2,i);

		size_t recv_size_new = recv.size();

incardon's avatar
incardon committed
119
		if (sz_byte != NULL)
incardon's avatar
incardon committed
120 121 122 123 124
			sz_byte->get(i) = recv_buf.get(i).size();
		if (sz != NULL)
			sz->get(i) = recv_size_new - recv_size_old;
	}
};
incardon's avatar
incardon committed
125

incardon's avatar
incardon committed
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
/*! \brief this class is a functor for "for_each" algorithm
 *
 * This class is a functor for "for_each" algorithm. For each
 * element of the boost::vector the operator() is called.
 * Is mainly used to process the receive buffers in case of memory_traits_inte layout receive
 *
 * \tparam encap source
 * \tparam encap dst
 *
 */

template<typename sT, template<typename> class layout_base>
struct process_receive_mem_traits_inte
{
	//! set of pointers
	size_t i;

	//! Receive buffer
144
	openfpm::vector<BMemory<HeapMemory>> & recv_buf;
incardon's avatar
incardon committed
145 146 147 148 149 150

	//! Fake vector that map over received memory
	openfpm::vector<typename sT::value_type,PtrMemory,typename layout_base<typename sT::value_type>::type,layout_base,openfpm::grow_policy_identity> & v2;

	size_t n_ele = 0;

151 152 153
	// options
	size_t opt;

incardon's avatar
incardon committed
154 155 156 157 158 159
	/*! \brief constructor
	 *
	 * \param v set of pointer buffers to set
	 *
	 */
	inline process_receive_mem_traits_inte(openfpm::vector<typename sT::value_type,PtrMemory,typename layout_base<typename sT::value_type>::type,layout_base,openfpm::grow_policy_identity> & v2,
160 161 162 163
			                               openfpm::vector<BMemory<HeapMemory>> & recv_buf,
			                               size_t i,
			                               size_t opt)
	:i(i),recv_buf(recv_buf),v2(v2),opt(opt)
incardon's avatar
incardon committed
164 165 166 167 168 169 170 171 172 173 174
	{};

	//! It call the copy function for each property
	template<typename T>
	inline void operator()(T& t)
	{
		typedef typename boost::mpl::at<typename sT::value_type::type,T>::type type_prp;

		// calculate the number of received elements
		this->n_ele = recv_buf.get(i).size() / sizeof(type_prp);

175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
		PtrMemory * ptr1;

		if (opt == MPI_GPU_DIRECT)
		{
#if defined(MPIX_CUDA_AWARE_SUPPORT) && MPIX_CUDA_AWARE_SUPPORT
			// add the received particles to the vector
			ptr1 = new PtrMemory(recv_buf.get(i).getDevicePointer(),recv_buf.get(i).size());
#else
			// add the received particles to the vector
			ptr1 = new PtrMemory(recv_buf.get(i).getPointer(),recv_buf.get(i).size());
#endif
		}
		else
		{
			// add the received particles to the vector
			ptr1 = new PtrMemory(recv_buf.get(i).getPointer(),recv_buf.get(i).size());
		}
incardon's avatar
incardon committed
192 193 194 195 196 197 198

		v2.template setMemory<T::value>(*ptr1);

		++i;
	}
};

incardon's avatar
incardon committed
199 200 201 202
template<bool inte_or_lin,typename T, typename S, template<typename> class layout_base>
struct unpack_selector_with_prp_lin
{
	template<typename op, unsigned int ... prp> static int call_unpack_impl(S & recv,
203
                                                                             openfpm::vector<BMemory<HeapMemory>> & recv_buf,
incardon's avatar
incardon committed
204 205 206
                                                                             openfpm::vector<size_t> * sz,
                                                                             openfpm::vector<size_t> * sz_byte,
                                                                             op & op_param,
incardon's avatar
incardon committed
207 208
                                                                             size_t i,
                                                                             size_t opt)
incardon's avatar
incardon committed
209 210 211
	{
		// create vector representation to a piece of memory already allocated
		openfpm::vector<typename T::value_type,PtrMemory,typename layout_base<typename T::value_type>::type,layout_base,openfpm::grow_policy_identity> v2;
incardon's avatar
incardon committed
212

213
		process_receive_mem_traits_inte<T,layout_base> prmti(v2,recv_buf,i,opt);
incardon's avatar
incardon committed
214

incardon's avatar
incardon committed
215 216 217
		boost::mpl::for_each_ref<boost::mpl::range_c<int,0,T::value_type::max_prop>>(prmti);

		v2.resize(prmti.n_ele);
incardon's avatar
incardon committed
218

incardon's avatar
incardon committed
219
		// Merge the information
incardon's avatar
incardon committed
220

incardon's avatar
incardon committed
221
		size_t recv_size_old = recv.size();
incardon's avatar
incardon committed
222

incardon's avatar
incardon committed
223
		op_param.template execute<false,T,decltype(recv),decltype(v2),layout_base,prp...>(recv,v2,i,opt);
incardon's avatar
incardon committed
224

incardon's avatar
incardon committed
225
		size_t recv_size_new = recv.size();
incardon's avatar
incardon committed
226

incardon's avatar
incardon committed
227 228 229 230 231 232 233 234 235 236 237 238 239
		if (sz_byte != NULL)
			sz_byte->get(i) = recv_buf.get(i).size();
		if (sz != NULL)
			sz->get(i) = recv_size_new - recv_size_old;

		return sizeof...(prp);
	}
};

template<typename T, typename S, template<typename> class layout_base>
struct unpack_selector_with_prp_lin<true,T,S,layout_base>
{
	template<typename op, unsigned int ... prp> static int call_unpack_impl(S & recv,
240
                                                                             openfpm::vector<BMemory<HeapMemory>> & recv_buf,
incardon's avatar
incardon committed
241 242 243
                                                                             openfpm::vector<size_t> * sz,
                                                                             openfpm::vector<size_t> * sz_byte,
                                                                             op & op_param,
incardon's avatar
incardon committed
244 245
                                                                             size_t i,
                                                                             size_t opt)
incardon's avatar
incardon committed
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
	{
		// calculate the number of received elements
		size_t n_ele = recv_buf.get(i).size() / sizeof(typename T::value_type);

		// add the received particles to the vector
		PtrMemory * ptr1 = new PtrMemory(recv_buf.get(i).getPointer(),recv_buf.get(i).size());

		// create vector representation to a piece of memory already allocated
		openfpm::vector<typename T::value_type,PtrMemory,typename layout_base<typename T::value_type>::type,layout_base,openfpm::grow_policy_identity> v2;

		v2.setMemory(*ptr1);

		// resize with the number of elements
		v2.resize(n_ele);

		// Merge the information

		size_t recv_size_old = recv.size();

incardon's avatar
incardon committed
265
		op_param.template execute<false,T,decltype(recv),decltype(v2),layout_base,prp...>(recv,v2,i,opt);
incardon's avatar
incardon committed
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284

		size_t recv_size_new = recv.size();

		if (sz_byte != NULL)
			sz_byte->get(i) = recv_buf.get(i).size();
		if (sz != NULL)
			sz->get(i) = recv_size_new - recv_size_old;

		return 1;
	}
};

typedef aggregate<int,int> dummy_type;

//
template<typename T, typename S, template<typename> class layout_base>
struct unpack_selector_with_prp<true,T,S,layout_base>
{
	template<typename op, unsigned int ... prp> static void call_unpack(S & recv,
285
			                                                            openfpm::vector<BMemory<HeapMemory>> & recv_buf,
incardon's avatar
incardon committed
286 287
			                                                            openfpm::vector<size_t> * sz,
			                                                            openfpm::vector<size_t> * sz_byte,
incardon's avatar
incardon committed
288 289
			                                                            op & op_param,
			                                                            size_t opt)
incardon's avatar
incardon committed
290 291 292 293 294 295
	{
		if (sz_byte != NULL)
			sz_byte->resize(recv_buf.size());

		for (size_t i = 0 ; i < recv_buf.size() ; )
		{
incardon's avatar
incardon committed
296
			i += unpack_selector_with_prp_lin<is_layout_mlin<layout_base<dummy_type>>::value,T,S,layout_base>::template call_unpack_impl<op,prp...>(recv,recv_buf,sz,sz_byte,op_param,i,opt);
incardon's avatar
incardon committed
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
		}
	}
};


template<typename T>
struct call_serialize_variadic {};

template<int ... prp>
struct call_serialize_variadic<index_tuple<prp...>>
{
	template<typename T> inline static void call_pr(T & send, size_t & tot_size)
	{
		Packer<T,HeapMemory>::template packRequest<prp...>(send,tot_size);
	}

	template<typename T> inline static void call_pack(ExtPreAlloc<HeapMemory> & mem, T & send, Pack_stat & sts)
	{
		Packer<T,HeapMemory>::template pack<prp...>(mem,send,sts);
	}

incardon's avatar
incardon committed
318 319
	template<typename op, typename T, typename S, template<typename> class layout_base>
	inline static void call_unpack(S & recv,
320
			                       openfpm::vector<BMemory<HeapMemory>> & recv_buf,
incardon's avatar
incardon committed
321 322
			                       openfpm::vector<size_t> * sz,
			                       openfpm::vector<size_t> * sz_byte,
incardon's avatar
incardon committed
323 324
			                       op & op_param,
			                       size_t opt)
incardon's avatar
incardon committed
325 326 327
	{
		const bool result = has_pack_gen<typename T::value_type>::value == false && is_vector<T>::value == true;

incardon's avatar
incardon committed
328
		unpack_selector_with_prp<result, T, S,layout_base>::template call_unpack<op,prp...>(recv, recv_buf, sz, sz_byte, op_param,opt);
incardon's avatar
incardon committed
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
	}
};

/*! \brief this class is a functor for "for_each" algorithm
 *
 * This class is a functor for "for_each" algorithm. For each
 * element of the boost::vector the operator() is called.
 * Is mainly used to copy one encap into another encap object
 *
 * \tparam encap source
 * \tparam encap dst
 *
 */
template<typename sT>
struct set_buf_pointer_for_each_prop
{
	//! set of pointers
	sT & v;

	openfpm::vector<const void *> & send_buf;

350 351
	size_t opt;

incardon's avatar
incardon committed
352 353 354 355 356
	/*! \brief constructor
	 *
	 * \param v set of pointer buffers to set
	 *
	 */
357 358
	inline set_buf_pointer_for_each_prop(sT & v, openfpm::vector<const void *> & send_buf, size_t opt)
	:v(v),send_buf(send_buf),opt(opt)
incardon's avatar
incardon committed
359 360 361 362 363 364
	{};

	//! It call the copy function for each property
	template<typename T>
	inline void operator()(T& t) const
	{
365 366 367 368
		// If we have GPU direct activated use directly the cuda buffer
		if (opt & MPI_GPU_DIRECT)
		{
#if defined(MPIX_CUDA_AWARE_SUPPORT) && MPIX_CUDA_AWARE_SUPPORT
incardon's avatar
incardon committed
369
			send_buf.add(v.template getDeviceBuffer<T::value>());
370 371 372 373 374 375 376 377 378
#else
			v.template deviceToHost<T::value>();
			send_buf.add(v.template getPointer<T::value>());
#endif
		}
		else
		{
			send_buf.add(v.template getPointer<T::value>());
		}
incardon's avatar
incardon committed
379 380 381
	}
};

incardon's avatar
incardon committed
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
/*! \brief this class is a functor for "for_each" algorithm
 *
 * This class is a functor for "for_each" algorithm. For each
 * element of the boost::vector the operator() is called.
 * Is mainly used to copy one encap into another encap object
 *
 * \tparam encap source
 * \tparam encap dst
 *
 */

template<typename sT>
struct set_buf_size_for_each_prop
{
	//! set of pointers
	sT & v;

	openfpm::vector<size_t> & sz;

	/*! \brief constructor
	 *
	 * \param v set of pointer buffers to set
	 *
	 */
	inline set_buf_size_for_each_prop(sT & v, openfpm::vector<size_t> & sz)
	:v(v),sz(sz)
	{};

	//! It call the copy function for each property
	template<typename T>
	inline void operator()(T& t) const
	{
		typedef typename boost::mpl::at<typename sT::value_type::type,T>::type type_prp;

		sz.add(sizeof(type_prp)*v.size());
	}
};

incardon's avatar
incardon committed
420 421 422
template<typename T, bool impl = is_multiple_buffer_each_prp<T>::value >
struct pack_unpack_cond_with_prp_inte_lin
{
423
	static void set_buffers(T & send, openfpm::vector<const void *> & send_buf, size_t opt)
incardon's avatar
incardon committed
424 425 426
	{
		send_buf.add(send.getPointer());
	}
incardon's avatar
incardon committed
427 428 429 430 431 432 433 434 435 436 437 438 439

	static void set_size_buffers(T & send, openfpm::vector<size_t> & sz)
	{
		sz.add(send.size()*sizeof(typename T::value_type));
	}

	static void construct_prc(openfpm::vector<size_t> & prc_send, openfpm::vector<size_t> & prc_send_)
	{
		for (size_t i = 0 ; i < prc_send.size() ; i++)
		{
			prc_send_.add(prc_send.get(i));
		}
	}
incardon's avatar
incardon committed
440 441 442 443 444 445
};

// memory_traits_inte
template<typename T>
struct pack_unpack_cond_with_prp_inte_lin<T,true>
{
446
	static void set_buffers(T & send, openfpm::vector<const void *> & send_buf, size_t opt)
incardon's avatar
incardon committed
447
	{
448
		set_buf_pointer_for_each_prop<T> sbp(send,send_buf,opt);
incardon's avatar
incardon committed
449 450

		boost::mpl::for_each_ref<boost::mpl::range_c<int,0,T::value_type::max_prop>>(sbp);
incardon's avatar
incardon committed
451
	}
incardon's avatar
incardon committed
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466

	static void set_size_buffers(T & send, openfpm::vector<size_t> & sz)
	{
		set_buf_size_for_each_prop<T> sbp(send,sz);

		boost::mpl::for_each_ref<boost::mpl::range_c<int,0,T::value_type::max_prop>>(sbp);
	}

	static void construct_prc(openfpm::vector<size_t> & prc_send, openfpm::vector<size_t> & prc_send_)
	{
		for (size_t i = 0 ; i < prc_send.size() ; i++)
		{
			for (size_t j = 0 ; j < T::value_type::max_prop ; j++)
			{prc_send_.add(prc_send.get(i));}
		}
467
	}
incardon's avatar
incardon committed
468 469 470
};

//! There is max_prop inside
incardon's avatar
incardon committed
471 472 473 474 475 476
template<bool cond,
         typename op,
		 typename T,
		 typename S,
		 template <typename> class layout_base,
		 unsigned int ... prp>
incardon's avatar
incardon committed
477 478 479 480 481 482 483
struct pack_unpack_cond_with_prp
{
	static void packingRequest(T & send, size_t & tot_size, openfpm::vector<size_t> & sz)
	{
		typedef typename ::generate_indexes<int, has_max_prop<T, has_value_type<T>::value>::number, MetaFuncOrd>::result ind_prop_to_pack;
		if (has_pack_gen<typename T::value_type>::value == false && is_vector<T>::value == true)
		{
incardon's avatar
incardon committed
484
			pack_unpack_cond_with_prp_inte_lin<T>::set_size_buffers(send,sz);
incardon's avatar
incardon committed
485 486 487 488 489 490 491 492 493
		}
		else
		{
			call_serialize_variadic<ind_prop_to_pack>::call_pr(send,tot_size);

			sz.add(tot_size);
		}
	}

494
	static void packing(ExtPreAlloc<HeapMemory> & mem, T & send, Pack_stat & sts, openfpm::vector<const void *> & send_buf, size_t opt = 0)
incardon's avatar
incardon committed
495 496 497 498
	{
		typedef typename ::generate_indexes<int, has_max_prop<T, has_value_type<T>::value>::number, MetaFuncOrd>::result ind_prop_to_pack;
		if (has_pack_gen<typename T::value_type>::value == false && is_vector<T>::value == true)
		{
499
			pack_unpack_cond_with_prp_inte_lin<T>::set_buffers(send,send_buf,opt);
incardon's avatar
incardon committed
500 501 502 503 504 505 506 507
		}
		else
		{
			send_buf.add(mem.getPointerEnd());
			call_serialize_variadic<ind_prop_to_pack>::call_pack(mem,send,sts);
		}
	}

incardon's avatar
incardon committed
508
	static void unpacking(S & recv,
509
			              openfpm::vector<BMemory<HeapMemory>> & recv_buf,
incardon's avatar
incardon committed
510 511
						  openfpm::vector<size_t> * sz,
						  openfpm::vector<size_t> * sz_byte,
incardon's avatar
incardon committed
512 513
						  op & op_param,
						  size_t opt)
incardon's avatar
incardon committed
514 515
	{
		typedef index_tuple<prp...> ind_prop_to_pack;
incardon's avatar
incardon committed
516
		call_serialize_variadic<ind_prop_to_pack>::template call_unpack<op,T,S,layout_base>(recv, recv_buf, sz, sz_byte, op_param,opt);
incardon's avatar
incardon committed
517 518 519 520 521 522 523 524 525 526 527
	}
};


/////////////////////////////

//! Helper class to add data without serialization
template<bool sr>
struct op_ssend_recv_add_sr
{
	//! Add data
incardon's avatar
incardon committed
528 529 530 531
	template<typename T,
	         typename D,
			 typename S,
			 template <typename> class layout_base,
incardon's avatar
incardon committed
532
			 int ... prp> static void execute(D & recv,S & v2, size_t i, size_t opt)
incardon's avatar
incardon committed
533
	{
incardon's avatar
incardon committed
534 535 536 537 538 539 540 541 542 543 544 545 546 547 548
		if (opt & MPI_GPU_DIRECT)
		{
#if defined(MPIX_CUDA_AWARE_SUPPORT) && MPIX_CUDA_AWARE_SUPPORT

			// Merge the information
			recv.template add_prp_device<typename T::value_type,
		                      PtrMemory,
							  openfpm::grow_policy_identity,
							  openfpm::vect_isel<typename T::value_type>::value,
							  layout_base,
							  prp...>(v2);
#else

			// Merge the information
			recv.template add_prp<typename T::value_type,
incardon's avatar
incardon committed
549
		                      PtrMemory,
incardon's avatar
incardon committed
550 551
							  openfpm::grow_policy_identity,
							  openfpm::vect_isel<typename T::value_type>::value,
incardon's avatar
incardon committed
552
							  layout_base,
incardon's avatar
incardon committed
553
							  prp...>(v2);
incardon's avatar
incardon committed
554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569

			recv.template hostToDevice<prp...>();

#endif

		}
		else
		{
			// Merge the information
			recv.template add_prp<typename T::value_type,
		                      PtrMemory,
							  openfpm::grow_policy_identity,
							  openfpm::vect_isel<typename T::value_type>::value,
							  layout_base,
							  prp...>(v2);
		}
incardon's avatar
incardon committed
570 571 572 573 574 575 576 577
	}
};

//! Helper class to add data with serialization
template<>
struct op_ssend_recv_add_sr<true>
{
	//! Add data
incardon's avatar
incardon committed
578 579 580 581 582
	template<typename T,
	         typename D,
			 typename S,
			 template <typename> class layout_base,
			 int ... prp>
incardon's avatar
incardon committed
583
	static void execute(D & recv,S & v2, size_t i,size_t opt)
incardon's avatar
incardon committed
584 585
	{
		// Merge the information
incardon's avatar
incardon committed
586 587
		recv.template add_prp<typename T::value_type,
		                      HeapMemory,
588
		                      typename T::grow_policy,
incardon's avatar
incardon committed
589 590 591
							  openfpm::vect_isel<typename T::value_type>::value,
							  layout_base,
							  prp...>(v2);
incardon's avatar
incardon committed
592 593 594 595 596 597 598 599
	}
};

//! Helper class to add data
template<typename op>
struct op_ssend_recv_add
{
	//! Add data
incardon's avatar
incardon committed
600 601 602 603 604 605
	template<bool sr,
	         typename T,
			 typename D,
			 typename S,
			 template <typename> class layout_base,
			 int ... prp>
incardon's avatar
incardon committed
606
	static void execute(D & recv,S & v2, size_t i, size_t opt)
incardon's avatar
incardon committed
607 608
	{
		// Merge the information
incardon's avatar
incardon committed
609
		op_ssend_recv_add_sr<sr>::template execute<T,D,S,layout_base,prp...>(recv,v2,i,opt);
incardon's avatar
incardon committed
610 611 612 613 614 615 616 617
	}
};

//! Helper class to merge data without serialization
template<bool sr,template<typename,typename> class op>
struct op_ssend_recv_merge_impl
{
	//! Merge the
incardon's avatar
incardon committed
618 619 620 621 622 623
	template<typename T,
	         typename D,
			 typename S,
			 template <typename> class layout_base,
			 int ... prp>
	inline static void execute(D & recv,S & v2,size_t i,openfpm::vector<openfpm::vector<aggregate<size_t,size_t>>> & opart)
incardon's avatar
incardon committed
624 625
	{
		// Merge the information
incardon's avatar
incardon committed
626 627 628 629 630 631
		recv.template merge_prp_v<op,
		                          typename T::value_type,
								  PtrMemory,
								  openfpm::grow_policy_identity,
								  layout_base,
								  prp...>(v2,opart.get(i));
incardon's avatar
incardon committed
632 633 634 635 636 637 638 639
	}
};

//! Helper class to merge data with serialization
template<template<typename,typename> class op>
struct op_ssend_recv_merge_impl<true,op>
{
	//! merge the data
incardon's avatar
incardon committed
640 641 642 643 644 645
	template<typename T,
	         typename D,
			 typename S,
			 template <typename> class layout_base,
			 int ... prp>
	inline static void execute(D & recv,S & v2,size_t i,openfpm::vector<openfpm::vector<aggregate<size_t,size_t>>> & opart)
incardon's avatar
incardon committed
646 647
	{
		// Merge the information
incardon's avatar
incardon committed
648 649 650 651 652 653
		recv.template merge_prp_v<op,
		                          typename T::value_type,
								  HeapMemory,
								  openfpm::grow_policy_double,
								  layout_base,
								  prp...>(v2,opart.get(i));
incardon's avatar
incardon committed
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
	}
};

//! Helper class to merge data
template<template<typename,typename> class op>
struct op_ssend_recv_merge
{
	//! For each processor contain the list of the particles with which I must merge the information
	openfpm::vector<openfpm::vector<aggregate<size_t,size_t>>> & opart;

	//! constructor
	op_ssend_recv_merge(openfpm::vector<openfpm::vector<aggregate<size_t,size_t>>> & opart)
	:opart(opart)
	{}

	//! execute the merge
incardon's avatar
incardon committed
670 671 672 673 674 675 676
	template<bool sr,
	         typename T,
			 typename D,
			 typename S,
			 template <typename> class layout_base,
			 int ... prp>
	void execute(D & recv,S & v2,size_t i)
incardon's avatar
incardon committed
677
	{
incardon's avatar
incardon committed
678
		op_ssend_recv_merge_impl<sr,op>::template execute<T,D,S,layout_base,prp...>(recv,v2,i,opart);
incardon's avatar
incardon committed
679 680 681 682 683 684 685 686
	}
};

//! Helper class to merge data without serialization
template<bool sr>
struct op_ssend_gg_recv_merge_impl
{
	//! Merge the
incardon's avatar
incardon committed
687 688 689 690 691 692
	template<typename T,
	         typename D,
			 typename S,
			 template <typename> class layout_base,
			 int ... prp>
	inline static void execute(D & recv,S & v2,size_t i,size_t & start)
incardon's avatar
incardon committed
693 694
	{
		// Merge the information
incardon's avatar
incardon committed
695 696 697 698 699 700
		recv.template merge_prp_v<replace_,
		                          typename T::value_type,
								  PtrMemory,
								  openfpm::grow_policy_identity,
								  layout_base,
								  prp...>(v2,start);
incardon's avatar
incardon committed
701 702 703 704 705 706 707 708 709 710

		start += v2.size();
	}
};

//! Helper class to merge data with serialization
template<>
struct op_ssend_gg_recv_merge_impl<true>
{
	//! merge the data
incardon's avatar
incardon committed
711 712 713 714 715
	template<typename T,
	         typename D,
			 typename S,
			 template <typename> class layout_base,
			 int ... prp> inline static void execute(D & recv,S & v2,size_t i,size_t & start)
incardon's avatar
incardon committed
716 717
	{
		// Merge the information
incardon's avatar
incardon committed
718 719 720 721 722 723
		recv.template merge_prp_v<replace_,
		                          typename T::value_type,
								  HeapMemory,
								  openfpm::grow_policy_double,
								  layout_base,
								  prp...>(v2,start);
incardon's avatar
incardon committed
724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741

		// from
		start += v2.size();
	}
};

//! Helper class to merge data
struct op_ssend_gg_recv_merge
{
	//! starting marker
	size_t start;

	//! constructor
	op_ssend_gg_recv_merge(size_t start)
	:start(start)
	{}

	//! execute the merge
incardon's avatar
incardon committed
742
	template<bool sr, typename T, typename D, typename S, template<typename> class layout_base, int ... prp> void execute(D & recv,S & v2,size_t i)
incardon's avatar
incardon committed
743
	{
incardon's avatar
incardon committed
744
		op_ssend_gg_recv_merge_impl<sr>::template execute<T,D,S,layout_base,prp...>(recv,v2,i,start);
incardon's avatar
incardon committed
745 746 747 748 749 750 751 752
	}
};

//////////////////////////////////////////////////



#endif /* OPENFPM_VCLUSTER_SRC_VCLUSTER_VCLUSTER_META_FUNCTION_HPP_ */