VCluster_meta_function.hpp 22 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"

14
template<bool result, typename T, typename S, template<typename> class layout_base, typename Memory>
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,
incardon's avatar
incardon committed
20
			                openfpm::vector_fr<BMemory<Memory>> & 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;

incardon's avatar
incardon committed
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,
incardon's avatar
incardon committed
79
			                       openfpm::vector_fr<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
/*! \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
 *
 */

137
template<typename sT, template<typename> class layout_base,typename Memory>
incardon's avatar
incardon committed
138 139 140 141 142 143
struct process_receive_mem_traits_inte
{
	//! set of pointers
	size_t i;

	//! Receive buffer
incardon's avatar
incardon committed
144
	openfpm::vector_fr<BMemory<Memory>> & 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;

incardon's avatar
incardon committed
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,
incardon's avatar
incardon committed
160
			                               openfpm::vector_fr<BMemory<Memory>> & recv_buf,
incardon's avatar
incardon committed
161 162 163
			                               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);

incardon's avatar
incardon committed
175 176
		PtrMemory * ptr1;

incardon's avatar
incardon committed
177
		if (opt & MPI_GPU_DIRECT)
incardon's avatar
incardon committed
178 179 180
		{
#if defined(MPIX_CUDA_AWARE_SUPPORT) && MPIX_CUDA_AWARE_SUPPORT
			// add the received particles to the vector
incardon's avatar
incardon committed
181
			ptr1 = new PtrMemory(recv_buf.get(i).getDevicePointer(),recv_buf.get(i).size());
incardon's avatar
incardon committed
182 183 184 185 186 187 188 189 190 191
#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;
	}
};

199
template<bool inte_or_lin,typename T, typename S, template<typename> class layout_base,typename Memory>
incardon's avatar
incardon committed
200 201 202
struct unpack_selector_with_prp_lin
{
	template<typename op, unsigned int ... prp> static int call_unpack_impl(S & recv,
incardon's avatar
incardon committed
203
                                                                             openfpm::vector_fr<BMemory<Memory>> & 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,Memory> 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
		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);
	}
};

236 237
template<typename T, typename S, template<typename> class layout_base, typename Memory>
struct unpack_selector_with_prp_lin<true,T,S,layout_base,Memory>
incardon's avatar
incardon committed
238 239
{
	template<typename op, unsigned int ... prp> static int call_unpack_impl(S & recv,
incardon's avatar
incardon committed
240
                                                                             openfpm::vector_fr<BMemory<Memory>> & 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

		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;

//
281 282
template<typename T, typename S, template<typename> class layout_base, typename Memory>
struct unpack_selector_with_prp<true,T,S,layout_base,Memory>
incardon's avatar
incardon committed
283 284
{
	template<typename op, unsigned int ... prp> static void call_unpack(S & recv,
incardon's avatar
incardon committed
285
			                                                            openfpm::vector_fr<BMemory<Memory>> & 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() ; )
		{
296
			i += unpack_selector_with_prp_lin<is_layout_mlin<layout_base<dummy_type>>::value,T,S,layout_base,Memory>::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);
	}

318
	template<typename op, typename T, typename S, template<typename> class layout_base, typename Memory>
incardon's avatar
incardon committed
319
	inline static void call_unpack(S & recv,
incardon's avatar
incardon committed
320
			                       openfpm::vector_fr<BMemory<Memory>> & 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;

328
		unpack_selector_with_prp<result, T, S,layout_base,Memory>::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;

incardon's avatar
incardon committed
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
	 *
	 */
incardon's avatar
incardon committed
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
	{
incardon's avatar
incardon committed
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>());
incardon's avatar
incardon committed
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
{
incardon's avatar
incardon committed
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>
{
incardon's avatar
incardon committed
446
	static void set_buffers(T & send, openfpm::vector<const void *> & send_buf, size_t opt)
incardon's avatar
incardon committed
447
	{
incardon's avatar
incardon committed
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));}
		}
incardon's avatar
incardon committed
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);
		}
	}

incardon's avatar
incardon committed
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)
		{
incardon's avatar
incardon committed
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);
		}
	}

508
	template<typename Memory>
incardon's avatar
incardon committed
509
	static void unpacking(S & recv,
incardon's avatar
incardon committed
510
			              openfpm::vector_fr<BMemory<Memory>> & recv_buf,
incardon's avatar
incardon committed
511 512
						  openfpm::vector<size_t> * sz,
						  openfpm::vector<size_t> * sz_byte,
incardon's avatar
incardon committed
513 514
						  op & op_param,
						  size_t opt)
incardon's avatar
incardon committed
515 516
	{
		typedef index_tuple<prp...> ind_prop_to_pack;
incardon's avatar
incardon committed
517
		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
518 519 520 521 522 523 524 525 526 527 528
	}
};


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

//! Helper class to add data without serialization
template<bool sr>
struct op_ssend_recv_add_sr
{
	//! Add data
incardon's avatar
incardon committed
529 530 531 532
	template<typename T,
	         typename D,
			 typename S,
			 template <typename> class layout_base,
incardon's avatar
incardon committed
533
			 int ... prp> static void execute(D & recv,S & v2, size_t i, size_t opt)
incardon's avatar
incardon committed
534
	{
incardon's avatar
incardon committed
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
		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
550
		                      PtrMemory,
incardon's avatar
incardon committed
551 552
							  openfpm::grow_policy_identity,
							  openfpm::vect_isel<typename T::value_type>::value,
incardon's avatar
incardon committed
553
							  layout_base,
incardon's avatar
incardon committed
554
							  prp...>(v2);
incardon's avatar
incardon committed
555

incardon's avatar
incardon committed
556
			recv.template hostToDevice<prp...>(recv.size(),recv.size()+v2.size());
incardon's avatar
incardon committed
557 558 559 560 561 562 563 564 565 566 567 568 569 570

#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
571 572 573 574 575 576 577 578
	}
};

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

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

//! 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
619 620 621 622 623 624
	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
625 626
	{
		// Merge the information
incardon's avatar
incardon committed
627 628 629 630 631 632
		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
633 634 635 636 637 638 639 640
	}
};

//! 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
641 642 643 644 645 646
	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
647 648
	{
		// Merge the information
incardon's avatar
incardon committed
649 650 651 652 653 654
		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
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
	}
};

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

//! Helper class to merge data without serialization
template<bool sr>
struct op_ssend_gg_recv_merge_impl
{
	//! Merge the
incardon's avatar
incardon committed
688 689 690 691 692 693
	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
694 695
	{
		// Merge the information
incardon's avatar
incardon committed
696 697 698 699 700 701
		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
702 703 704 705 706 707 708 709 710 711

		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
712 713 714 715 716
	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
717 718
	{
		// Merge the information
incardon's avatar
incardon committed
719 720 721
		recv.template merge_prp_v<replace_,
		                          typename T::value_type,
								  HeapMemory,
incardon's avatar
incardon committed
722
								  typename S::grow_policy,
incardon's avatar
incardon committed
723 724
								  layout_base,
								  prp...>(v2,start);
incardon's avatar
incardon committed
725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742

		// 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
743
	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,size_t opt)
incardon's avatar
incardon committed
744
	{
incardon's avatar
incardon committed
745
		op_ssend_gg_recv_merge_impl<sr>::template execute<T,D,S,layout_base,prp...>(recv,v2,i,start);
incardon's avatar
incardon committed
746 747 748 749 750 751 752 753
	}
};

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



#endif /* OPENFPM_VCLUSTER_SRC_VCLUSTER_VCLUSTER_META_FUNCTION_HPP_ */