VCluster_semantic.ipp 17.3 KB
Newer Older
1
2
3
4
5
6
7
8
9
/*
 * VCluster_semantic.hpp
 *
 * Implementation of semantic communications
 *
 *  Created on: Feb 8, 2016
 *      Author: Pietro Incardona
 */

Pietro Incardona's avatar
Pietro Incardona committed
10
11
private:

12
13
14
15
16
17
18
19
	// Structures that do an unpack, depending on the existence of max_prop inside 'send'

	//
	template<bool result, typename T, typename S>
	struct unpack_selector
	{
		template<int ... prp> static void call_unpack(S & recv, openfpm::vector<BHeapMemory> & recv_buf, openfpm::vector<size_t> * sz = NULL)
		{
Yaroslav's avatar
Yaroslav committed
20
#ifdef DEBUG
21
			std::cout << "Sz.size(): " << sz->size() << std::endl;
Yaroslav's avatar
Yaroslav committed
22
#endif						
23
24
			for (size_t i = 0 ; i < recv_buf.size() ; i++)
			{
Yaroslav's avatar
Yaroslav committed
25
#ifdef DEBUG				
26
				std::cout << "Recv_buf.get(i).size(): " << recv_buf.get(i).size() << std::endl;
Yaroslav's avatar
Yaroslav committed
27
#endif				
28
				T unp;
Yaroslav's avatar
Yaroslav committed
29

30
31
32
33
34
35
36
				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<prp...>(mem, unp, ps);
				
Yaroslav's avatar
Yaroslav committed
37
				size_t recv_size_old = recv.size();
38
39
				// Merge the information
				recv.add(unp);
Yaroslav's avatar
Yaroslav committed
40
				size_t recv_size_new = recv.size();
41
				
Yaroslav's avatar
Yaroslav committed
42
43
				if (sz != NULL)
					sz->get(i) = recv_size_new - recv_size_old;
44
45
46
47
48
49
50
51
52
53
54
55
56
			}
		}
	};

	
	//
	template<typename T, typename S>
	struct unpack_selector<true, T, S>
	{
		template<int ... prp> static void call_unpack(S & recv, openfpm::vector<BHeapMemory> & recv_buf, openfpm::vector<size_t> * sz = NULL)
		{
			for (size_t i = 0 ; i < recv_buf.size() ; i++)
			{
57
58
59
60
61
62
63
64
65

				/*ExtPreAlloc<HeapMemory> & mem = *(new ExtPreAlloc<HeapMemory>(recv_buf.get(i).size(),recv_buf.get(i)));
				mem.incRef();
				
				Unpack_stat ps;
				
				size_t n_ele = 0;
				Unpacker<size_t, HeapMemory>::unpack(mem,n_ele,ps);*/
				
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
				// 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
				recv.add(v2);
				
				if (sz != NULL)
					sz->get(i) = v2.size();
			}
		}
	};
	
	
	
	

Yaroslav's avatar
Yaroslav committed
93
94
95
96
97
	template<typename T>
	struct call_serialize_variadic {};
	
	template<int ... prp>
	struct call_serialize_variadic<index_tuple<prp...>>
Yaroslav's avatar
Yaroslav committed
98
	{
Yaroslav's avatar
Yaroslav committed
99
		template<typename T> inline static void call_pr(T & send, size_t & tot_size)
Yaroslav's avatar
Yaroslav committed
100
		{
Yaroslav's avatar
Yaroslav committed
101
			Packer<T,HeapMemory>::template packRequest<prp...>(send,tot_size);
Yaroslav's avatar
Yaroslav committed
102
		}
Yaroslav's avatar
Yaroslav committed
103
		
Yaroslav's avatar
Yaroslav committed
104
		template<typename T> inline static void call_pack(ExtPreAlloc<HeapMemory> & mem, T & send, Pack_stat & sts)
Yaroslav's avatar
Yaroslav committed
105
		{
Yaroslav's avatar
Yaroslav committed
106
			Packer<T,HeapMemory>::template pack<prp...>(mem,send,sts);
Yaroslav's avatar
Yaroslav committed
107
108
		}
		
109
		template<typename T, typename S> inline static void call_unpack(S & recv, openfpm::vector<BHeapMemory> & recv_buf, openfpm::vector<size_t> * sz = NULL)
Yaroslav's avatar
Yaroslav committed
110
		{
Yaroslav's avatar
Yaroslav committed
111
			const bool result = has_pack<typename T::value_type>::type::value == false && is_vector<T>::value == true;
112
			unpack_selector<result, T, S>::template call_unpack<prp...>(recv, recv_buf, sz);
Yaroslav's avatar
Yaroslav committed
113
114
115
116
117
118
119
120
121
		}		
	};

	// Structures that do a pack request, depending on the existence of max_prop inside 'send'

	//There is max_prop inside
	template<bool cond, typename T, typename S>
	struct pack_unpack_cond
	{
122
		static void packingRequest(T & send, size_t & tot_size, openfpm::vector<size_t> & sz)
Yaroslav's avatar
Yaroslav committed
123
124
		{
			typedef typename ::generate_indexes<int, has_max_prop<T, has_value_type<T>::value>::number, MetaFuncOrd>::result ind_prop_to_pack;
125
126
127
128
129
130
131
			if (has_pack<typename T::value_type>::type::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);
Yaroslav's avatar
Yaroslav committed
132
#ifdef DEBUG			
133
				std::cout << "Tot_size: " << tot_size << std::endl;
Yaroslav's avatar
Yaroslav committed
134
#endif
135
136
				sz.add(tot_size);
			}
Yaroslav's avatar
Yaroslav committed
137
138
		}
		
139
		static void packing(ExtPreAlloc<HeapMemory> & mem, T & send, Pack_stat & sts, openfpm::vector<const void *> & send_buf)
Yaroslav's avatar
Yaroslav committed
140
141
		{
			typedef typename ::generate_indexes<int, has_max_prop<T, has_value_type<T>::value>::number, MetaFuncOrd>::result ind_prop_to_pack;
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
			if (has_pack<typename T::value_type>::type::value == false && is_vector<T>::value == true)
			{
#ifdef DEBUG
				std::cout << "Inside SGather pack (has prp) (vector case) " << std::endl;
#endif
				send_buf.add(send.getPointer());

			}
			else
			{
#ifdef DEBUG
				std::cout << "Inside SGather pack (has prp) (general case) " << std::endl;
#endif
				call_serialize_variadic<ind_prop_to_pack>::call_pack(mem,send,sts);
				send_buf.add(mem.getPointerBase());
			}			
Yaroslav's avatar
Yaroslav committed
158
159
		}
		
160
		static void unpacking(S & recv, openfpm::vector<BHeapMemory> & recv_buf, openfpm::vector<size_t> * sz = NULL)
Yaroslav's avatar
Yaroslav committed
161
162
		{
			typedef typename ::generate_indexes<int, has_max_prop<T, has_value_type<T>::value>::number, MetaFuncOrd>::result ind_prop_to_pack;
163
			call_serialize_variadic<ind_prop_to_pack>::template call_unpack<T,S>(recv, recv_buf, sz);
Yaroslav's avatar
Yaroslav committed
164
		}	
Yaroslav's avatar
Yaroslav committed
165
166
167
168
	};

	
	//There is no max_prop inside
Yaroslav's avatar
Yaroslav committed
169
170
	template<typename T, typename S>
	struct pack_unpack_cond<false, T, S>
Yaroslav's avatar
Yaroslav committed
171
	{
172
		static void packingRequest(T & send, size_t & tot_size, openfpm::vector<size_t> & sz)
Yaroslav's avatar
Yaroslav committed
173
		{
174
175

			if (has_pack<typename T::value_type>::type::value == false && is_vector<T>::value == true)
176
			{
Yaroslav's avatar
Yaroslav committed
177
#ifdef DEBUG				
178
				std::cout << "Inside SGather pack request (no prp) (vector case) " << std::endl;
Yaroslav's avatar
Yaroslav committed
179
#endif
180
181
182
183
184
				sz.add(send.size()*sizeof(typename T::value_type));			
			}
			
			else
			{
Yaroslav's avatar
Yaroslav committed
185
186
187
				Packer<T,HeapMemory>::packRequest(send,tot_size);	
				
#ifdef DEBUG
188
				std::cout << "Tot_size: " << tot_size << std::endl; 
Yaroslav's avatar
Yaroslav committed
189
#endif
190
191
				sz.add(tot_size);
			}
Yaroslav's avatar
Yaroslav committed
192
193
		}
		
194
		static void packing(ExtPreAlloc<HeapMemory> & mem, T & send, Pack_stat & sts, openfpm::vector<const void *> & send_buf)
Yaroslav's avatar
Yaroslav committed
195
		{
196
197
198

			if (has_pack<typename T::value_type>::type::value == false && is_vector<T>::value == true)
			{
Yaroslav's avatar
Yaroslav committed
199
#ifdef DEBUG
200
				std::cout << "Inside SGather pack (no prp) (vector case)" << std::endl;
Yaroslav's avatar
Yaroslav committed
201
#endif
202
				send_buf.add(send.getPointer());
203
204
205
206
			}
			
			else
			{
Yaroslav's avatar
Yaroslav committed
207
#ifdef DEBUG
208
				std::cout << "Inside SGather pack (no prp) (genaral case) " << std::endl;
Yaroslav's avatar
Yaroslav committed
209
#endif
210
211
				Packer<T,HeapMemory>::pack(mem,send,sts);
				send_buf.add(mem.getPointerBase());		
212
			}
Yaroslav's avatar
Yaroslav committed
213
214
		}

215
		static void unpacking(S & recv, openfpm::vector<BHeapMemory> & recv_buf, openfpm::vector<size_t> * sz = NULL)
Yaroslav's avatar
Yaroslav committed
216
		{
Yaroslav's avatar
Yaroslav committed
217
#ifdef DEBUG			
Yaroslav's avatar
Yaroslav committed
218
			std::cout << "Inside SGather unpack (no prp) " << std::endl;
Yaroslav's avatar
Yaroslav committed
219
#endif
Yaroslav's avatar
Yaroslav committed
220
			
Yaroslav's avatar
Yaroslav committed
221
222
223
			const bool result = has_pack<typename T::value_type>::type::value == false && is_vector<T>::value == true;	
		
			unpack_selector<result, T, S>::template call_unpack<>(recv, recv_buf, sz);
Yaroslav's avatar
Yaroslav committed
224
225
226
227
		}
	};


Pietro Incardona's avatar
Pietro Incardona committed
228
229
230
231
232
233
234
235
/*! \brief Reset the receive buffer
 * 
 * 
 */
void reset_recv_buf()
{
	for (size_t i = 0 ; i < recv_buf.size() ; i++)
		recv_buf.get(i).resize(0);
236
237

	recv_buf.resize(0);
Pietro Incardona's avatar
Pietro Incardona committed
238
239
}

Pietro Incardona's avatar
Pietro Incardona committed
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*! \brief Base info
 *
 * \param recv_buf receive buffers
 * \param prc processors involved
 * \param size of the received data
 *
 */
struct base_info
{
	openfpm::vector<BHeapMemory> * recv_buf;
	openfpm::vector<size_t> & prc;
	openfpm::vector<size_t> & sz;

	// constructor
	base_info(openfpm::vector<BHeapMemory> * recv_buf, openfpm::vector<size_t> & prc, openfpm::vector<size_t> & sz)
	:recv_buf(recv_buf),prc(prc),sz(sz)
	{}
};

259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*! \brief Call-back to allocate buffer to receive data
 *
 * \param msg_i size required to receive the message from i
 * \param total_msg total size to receive from all the processors
 * \param total_p the total number of processor that want to communicate with you
 * \param i processor id
 * \param ri request id (it is an id that goes from 0 to total_p, and is unique
 *           every time message_alloc is called)
 * \param ptr a pointer to the vector_dist structure
 *
 * \return the pointer where to store the message for the processor i
 *
 */
static void * msg_alloc(size_t msg_i ,size_t total_msg, size_t total_p, size_t i, size_t ri, void * ptr)
{
Pietro Incardona's avatar
Pietro Incardona committed
274
	base_info & rinfo = *(base_info *)ptr;
275

Pietro Incardona's avatar
Pietro Incardona committed
276
	if (rinfo.recv_buf == NULL)
277
278
		std::cerr << __FILE__ << ":" << __LINE__ << " Internal error this processor is not suppose to receive\n";

Pietro Incardona's avatar
Pietro Incardona committed
279
	rinfo.recv_buf->resize(ri+1);
280

Pietro Incardona's avatar
Pietro Incardona committed
281
282
283
284
285
	rinfo.recv_buf->get(ri).resize(msg_i);

	// Receive info
	rinfo.prc.add(i);
	rinfo.sz.add(msg_i);
286
287

	// return the pointer
Pietro Incardona's avatar
Pietro Incardona committed
288
	return rinfo.recv_buf->last().getPointer();
289
290
}

Pietro Incardona's avatar
Pietro Incardona committed
291
292
293
294
295
296
297
298
/*! \brief Process the receive buffer
 *
 * \tparam T type of sending object
 * \tparam S type of receiving object
 *
 * \param recv receive object
 *
 */
Pietro Incardona's avatar
Pietro Incardona committed
299
template<typename T, typename S> void process_receive_buffer(S & recv, openfpm::vector<size_t> * sz = NULL)
Pietro Incardona's avatar
Pietro Incardona committed
300
{
Pietro Incardona's avatar
Pietro Incardona committed
301
302
303
	if (sz != NULL)
		sz->resize(recv_buf.size());

304
	pack_unpack_cond<has_max_prop<T, has_value_type<T>::value>::value, T, S>::unpacking(recv, recv_buf, sz);
Pietro Incardona's avatar
Pietro Incardona committed
305
306
307
308
}

public:

309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*! \brief Semantic Gather, gather the data from all processors into one node
 *
 * Semantic communication differ from the normal one. They in general 
 * follow the following model.
 * 
 * Gather(T,S,root,op=add);
 *
 * "Gather" indicate the communication pattern, or how the information flow
 * T is the object to send, S is the object that will receive the data. 
 * In order to work S must implement the interface S.add(T).
 *
 * ### Example send a vector of structures, and merge all together in one vector
 * \snippet VCluster_semantic_unit_tests.hpp Gather the data on master
 *
 * ### Example send a vector of structures, and merge all together in one vector
 * \snippet VCluster_semantic_unit_tests.hpp Gather the data on master complex
 *
Pietro Incardona's avatar
Pietro Incardona committed
326
327
328
 * \tparam T type of sending object
 * \tparam S type of receiving object
 *
329
330
331
332
333
334
335
 * \param Object to send
 * \param Object to receive
 * \param root witch node should collect the information
 *
 * \return true if the function completed succefully
 *
 */
Pietro Incardona's avatar
Pietro Incardona committed
336
337
338
339
340
341
342
343
template<typename T, typename S> bool SGather(T & send, S & recv,size_t root)
{
	openfpm::vector<size_t> prc;
	openfpm::vector<size_t> sz;

	return SGather(send,recv,prc,sz,root);
}

Yaroslav's avatar
Yaroslav committed
344
345
346
347
template<size_t index, size_t N> struct MetaFuncOrd {
   enum { value = index };
};

Pietro Incardona's avatar
Pietro Incardona committed
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/*! \brief Semantic Gather, gather the data from all processors into one node
 *
 * Semantic communication differ from the normal one. They in general
 * follow the following model.
 *
 * Gather(T,S,root,op=add);
 *
 * "Gather" indicate the communication pattern, or how the information flow
 * T is the object to send, S is the object that will receive the data.
 * In order to work S must implement the interface S.add(T).
 *
 * ### Example send a vector of structures, and merge all together in one vector
 * \snippet VCluster_semantic_unit_tests.hpp Gather the data on master
 *
 * ### Example send a vector of structures, and merge all together in one vector
 * \snippet VCluster_semantic_unit_tests.hpp Gather the data on master complex
 *
 * \tparam T type of sending object
 * \tparam S type of receiving object
 *
 * \param Object to send
 * \param Object to receive
 * \param root witch node should collect the information
 * \param prc processors from witch we received the information
 * \param sz size of the received information for each processor
 *
 * \return true if the function completed succefully
 *
 */
template<typename T, typename S> bool SGather(T & send, S & recv, openfpm::vector<size_t> & prc, openfpm::vector<size_t> & sz,size_t root)
378
{
Pietro Incardona's avatar
Pietro Incardona committed
379
380
	// Reset the receive buffer
	reset_recv_buf();
Yaroslav's avatar
Yaroslav committed
381
	
382
383
384
	// If we are on master collect the information
	if (getProcessUnitID() == root)
	{
Yaroslav's avatar
Yaroslav committed
385
#ifdef DEBUG
Yaroslav's avatar
Yaroslav committed
386
		std::cout << "Inside root " << root << std::endl;
Yaroslav's avatar
Yaroslav committed
387
#endif
388
389
390
391
		// send buffer (master does not send anything) so send req and send_buf
		// remain buffer with size 0
		openfpm::vector<size_t> send_req;

Pietro Incardona's avatar
Pietro Incardona committed
392
393
394
		// receive information
		base_info bi(&recv_buf,prc,sz);

395
		// Send and recv multiple messages
Pietro Incardona's avatar
Pietro Incardona committed
396
397
398
		sendrecvMultipleMessagesNBX(send_req.size(),NULL,NULL,NULL,msg_alloc,&bi);

		// process the received information
Pietro Incardona's avatar
Pietro Incardona committed
399
		process_receive_buffer<T,S>(recv,&sz);
400

Pietro Incardona's avatar
Pietro Incardona committed
401
		recv.add(send);
Pietro Incardona's avatar
Pietro Incardona committed
402
		prc.add(root);
Yaroslav's avatar
Yaroslav committed
403
		sz.add(send.size());
404
405
406
	}
	else
	{
Yaroslav's avatar
Yaroslav committed
407
408
409
#ifdef DEBUG
		std::cout << "Inside slave " << getProcessUnitID() << std::endl;
#endif
410
411
412
		// send buffer (master does not send anything) so send req and send_buf
		// remain buffer with size 0
		openfpm::vector<size_t> send_prc;
Pietro Incardona's avatar
Pietro Incardona committed
413
		send_prc.add(root);
Yaroslav's avatar
Yaroslav committed
414
				
415
416
417
		openfpm::vector<size_t> sz;
				
		openfpm::vector<const void *> send_buf;
Yaroslav's avatar
Yaroslav committed
418
419
420
			
		//Pack requesting
		
421
		size_t tot_size = 0;
422
423
		
		pack_unpack_cond<has_max_prop<T, has_value_type<T>::value>::value, T, S>::packingRequest(send, tot_size, sz);
Yaroslav's avatar
Yaroslav committed
424
425
		
		HeapMemory pmem;
Yaroslav's avatar
Yaroslav committed
426
		
Yaroslav's avatar
Yaroslav committed
427
428
429
430
431
432
433
		ExtPreAlloc<HeapMemory> & mem = *(new ExtPreAlloc<HeapMemory>(tot_size,pmem));
		mem.incRef();

		//Packing

		Pack_stat sts;
		
434
		pack_unpack_cond<has_max_prop<T, has_value_type<T>::value>::value, T, S>::packing(mem, send, sts, send_buf);
435

Pietro Incardona's avatar
Pietro Incardona committed
436
437
438
		// receive information
		base_info bi(NULL,prc,sz);

439
		// Send and recv multiple messages
Pietro Incardona's avatar
Pietro Incardona committed
440
		sendrecvMultipleMessagesNBX(send_prc.size(),(size_t *)sz.getPointer(),(size_t *)send_prc.getPointer(),(void **)send_buf.getPointer(),msg_alloc,(void *)&bi);
441
442
443
444
	}
	
	return true;
}
Pietro Incardona's avatar
Pietro Incardona committed
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502

/*! \brief Semantic Scatter, scatter the data from one processor to the other node
 *
 * Semantic communication differ from the normal one. They in general
 * follow the following model.
 *
 * Scatter(T,S,...,op=add);
 *
 * "Scatter" indicate the communication pattern, or how the information flow
 * T is the object to send, S is the object that will receive the data.
 * In order to work S must implement the interface S.add(T).
 *
 * ### Example scatter a vector of structures, to other processors
 * \snippet VCluster_semantic_unit_tests.hpp Scatter the data from master
 *
 * \tparam T type of sending object
 * \tparam S type of receiving object
 *
 * \param Object to send
 * \param Object to receive
 * \param prc processor involved in the scatter
 * \param sz size of each chunks
 * \param root which processor should scatter the information
 *
 * \return true if the function completed succefully
 *
 */
template<typename T, typename S> bool SScatter(T & send, S & recv, openfpm::vector<size_t> & prc, openfpm::vector<size_t> & sz, size_t root)
{
	// Reset the receive buffer
	reset_recv_buf();

	// If we are on master scatter the information
	if (getProcessUnitID() == root)
	{
		// Prepare the sending buffer
		openfpm::vector<const void *> send_buf;


		openfpm::vector<size_t> sz_byte;
		sz_byte.resize(sz.size());

		size_t ptr = 0;

		for (size_t i = 0; i < sz.size() ; i++)
		{
			send_buf.add((char *)send.getPointer() + sizeof(typename T::value_type)*ptr );
			sz_byte.get(i) = sz.get(i) * sizeof(typename T::value_type);
			ptr += sz.get(i);
		}

		// receive information
		base_info bi(&recv_buf,prc,sz);

		// Send and recv multiple messages
		sendrecvMultipleMessagesNBX(prc.size(),(size_t *)sz_byte.getPointer(),(size_t *)prc.getPointer(),(void **)send_buf.getPointer(),msg_alloc,(void *)&bi);

		// process the received information
Pietro Incardona's avatar
Pietro Incardona committed
503
		process_receive_buffer<T,S>(recv,NULL);
Pietro Incardona's avatar
Pietro Incardona committed
504
505
506
507
508
509
510
511
512
513
514
515
	}
	else
	{
		// The non-root receive
		openfpm::vector<size_t> send_req;

		// receive information
		base_info bi(&recv_buf,prc,sz);

		// Send and recv multiple messages
		sendrecvMultipleMessagesNBX(send_req.size(),NULL,NULL,NULL,msg_alloc,&bi);

Pietro Incardona's avatar
Pietro Incardona committed
516
		process_receive_buffer<T,S>(recv,NULL);
Pietro Incardona's avatar
Pietro Incardona committed
517
518
519
520
521
522
	}

	return true;
}


Pietro Incardona's avatar
Pietro Incardona committed
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
/*! \brief Semantic Send and receive, send the data to processors and receive from the other processors
 *
 * Semantic communication differ from the normal one. They in general
 * follow the following model.
 *
 * SSendRecv(T,S,...,op=add);
 *
 * "SendRecv" indicate the communication pattern, or how the information flow
 * T is the object to send, S is the object that will receive the data.
 * In order to work S must implement the interface S.add(T).
 *
 * ### Example scatter a vector of structures, to other processors
 * \snippet VCluster_semantic_unit_tests.hpp Scatter the data from master
 *
 * \tparam T type of sending object
 * \tparam S type of receiving object
 *
 * \param Object to send
 * \param Object to receive
 * \param prc processor involved in the scatter
 * \param sz size of each chunks
 * \param root which processor should scatter the information
 *
 * \return true if the function completed succefully
 *
 */
template<typename T, typename S> bool SSendRecv(openfpm::vector<T> & send, S & recv, openfpm::vector<size_t> & prc_send, openfpm::vector<size_t> & prc_recv, openfpm::vector<size_t> & sz_recv)
{
	// Reset the receive buffer
	reset_recv_buf();

#ifdef SE_CLASS1

	if (send.size() != prc_send.size())
		std::cerr << __FILE__ << ":" << __LINE__ << " Error, the number of processor involved \"prc.size()\" must match the number of sending buffers \"send.size()\" " << std::endl;

#endif

	// Prepare the sending buffer
	openfpm::vector<const void *> send_buf;

	openfpm::vector<size_t> sz_byte;
565
	
Pietro Incardona's avatar
Pietro Incardona committed
566
567
	for (size_t i = 0; i < send.size() ; i++)
	{
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
		size_t tot_size = 0;
	
		//Pack requesting
		
		pack_unpack_cond<has_max_prop<T, has_value_type<T>::value>::value, T, S>::packingRequest(send.get(i), tot_size, sz_byte);
		
		HeapMemory pmem;
		
		ExtPreAlloc<HeapMemory> & mem = *(new ExtPreAlloc<HeapMemory>(tot_size,pmem));
		mem.incRef();

		//Packing

		Pack_stat sts;
		
		pack_unpack_cond<has_max_prop<T, has_value_type<T>::value>::value, T, S>::packing(mem, send.get(i), sts, send_buf);

Pietro Incardona's avatar
Pietro Incardona committed
585
586
587
588
589
590
591
592
593
594
595
596
597
	}

	// receive information
	base_info bi(&recv_buf,prc_recv,sz_recv);

	// Send and recv multiple messages
	sendrecvMultipleMessagesNBX(prc_send.size(),(size_t *)sz_byte.getPointer(),(size_t *)prc_send.getPointer(),(void **)send_buf.getPointer(),msg_alloc,(void *)&bi);

	// process the received information
	process_receive_buffer<T,S>(recv,&sz_recv);

	return true;
}