Scippy

UG

Ubiquity Generator framework

scipDiffParamSetMpi.cpp
Go to the documentation of this file.
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2 /* */
3 /* This file is part of the program and software framework */
4 /* UG --- Ubquity Generator Framework */
5 /* */
6 /* Copyright Written by Yuji Shinano <shinano@zib.de>, */
7 /* Copyright (C) 2021 by Zuse Institute Berlin, */
8 /* licensed under LGPL version 3 or later. */
9 /* Commercial licenses are available through <licenses@zib.de> */
10 /* */
11 /* This code is free software; you can redistribute it and/or */
12 /* modify it under the terms of the GNU Lesser General Public License */
13 /* as published by the Free Software Foundation; either version 3 */
14 /* of the License, or (at your option) any later version. */
15 /* */
16 /* This program is distributed in the hope that it will be useful, */
17 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
18 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
19 /* GNU Lesser General Public License for more details. */
20 /* */
21 /* You should have received a copy of the GNU Lesser General Public License */
22 /* along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 /* */
24 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
25 
26 /**@file scipDiffParamSetMpi.cpp
27  * @brief ScipDiffParamSet extension for MPI communication.
28  * @author Yuji Shinano
29  *
30  *
31  *
32  */
33 
34 /*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
35 
36 
37 #include <string.h>
38 #include <cassert>
39 #include "scip/scip.h"
40 #include "scipParaTagDef.h"
41 #include "ug/paraCommMpi.h"
42 #include "scipDiffParamSetMpi.h"
43 
44 using namespace UG;
45 using namespace ParaSCIP;
46 
47 /** create scipDiffParamSetPreType */
48 MPI_Datatype
49 ScipDiffParamSetMpi::createDatatype1(
50  )
51 {
52  MPI_Datatype datatype;
53 
54  int blockLengthsPre[13];
55  MPI_Aint displacementsPre[13];
56  MPI_Datatype typesPre[13];
57 
58  MPI_Aint startAddress = 0;
59  MPI_Aint address = 0;
60 
61  for( int i = 0; i < 13; i++ ){
62  blockLengthsPre[i] = 1;
63  typesPre[i] = MPI_INT;
64  }
65 
66  MPI_CALL(
67  MPI_Get_address( &numBoolParams, &startAddress )
68  );
69  displacementsPre[0] = 0;
70  MPI_CALL(
71  MPI_Get_address( &boolParamNamesSize, &address )
72  );
73  displacementsPre[1] = address - startAddress;
74  MPI_CALL(
75  MPI_Get_address( &numIntParams, &address )
76  );
77  displacementsPre[2] = address - startAddress;
78  MPI_CALL(
79  MPI_Get_address( &intParamNamesSize, &address )
80  );
81  displacementsPre[3] = address - startAddress;
82  MPI_CALL(
83  MPI_Get_address( &numLongintParams, &address )
84  );
85  displacementsPre[4] = address - startAddress;
86  MPI_CALL(
87  MPI_Get_address( &longintParamNamesSize, &address )
88  );
89  displacementsPre[5] = address - startAddress;
90  MPI_CALL(
91  MPI_Get_address( &numRealParams, &address )
92  );
93  displacementsPre[6] = address - startAddress;
94  MPI_CALL(
95  MPI_Get_address( &realParamNamesSize, &address )
96  );
97  displacementsPre[7] = address - startAddress;
98  MPI_CALL(
99  MPI_Get_address( &numCharParams, &address )
100  );
101  displacementsPre[8] = address - startAddress;
102  MPI_CALL(
103  MPI_Get_address( &charParamNamesSize, &address )
104  );
105  displacementsPre[9] = address - startAddress;
106  MPI_CALL(
107  MPI_Get_address( &numStringParams, &address )
108  );
109  displacementsPre[10] = address - startAddress;
110  MPI_CALL(
111  MPI_Get_address( &stringParamNamesSize, &address )
112  );
113  displacementsPre[11] = address - startAddress;
114  MPI_CALL(
115  MPI_Get_address( &stringParamValuesSize, &address )
116  );
117  displacementsPre[12] = address - startAddress;
118 
119  MPI_CALL(
120  MPI_Type_create_struct(13, blockLengthsPre, displacementsPre, typesPre, &datatype)
121  );
122 
123  return datatype;
124 
125 }
126 
127 /** create scipDiffParamSetType */
128 MPI_Datatype
129 ScipDiffParamSetMpi::createDatatype2(
130  bool memAllocNecessary
131  )
132 {
133  MPI_Datatype datatype;
134 
135  int blockLengths[13];
136  MPI_Aint displacements[13];
137  MPI_Datatype types[13];
138 
139  MPI_Aint startAddress = 0;
140  MPI_Aint address = 0;
141 
142  if( memAllocNecessary ){
143  allocateMemoty();
144  }
145 
146  int nBlocks = 0;
147 
148  /** this is dummy */
149  MPI_CALL(
150  MPI_Get_address( &numBoolParams, &startAddress )
151  );
152  blockLengths[nBlocks] = 1;
153  displacements[nBlocks] = 0;
154  types[nBlocks++] = MPI_INT;
155 
156  if( boolParamNamesSize > 0 )
157  {
158  MPI_CALL(
159  MPI_Get_address( boolParamNames, &address )
160  );
161  blockLengths[nBlocks] = boolParamNamesSize;
162  displacements[nBlocks] = address - startAddress;
163  types[nBlocks++] = MPI_CHAR;
164  }
165 
166  if( numBoolParams > 0 )
167  {
168  MPI_CALL(
169  MPI_Get_address( boolParamValues, &address )
170  );
171  blockLengths[nBlocks] = numBoolParams;
172  displacements[nBlocks] = address - startAddress;
173  types[nBlocks++] = MPI_UNSIGNED;
174  }
175 
176 
177  if( intParamNamesSize > 0 )
178  {
179  MPI_CALL(
180  MPI_Get_address( intParamNames, &address )
181  );
182  blockLengths[nBlocks] = intParamNamesSize;
183  displacements[nBlocks] = address - startAddress;
184  types[nBlocks++] = MPI_CHAR;
185  }
186 
187 
188  if( numIntParams > 0 )
189  {
190  MPI_CALL(
191  MPI_Get_address( intParamValues, &address )
192  );
193  blockLengths[nBlocks] = numIntParams;
194  displacements[nBlocks] = address - startAddress;
195  types[nBlocks++] = MPI_INT;
196  }
197 
198  if( longintParamNamesSize > 0 )
199  {
200  MPI_CALL(
201  MPI_Get_address( longintParamNames, &address )
202  );
203  blockLengths[nBlocks] = longintParamNamesSize;
204  displacements[nBlocks] = address - startAddress;
205  types[nBlocks++] = MPI_CHAR;
206  }
207 
208  if( numLongintParams > 0 )
209  {
210  MPI_CALL(
211  MPI_Get_address( longintParamValues, &address )
212  );
213  blockLengths[nBlocks] = numLongintParams;
214  displacements[nBlocks] = address - startAddress;
215  #ifdef _ALIBABA
216  types[nBlocks++] = MPI_LONG;
217  #else
218  types[nBlocks++] = MPI_LONG_LONG;
219  #endif
220  }
221 
222  if( realParamNamesSize > 0 )
223  {
224  MPI_CALL(
225  MPI_Get_address( realParamNames, &address )
226  );
227  blockLengths[nBlocks] = realParamNamesSize;
228  displacements[nBlocks] = address - startAddress;
229  types[nBlocks++] = MPI_CHAR;
230  }
231 
232  if( numRealParams > 0 )
233  {
234  MPI_CALL(
235  MPI_Get_address( realParamValues, &address )
236  );
237  blockLengths[nBlocks] = numRealParams;
238  displacements[nBlocks] = address - startAddress;
239  types[nBlocks++] = MPI_DOUBLE;
240  }
241 
242  if( charParamNamesSize > 0 )
243  {
244  MPI_CALL(
245  MPI_Get_address( charParamNames, &address )
246  );
247  blockLengths[nBlocks] = charParamNamesSize;
248  displacements[nBlocks] = address - startAddress;
249  types[nBlocks++] = MPI_CHAR;
250  }
251 
252  if( numCharParams > 0 )
253  {
254  MPI_CALL(
255  MPI_Get_address( charParamValues, &address )
256  );
257  blockLengths[nBlocks] = numCharParams;
258  displacements[nBlocks] = address - startAddress;
259  types[nBlocks++] = MPI_CHAR;
260  }
261 
262  if( stringParamNamesSize > 0 )
263  {
264  MPI_CALL(
265  MPI_Get_address( stringParamNames, &address )
266  );
267  blockLengths[nBlocks] = stringParamNamesSize;
268  displacements[nBlocks] = address - startAddress;
269  types[nBlocks++] = MPI_CHAR;
270  }
271 
272  if( stringParamValuesSize > 0 )
273  {
274  MPI_CALL(
275  MPI_Get_address( stringParamValues, &address )
276  );
277  blockLengths[nBlocks] = stringParamValuesSize;
278  displacements[nBlocks] = address - startAddress;
279  types[nBlocks++] = MPI_CHAR;
280  }
281 
282 
283  MPI_CALL(
284  MPI_Type_create_struct(nBlocks, blockLengths, displacements, types, &datatype)
285  );
286 
287  return datatype;
288 }
289 
290 /** send solution data to the rank */
291 int
292 ScipDiffParamSetMpi::bcast(
293  ParaComm *comm,
294  int root
295  )
296 {
297  DEF_PARA_COMM( commMpi, comm);
298 
299  MPI_Datatype datatype = createDatatype1();
300  MPI_CALL(
301  MPI_Type_commit( &datatype )
302  );
304  commMpi->ubcast(&numBoolParams, 1, datatype, root)
305  );
306  MPI_CALL(
307  MPI_Type_free( &datatype )
308  );
309 
310  if( comm->getRank() == root )
311  {
312  datatype = createDatatype2(false);
313  }
314  else
315  {
316  datatype = createDatatype2(true);
317  }
318  MPI_CALL(
319  MPI_Type_commit( &datatype )
320  );
322  commMpi->ubcast(&numBoolParams, 1, datatype, root)
323  );
324  MPI_CALL(
325  MPI_Type_free( &datatype )
326  );
327  return 0;
328 }
329 
330 /** send solution data to the rank */
331 int
332 ScipDiffParamSetMpi::send(
333  ParaComm *comm,
334  int dest
335  )
336 {
337  DEF_PARA_COMM( commMpi, comm);
338  MPI_Datatype datatype = createDatatype1();
339  MPI_CALL(
340  MPI_Type_commit( &datatype )
341  );
343  commMpi->usend(&numBoolParams, 1, datatype, dest, UG::TagSolverDiffParamSet)
344  );
345  MPI_CALL(
346  MPI_Type_free( &datatype )
347  );
348 
349  datatype = createDatatype2(false);
350  MPI_CALL(
351  MPI_Type_commit( &datatype )
352  );
354  commMpi->usend(&numBoolParams, 1, datatype, dest, TagSolverDiffParamSet1)
355  );
356  MPI_CALL(
357  MPI_Type_free( &datatype )
358  );
359  // std::cout << "Send Rank " << comm->getRank() << ": " << toString() << std::endl;
360  return 0;
361 }
362 
363  /** receive solution data from the source rank */
364 int
365 ScipDiffParamSetMpi::receive(
366  ParaComm *comm,
367  int source
368  )
369 {
370  DEF_PARA_COMM( commMpi, comm);
371  MPI_Datatype datatype = createDatatype1();
372  MPI_CALL(
373  MPI_Type_commit( &datatype )
374  );
376  commMpi->ureceive(&numBoolParams, 1, datatype, source, UG::TagSolverDiffParamSet)
377  );
378  MPI_CALL(
379  MPI_Type_free( &datatype )
380  );
381 
382  datatype = createDatatype2(true);
383  MPI_CALL(
384  MPI_Type_commit( &datatype )
385  );
387  commMpi->ureceive(&numBoolParams, 1, datatype, source, TagSolverDiffParamSet1)
388  );
389  MPI_CALL(
390  MPI_Type_free( &datatype )
391  );
392  // std::cout << "Recieve Rank " << comm->getRank() << ": " << toString() << std::endl;
393  return 0;
394 }
static ScipParaCommTh * comm
Definition: fscip.cpp:73
#define PARA_COMM_CALL(paracommcall)
Definition: paraComm.h:47
virtual int getRank()=0
get rank of this process or this thread depending on run-time environment
ParaComm extension for MPI communication.
#define DEF_PARA_COMM(para_comm, comm)
static const int TagSolverDiffParamSet
Definition: bbParaTagDef.h:68
#define MPI_CALL(mpicall)
Definition: paraCommMpi.h:68
Base class of communicator object.
Definition: paraComm.h:101
ScipDiffParamSet extension for MPI communication.