mpi_partition.cpp
Go to the documentation of this file.
1 // Copyright 2019, University of Maryland and the MANGO development team.
2 //
3 // This file is part of MANGO.
4 //
5 // MANGO is free software: you can redistribute it and/or modify it
6 // under the terms of the GNU Lesser General Public License as
7 // published by the Free Software Foundation, either version 3 of the
8 // License, or (at your option) any later version.
9 //
10 // MANGO is distributed in the hope that it will be useful, but
11 // WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with MANGO. If not, see
17 // <https://www.gnu.org/licenses/>.
18 
19 #include <iostream>
20 #include <string>
21 #include <stdexcept>
22 #include "mango.hpp"
23 
24 // Constructor
26  N_worker_groups = -1;
27  initialized = false;
28  verbose = false;
29 }
30 
31 // Destructor
33 }
34 
35 void mango::MPI_Partition::verify_initialized() {
36  if (!initialized) {
37  throw std::runtime_error("Error! MPI_Partition get method was called before initialization.");
38  }
39 }
40 
42  verify_initialized();
43  return comm_world;
44 }
45 
47  verify_initialized();
48  return comm_worker_groups;
49 }
50 
52  verify_initialized();
53  return comm_group_leaders;
54 }
55 
57  verify_initialized();
58  return proc0_world;
59 }
60 
62  verify_initialized();
63  return proc0_worker_groups;
64 }
65 
67  verify_initialized();
68  return rank_world;
69 }
70 
72  verify_initialized();
73  return rank_worker_groups;
74 }
75 
77  verify_initialized();
78  return rank_group_leaders;
79 }
80 
82  verify_initialized();
83  return N_procs_world;
84 }
85 
87  verify_initialized();
88  return N_procs_worker_groups;
89 }
90 
92  verify_initialized();
93  return N_procs_group_leaders;
94 }
95 
97  verify_initialized();
98  return worker_group;
99 }
100 
102  // Don't call verify_initialized() for this get method. problem::mpi_init fails otherwise, and there is no problem with querying N_worker_groups before initialization.
103  return N_worker_groups;
104 }
105 
106 void mango::MPI_Partition::set_N_worker_groups(int N_worker_groups_in) {
107  if (initialized) throw std::runtime_error("Error! MPI_Partition::set_N_worker_groups called after initialization.");
108  N_worker_groups = N_worker_groups_in;
109 }
110 
112  // This method should only be called from group leaders.
113  if (!proc0_worker_groups) throw std::runtime_error("mango::MPI_Partition::stop_workers() should only be called from group leaders.");
114  int data = -1; // Any negative value will do here.
115  MPI_Bcast(&data, 1, MPI_INT, 0, comm_worker_groups);
116 }
117 
119  // This method should only be called from group leaders.
120  if (!proc0_worker_groups) throw std::runtime_error("mango::MPI_Partition::mobilize_workers() should only be called from group leaders.");
121  int data = 1; // Any nonnegative value will do here.
122  MPI_Bcast(&data, 1, MPI_INT, 0, comm_worker_groups);
123 }
124 
126  // This method should NOT be called from group leaders.
127  if (proc0_worker_groups) throw std::runtime_error("mango::MPI_Partition::continue_worker_loop() should not be called from group leaders.");
128  int data;
129  MPI_Bcast(&data, 1, MPI_INT, 0, comm_worker_groups);
130  return (data >= 0);
131 }
mango::MPI_Partition::set_N_worker_groups
void set_N_worker_groups(int N_worker_groups)
Set the number of worker groups to the given integer.
Definition: mpi_partition.cpp:106
mango::MPI_Partition::stop_workers
void stop_workers()
Tell the worker MPI processes (i.e. those that are not group leaders) that the optimization problem i...
Definition: mpi_partition.cpp:111
mango::MPI_Partition::get_comm_world
MPI_Comm get_comm_world()
Get the MPI communicator for MANGO's world communicator.
Definition: mpi_partition.cpp:41
mango::MPI_Partition::get_proc0_worker_groups
bool get_proc0_worker_groups()
Determine whether this MPI processor has rank 0 in MANGO's "worker groups" communicator.
Definition: mpi_partition.cpp:61
mango::MPI_Partition::~MPI_Partition
~MPI_Partition()
Destructor.
Definition: mpi_partition.cpp:32
mango::MPI_Partition::get_comm_group_leaders
MPI_Comm get_comm_group_leaders()
Get the MPI communicator for MANGO's "group leaders" communicator.
Definition: mpi_partition.cpp:51
mango::MPI_Partition::get_worker_group
int get_worker_group()
Returns an integer indicating the worker group to which this MPI process belongs.
Definition: mpi_partition.cpp:96
mango::MPI_Partition::MPI_Partition
MPI_Partition()
Constructor.
Definition: mpi_partition.cpp:25
mango.hpp
mango::MPI_Partition::get_rank_group_leaders
int get_rank_group_leaders()
Get the MPI rank of this processor in MANGO's "group leaders" communicator.
Definition: mpi_partition.cpp:76
mango::MPI_Partition::get_N_procs_group_leaders
int get_N_procs_group_leaders()
Get the number of processors in MANGO's "group leaders" communicator.
Definition: mpi_partition.cpp:91
mango::MPI_Partition::get_N_procs_worker_groups
int get_N_procs_worker_groups()
Get the number of processors in MANGO's "worker groups" communicator.
Definition: mpi_partition.cpp:86
mango::MPI_Partition::get_rank_world
int get_rank_world()
Get the MPI rank of this processor in MANGO's world communicator.
Definition: mpi_partition.cpp:66
mango::MPI_Partition::get_proc0_world
bool get_proc0_world()
Determine whether this MPI processor has rank 0 in MANGO's world communicator.
Definition: mpi_partition.cpp:56
mango::MPI_Partition::get_N_worker_groups
int get_N_worker_groups()
Returns the number of worker groups.
Definition: mpi_partition.cpp:101
mango::MPI_Partition::get_rank_worker_groups
int get_rank_worker_groups()
Get the MPI rank of this processor in MANGO's "worker groups" communicator.
Definition: mpi_partition.cpp:71
mango::MPI_Partition::verbose
int verbose
If true, information is printed to stdout that may be useful for debugging.
Definition: mango.hpp:219
mango::MPI_Partition::get_comm_worker_groups
MPI_Comm get_comm_worker_groups()
Get the MPI communicator for MANGO's "worker groups" communicator.
Definition: mpi_partition.cpp:46
mango::MPI_Partition::mobilize_workers
void mobilize_workers()
Tell the worker MPI processes (i.e. those that are not group leaders) to begin an evaluation of the o...
Definition: mpi_partition.cpp:118
mango::MPI_Partition::continue_worker_loop
bool continue_worker_loop()
For an MPI worker, determine whether to carry out another evaluation of the objective function or exi...
Definition: mpi_partition.cpp:125
mango::MPI_Partition::get_N_procs_world
int get_N_procs_world()
Get the number of processors in MANGO's world communicator.
Definition: mpi_partition.cpp:81