StdMpi.h 7.22 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
// ============================================================================
// ==                                                                        ==
// == 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"
27
#include "MeshStructure.h"
28
29
30
31
32
33
34
35

namespace AMDiS {

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

36
37
38
  int intSizeOf(std::vector<MeshStructure> &data)
  {
    int s = 0;
39
    for (unsigned int i = 0; i < data.size(); i++)
40
41
42
43
44
45
      s += data[i].getCode().size() + 2;

    return s;
  }

  void makeBuf(std::map<const DegreeOfFreedom*, DegreeOfFreedom> &data, int* buf)
46
47
48
49
50
51
52
53
54
  {
    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;
    }
  }
  
55
56
  void makeFromBuf(std::vector<std::pair<DegreeOfFreedom, DegreeOfFreedom> > &data, 
		   int *buf, int bufSize)
57
  {    
58
59
60
    if (bufSize == 0)
      return;

61
62
63
    data.clear();
    data.reserve(bufSize / 2);

64
65
    int i = 0;
    do {
66
      data.push_back(std::make_pair(buf[i], buf[i + 1]));
67
68
69
70
      i += 2;
    } while (i < bufSize);
  }

71
72
73
  void makeBuf(std::vector<MeshStructure> &data, unsigned long int *buf)
  {
    int pos = 0;
74
    for (unsigned int i = 0; i < data.size(); i++) {
75
76
      buf[pos++] = data[i].getCode().size();
      buf[pos++] = data[i].getNumElements();
77
      for (unsigned int j = 0; j < data[i].getCode().size(); j++)
78
79
80
	buf[pos++] = data[i].getCode()[j];
    }
  }
81

82
83
84
  void makeFromBuf(std::vector<MeshStructure> &data, unsigned long int *buf, int bufSize)
  {
    int pos = 0;
85
    
86
87
88
89
    while (pos < bufSize) {       
      int codeSize = buf[pos++];
      int nElements = buf[pos++];
      std::vector<unsigned long int> code;
90
      code.resize(codeSize);
91
92
93
94
95
96
97
98
99
      for (int i = 0; i < codeSize; i++)
	code[i] = buf[pos++];

      MeshStructure meshCode;
      meshCode.init(code, nElements);

      data.push_back(meshCode);
    }	
  }
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
  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;
    }

152
153
154
155
156
157
158
159
160
161
162
163
    template<class T>
    void recv(std::map<int, T> &fromRanks)
    {
      FUNCNAME("StdMpi::recv()");
      
      TEST_EXIT_DBG(commPrepared)("Communication is not prepared!\n");

      for (typename std::map<int, T>::iterator it = fromRanks.begin();
	   it != fromRanks.end(); ++it)
	recvDataSize[it->first] = -1;
    }

164
165
166
167
168
    std::map<int, RecvT>& getRecvData()
    {
      return recvData;
    }

169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
    void commDataSize()
    {
      MPI::Request request[sendData.size() + recvDataSize.size()];
      std::vector<int> sendBuffers, recvBuffers;
      int requestCounter = 0;

      int i = 0;
      for (typename std::map<int, int>::iterator sendIt = sendDataSize.begin();
	   sendIt != sendDataSize.end(); ++sendIt) {
	sendBuffers.push_back(sendIt->second);
	request[requestCounter++] =
	  mpiComm.Isend(&(sendBuffers[i]), 1, MPI_INT, sendIt->first, 0);
	i++;
      }

      for (std::map<int, int>::iterator recvIt = recvDataSize.begin();
	   recvIt != recvDataSize.end(); ++recvIt)
	request[requestCounter++] =
	  mpiComm.Irecv(&(recvIt->second), 1, MPI_INT, recvIt->first, 0);
      
      MPI::Request::Waitall(requestCounter, request);      
    }

    template<class T>
193
194
195
196
197
198
    void startCommunication()
    {
      FUNCNAME("StdMpi::startCommunication()");

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

199
200
201
      if (exchangeDataSize)
	commDataSize();

202
203
      MPI::Request request[sendData.size() + recvDataSize.size()];
      int requestCounter = 0;
204
      std::vector<T*> sendBuffers, recvBuffers;
205
206
207

      for (typename std::map<int, SendT>::iterator sendIt = sendData.begin();
	   sendIt != sendData.end(); ++sendIt) {
208
209
210
	int bufferSize = sendDataSize[sendIt->first];
	T* buf = new T[bufferSize];
	makeBuf(sendIt->second, buf);
211
212
213
214
215
216
217
218
219

  	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) {
220
	T* buf = new T[recvIt->second];
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236

  	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) {
237
	makeFromBuf(recvData[recvIt->first], recvBuffers[i], recvIt->second);
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
	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