|
1 | 1 | #include "parallel_reduce.h" |
2 | | - |
3 | 2 | #include "parallel_comm.h" |
4 | | - |
5 | 3 | #include <vector> |
6 | | -#include <iostream> |
7 | 4 | #include <cassert> |
8 | 5 |
|
9 | 6 | // Helper to safely check MPI status for WORLD comm |
@@ -366,3 +363,63 @@ void Parallel_Reduce::reduce_bgroup<int>(int* object, const int n) |
366 | 363 | MPI_Allreduce(MPI_IN_PLACE, object, n, MPI_INT, MPI_SUM, INT_BGROUP); |
367 | 364 | #endif |
368 | 365 | } |
| 366 | + |
| 367 | +template <> |
| 368 | +void Parallel_Reduce::reduce_min_all<int>(int& object) |
| 369 | +{ |
| 370 | +#ifdef __MPI |
| 371 | + if (!is_mpi_initialized()) return; |
| 372 | + MPI_Allreduce(MPI_IN_PLACE, &object, 1, MPI_INT, MPI_MIN, MPI_COMM_WORLD); |
| 373 | +#endif |
| 374 | +} |
| 375 | + |
| 376 | +template <> |
| 377 | +void Parallel_Reduce::reduce_min_all<double>(double& object) |
| 378 | +{ |
| 379 | +#ifdef __MPI |
| 380 | + if (!is_mpi_initialized()) return; |
| 381 | + MPI_Allreduce(MPI_IN_PLACE, &object, 1, MPI_DOUBLE, MPI_MIN, MPI_COMM_WORLD); |
| 382 | +#endif |
| 383 | +} |
| 384 | + |
| 385 | +void Parallel_Reduce::gather_int_all(int& v, int* all) |
| 386 | +{ |
| 387 | +#ifdef __MPI |
| 388 | + if (!is_mpi_initialized()) return; |
| 389 | + MPI_Allgather(&v, 1, MPI_INT, all, 1, MPI_INT, MPI_COMM_WORLD); |
| 390 | +#else |
| 391 | + all[0] = v; |
| 392 | +#endif |
| 393 | +} |
| 394 | + |
| 395 | +template<> |
| 396 | +void Parallel_Reduce::reduce_kp<double>(double* object, const int n) |
| 397 | +{ |
| 398 | +#ifdef __MPI |
| 399 | + if (!is_mpi_initialized()) return; |
| 400 | + if (KP_WORLD != MPI_COMM_NULL) |
| 401 | + { |
| 402 | + MPI_Allreduce(MPI_IN_PLACE, object, n, MPI_DOUBLE, MPI_SUM, KP_WORLD); |
| 403 | + } |
| 404 | +#endif |
| 405 | +} |
| 406 | + |
| 407 | +template<> |
| 408 | +void Parallel_Reduce::reduce_bp<double>(double* object, const int n) |
| 409 | +{ |
| 410 | +#ifdef __MPI |
| 411 | + if (!is_mpi_initialized()) return; |
| 412 | + MPI_Allreduce(MPI_IN_PLACE, object, n, MPI_DOUBLE, MPI_SUM, BP_WORLD); |
| 413 | +#endif |
| 414 | +} |
| 415 | + |
| 416 | +template<> |
| 417 | +void Parallel_Reduce::reduce_or_bp<bool>(bool& object) |
| 418 | +{ |
| 419 | +#ifdef __MPI |
| 420 | + if (!is_mpi_initialized()) return; |
| 421 | + int v_int = object ? 1 : 0; |
| 422 | + MPI_Allreduce(MPI_IN_PLACE, &v_int, 1, MPI_INT, MPI_LOR, BP_WORLD); |
| 423 | + object = (v_int != 0); |
| 424 | +#endif |
| 425 | +} |
0 commit comments