StdMpi.h 5.03 KB
Newer Older
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
// ============================================================================
// ==                                                                        ==
// == AMDiS - Adaptive multidimensional simulations                          ==
// ==                                                                        ==
// ============================================================================
// ==                                                                        ==
// ==  TU Dresden                                                            ==
// ==                                                                        ==
// ==  Institut fr Wissenschaftliches Rechnen                               ==
// ==  Zellescher Weg 12-14                                                  ==
// ==  01069 Dresden                                                         ==
// ==  germany                                                               ==
// ==                                                                        ==
// ============================================================================
// ==                                                                        ==
// ==  https://gforge.zih.tu-dresden.de/projects/amdis/                      ==
// ==                                                                        ==
// ============================================================================

/** \file StdMpi.h */

#ifndef AMDIS_STDMPI_H
#define AMDIS_STDMPI_H

#include <map>
#include "mpi.h"

namespace AMDiS {

  int intSizeOf(std::map<const DegreeOfFreedom*, DegreeOfFreedom> &data)
  {
    return data.size() * 2;
  }

  void makeIntBuf(std::map<const DegreeOfFreedom*, DegreeOfFreedom> &data, int* buf)
  {
    int i = 0;
    for (std::map<const DegreeOfFreedom*, DegreeOfFreedom>::iterator it = data.begin();
	 it != data.end(); ++it) {
      buf[i++] = *(it->first);
      buf[i++] = it->second;
    }
  }
  
  void makeFromIntBuf(std::map<DegreeOfFreedom, DegreeOfFreedom> &data, 
		      int *buf, int bufSize)
  {
    if (bufSize == 0)
      return;

    int i = 0;
    do {
      data[buf[i]] = buf[i + 1];
      i += 2;
    } while (i < bufSize);
  }


  template<typename SendT, typename RecvT>
  class StdMpi 
  {
  public:
    StdMpi(MPI::Intracomm &comm) :
      mpiComm(comm),
      commPrepared(false),
      exchangeDataSize(true)
    {}

    void prepareCommunication(bool b)
    {
      sendData.clear();
      recvData.clear();
            
      exchangeDataSize = b;
      commPrepared = true;
    }

    void send(int toRank, SendT& data)
    {
      FUNCNAME("StdMpi::send()");
      
      TEST_EXIT_DBG(commPrepared)("Communication is not prepared!\n");

      sendData[toRank] = data;
      sendDataSize[toRank] = intSizeOf(data);
    }

    void send(std::map<int, SendT>& data)
    {
      FUNCNAME("StdMpi::send()");
      
      TEST_EXIT_DBG(commPrepared)("Communication is not prepared!\n");

      for (typename std::map<int, SendT>::iterator it = data.begin(); 
	   it != data.end(); ++it) {
	sendData[it->first] = it->second;
	sendDataSize[it->first] = intSizeOf(it->second);
      }
    }

    void recv(int fromRank, int size = -1)
    {
      FUNCNAME("StdMpi::recv()");
      
      TEST_EXIT_DBG(commPrepared)("Communication is not prepared!\n");

      recvDataSize[fromRank] = size;
    }

    std::map<int, RecvT>& getRecvData()
    {
      return recvData;
    }

    void startCommunication()
    {
      FUNCNAME("StdMpi::startCommunication()");

      TEST_EXIT_DBG(commPrepared)("Communication is not prepared!\n");

      MPI::Request request[sendData.size() + recvDataSize.size()];
      int requestCounter = 0;
      std::vector<int*> sendBuffers, recvBuffers;

      for (typename std::map<int, SendT>::iterator sendIt = sendData.begin();
	   sendIt != sendData.end(); ++sendIt) {
	int bufferSize = intSizeOf(sendIt->second);
	int* buf = new int[bufferSize];
	makeIntBuf(sendIt->second, buf);

  	request[requestCounter++] = 
  	  mpiComm.Isend(buf, bufferSize, MPI_INT, sendIt->first, 0);

	sendBuffers.push_back(buf);
      }

      for (std::map<int, int>::iterator recvIt = recvDataSize.begin();
	   recvIt != recvDataSize.end(); ++recvIt) {
	int* buf = new int[recvIt->second];

  	request[requestCounter++] =
  	  mpiComm.Irecv(buf, recvIt->second, MPI_INT, recvIt->first, 0);

	recvBuffers.push_back(buf);	
      }

      MPI::Request::Waitall(requestCounter, request);

      for (int i = 0; i < static_cast<int>(sendBuffers.size()); i++)
	delete [] sendBuffers[i];
      sendBuffers.clear();

      int i = 0;
      for (std::map<int, int>::iterator recvIt = recvDataSize.begin();
	   recvIt != recvDataSize.end(); ++recvIt) {
	makeFromIntBuf(recvData[recvIt->first], recvBuffers[i], recvIt->second);
	delete [] recvBuffers[i];
	i++;
      }      
    }

  protected:
    ///
    MPI::Intracomm mpiComm;

    ///
    std::map<int, SendT> sendData;

    ///
    std::map<int, RecvT> recvData;

    std::map<int, int> sendDataSize;

    std::map<int, int> recvDataSize;

    bool commPrepared;

    bool exchangeDataSize;
  };

}

#endif