| 1 |
// @HEADER |
| 2 |
// ***************************************************************************** |
| 3 |
// Rapid Optimization Library (ROL) Package |
| 4 |
// |
| 5 |
// Copyright 2014 NTESS and the ROL contributors. |
| 6 |
// SPDX-License-Identifier: BSD-3-Clause |
| 7 |
// ***************************************************************************** |
| 8 |
// @HEADER |
| 9 |
|
| 10 |
#ifndef OED_SPLITCOMM_HPP |
| 11 |
#define OED_SPLITCOMM_HPP |
| 12 |
|
| 13 |
#ifdef HAVE_MPI |
| 14 |
|
| 15 |
#include "ROL_Ptr.hpp" |
| 16 |
#include <mpi.h> |
| 17 |
|
| 18 |
template<class Ordinal, class Comm> |
| 19 |
class OED_SplitComm { |
| 20 |
private: |
| 21 |
ROL::Ptr<Comm> comm_design_; |
| 22 |
ROL::Ptr<Comm> comm_sample_; |
| 23 |
|
| 24 |
public: |
| 25 |
OED_SplitComm(int m) { |
| 26 |
Ordinal rank, Ngroups, size; |
| 27 |
MPI_Comm_rank(MPI_COMM_WORLD, &rank); |
| 28 |
MPI_Comm_size(MPI_COMM_WORLD, &size); |
| 29 |
Ordinal M = (m > size ? 1 : m); |
| 30 |
Ngroups = size/M; |
| 31 |
// Instantiate Design Communicator. |
| 32 |
MPI_Comm design_comm; |
| 33 |
MPI_Comm_split(MPI_COMM_WORLD, rank/M, rank, &design_comm); |
| 34 |
Ordinal comRank; // Process rank in linear algebra communicator. |
| 35 |
Ordinal comSize; // Number of processes in linear algebra communicator. |
| 36 |
MPI_Comm_rank(design_comm,&comRank); // Get process rank. |
| 37 |
MPI_Comm_size(design_comm,&comSize); // Get communicator size. |
| 38 |
comm_design_ = ROL::makePtr<Comm>(design_comm); // Wrap as Comm. |
| 39 |
|
| 40 |
// Determine group ranks for sampling. |
| 41 |
std::vector<Ordinal> granks(Ngroups); |
| 42 |
for (Ordinal i=0;i<Ngroups;i++) granks[i] = comRank + i*M; |
| 43 |
|
| 44 |
// Build MPI groups for sampling. |
| 45 |
MPI_Group world_comm; // Grab MPI_COMM_WORLD and place in world_comm. |
| 46 |
MPI_Comm_group(MPI_COMM_WORLD,&world_comm); |
| 47 |
MPI_Group group; |
| 48 |
MPI_Group_incl(world_comm,Ngroups,&granks[0],&group); |
| 49 |
|
| 50 |
// Instantiate Sample Communicator based on group. |
| 51 |
MPI_Comm sample_comm; |
| 52 |
MPI_Comm_create(MPI_COMM_WORLD, group, &sample_comm); |
| 53 |
Ordinal comRank1; // Process rank in sample communicator. |
| 54 |
Ordinal comSize1; // Number of processes in sample communicator. |
| 55 |
MPI_Comm_rank(sample_comm,&comRank1); // Get process rank. |
| 56 |
MPI_Comm_size(sample_comm,&comSize1); // Get communicator size. |
| 57 |
comm_sample_ = ROL::makePtr<Comm>(sample_comm); // Wrap as Comm. |
| 58 |
} |
| 59 |
|
| 60 |
const ROL::Ptr<Comm> getDesignComm() const { |
| 61 |
return comm_design_; |
| 62 |
} |
| 63 |
|
| 64 |
const ROL::Ptr<Comm> getSampleComm() const { |
| 65 |
return comm_sample_; |
| 66 |
} |
| 67 |
}; |
| 68 |
|
| 69 |
#endif // HAVE_MPI |
| 70 |
#endif // OED_SPLITCOMM_HPP |