File: mpisgbuf.cpp

package info (click to toggle)
esys-particle 2.3.4%2Bdfsg1-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 13,036 kB
  • ctags: 10,805
  • sloc: cpp: 80,009; python: 5,872; makefile: 1,243; sh: 313; perl: 225
file content (318 lines) | stat: -rw-r--r-- 7,924 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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
83
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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
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
219
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/////////////////////////////////////////////////////////////
//                                                         //
// Copyright (c) 2003-2014 by The University of Queensland //
// Centre for Geoscience Computing                         //
// http://earth.uq.edu.au/centre-geoscience-computing      //
//                                                         //
// Primary Business: Brisbane, Queensland, Australia       //
// Licensed under the Open Software License version 3.0    //
// http://www.apache.org/licenses/LICENSE-2.0          //
//                                                         //
/////////////////////////////////////////////////////////////

#include "mpisgbuf.h"
#include <cstdlib> // NULL
#include <string.h>

//-- AMPISGBufferRoot member functions----

/*!
  Constructor for AMPISGBufferRoot

  \param comm the MPI communicator
 */
AMPISGBufferRoot::AMPISGBufferRoot(MPI_Comm comm)
{
  m_comm=comm;
  MPI_Comm_rank(comm,&m_rank);
  MPI_Comm_size(comm,&m_size);
  MPI_Pack_size(1,MPI_INT,m_comm,&m_int_increment);
  MPI_Pack_size(1,MPI_DOUBLE,m_comm,&m_dbl_increment);
}

void AMPISGBufferRoot::append(const Vec3 &v,int nslice)
{
  append(v.X(),nslice);
  append(v.Y(),nslice);
  append(v.Z(),nslice);
}

Vec3 AMPISGBufferRoot::pop_vector(int nslice)
{
  double x[3] ;
  pop_doubles(nslice,x,3);
  return Vec3(x[0],x[1],x[2]);
}

//-- CMPISGBufferRoot member functions----
/*!
  Constructor for CMPISGBufferRoot

  \param comm the MPI communicator
  \param buffersize buffer size per slice
 */
CMPISGBufferRoot::CMPISGBufferRoot(MPI_Comm comm,int buffersize):AMPISGBufferRoot(comm)
{
  m_buffersize=buffersize;
  m_buffer=new char[m_buffersize*m_size];
  m_dummy_buffer=new char[m_buffersize];
  m_position=new int[m_size];
  for(int i=0;i<m_size;i++){
    m_position[i]=i*m_buffersize;
  }
}

CMPISGBufferRoot::~CMPISGBufferRoot()
{
  delete [] m_buffer;
  delete [] m_dummy_buffer;
  delete [] m_position;
}

void CMPISGBufferRoot::clear()
{
  for(int i=0;i<m_size;i++){
    m_position[i]=i*m_buffersize;
  }
}

/*!
  Get data from all other members of the communicator, using MPI_Gather 
*/
void CMPISGBufferRoot::gather()
{
  MPI_Gather(m_dummy_buffer,m_buffersize,MPI_PACKED,m_buffer,m_buffersize,MPI_PACKED,m_rank,m_comm);
}

/*!
  Send data to all other  members of the communicator, using MPI_Scatter
*/
void CMPISGBufferRoot::scatter()
{
  MPI_Scatter(m_buffer,m_buffersize,MPI_PACKED,m_dummy_buffer,m_buffersize,MPI_PACKED,m_rank,m_comm);
}

/*!
  Append an integer to a given slice of the buffer.

  \param i the integer
  \param nslice the nr. of the slice

  \warning No check for overflow
*/
void CMPISGBufferRoot::append(int i,int nslice)
{
  MPI_Pack(&i,1,MPI_INT,m_buffer,m_buffersize*m_size,&(m_position[nslice]),m_comm);
}

/*!
  Append an double to a given slice of the buffer.

  \param d the double
  \param nslice the nr. of the slice

  \warning No check for overflow
*/
void CMPISGBufferRoot::append(double d,int nslice)
{
  MPI_Pack(&d,1,MPI_DOUBLE,m_buffer,m_buffersize*m_size,&(m_position[nslice]),m_comm);
}
/*!
  Append an C string (char*) to a given slice of the buffer.

  \param str the string
  \param nslice the nr. of the slice

  \warning No check for overflow
*/
void CMPISGBufferRoot::append(const char* str,int nslice)
{
  int len=strlen(str);
  MPI_Pack(&len,1,MPI_INT,m_buffer,m_buffersize*m_size,&(m_position[nslice]),m_comm);
  MPI_Pack((void *)str,len,MPI_CHAR,m_buffer,m_buffersize*m_size,&(m_position[nslice]),m_comm);
}

/*!
  Pops an integer from a given slice of the the buffer, i.e. it pops the last sizeof(MPI_INT) bytes of the buffer, interpreting them as an int. 

  \param nslice the nr. of the slice
  \return the int.

  \warning No check for underflow
*/
int CMPISGBufferRoot::pop_int(int nslice)
{
  int res;
  MPI_Unpack(m_buffer,m_buffersize*m_size,&(m_position[nslice]),&res,1,MPI_INT,m_comm);
  
  return res;  
}


/*!
  Pops an double from a given slice of the the buffer. 

  \param nslice the nr. of the slice
  \return the double.

  \warning No check for underflow
*/
double CMPISGBufferRoot::pop_double(int nslice)
{
  double res;
  MPI_Unpack(m_buffer,m_buffersize,&(m_position[nslice]),&res,1,MPI_DOUBLE,m_comm);

  return res;
}

void CMPISGBufferRoot::pop_doubles(int nslice, double *dbl, int ndb)
{
  MPI_Unpack(m_buffer,m_buffersize,&(m_position[nslice]),dbl,ndb,MPI_DOUBLE,m_comm);
}

//-- AMPISGBufferLeaf member functions----

/*!
  Constuctor for AMPISGBufferLeaf

  \param comm the MPI communicator
  \param root rank of the root process
*/
AMPISGBufferLeaf::AMPISGBufferLeaf(MPI_Comm comm, int root):AMPIBuffer(comm)
{
  m_root=root;
  MPI_Pack_size(1,MPI_INT,m_comm,&m_int_increment);
  MPI_Pack_size(1,MPI_DOUBLE,m_comm,&m_dbl_increment);
}

//-- CMPISGBufferLeaf member functions----

/*!
  Constuctor for CMPISGBufferLeaf

  \param comm the MPI communicator
  \param root rank of the root process
  \param buffersize size of the communication buffer
*/
CMPISGBufferLeaf::CMPISGBufferLeaf(MPI_Comm comm,int root,int buffersize):AMPISGBufferLeaf(comm,root)
{
  m_buffersize=buffersize;
  m_buffer = new char[m_buffersize];
  for (int i = 0; i < m_buffersize; i++)
  {
    m_buffer[i] = '\0';
  }
  m_position=0;
}

CMPISGBufferLeaf::~CMPISGBufferLeaf()
{
  delete [] m_buffer;
}

void CMPISGBufferLeaf::clear()
{
  m_position=0;
}

/*!
  Send data to the root process, using MPI_Gather
*/
void CMPISGBufferLeaf::send()
{
  MPI_Gather(m_buffer,m_buffersize,MPI_PACKED,NULL,0,MPI_PACKED,m_root,m_comm);
}

/*!
  Receive data from root process, using MPI_Scatter
*/
void CMPISGBufferLeaf::receive()
{
  MPI_Scatter(NULL,0,MPI_PACKED,m_buffer,m_buffersize,MPI_PACKED,m_root,m_comm); 
}

/*!
  Append an integer to the buffer.

  \warning No check for overflow
*/
void CMPISGBufferLeaf::append(int i)
{
  MPI_Pack(&i,1,MPI_INT,m_buffer,m_buffersize,&m_position,m_comm);
}

/*!
  Append a double to the buffer.

  \warning No check for overflow
*/
void CMPISGBufferLeaf::append(double d)
{
  MPI_Pack(&d,1,MPI_DOUBLE,m_buffer,m_buffersize,&m_position,m_comm);
}

/*!
  Append a string to the buffer. The string appended is a normal (zero-terminated) C-string, but is internally handeled by packing the length frist and then the string.

  \warning No check for overflow
*/
void CMPISGBufferLeaf::append(const char* str)
{
  int len=strlen(str);
  MPI_Pack(&len,1,MPI_INT,m_buffer,m_buffersize,&m_position,m_comm);
  MPI_Pack((void *)str,len,MPI_CHAR,m_buffer,m_buffersize,&m_position,m_comm);
}

/*!
  Pops an integer from the buffer, i.e. it pops the last sizeof(MPI_INT) bytes of the buffer, interpreting them as an int. 

  \warning No check for underflow
  \return the int.
 */
int CMPISGBufferLeaf::pop_int()
{
  int res;
  MPI_Unpack(m_buffer,m_buffersize,&m_position,&res,1,MPI_INT,m_comm);

  return res;
}

/*! 
  Pops a double from the buffer.
  \warning No check for underflow
  \return the double.
  \sa CMPIBuffer::pop_int()
*/
double CMPISGBufferLeaf::pop_double()
{
  double res;
  MPI_Unpack(m_buffer,m_buffersize,&m_position,&res,1,MPI_DOUBLE,m_comm);

  return res;
}

void CMPISGBufferLeaf::pop_doubles(double *dbl, int ndb)
{
  MPI_Unpack(m_buffer,m_buffersize,&m_position,dbl,ndb,MPI_DOUBLE,m_comm);
}
/*! 
  Pops a string from the buffer. The first for bytes are interpreted as int, giving the length of the string (without terminating '\0'), the rest as the characters.

  \warning no consistency check, i.e. it is not checked if the length is smaller than the buffersize.
  \return the double.
  \sa CMPISingle::pop_int()
*/
std::string CMPISGBufferLeaf::pop_string()
{
  int len;
  char* res;

  MPI_Unpack(m_buffer,m_buffersize,&m_position,&len,1,MPI_INT,m_comm);
  res=new char[len+1]; // +1 for terminating '\0'
  MPI_Unpack(m_buffer,m_buffersize,&m_position,res,len,MPI_CHAR,m_comm);
  res[len]='\0';

  std::string poppedString = res;
  delete [] res;
  return poppedString;
}