| 1 |
// SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file LICENSE.md in module root |
| 2 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 |
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 4 |
// vi: set et ts=4 sw=2 sts=2: |
| 5 |
/****************************************************************************/ |
| 6 |
/* */ |
| 7 |
/* File: reduct.c */ |
| 8 |
/* */ |
| 9 |
/* Purpose: standard parallel routines not supported by ddd */ |
| 10 |
/* reduction operations (GlobalSum, GlobalMax etc) */ |
| 11 |
/* */ |
| 12 |
/* Author: Klaus Birken */ |
| 13 |
/* Rechenzentrum Uni Stuttgart */ |
| 14 |
/* Universitaet Stuttgart */ |
| 15 |
/* Allmandring 30 */ |
| 16 |
/* 70550 Stuttgart */ |
| 17 |
/* internet: [email protected] */ |
| 18 |
/* */ |
| 19 |
/* History: 940128 kb begin */ |
| 20 |
/* 960902 kb copied from fedemo, adapted */ |
| 21 |
/* */ |
| 22 |
/* Remarks: */ |
| 23 |
/* */ |
| 24 |
/****************************************************************************/ |
| 25 |
|
| 26 |
/****************************************************************************/ |
| 27 |
/* */ |
| 28 |
/* include files */ |
| 29 |
/* system include files */ |
| 30 |
/* application include files */ |
| 31 |
/* */ |
| 32 |
/****************************************************************************/ |
| 33 |
|
| 34 |
/* standard C library */ |
| 35 |
#include <config.h> |
| 36 |
#include <cstdlib> |
| 37 |
#include <cstdio> |
| 38 |
#include <cstddef> |
| 39 |
|
| 40 |
#include <type_traits> |
| 41 |
|
| 42 |
#ifdef ModelP |
| 43 |
# include <mpi.h> |
| 44 |
# include <dune/uggrid/parallel/ppif/ppifcontext.hh> |
| 45 |
#endif |
| 46 |
|
| 47 |
#include <dune/uggrid/parallel/ppif/ppif.h> |
| 48 |
#include <dune/uggrid/low/namespace.h> |
| 49 |
#include <dune/uggrid/low/ugtypes.h> |
| 50 |
#include <dune/uggrid/parallel/ddd/include/memmgr.h> |
| 51 |
|
| 52 |
#include <dune/uggrid/gm/pargm.h> |
| 53 |
|
| 54 |
|
| 55 |
/* UG namespaces: */ |
| 56 |
USING_UG_NAMESPACES |
| 57 |
|
| 58 |
/* PPIF namespaces: */ |
| 59 |
using namespace PPIF; |
| 60 |
|
| 61 |
START_UGDIM_NAMESPACE |
| 62 |
|
| 63 |
/****************************************************************************/ |
| 64 |
/* */ |
| 65 |
/* routines */ |
| 66 |
/* */ |
| 67 |
/****************************************************************************/ |
| 68 |
|
| 69 |
|
| 70 |
/* some useful functions by Peter Bastian, from ugp/ug/ugcom.c */ |
| 71 |
|
| 72 |
#ifdef ModelP |
| 73 |
|
| 74 |
static_assert( |
| 75 |
std::is_same<int, INT>::value, |
| 76 |
"The implementation assumes that `int` and `INT` are the same type."); |
| 77 |
static_assert( |
| 78 |
std::is_same<double, DOUBLE>::value, |
| 79 |
"The implementation assumes that `double` and `DOUBLE` are the same type."); |
| 80 |
|
| 81 |
/****************************************************************************/ |
| 82 |
/*D |
| 83 |
UG_GlobalMaxINT - get maximum for INT value |
| 84 |
|
| 85 |
SYNOPSIS: |
| 86 |
INT UG_GlobalMaxINT (INT i) |
| 87 |
|
| 88 |
PARAMETERS: |
| 89 |
. i - calculate maximum for this value |
| 90 |
|
| 91 |
DESCRIPTION: |
| 92 |
This function calculates the maximum of i over all processors. |
| 93 |
|
| 94 |
RETURN VALUE: |
| 95 |
The maximum value of i over all processors. |
| 96 |
|
| 97 |
D*/ |
| 98 |
/****************************************************************************/ |
| 99 |
INT UG_GlobalMaxINT(const PPIF::PPIFContext& context, INT i) |
| 100 |
{ |
| 101 |
MPI_Allreduce(MPI_IN_PLACE, &i, 1, MPI_INT, MPI_MAX, context.comm()); |
| 102 |
return i; |
| 103 |
} |
| 104 |
|
| 105 |
/****************************************************************************/ |
| 106 |
/*D |
| 107 |
UG_GlobalMinINT - get global minimum for INT value |
| 108 |
|
| 109 |
SYNOPSIS: |
| 110 |
INT UG_GlobalMaxINT (INT i) |
| 111 |
|
| 112 |
PARAMETERS: |
| 113 |
. i - calculate minimum for this value |
| 114 |
|
| 115 |
DESCRIPTION: |
| 116 |
This function calculates the minimum of i over all processors. |
| 117 |
|
| 118 |
RETURN VALUE: |
| 119 |
The minimum value of i over all processors |
| 120 |
|
| 121 |
D*/ |
| 122 |
/****************************************************************************/ |
| 123 |
|
| 124 |
INT UG_GlobalMinINT (const PPIF::PPIFContext& context, INT i) |
| 125 |
{ |
| 126 |
MPI_Allreduce(MPI_IN_PLACE, &i, 1, MPI_INT, MPI_MIN, context.comm()); |
| 127 |
return i; |
| 128 |
} |
| 129 |
|
| 130 |
/****************************************************************************/ |
| 131 |
/*D |
| 132 |
UG_GlobalSumINT - get global sum for INT value |
| 133 |
|
| 134 |
SYNOPSIS: |
| 135 |
INT UG_GlobalSumINT (INT i) |
| 136 |
|
| 137 |
PARAMETERS: |
| 138 |
. i - calculate sum for this variable |
| 139 |
|
| 140 |
DESCRIPTION: |
| 141 |
This function calculates the sum of i over all processors. |
| 142 |
|
| 143 |
RETURN VALUE: |
| 144 |
The sum of i over all processors |
| 145 |
|
| 146 |
D*/ |
| 147 |
/****************************************************************************/ |
| 148 |
|
| 149 |
INT UG_GlobalSumINT (const PPIF::PPIFContext& context, INT x) |
| 150 |
{ |
| 151 |
MPI_Allreduce(MPI_IN_PLACE, &x, 1, MPI_INT, MPI_SUM, context.comm()); |
| 152 |
return x; |
| 153 |
} |
| 154 |
|
| 155 |
/****************************************************************************/ |
| 156 |
/*D |
| 157 |
UG_GlobalMaxNINT - get maximum for n integer values |
| 158 |
|
| 159 |
SYNOPSIS: |
| 160 |
void UG_GlobalMaxNINT (INT n, INT *x) |
| 161 |
|
| 162 |
PARAMETERS: |
| 163 |
. n - number of elements in array x to be used |
| 164 |
. x - array of size n |
| 165 |
|
| 166 |
DESCRIPTION: |
| 167 |
This function calculates the maximum of x[i] over all processors for all |
| 168 |
i from 0 to n-1. x is overwritten with the maximum value. |
| 169 |
|
| 170 |
RETURN VALUE: |
| 171 |
none |
| 172 |
|
| 173 |
D*/ |
| 174 |
/****************************************************************************/ |
| 175 |
|
| 176 |
void UG_GlobalMaxNINT(const PPIF::PPIFContext& context, INT n, INT *x) |
| 177 |
{ |
| 178 |
MPI_Allreduce(MPI_IN_PLACE, x, n, MPI_INT, MPI_MAX, context.comm()); |
| 179 |
} |
| 180 |
|
| 181 |
/****************************************************************************/ |
| 182 |
/*D |
| 183 |
UG_GlobalMinNINT - get minimum for n integer values |
| 184 |
|
| 185 |
SYNOPSIS: |
| 186 |
void UG_GlobalMinNINT (INT n, INT *x) |
| 187 |
|
| 188 |
PARAMETERS: |
| 189 |
. n - number of elements in array x to be used |
| 190 |
. x - array of size n |
| 191 |
|
| 192 |
DESCRIPTION: |
| 193 |
This function calculates the minimum of x[i] over all processors for all |
| 194 |
i from 0 to n-1. x is overwritten with the minimum value. |
| 195 |
|
| 196 |
RETURN VALUE: |
| 197 |
none |
| 198 |
|
| 199 |
D*/ |
| 200 |
/****************************************************************************/ |
| 201 |
|
| 202 |
void UG_GlobalMinNINT(const PPIF::PPIFContext& context, INT n, INT *x) |
| 203 |
{ |
| 204 |
MPI_Allreduce(MPI_IN_PLACE, x, n, MPI_INT, MPI_MIN, context.comm()); |
| 205 |
} |
| 206 |
|
| 207 |
/****************************************************************************/ |
| 208 |
/*D |
| 209 |
UG_GlobalSumNINT - calculate global sum for n integer values |
| 210 |
|
| 211 |
SYNOPSIS: |
| 212 |
void UG_GlobalSumNINT (INT n, INT *x) |
| 213 |
|
| 214 |
PARAMETERS: |
| 215 |
. n - number of elements in array x to be used |
| 216 |
. x - array of size n |
| 217 |
|
| 218 |
DESCRIPTION: |
| 219 |
This function calculates the sum of x[i] over all processors for each |
| 220 |
i from 0 to n-1. x is overwritten with the result. |
| 221 |
|
| 222 |
RETURN VALUE: |
| 223 |
none |
| 224 |
|
| 225 |
D*/ |
| 226 |
/****************************************************************************/ |
| 227 |
|
| 228 |
void UG_GlobalSumNINT (const PPIF::PPIFContext& context, INT n, INT *xs) |
| 229 |
{ |
| 230 |
MPI_Allreduce(MPI_IN_PLACE, xs, n, MPI_INT, MPI_SUM, context.comm()); |
| 231 |
} |
| 232 |
|
| 233 |
/****************************************************************************/ |
| 234 |
/*D |
| 235 |
UG_GlobalMaxDOUBLE - get global maximum for DOUBLE value |
| 236 |
|
| 237 |
SYNOPSIS: |
| 238 |
DOUBLE UG_GlobalMaxDOUBLE (DOUBLE x) |
| 239 |
|
| 240 |
PARAMETERS: |
| 241 |
. x - calculate maximum for this value |
| 242 |
|
| 243 |
DESCRIPTION: |
| 244 |
This function calculates the maximum of x over all processors. |
| 245 |
|
| 246 |
RETURN VALUE: |
| 247 |
The maximum value of x over all processors. |
| 248 |
|
| 249 |
D*/ |
| 250 |
/****************************************************************************/ |
| 251 |
|
| 252 |
DOUBLE UG_GlobalMaxDOUBLE (const PPIF::PPIFContext& context, DOUBLE x) |
| 253 |
{ |
| 254 |
MPI_Allreduce(MPI_IN_PLACE, &x, 1, MPI_DOUBLE, MPI_MAX, context.comm()); |
| 255 |
return x; |
| 256 |
} |
| 257 |
|
| 258 |
/****************************************************************************/ |
| 259 |
/*D |
| 260 |
UG_GlobalMinDOUBLE - get global minimum for DOUBLE value |
| 261 |
|
| 262 |
SYNOPSIS: |
| 263 |
DOUBLE UG_GlobalMinDOUBLE (DOUBLE x) |
| 264 |
|
| 265 |
PARAMETERS: |
| 266 |
. x - calculate minimum for this value |
| 267 |
|
| 268 |
DESCRIPTION: |
| 269 |
This function calculates the minimum of x over all processors. |
| 270 |
|
| 271 |
RETURN VALUE: |
| 272 |
The minimum value of x over all processors. |
| 273 |
|
| 274 |
D*/ |
| 275 |
/****************************************************************************/ |
| 276 |
|
| 277 |
DOUBLE UG_GlobalMinDOUBLE (const PPIF::PPIFContext& context, DOUBLE x) |
| 278 |
{ |
| 279 |
MPI_Allreduce(MPI_IN_PLACE, &x, 1, MPI_DOUBLE, MPI_MIN, context.comm()); |
| 280 |
return x; |
| 281 |
} |
| 282 |
|
| 283 |
/****************************************************************************/ |
| 284 |
/*D |
| 285 |
UG_GlobalSumDOUBLE - get global sum for DOUBLE value |
| 286 |
|
| 287 |
SYNOPSIS: |
| 288 |
DOUBLE UG_GlobalSumDOUBLE (DOUBLE i) |
| 289 |
|
| 290 |
PARAMETERS: |
| 291 |
. i - calculate sum for this variable |
| 292 |
|
| 293 |
DESCRIPTION: |
| 294 |
This function calculates the sum of i over all processors. |
| 295 |
|
| 296 |
RETURN VALUE: |
| 297 |
The sum of i over all processors |
| 298 |
|
| 299 |
D*/ |
| 300 |
/****************************************************************************/ |
| 301 |
|
| 302 |
DOUBLE UG_GlobalSumDOUBLE (const PPIF::PPIFContext& context, DOUBLE x) |
| 303 |
{ |
| 304 |
MPI_Allreduce(MPI_IN_PLACE, &x, 1, MPI_DOUBLE, MPI_SUM, context.comm()); |
| 305 |
return x; |
| 306 |
} |
| 307 |
|
| 308 |
/****************************************************************************/ |
| 309 |
/*D |
| 310 |
UG_GlobalMaxNDOUBLE - get maximum over n integer values |
| 311 |
|
| 312 |
SYNOPSIS: |
| 313 |
void UG_GlobalMaxNDOUBLE (INT n, DOUBLE *x) |
| 314 |
|
| 315 |
PARAMETERS: |
| 316 |
. n - number of elements in array x to be used |
| 317 |
. x - array of size n |
| 318 |
|
| 319 |
DESCRIPTION: |
| 320 |
This function calculates the maximum of x[i] over all processors for all |
| 321 |
i from 0 to n-1. x is overwritten with the maximum value. |
| 322 |
|
| 323 |
RETURN VALUE: |
| 324 |
none |
| 325 |
|
| 326 |
D*/ |
| 327 |
/****************************************************************************/ |
| 328 |
|
| 329 |
void UG_GlobalMaxNDOUBLE (const PPIF::PPIFContext& context, INT n, DOUBLE *x) |
| 330 |
{ |
| 331 |
MPI_Allreduce(MPI_IN_PLACE, x, n, MPI_DOUBLE, MPI_MAX, context.comm()); |
| 332 |
} |
| 333 |
|
| 334 |
/****************************************************************************/ |
| 335 |
/*D |
| 336 |
UG_GlobalMinNDOUBLE - get minimum over n integer values |
| 337 |
|
| 338 |
SYNOPSIS: |
| 339 |
void UG_GlobalMinNDOUBLE (INT n, DOUBLE *x) |
| 340 |
|
| 341 |
PARAMETERS: |
| 342 |
. n - number of elements in array x to be used |
| 343 |
. x - array of size n |
| 344 |
|
| 345 |
DESCRIPTION: |
| 346 |
This function calculates the minimum of x[i] over all processors for all |
| 347 |
i from 0 to n-1. x is overwritten with the minimum value. |
| 348 |
|
| 349 |
RETURN VALUE: |
| 350 |
none |
| 351 |
|
| 352 |
D*/ |
| 353 |
/****************************************************************************/ |
| 354 |
|
| 355 |
void UG_GlobalMinNDOUBLE (const PPIF::PPIFContext& context, INT n, DOUBLE *x) |
| 356 |
{ |
| 357 |
MPI_Allreduce(MPI_IN_PLACE, x, n, MPI_DOUBLE, MPI_MIN, context.comm()); |
| 358 |
} |
| 359 |
|
| 360 |
/****************************************************************************/ |
| 361 |
/*D |
| 362 |
UG_GlobalSumNDOUBLE - calculate global sum for n DOUBLE values |
| 363 |
|
| 364 |
SYNOPSIS: |
| 365 |
void UG_GlobalSumNDOUBLE (INT n, DOUBLE *x) |
| 366 |
|
| 367 |
PARAMETERS: |
| 368 |
. n - number of elements in array x to be used |
| 369 |
. x - array of size n |
| 370 |
|
| 371 |
DESCRIPTION: |
| 372 |
This function calculates the sum of x[i] over all processors for each |
| 373 |
i from 0 to n-1. x is overwritten with the result. |
| 374 |
|
| 375 |
RETURN VALUE: |
| 376 |
none |
| 377 |
|
| 378 |
D*/ |
| 379 |
/****************************************************************************/ |
| 380 |
|
| 381 |
void UG_GlobalSumNDOUBLE (const PPIF::PPIFContext& context, INT n, DOUBLE *x) |
| 382 |
{ |
| 383 |
MPI_Allreduce(MPI_IN_PLACE, x, n, MPI_DOUBLE, MPI_SUM, context.comm()); |
| 384 |
} |
| 385 |
|
| 386 |
#endif /* ModelP */ |
| 387 |
|
| 388 |
END_UGDIM_NAMESPACE |