aboutsummaryrefslogtreecommitdiff
path: root/src/MPIutils.cc
blob: 6f2c83e8e38d8bf6e0f221af9315f5d7b90d4508 (plain)
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
#include "MPIutils.hh"


void MPIbuffer::attach(int size){
  bufsize=size;
  char *mybuf = new char[bufsize];
  MPI_Buffer_attach(mybuf,bufsize);
}
void MPIbuffer::detach(){
  char *tmp;
  int size;
  MPI_Buffer_detach(&tmp,&size);
  if(tmp==mybuf) delete tmp;
  else MPI_Buffer_attach(tmp,size); // wasn't my buffer
}

MPIbuffer::MPIbuffer(int size):mybuf(0),bufsize(size){
  attach(size);
}
MPIbuffer::~MPIbuffer() {detach();}
void MPIbuffer::resize(int size){
  // check for pending requests using the buffer detach 
  // actually done by MPI_Buffer_detach() itself
  detach();
  attach(size);
}
void MPIbuffer::grow(int size){
  if(bufsize<size)
    resize(size);
}
void MPIbuffer::check(){ // make sure the buffersize is what we thought it was..
  char *tmp;
  int size;
  MPI_Buffer_detach(&tmp,&size);
  if(!tmp || tmp!=mybuf) 
    attach(bufsize);
  else if(tmp!=mybuf){
    if(size>=bufsize)
      MPI_Buffer_attach(tmp,size); // reattach
    else {
      delete tmp;
      attach(bufsize);
    }
  }
  else
    MPI_Buffer_attach(tmp,size); // reattach
  /* fails if existing buffer was too small and
     the source process deallocates the buffer without getting
     a handle to it.
     Can also fail if malloc != new on that architecture.
  */
}


// Must watch buffer attachment from external processes
MPIbuffer *MPIcomm::buffer;
MPI_Status MPIcomm::defstat;