File: mpisgvbuf.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 (414 lines) | stat: -rw-r--r-- 11,545 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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/////////////////////////////////////////////////////////////
//                                                         //
// 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 "mpisgvbuf.h"
//#include "console.h"

#include <string.h>

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

  \param comm the MPI communicator
  \param isize initial buffer size per slice, default 16 byte
 */
CMPIVarSGBufferRoot::CMPIVarSGBufferRoot(MPI_Comm comm,int isize):AMPISGBufferRoot(comm)
{
  m_vbuffersize=isize;
  m_vbuffer=new char[m_vbuffersize*m_size];
  m_dummy_vbuffer=new char[m_vbuffersize];
  m_position=new int[m_size];
  m_rpos=new int[m_size];
  m_recvcount=new int[m_size];
  // setup initial displacements in buffer
  m_displ=new int[m_size];
  for(int i=0;i<m_size;i++){
    m_displ[i]=i*m_vbuffersize;
    m_position[i]=i*m_vbuffersize;
  }
  m_position[0]=0;
  m_recvcount[0]=0;
}


CMPIVarSGBufferRoot::~CMPIVarSGBufferRoot()
{
  delete m_vbuffer;
  delete m_dummy_vbuffer;
  delete m_position;
  delete m_recvcount;
  delete m_displ;
  delete m_rpos;
}

/*!
  Grows the buffer to twice its current size, thus guaranteeing that append
  works in amortized constant time. Currently grows the buffer homogeneously,
  i.e. all slices have the same size. 

  \warning no check if there is enough space for the new buffer
*/
void CMPIVarSGBufferRoot::grow()
{
  char *temp=m_vbuffer;
  int vbs_old=m_vbuffersize;
  m_vbuffersize+=m_vbuffersize;
  m_vbuffer=new char[m_vbuffersize*m_size];
  for(int i=0;i<m_size;i++){
    memcpy((void*)(&m_vbuffer[i*m_vbuffersize]),(void*)(&temp[i*vbs_old]),size_t(m_position[i]-m_displ[i]));
    m_position[i]=m_position[i]+i*vbs_old;;
    m_displ[i]+=m_displ[i];
  }
  delete temp;
}

void CMPIVarSGBufferRoot::growTo(int size)
{
  if(size>m_vbuffersize){
    char *temp=m_vbuffer;
    int vbs_old=m_vbuffersize;
    m_vbuffersize=size;
    m_vbuffer=new char[m_vbuffersize*m_size];
    for(int i=0;i<m_size;i++){
      memcpy((void*)(&m_vbuffer[i*m_vbuffersize]),(void*)(&temp[m_displ[i]]),size_t(m_position[i]-m_displ[i]));
      m_position[i]=m_position[i]+i*(m_vbuffersize-vbs_old);
      m_displ[i]=i*m_vbuffersize;
    }
    delete temp;
  }  
}

void CMPIVarSGBufferRoot::clear()
{
  for(int i=1;i<m_size;i++) m_position[i]=m_displ[i];
}

/*!
  Send data to the root process, using MPI_Gather and MPI_Gatherv.
  The receive buffer grows to fit the data if neccessary.
*/
void CMPIVarSGBufferRoot::gather()
{
  int i;

  // zero positions (rel.)
  for(i=1;i<m_size;i++) m_position[i]=m_displ[i];
  // get data sizes
  MPI_Gather(m_dummy_vbuffer,1,MPI_INT,m_recvcount,1,MPI_INT,m_rank,m_comm);
  // if buffer to small, grow
  int max_recv=0;
  for(i=1;i<m_size;i++){ // from 1, i.e. excluding the dummy !
    max_recv=(m_recvcount[i]>max_recv) ? m_recvcount[i] : max_recv;
  }
  if(max_recv>m_vbuffersize){
    growTo(max_recv);
  }
  m_recvcount[0]=1;
  // get data
  MPI_Gatherv(m_dummy_vbuffer,1,MPI_PACKED,m_vbuffer,m_recvcount,m_displ,MPI_PACKED,m_rank,m_comm);
}

/*!
  Send data to all other  members of the communicator, using MPI_Scatter/MPI_Scatterv
*/
void CMPIVarSGBufferRoot::scatter()
{
  //console.XDebug() << "CMPIVarSGBufferRoot::scatter()\n";
  //console.XDebug() << "m_vbuffersize " << m_vbuffersize << "\n";
  // distribute buffer size
  for(int j=1;j<m_size;j++){
    m_rpos[j]=m_position[j]-m_displ[j];
    //console.XDebug() << "posn: " << j << " , " << m_rpos[j] << "\n";
  }
  m_rpos[0]=1;
  MPI_Scatter((void *)m_rpos,1,MPI_INT,&m_ndummy,1,MPI_INT,m_rank,m_comm);
  // send the actual data
  MPI_Scatterv(m_vbuffer,m_rpos,m_displ,MPI_PACKED,m_dummy_vbuffer,1,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 CMPIVarSGBufferRoot::append(int i,int nslice)
{
  if((m_position[nslice]-m_displ[nslice])+m_int_increment>m_vbuffersize){ // to small, grow
    grow();
  }
  MPI_Pack(&i,1,MPI_INT,m_vbuffer,m_vbuffersize*m_size,&(m_position[nslice]),m_comm);
}


/*!
  Append a 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 CMPIVarSGBufferRoot::append(double d,int nslice)
{
  if((m_position[nslice]-m_displ[nslice])+m_dbl_increment>m_vbuffersize){ // to small, grow
    grow();
  }
  MPI_Pack(&d,1,MPI_DOUBLE,m_vbuffer,m_vbuffersize*m_size,&(m_position[nslice]),m_comm);
}

/*!
  Append a C-string to a given slice of the buffer.

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

  \warning No check for overflow
*/
void CMPIVarSGBufferRoot::append(const char* str,int nslice)
{
  int len=strlen(str);
  //console.XDebug()<< "append string,len " << str  << " , " <<  len << "\n";
  if((m_position[nslice]-m_displ[nslice])+m_int_increment+len>m_vbuffersize){ // to small, grow
    grow();
  }
  MPI_Pack(&len,1,MPI_INT,m_vbuffer,m_vbuffersize*m_size,&(m_position[nslice]),m_comm);
  MPI_Pack((void *)str,len,MPI_CHAR,m_vbuffer,m_vbuffersize*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 CMPIVarSGBufferRoot::pop_int(int nslice)
{
  int res;
  MPI_Unpack(m_vbuffer,m_vbuffersize*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 CMPIVarSGBufferRoot::pop_double(int nslice)
{
  double res;
  MPI_Unpack(m_vbuffer,m_vbuffersize*m_size,&(m_position[nslice]),&res,1,MPI_DOUBLE,m_comm);

  return res;
}

void CMPIVarSGBufferRoot::pop_doubles(int nslice, double *dbl, int ndb)
{
  MPI_Unpack(m_vbuffer,m_vbuffersize*m_size,&(m_position[nslice]),dbl,ndb,MPI_DOUBLE,m_comm);
}

//-- CMPIVarSGBufferLeaf member functions ----  

/*!
  Constuctor for CMPISGBufferLeaf

  \param comm the MPI communicator
  \param root rank of the root process
  \param isize initial size of the communication buffer, default 16
*/
CMPIVarSGBufferLeaf::CMPIVarSGBufferLeaf(MPI_Comm comm,int root,int isize):AMPISGBufferLeaf(comm,root)
{
  m_vbuffersize=isize;
  m_vbuffer=new char[m_vbuffersize];
  m_position=0;
}

CMPIVarSGBufferLeaf::~CMPIVarSGBufferLeaf()
{
  delete m_vbuffer;
}

/*!
  Grows the buffer to twice its current size, thus guaranteeing that append works in amortized constant time.  
*/
void CMPIVarSGBufferLeaf::grow()
{
  char *temp=m_vbuffer;
  m_vbuffersize+=m_vbuffersize;
  m_vbuffer=new char[m_vbuffersize];
  memcpy((void*)m_vbuffer,(void*)temp,size_t(m_position));
  delete temp;
}

/*!
  Grows the buffer to a given size. If the buffer is already larger
  that the given size, nothing is done. Used by receiveFrom.
  
  \param size size to which the buffer is grown
*/
void CMPIVarSGBufferLeaf::growTo(int size)
{
  if(size>m_vbuffersize){
    char *temp=m_vbuffer;
    m_vbuffersize=size;
    m_vbuffer=new char[m_vbuffersize];
    memcpy((void*)m_vbuffer,(void*)temp,size_t(m_position));
    delete temp;
  }
}

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

/*!
  Send data to the root process, using MPI_Gather/MPI_Gatherv
*/
void CMPIVarSGBufferLeaf::send()
{
  // send size of data
  MPI_Gather(&m_position,1,MPI_INT,NULL,0,MPI_INT,m_root,m_comm);
  // send data
  MPI_Gatherv(m_vbuffer,m_position,MPI_PACKED,NULL,NULL,NULL,MPI_PACKED,m_root,m_comm);
}

/*!
  Receive data from root process, using MPI_Scatter/MPI_Scatterv.
  The buffer grows to fit the data, if neccesary. 
*/
void CMPIVarSGBufferLeaf::receive()
{
  //console.XDebug() <<  "CMPIVarSGBufferLeaf::receive()\n";
  // get size of data
  MPI_Scatter(NULL,0,MPI_INT,&m_data_size,1,MPI_INT,m_root,m_comm);
  //console.XDebug() <<  "recv. data size : " << m_data_size << "\n";
  // if buffer to small, grow
  if(m_data_size>m_vbuffersize){
    growTo(m_data_size);
  }
  //console.XDebug() << "buffersize : " << m_vbuffersize << "\n";
  // get data
  MPI_Scatterv(NULL,NULL,NULL,MPI_PACKED,m_vbuffer,m_data_size,MPI_PACKED,m_root,m_comm);
}

/*!
  Append an integer to the buffer. If necessary, the buffer is enlarged.

  \warning currently does not check if there is enough free space to allocate larger buffer
*/
void CMPIVarSGBufferLeaf::append(int i)
{
  if(m_position+m_int_increment>m_vbuffersize){ // to small, grow
    grow();
  }
  MPI_Pack(&i,1,MPI_INT,m_vbuffer,m_vbuffersize,&m_position,m_comm);
}

/*!
  Append a double to the buffer. If necessary, the buffer is enlarged.

  \warning currently does not check if there is enough free space to allocate larger buffer
  \sa grow()
*/
void CMPIVarSGBufferLeaf::append(double d)
{
  if(m_position+m_dbl_increment>m_vbuffersize){ // to small, grow
    grow();
  }
  MPI_Pack(&d,1,MPI_DOUBLE,m_vbuffer,m_vbuffersize,&m_position,m_comm);
}

/*!
  Append a string to the buffer. If necessary, the buffer is enlarged.

  \warning currently does not check if there is enough free space to allocate larger buffer
  \sa grow()
*/
void CMPIVarSGBufferLeaf::append(const char* str)
{
  int len=strlen(str);
  if(m_position+m_int_increment+len<m_vbuffersize){
    grow();
  }
  MPI_Pack(&len,1,MPI_INT,m_vbuffer,m_vbuffersize,&m_position,m_comm);
  MPI_Pack((void *)str,len,MPI_CHAR,m_vbuffer,m_vbuffersize,&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 CMPIVarSGBufferLeaf::pop_int()
{
  int res;
  MPI_Unpack(m_vbuffer,m_vbuffersize,&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 CMPIVarSGBufferLeaf::pop_double()
{
  double res;
  MPI_Unpack(m_vbuffer,m_vbuffersize,&m_position,&res,1,MPI_DOUBLE,m_comm);

  return res;
}

void CMPIVarSGBufferLeaf::pop_doubles(double *dbl, int ndb)
{
  MPI_Unpack(m_vbuffer,m_vbuffersize,&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 string.
  \sa CVarMPISingle::pop_int()
*/
std::string CMPIVarSGBufferLeaf::pop_string()
{
  int len = 0;
  MPI_Unpack(m_vbuffer,m_vbuffersize,&m_position,&len,1,MPI_INT,m_comm);
  char *res=new char[len+1]; // +1 for terminating '\0'
  //console.XDebug()<< "pop string,len " << len  << "\n";
  MPI_Unpack(m_vbuffer,m_vbuffersize,&m_position,res,len,MPI_CHAR,m_comm);
  res[len]='\0';

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