VCluster_semantic.ipp 8.48 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:

Pietro Incardona's avatar
Pietro Incardona committed
12
13
14
15
16
17
18
19
20
21
/*! \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);
}

Pietro Incardona's avatar
Pietro Incardona committed
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/*! \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)
	{}
};

41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*! \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
56
	base_info & rinfo = *(base_info *)ptr;
57

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

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

Pietro Incardona's avatar
Pietro Incardona committed
63
64
65
66
67
	rinfo.recv_buf->get(ri).resize(msg_i);

	// Receive info
	rinfo.prc.add(i);
	rinfo.sz.add(msg_i);
68
69

	// return the pointer
Pietro Incardona's avatar
Pietro Incardona committed
70
	return rinfo.recv_buf->last().getPointer();
71
72
}

Pietro Incardona's avatar
Pietro Incardona committed
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*! \brief Process the receive buffer
 *
 * \tparam T type of sending object
 * \tparam S type of receiving object
 *
 * \param recv receive object
 *
 */
template<typename T, typename S> void process_receive_buffer(S & recv)
{
	for (size_t i = 0 ; i < recv_buf.size() ; i++)
	{
		// for each received buffer create a memory reppresentation
		// 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,openfpm::grow_policy_identity> v2;

		v2.setMemory(*ptr1);

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

		// Merge the information
		recv.add(v2);
	}
}

public:

107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*! \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
124
125
126
 * \tparam T type of sending object
 * \tparam S type of receiving object
 *
127
128
129
130
131
132
133
 * \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
134
135
136
137
138
139
140
141
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
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);
}

/*! \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)
172
{
Pietro Incardona's avatar
Pietro Incardona committed
173
174
175
	// Reset the receive buffer
	reset_recv_buf();
	
176
177
178
179
180
181
182
	// If we are on master collect the information
	if (getProcessUnitID() == root)
	{
		// 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
183
184
185
		// receive information
		base_info bi(&recv_buf,prc,sz);

186
		// Send and recv multiple messages
Pietro Incardona's avatar
Pietro Incardona committed
187
188
189
190
191
192
193
194
		sendrecvMultipleMessagesNBX(send_req.size(),NULL,NULL,NULL,msg_alloc,&bi);

		// Convert the received byte into number of elements
		for (size_t i = 0 ; i < sz.size() ; i++)
			sz.get(i) /= sizeof(typename T::value_type);

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

Pietro Incardona's avatar
Pietro Incardona committed
196
		recv.add(send);
Pietro Incardona's avatar
Pietro Incardona committed
197
198
		prc.add(root);
		sz.add(send.size());
199
200
201
202
203
204
	}
	else
	{
		// 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
205
		send_prc.add(root);
Pietro Incardona's avatar
Pietro Incardona committed
206
		openfpm::vector<const void *> send_buf;
207
208
209
210
		send_buf.add(send.getPointer());
		openfpm::vector<size_t> sz;
		sz.add(send.size()*sizeof(typename T::value_type));

Pietro Incardona's avatar
Pietro Incardona committed
211
212
213
		// receive information
		base_info bi(NULL,prc,sz);

214
		// Send and recv multiple messages
Pietro Incardona's avatar
Pietro Incardona committed
215
		sendrecvMultipleMessagesNBX(send_prc.size(),(size_t *)sz.getPointer(),(size_t *)send_prc.getPointer(),(void **)send_buf.getPointer(),msg_alloc,(void *)&bi);
216
217
218
219
	}
	
	return true;
}
Pietro Incardona's avatar
Pietro Incardona committed
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
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

/*! \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
		process_receive_buffer<T,S>(recv);
	}
	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);

		process_receive_buffer<T,S>(recv);
	}

	return true;
}