VCluster_meta_function.hpp 10.8 KB
Newer Older
incardon's avatar
incardon committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * 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"

template<bool result, typename T, typename S>
struct unpack_selector_with_prp
{
incardon's avatar
incardon committed
17 18 19 20 21 22 23 24
	template<typename op,
	         template <typename> class layout_base,
			 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)
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);
incardon's avatar
incardon committed
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61

			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;
		}
	}
};

//
template<typename T, typename S>
struct unpack_selector_with_prp<true,T,S>
{
incardon's avatar
incardon committed
62 63 64 65
	template<typename op,
			 template <typename> class layout_base,
	         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)
incardon's avatar
incardon committed
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
	{
		if (sz_byte != NULL)
			sz_byte->resize(recv_buf.size());

		for (size_t i = 0 ; i < recv_buf.size() ; 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 memory_traits_lin<typename T::value_type>::type, memory_traits_lin,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
90
			op_param.template execute<false,T,decltype(recv),decltype(v2),memory_traits_lin,prp...>(recv,v2,i);
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

			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;
		}
	}
};


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
119 120 121 122 123 124 125 126 127
	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
128 129 130
	{
		const bool result = has_pack_gen<typename T::value_type>::value == false && is_vector<T>::value == true;

incardon's avatar
incardon committed
131
		unpack_selector_with_prp<result, T, S>::template call_unpack<op,layout_base,prp...>(recv, recv_buf, sz, sz_byte, op_param);
incardon's avatar
incardon committed
132 133 134 135
	}
};

//! There is max_prop inside
incardon's avatar
incardon committed
136 137 138 139 140 141
template<bool cond,
         typename op,
		 typename T,
		 typename S,
		 template <typename> class layout_base,
		 unsigned int ... prp>
incardon's avatar
incardon committed
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
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)
		//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)
		{
			//std::cout << demangle(typeid(T).name()) << std::endl;
			send_buf.add(send.getPointer());
		}
		else
		{
			send_buf.add(mem.getPointerEnd());
			call_serialize_variadic<ind_prop_to_pack>::call_pack(mem,send,sts);
		}
	}

incardon's avatar
incardon committed
176 177 178 179 180
	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
181 182
	{
		typedef index_tuple<prp...> ind_prop_to_pack;
incardon's avatar
incardon committed
183
		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
184 185 186 187 188 189 190 191 192 193 194
	}
};


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

//! Helper class to add data without serialization
template<bool sr>
struct op_ssend_recv_add_sr
{
	//! Add data
incardon's avatar
incardon committed
195 196 197 198 199
	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
200 201
	{
		// Merge the information
incardon's avatar
incardon committed
202 203 204 205 206 207
		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
208 209 210 211 212 213 214 215
	}
};

//! Helper class to add data with serialization
template<>
struct op_ssend_recv_add_sr<true>
{
	//! Add data
incardon's avatar
incardon committed
216 217 218 219 220 221
	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
222 223
	{
		// Merge the information
incardon's avatar
incardon committed
224 225 226 227 228 229
		recv.template add_prp<typename T::value_type,
		                      HeapMemory,
							  openfpm::grow_policy_double,
							  openfpm::vect_isel<typename T::value_type>::value,
							  layout_base,
							  prp...>(v2);
incardon's avatar
incardon committed
230 231 232 233 234 235 236 237
	}
};

//! Helper class to add data
template<typename op>
struct op_ssend_recv_add
{
	//! Add data
incardon's avatar
incardon committed
238 239 240 241 242 243 244
	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
245 246
	{
		// Merge the information
incardon's avatar
incardon committed
247
		op_ssend_recv_add_sr<sr>::template execute<T,D,S,layout_base,prp...>(recv,v2,i);
incardon's avatar
incardon committed
248 249 250 251 252 253 254 255
	}
};

//! 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
256 257 258 259 260 261
	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
262 263
	{
		// Merge the information
incardon's avatar
incardon committed
264 265 266 267 268 269
		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
270 271 272 273 274 275 276 277
	}
};

//! 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
278 279 280 281 282 283
	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
284 285
	{
		// Merge the information
incardon's avatar
incardon committed
286 287 288 289 290 291
		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
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
	}
};

//! 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
308 309 310 311 312 313 314
	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
315
	{
incardon's avatar
incardon committed
316
		op_ssend_recv_merge_impl<sr,op>::template execute<T,D,S,layout_base,prp...>(recv,v2,i,opart);
incardon's avatar
incardon committed
317 318 319 320 321 322 323 324
	}
};

//! Helper class to merge data without serialization
template<bool sr>
struct op_ssend_gg_recv_merge_impl
{
	//! Merge the
incardon's avatar
incardon committed
325 326 327 328 329 330
	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
331 332
	{
		// Merge the information
incardon's avatar
incardon committed
333 334 335 336 337 338
		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
339 340 341 342 343 344 345 346 347 348

		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
349 350 351 352 353
	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
354 355
	{
		// Merge the information
incardon's avatar
incardon committed
356 357 358 359 360 361
		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
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379

		// 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
380
	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
381
	{
incardon's avatar
incardon committed
382
		op_ssend_gg_recv_merge_impl<sr>::template execute<T,D,S,layout_base,prp...>(recv,v2,i,start);
incardon's avatar
incardon committed
383 384 385 386 387 388 389 390
	}
};

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



#endif /* OPENFPM_VCLUSTER_SRC_VCLUSTER_VCLUSTER_META_FUNCTION_HPP_ */