Scippy

UG

Ubiquity Generator framework

paraParamSetMpi.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 paraParamSetMpi.cpp
27  * @brief ParaParamSet 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 "paraCommMpi.h"
38 #include "paraParamSetMpi.h"
39 #include <cstring>
40 
41 using namespace UG;
42 
43 /** allocate memory for transfer */
44 void
46  )
47 {
48  if( getNumBoolParams() )
49  {
50  // std::cout << "getNumBoolParams() = " << getNumBoolParams() << std::endl;
51  boolParams = new int[getNumBoolParams()];
52  boolParamValues = new char[getNumBoolParams()];
53  }
54  if( getNumIntParams() )
55  {
56  // std::cout << "getNumIntParams() = " << getNumIntParams() << std::endl;
57  intParams = new int[getNumIntParams()];
58  intParamValues = new int[getNumIntParams()];
59  }
60  if( getNumLongintParams() )
61  {
62  // std::cout << "getNumLongintParams() = " << getNumLongintParams() << std::endl;
64  longintParamValues = new long long[getNumLongintParams()];
65  }
66  if( getNumRealParams() )
67  {
68  // std::cout << "getNumRealParams() = " << getNumRealParams() << std::endl;
69  realParams = new int[getNumRealParams()];
70  realParamValues = new double[getNumRealParams()];
71  }
72  if( getNumCharParams() )
73  {
74  // std::cout << "getNumCharParams() = " << getNumCharParams() << std::endl;
75  charParams = new int[getNumCharParams()];
76  charParamValues = new char[getNumCharParams()];
77  }
78  if( getNumStringParams() )
79  {
80  // std::cout << "getNumStringParams() = " << getNumStringParams() << std::endl;
81  int allStringParamValusesSize = 0;
82  stringParams = new int[getNumStringParams()];
83  for( size_t i = ParaParamsStringFirst; i < nParaParams; i++ )
84  {
85  if( paraParams[i]->getType() == ParaParamTypeString )
86  {
87  ParaParamString *paraParamString = dynamic_cast< ParaParamString * >(paraParams[i]);
88  if( !paraParamString->isDefaultValue() )
89  {
90  allStringParamValusesSize += (std::strlen(paraParamString->getValue()) + 1);
91  }
92  }
93  }
94  stringParamValues = new char[allStringParamValusesSize];
95  }
96 }
97 
98 /** free memory for transfer */
99 void
101  )
102 {
103  if( getNumBoolParams() )
104  {
105  delete [] boolParams;
106  delete [] boolParamValues;
107  }
108  if( getNumIntParams() )
109  {
110  delete [] intParams;
111  delete [] intParamValues;
112  }
113  if( getNumLongintParams() )
114  {
115  delete [] longintParams;
116  delete [] longintParamValues;
117  }
118  if( getNumRealParams() )
119  {
120  delete [] realParams;
121  delete [] realParamValues;
122  }
123  if( getNumCharParams() )
124  {
125  delete [] charParams;
126  delete [] charParamValues;
127  }
128  if( getNumStringParams() )
129  {
130  delete [] stringParams;
131  delete [] stringParamValues;
132  }
133 }
134 
135 
136 /** constructor with scip */
137 void
139  )
140 {
141 
142  nBoolParams = 0;
143  nIntParams = 0;
144  nLongintParams = 0;
145  nRealParams = 0;
146  nCharParams = 0;
147  nStringParams = 0;
149  int pos = 0;
150 
151  for( size_t i = 0; i < nParaParams; i++ )
152  {
153  switch ( paraParams[i]->getType() )
154  {
155  case ParaParamTypeBool:
156  {
157  ParaParamBool *paraParamBool = dynamic_cast< ParaParamBool * >(paraParams[i]);
158  if( !paraParamBool->isDefaultValue() )
159  {
160  boolParams[nBoolParams] = i;
161  if( paraParamBool->getValue() )
162  {
164  }
165  else
166  {
168  }
169  nBoolParams++;
170  }
171  break;
172  }
173  case ParaParamTypeInt:
174  {
175  ParaParamInt *paraParamInt = dynamic_cast< ParaParamInt * >(paraParams[i]);
176  if( !paraParamInt->isDefaultValue() )
177  {
178  intParams[nIntParams] = i;
179  intParamValues[nIntParams] = paraParamInt->getValue();
180  nIntParams++;
181  }
182  break;
183  }
185  {
186  ParaParamLongint *paraParamLongint = dynamic_cast< ParaParamLongint * >(paraParams[i]);
187  if( !paraParamLongint->isDefaultValue() )
188  {
190  intParamValues[nLongintParams] = paraParamLongint->getValue();
191  nLongintParams++;
192  }
193  break;
194  }
195  case ParaParamTypeReal:
196  {
197  ParaParamReal *paraParamReal = dynamic_cast< ParaParamReal * >(paraParams[i]);
198  if( !paraParamReal->isDefaultValue() )
199  {
200  realParams[nRealParams] = i;
201  realParamValues[nRealParams] = paraParamReal->getValue();
202  nRealParams++;
203  }
204  break;
205  }
206  case ParaParamTypeChar:
207  {
208  ParaParamChar *paraParamChar = dynamic_cast< ParaParamChar * >(paraParams[i]);
209  if( !paraParamChar->isDefaultValue() )
210  {
211  charParams[nCharParams] = i;
212  charParamValues[nCharParams] = paraParamChar->getValue();
213  nCharParams++;
214  }
215  break;
216  }
217  case ParaParamTypeString:
218  {
219  ParaParamString *paraParamString = dynamic_cast< ParaParamString * >(paraParams[i]);
220  if( !paraParamString->isDefaultValue() )
221  {
223  std::strcpy( &stringParamValues[pos], paraParamString->getValue() );
224  int len = (std::strlen(paraParamString->getValue()) + 1);
225  pos += len;
226  stringParamValuesSize += len;
227  nStringParams++;
228  }
229  break;
230  }
231  default:
232  {
233  THROW_LOGICAL_ERROR1("Unknown parameter type");
234  }
235  }
236  }
237 }
238 
239 /** set these parameter values in scip environment */
240 void
242  )
243 {
244  if ( nBoolParams )
245  {
246  for( int i = 0; i < nBoolParams; i++ )
247  {
248  ParaParamBool *paraParamBool = dynamic_cast< ParaParamBool * >(paraParams[boolParams[i]]);
249  if( boolParamValues[i] == 'T')
250  {
251  paraParamBool->setValue(true);
252  }
253  else
254  {
255  paraParamBool->setValue(false);
256  }
257  }
258  }
259  if ( nIntParams )
260  {
261  for( int i = 0; i < nIntParams; i++ )
262  {
263  ParaParamInt *paraParamInt = dynamic_cast< ParaParamInt * >(paraParams[intParams[i]]);
264  paraParamInt->setValue(intParamValues[i]);
265  }
266  }
267  if ( nLongintParams )
268  {
269  for( int i = 0; i < nLongintParams; i++ )
270  {
271  ParaParamLongint *paraParamLongint = dynamic_cast< ParaParamLongint * >(paraParams[longintParams[i]]);
272  paraParamLongint->setValue(longintParamValues[i]);
273  }
274  }
275  if ( nRealParams )
276  {
277  for( int i = 0; i < nRealParams; i++ )
278  {
279  ParaParamReal *paraParamReal = dynamic_cast< ParaParamReal * >(paraParams[realParams[i]]);
280  paraParamReal->setValue(realParamValues[i]);
281  }
282  }
283  if ( nCharParams )
284  {
285  for( int i = 0; i < nCharParams; i++ )
286  {
287  ParaParamChar *paraParamChar = dynamic_cast< ParaParamChar * >(paraParams[charParams[i]]);
288  paraParamChar->setValue(charParamValues[i]);
289  }
290  }
291  int pos = 0;
292  if ( nStringParams )
293  {
294  for( int i = 0; i < nStringParams; i++ )
295  {
296  ParaParamString *paraParamString = dynamic_cast< ParaParamString * >(paraParams[stringParams[i]]);
297  char *value = new char[std::strlen(&stringParamValues[pos]) + 1];
298  std::strcpy(value, &stringParamValues[pos]);
299  paraParamString->setValue(value);
300  pos += ( std::strlen(&stringParamValues[pos]) + 1 );
301  }
302  }
303 }
304 
305 /** create Datatype1 */
306 MPI_Datatype
308  )
309 {
310  const int nBlocks = 7;
311 
312  MPI_Datatype datatype;
313 
314  int blockLengths[nBlocks];
315  MPI_Aint displacements[nBlocks];
316  MPI_Datatype types[nBlocks];
317 
318  MPI_Aint startAddress = 0;
319  MPI_Aint address = 0;
320 
321  for( int i = 0; i < nBlocks; i++ )
322  {
323  blockLengths[i] = 1;
324  types[i] = MPI_INT;
325  }
326 
327  MPI_CALL(
328  MPI_Get_address( &nBoolParams, &startAddress )
329  );
330  displacements[0] = 0;
331  MPI_CALL(
332  MPI_Get_address( &nIntParams, &address )
333  );
334  displacements[1] = address - startAddress;
335  MPI_CALL(
336  MPI_Get_address( &nLongintParams, &address )
337  );
338  displacements[2] = address - startAddress;
339  MPI_CALL(
340  MPI_Get_address( &nRealParams, &address )
341  );
342  displacements[3] = address - startAddress;
343  MPI_CALL(
344  MPI_Get_address( &nCharParams, &address )
345  );
346  displacements[4] = address - startAddress;
347  MPI_CALL(
348  MPI_Get_address( &nStringParams, &address )
349  );
350  displacements[5] = address - startAddress;
351  MPI_CALL(
352  MPI_Get_address( &stringParamValuesSize, &address )
353  );
354  displacements[6] = address - startAddress;
355 
356  MPI_CALL(
357  MPI_Type_create_struct(nBlocks, blockLengths, displacements, types, &datatype)
358  );
359 
360  return datatype;
361 
362 }
363 
364 /** create Datatype2 */
365 MPI_Datatype
367  bool reallocateStringParamValues
368  )
369 {
370  const int nMaxBlocks = 13;
371 
372  MPI_Datatype datatype;
373 
374  int blockLengths[nMaxBlocks];
375  MPI_Aint displacements[nMaxBlocks];
376  MPI_Datatype types[nMaxBlocks];
377 
378  MPI_Aint startAddress = 0;
379  MPI_Aint address = 0;
380 
381  int nBlocks = 0;
382 
383  MPI_CALL(
384  MPI_Get_address( &nBoolParams, &startAddress )
385  );
386  blockLengths[nBlocks] = 1;
387  displacements[nBlocks] = 0;
388  types[nBlocks] = MPI_INT;
389  nBlocks++;
390 
391  if( nBoolParams )
392  {
393  MPI_CALL(
394  MPI_Get_address( boolParams, &address )
395  );
396  blockLengths[nBlocks] = nBoolParams;
397  displacements[nBlocks] = address - startAddress;
398  types[nBlocks] = MPI_INT;
399  nBlocks++;
400 
401  MPI_CALL(
402  MPI_Get_address( boolParamValues, &address )
403  );
404  blockLengths[nBlocks] = nBoolParams;
405  displacements[nBlocks] = address - startAddress;
406  types[nBlocks] = MPI_CHAR;
407  nBlocks++;
408  }
409 
410  if( nIntParams )
411  {
412  MPI_CALL(
413  MPI_Get_address( intParams, &address )
414  );
415  blockLengths[nBlocks] = nIntParams;
416  displacements[nBlocks] = address - startAddress;
417  types[nBlocks] = MPI_INT;
418  nBlocks++;
419 
420  MPI_CALL(
421  MPI_Get_address( intParamValues, &address )
422  );
423  blockLengths[nBlocks] = nIntParams;
424  displacements[nBlocks] = address - startAddress;
425  types[nBlocks] = MPI_INT;
426  nBlocks++;
427  }
428 
429  if( nLongintParams )
430  {
431  MPI_CALL(
432  MPI_Get_address( longintParams, &address )
433  );
434  blockLengths[nBlocks] = nLongintParams;
435  displacements[nBlocks] = address - startAddress;
436 #ifdef _ALIBABA
437  types[nBlocks] = MPI_LONG;
438 #else
439  types[nBlocks] = MPI_LONG_LONG;
440 #endif
441  nBlocks++;
442  MPI_CALL(
443  MPI_Get_address( longintParamValues, &address )
444  );
445  blockLengths[nBlocks] = nLongintParams;
446  displacements[nBlocks] = address - startAddress;
447 #ifdef _ALIBABA
448  types[nBlocks] = MPI_LONG;
449 #else
450  types[nBlocks] = MPI_LONG_LONG;
451 #endif
452  nBlocks++;
453  }
454 
455  if( nRealParams )
456  {
457  MPI_CALL(
458  MPI_Get_address( realParams, &address )
459  );
460  blockLengths[nBlocks] = nRealParams;
461  displacements[nBlocks] = address - startAddress;
462  types[nBlocks] = MPI_INT;
463  nBlocks++;
464  MPI_CALL(
465  MPI_Get_address( realParamValues, &address )
466  );
467  blockLengths[nBlocks] = nRealParams;
468  displacements[nBlocks] = address - startAddress;
469  types[nBlocks] = MPI_DOUBLE;
470 
471  nBlocks++;
472  }
473 
474  if( nCharParams )
475  {
476  MPI_CALL(
477  MPI_Get_address( charParams, &address )
478  );
479  blockLengths[nBlocks] = nCharParams;
480  displacements[nBlocks] = address - startAddress;
481  types[nBlocks] = MPI_INT;
482  nBlocks++;
483  MPI_CALL(
484  MPI_Get_address( charParamValues, &address )
485  );
486  blockLengths[nBlocks] = nCharParams;
487  displacements[nBlocks] = address - startAddress;
488  types[nBlocks] = MPI_CHAR;
489  nBlocks++;
490  }
491 
492  if( nStringParams )
493  {
494  if( reallocateStringParamValues )
495  {
496  delete[] stringParamValues;
498  }
499  MPI_CALL(
500  MPI_Get_address( stringParams, &address )
501  );
502  blockLengths[nBlocks] = nStringParams;
503  displacements[nBlocks] = address - startAddress;
504  types[nBlocks] = MPI_INT;
505  nBlocks++;
506  MPI_CALL(
507  MPI_Get_address( stringParamValues, &address )
508  );
509  blockLengths[nBlocks] = stringParamValuesSize;
510  displacements[nBlocks] = address - startAddress;
511  types[nBlocks] = MPI_CHAR;
512  nBlocks++;
513  }
514 
515  MPI_CALL(
516  MPI_Type_create_struct(nBlocks, blockLengths, displacements, types, &datatype)
517  );
518 
519  return datatype;
520 }
521 
522 int
524 {
525 
526  DEF_PARA_COMM( commMpi, comm);
527 
528  allocateMemory();
529  if( commMpi->getRank() == root )
530  {
532  }
533 
534  MPI_Datatype datatype1;
535  datatype1 = createDatatype1();
536  MPI_CALL(
537  MPI_Type_commit( &datatype1 )
538  );
540  commMpi->ubcast(&nBoolParams, 1, datatype1, root)
541  );
542  MPI_CALL(
543  MPI_Type_free( &datatype1 )
544  );
545 
546  MPI_Datatype datatype2;
547  if( commMpi->getRank() == root )
548  {
549  datatype2 = createDatatype2(false);
550  }
551  else
552  {
553  datatype2 = createDatatype2(true);
554  }
555  MPI_CALL(
556  MPI_Type_commit( &datatype2 )
557  );
559  commMpi->ubcast(&nBoolParams, 1, datatype2, root) // nBoolParams sending twice, to fix start point
560  );
561  MPI_CALL(
562  MPI_Type_free( &datatype2 )
563  );
564  if( commMpi->getRank() != root )
565  {
566  setDiffParams();
567  }
568  freeMemory();
569 
570  return 0;
571 
572 }
virtual size_t getNumIntParams()=0
get number of int parameters
long long * longintParamValues
long int parameter values
void freeMemory()
free allocated temporary memory for transfer
char * stringParamValues
string parameter values: values are concatenated
static const int ParaParamTypeString
arrays of characters
Definition: paraParamSet.h:63
virtual size_t getNumRealParams()=0
get number of real parameters
void setValue(char value)
set current value of this char parameter
Definition: paraParamSet.h:722
int stringParamValuesSize
size of stringParameterValues area
bool getValue() const
get current parameter value
Definition: paraParamSet.h:253
class ParaParamLongint
Definition: paraParamSet.h:412
double * realParamValues
real parameter values
char * boolParamValues
boolean parameter values
void setValue(const char *value)
set current value of this sting parameter
Definition: paraParamSet.h:827
static const int ParaParamTypeReal
real values
Definition: paraParamSet.h:61
int * charParams
char parameter ids
int * intParams
int parameter ids
static ScipParaCommTh * comm
Definition: fscip.cpp:73
void setValue(bool value)
set parameter value
Definition: paraParamSet.h:271
bool isDefaultValue() const
check if current value is default value or not
Definition: paraParamSet.h:282
int nIntParams
the number of int parameters
int nBoolParams
the number of bool parameters
int bcast(ParaComm *comm, int root)
broadcast ParaParams
int * longintParams
long int parameter ids
static const int ParaParamTypeChar
characters
Definition: paraParamSet.h:62
MPI_Datatype createDatatype1()
create ParaParamSetDatatype1
static const int ParaParamTypeInt
integer values
Definition: paraParamSet.h:59
MPI_Datatype createDatatype2(bool reallocateStringPramsValue)
create ParaParamSetDatatype2
bool isDefaultValue() const
check if current value is default value or not
Definition: paraParamSet.h:500
class ParaParamReal
Definition: paraParamSet.h:530
class ParaParamChar
Definition: paraParamSet.h:648
int nStringParams
the number of string parameters
#define PARA_COMM_CALL(paracommcall)
Definition: paraComm.h:47
int nLongintParams
the number of long int parameters
class ParaParamString
Definition: paraParamSet.h:754
static const int ParaParamTypeBool
Types of parameters.
Definition: paraParamSet.h:58
const char * getValue() const
get current value of this string parameter
Definition: paraParamSet.h:808
int * realParams
real parameter ids
int * stringParams
string parameter ids
char getValue() const
get current value of this char parameter
Definition: paraParamSet.h:704
ParaComm extension for MPI communication.
void setValue(int value)
set current value
Definition: paraParamSet.h:370
int * boolParams
boolean parameter ids
double getValue() const
get current value of this real parameter
Definition: paraParamSet.h:589
class ParaParamInt
Definition: paraParamSet.h:293
#define DEF_PARA_COMM(para_comm, comm)
bool isDefaultValue() const
check if current value is default value or not
Definition: paraParamSet.h:733
int nRealParams
the number of real parameters
virtual size_t getNumStringParams()=0
get number of string parameters
#define THROW_LOGICAL_ERROR1(msg1)
Definition: paraDef.h:52
virtual size_t getNumLongintParams()=0
get number of longint parameters
static const int ParaParamsStringFirst
String parameters.
Definition: paraParamSet.h:123
int getValue() const
get current value of this int parameter
Definition: paraParamSet.h:352
bool isDefaultValue() const
check if current value is default value or not
Definition: paraParamSet.h:618
bool isDefaultValue() const
check if current value is default value or not
Definition: paraParamSet.h:838
void setValue(long long value)
set current value of this long int parameter
Definition: paraParamSet.h:489
int nCharParams
the number of char parameters
bool isDefaultValue() const
check if current value is default value or not
Definition: paraParamSet.h:381
char * charParamValues
char parameter values
int * intParamValues
int parameter values
size_t nParaParams
number of ParaParams
Definition: paraParamSet.h:855
virtual size_t getNumCharParams()=0
get number of char parameters
class ParaParamBool
Definition: paraParamSet.h:200
virtual size_t getNumBoolParams()=0
get number of bool parameters
void setValue(double value)
set current value of this real parameter
Definition: paraParamSet.h:607
void createDiffParams()
create non default parameters for transfer
long long getValue() const
get current value of this long int parameter
Definition: paraParamSet.h:471
void allocateMemory()
allocate temporary memory for transfer
ParaParamSet extension for MPI communication.
void setDiffParams()
set non default parameters transferred
#define MPI_CALL(mpicall)
Definition: paraCommMpi.h:68
Base class of communicator object.
Definition: paraComm.h:101
ParaParam ** paraParams
array of ParaParams
Definition: paraParamSet.h:856
static const int ParaParamTypeLongint
long integer values
Definition: paraParamSet.h:60