VCluster_meta_function.hpp 16 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 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
struct unpack_selector_with_prp
{
	template<typename op, int ... prp> static void call_unpack(S & recv, openfpm::vector<BHeapMemory> & recv_buf, openfpm::vector<size_t> * sz, openfpm::vector<size_t> * sz_byte, op & op_param)
	{
		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
36
			op_param.template execute<true,T,decltype(recv),decltype(unp),layout_base,prp...>(recv,unp,i);
incardon's avatar
incardon committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50

			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
51 52
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
53
{
incardon's avatar
incardon committed
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
	S & recv;

	openfpm::vector<BHeapMemory> & recv_buf;

	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,
			                       openfpm::vector<BHeapMemory> & recv_buf,
			                       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
83
	{
incardon's avatar
incardon committed
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
		// 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
112
		if (sz_byte != NULL)
incardon's avatar
incardon committed
113 114 115 116 117
			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
118

incardon's avatar
incardon committed
119 120 121 122 123 124 125 126 127 128 129 130
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,
                                                                             openfpm::vector<BHeapMemory> & recv_buf,
                                                                             openfpm::vector<size_t> * sz,
                                                                             openfpm::vector<size_t> * sz_byte,
                                                                             op & op_param,
                                                                             size_t i)
	{
		// calculate the number of received elements
		size_t n_ele = recv_buf.get(i).size() / sizeof(typename T::value_type);
incardon's avatar
incardon committed
131

incardon's avatar
incardon committed
132 133
		// add the received particles to the vector
		PtrMemory * ptr1 = new PtrMemory(recv_buf.get(i).getPointer(),recv_buf.get(i).size());
incardon's avatar
incardon committed
134

incardon's avatar
incardon committed
135 136
		// 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
137

incardon's avatar
incardon committed
138
		v2.setMemory(*ptr1);
incardon's avatar
incardon committed
139

incardon's avatar
incardon committed
140 141
		// resize with the number of elements
		v2.resize(n_ele);
incardon's avatar
incardon committed
142

incardon's avatar
incardon committed
143
		// Merge the information
incardon's avatar
incardon committed
144

incardon's avatar
incardon committed
145
		size_t recv_size_old = recv.size();
incardon's avatar
incardon committed
146

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

incardon's avatar
incardon committed
149
		size_t recv_size_new = recv.size();
incardon's avatar
incardon committed
150

incardon's avatar
incardon committed
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
		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,
                                                                             openfpm::vector<BHeapMemory> & recv_buf,
                                                                             openfpm::vector<size_t> * sz,
                                                                             openfpm::vector<size_t> * sz_byte,
                                                                             op & op_param,
                                                                             size_t i)
	{
		// 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();

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

		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,
			                                                            openfpm::vector<BHeapMemory> & recv_buf,
			                                                            openfpm::vector<size_t> * sz,
			                                                            openfpm::vector<size_t> * sz_byte,
			                                                            op & op_param)
	{
		if (sz_byte != NULL)
			sz_byte->resize(recv_buf.size());

		for (size_t i = 0 ; i < recv_buf.size() ; )
		{
			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);
incardon's avatar
incardon committed
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
		}
	}
};


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
240 241 242 243 244 245
	template<typename op, typename T, typename S, template<typename> class layout_base>
	inline static void call_unpack(S & recv,
			                       openfpm::vector<BHeapMemory> & recv_buf,
			                       openfpm::vector<size_t> * sz,
			                       openfpm::vector<size_t> * sz_byte,
			                       op & op_param)
incardon's avatar
incardon committed
246 247 248
	{
		const bool result = has_pack_gen<typename T::value_type>::value == false && is_vector<T>::value == true;

incardon's avatar
incardon committed
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
		unpack_selector_with_prp<result, T, S,layout_base>::template call_unpack<op,prp...>(recv, recv_buf, sz, sz_byte, op_param);
	}
};


/*! \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;

	/*! \brief constructor
	 *
	 * \param v set of pointer buffers to set
	 *
	 */
	inline set_buf_pointer_for_each_prop(sT & v, openfpm::vector<const void *> & send_buf)
	:v(v),send_buf(send_buf)
	{};

	//! It call the copy function for each property
	template<typename T>
	inline void operator()(T& t) const
	{
		send_buf.add(v.template getPointer<T::value>());
	}
};

template<typename T, bool impl = is_multiple_buffer_each_prp<T>::value >
struct pack_unpack_cond_with_prp_inte_lin
{
	static void set_buffers(T & send, openfpm::vector<const void *> & send_buf)
	{
		send_buf.add(send.getPointer());
	}
};

// memory_traits_inte
template<typename T>
struct pack_unpack_cond_with_prp_inte_lin<T,true>
{
	static void set_buffers(T & send, openfpm::vector<const void *> & send_buf)
	{
		set_buf_pointer_for_each_prop<T> sbp(send,send_buf);

		boost::mpl::for_each_ref<boost::mpl::range_c<int,0,T::value_type::max_prop>>(sbp);
incardon's avatar
incardon committed
308 309 310 311
	}
};

//! There is max_prop inside
incardon's avatar
incardon committed
312
template<bool cond, typename op, typename T, typename S, template <typename> class layout_base, unsigned int ... prp>
incardon's avatar
incardon committed
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
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)
		//if (has_pack<typename T::value_type>::type::value == false && has_pack_agg<typename T::value_type>::result::value == false && is_vector<T>::value == true)
		{
			sz.add(send.size()*sizeof(typename T::value_type));
		}
		else
		{
			call_serialize_variadic<ind_prop_to_pack>::call_pr(send,tot_size);

			sz.add(tot_size);
		}
	}

	static void packing(ExtPreAlloc<HeapMemory> & mem, T & send, Pack_stat & sts, openfpm::vector<const void *> & send_buf)
	{
		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
336
			pack_unpack_cond_with_prp_inte_lin<T>::set_buffers(send,send_buf);
incardon's avatar
incardon committed
337 338 339 340 341 342 343 344
		}
		else
		{
			send_buf.add(mem.getPointerEnd());
			call_serialize_variadic<ind_prop_to_pack>::call_pack(mem,send,sts);
		}
	}

incardon's avatar
incardon committed
345 346 347 348 349
	static void unpacking(S & recv,
			              openfpm::vector<BHeapMemory> & recv_buf,
			              openfpm::vector<size_t> * sz,
			              openfpm::vector<size_t> * sz_byte,
			              op & op_param)
incardon's avatar
incardon committed
350 351
	{
		typedef index_tuple<prp...> ind_prop_to_pack;
incardon's avatar
incardon committed
352
		call_serialize_variadic<ind_prop_to_pack>::template call_unpack<op,T,S,layout_base>(recv, recv_buf, sz, sz_byte, op_param);
incardon's avatar
incardon committed
353 354 355 356 357 358 359 360 361 362 363
	}
};


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

//! Helper class to add data without serialization
template<bool sr>
struct op_ssend_recv_add_sr
{
	//! Add data
incardon's avatar
incardon committed
364
	template<typename T, typename D, typename S, template<typename> class layout_base, int ... prp> static void execute(D & recv,S & v2, size_t i)
incardon's avatar
incardon committed
365 366 367 368 369 370 371 372 373 374 375
	{
		// Merge the information
		recv.template add_prp<typename T::value_type,PtrMemory,openfpm::grow_policy_identity,openfpm::vect_isel<typename T::value_type>::value,prp...>(v2);
	}
};

//! Helper class to add data with serialization
template<>
struct op_ssend_recv_add_sr<true>
{
	//! Add data
incardon's avatar
incardon committed
376
	template<typename T, typename D, typename S, template<typename> class layout_base , int ... prp> static void execute(D & recv,S & v2, size_t i)
incardon's avatar
incardon committed
377 378 379 380 381 382 383 384 385 386 387
	{
		// Merge the information
		recv.template add_prp<typename T::value_type,HeapMemory,openfpm::grow_policy_double,openfpm::vect_isel<typename T::value_type>::value, prp...>(v2);
	}
};

//! Helper class to add data
template<typename op>
struct op_ssend_recv_add
{
	//! Add data
incardon's avatar
incardon committed
388
	template<bool sr, typename T, typename D, typename S, template<typename> class layout_base, int ... prp> static void execute(D & recv,S & v2, size_t i)
incardon's avatar
incardon committed
389 390
	{
		// Merge the information
incardon's avatar
incardon committed
391
		op_ssend_recv_add_sr<sr>::template execute<T,D,S,layout_base,prp...>(recv,v2,i);
incardon's avatar
incardon committed
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 420 421 422 423 424 425 426 427 428 429 430 431
	}
};

//! Helper class to merge data without serialization
template<bool sr,template<typename,typename> class op>
struct op_ssend_recv_merge_impl
{
	//! Merge the
	template<typename T, typename D, typename S, int ... prp> inline static void execute(D & recv,S & v2,size_t i,openfpm::vector<openfpm::vector<aggregate<size_t,size_t>>> & opart)
	{
		// Merge the information
		recv.template merge_prp_v<op,typename T::value_type, PtrMemory, openfpm::grow_policy_identity, prp...>(v2,opart.get(i));
	}
};

//! Helper class to merge data with serialization
template<template<typename,typename> class op>
struct op_ssend_recv_merge_impl<true,op>
{
	//! merge the data
	template<typename T, typename D, typename S, int ... prp> inline static void execute(D & recv,S & v2,size_t i,openfpm::vector<openfpm::vector<aggregate<size_t,size_t>>> & opart)
	{
		// Merge the information
		recv.template merge_prp_v<op,typename T::value_type, HeapMemory, openfpm::grow_policy_double, prp...>(v2,opart.get(i));
	}
};

//! 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
432
	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
433 434 435 436 437 438 439 440 441 442
	{
		op_ssend_recv_merge_impl<sr,op>::template execute<T,D,S,prp...>(recv,v2,i,opart);
	}
};

//! Helper class to merge data without serialization
template<bool sr>
struct op_ssend_gg_recv_merge_impl
{
	//! Merge the
incardon's avatar
incardon committed
443
	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
444 445 446 447 448 449 450 451 452 453 454 455 456
	{
		// Merge the information
		recv.template merge_prp_v<replace_,typename T::value_type, PtrMemory, openfpm::grow_policy_identity, prp...>(v2,start);

		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
457
	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
458 459
	{
		// Merge the information
incardon's avatar
incardon committed
460
		recv.template merge_prp_v<replace_,typename T::value_type, HeapMemory , openfpm::grow_policy_double, prp...>(v2,start);
incardon's avatar
incardon committed
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478

		// 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
479
	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
480
	{
incardon's avatar
incardon committed
481
		op_ssend_gg_recv_merge_impl<sr>::template execute<T,D,S,layout_base,prp...>(recv,v2,i,start);
incardon's avatar
incardon committed
482 483 484 485 486 487 488 489
	}
};

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



#endif /* OPENFPM_VCLUSTER_SRC_VCLUSTER_VCLUSTER_META_FUNCTION_HPP_ */