Zoltan2
Loading...
Searching...
No Matches
Environment.cpp
Go to the documentation of this file.
1// @HEADER
2// *****************************************************************************
3// Zoltan2: A package of combinatorial algorithms for scientific computing
4//
5// Copyright 2012 NTESS and the Zoltan2 contributors.
6// SPDX-License-Identifier: BSD-3-Clause
7// *****************************************************************************
8// @HEADER
9
10//
11// Testing Zoltan2::Environment
19#include <Teuchos_ParameterList.hpp>
20#include <Teuchos_DefaultComm.hpp>
21
22using std::string;
23using Teuchos::ParameterEntry;
24using Teuchos::RCP;
25using Teuchos::Comm;
27
28int checkErrorCode(Teuchos::RCP<const Teuchos::Comm<int> > &comm, int code)
29{
30 int rank = comm->getRank();
31 if (code > 0)
32 std::cerr << "Proc " << rank << " error: " << code << std::endl;
33 comm->barrier();
34
35 // Will return 1 if any process has a non-zero code
36 TEST_FAIL_AND_RETURN_VALUE(*comm, code==0, "test failure", 1);
37
38 return 0;
39}
40
41int main(int narg, char *arg[])
42{
43 Tpetra::ScopeGuard tscope(&narg, &arg);
44 Teuchos::RCP<const Teuchos::Comm<int> > comm = Tpetra::getDefaultComm();
45
46 int rank = comm->getRank();
47 int nprocs = comm->getSize();
48 int fail = 0;
49
51 // Test the Environment(comm) constructor - no param list
52
53 Environment *defEnv = NULL;
54
55 try{
56 defEnv = new Environment(comm);
57 }
58 catch(std::exception &e){
59 std::cerr << e.what() << std::endl;
60 fail=1000;
61 }
62
63 if (checkErrorCode(comm, fail))
64 return 1;
65
66 if (!fail && defEnv->myRank_ != rank)
67 fail = 1001;
68
69 if (!fail && defEnv->numProcs_ != nprocs)
70 fail = 1002;
71
72 if (!fail && defEnv->comm_->getSize() != nprocs)
73 fail = 1003;
74
75 if (!fail && defEnv->doStatus() != true)
76 fail = 1005;
77
78 if (!fail && defEnv->doTiming() != false)
79 fail = 1006;
80
81 if (!fail && defEnv->doMemoryProfiling() != false)
82 fail = 1007;
83
85 fail = 1008;
86
87 if (checkErrorCode(comm, fail))
88 return 1;
89
90 delete defEnv;
91
93 // Set a few parameters and create an Environment
94
95 Teuchos::ParameterList myParams("testParameterList");
96
97 myParams.set("debug_level", "detailed_status");
98 myParams.set("debug_procs", "all");
99 myParams.set("debug_output_stream", "std::cout");
100
101 if (nprocs > 3)
102 myParams.set("memory_procs", "0-1,3");
103 else
104 myParams.set("memory_procs", "0");
105
106 myParams.set("memory_output_file", "memInfo.txt");
107
108 myParams.set("partitioning_objective", "minimize_cut_edge_weight");
109 myParams.set("imbalance_tolerance", 1.2);
110
111 Environment *env = NULL;
112
113 try{
114 env = new Environment(myParams, comm);
115 }
116 catch(std::exception &e){
117 std::cerr << e.what() << std::endl;
118 fail=2000;
119 }
120
121 if (!fail){
122 try{
123 env->debug(Zoltan2::BASIC_STATUS, "A basic debugging message.");
124 }
125 catch(std::exception &e){
126 std::cerr << e.what() << std::endl;
127 fail=3000;
128 }
129 }
130
131 if (!fail){
132 try{
133 env->memory("Memory info");
134 env->memory("Memory info next");
135 env->memory("Memory info after");
136 }
137 catch(std::exception &e){
138 std::cerr << e.what() << std::endl;
139 fail=3002;
140 }
141 }
142
143 if (checkErrorCode(comm, fail))
144 return 1;
145
146 if (!fail && env->myRank_ != rank)
147 fail = 2001;
148
149 if (!fail && env->numProcs_ != nprocs)
150 fail = 2002;
151
152 if (!fail && env->comm_->getSize() != nprocs)
153 fail = 2003;
154
155 if (!fail){
156 const Teuchos::ParameterList &pl1 = env->getParameters();
157 const ParameterEntry *dl = pl1.getEntryPtr("debug_level");
158
159 if (!dl){
160 fail = 2004;
161 }
162 else if (!(dl->isType<int>())){
163 fail = 2013;
164 }
165 else{
166 int value;
167 int &val = dl->getValue<int>(&value);
168 if (val != Zoltan2::DETAILED_STATUS)
169 fail = 2005;
170 }
171 }
172
174 fail = 2008;
175
176 if (checkErrorCode(comm, fail))
177 return 1;
178
179 if (rank==0){
180 std::cout << "\nA test parameter list" << std::endl;
181 const Teuchos::ParameterList &envParams = env->getParameters();
182 try{
183 envParams.print();
184 }
185 catch(std::exception &e){
186 std::cerr << e.what() << std::endl;
187 fail=2013;
188 }
189 }
190
191 if (checkErrorCode(comm, fail))
192 return 1;
193
195 // Given an existing Environment, get its parameters and
196 // add some new parameters and create a new Environment.
197
198 RCP<const Comm<int> > oldComm = env->comm_;
199 const Teuchos::ParameterList &oldParams = env->getUnvalidatedParameters();
200
201 Teuchos::ParameterList newParams = oldParams;
202 newParams.set("error_check_level", "debug_mode_assertions");
203 newParams.remove("memory_output_file");
204 newParams.set("imbalance_tolerance", 1.05);
205 newParams.set("algorithm", "phg");
206 newParams.set("partitioning_objective", "minimize_cut_edge_weight");
207
208 RCP<Environment> newEnv;
209
210 try{
211 newEnv = Teuchos::rcp(new Environment(newParams, oldComm));
212 }
213 catch(std::exception &e){
214 std::cerr << e.what() << std::endl;
215 fail=3000;
216 }
217
218 if (checkErrorCode(comm, fail))
219 return 1;
220
221 if (!fail && newEnv->errorCheckLevel_ != Zoltan2::DEBUG_MODE_ASSERTION)
222 fail = 3001;
223
224 if (!fail && rank==0){
225 std::cout << "\nA few changes/additions to the list" << std::endl;
226 const Teuchos::ParameterList &envParams = newEnv->getParameters();
227 try{
228 envParams.print();
229 }
230 catch(std::exception &e){
231 std::cerr << e.what() << std::endl;
232 fail=3003;
233 }
234 }
235
236 if (checkErrorCode(comm, fail))
237 return 1;
238
239 delete env;
240
241 if (rank==0)
242 std::cout << "PASS" << std::endl;
243
244 return 0;
245}
int checkErrorCode(Teuchos::RCP< const Teuchos::Comm< int > > &comm, int code)
#define TEST_FAIL_AND_RETURN_VALUE(comm, ok, s, rc)
Defines the Environment class.
Defines Parameter related enumerators, declares functions.
common code used by tests
int main()
The user parameters, debug, timing and memory profiling output objects, and error checking methods.
int myRank_
mpi rank (relative to comm_)
const Teuchos::ParameterList & getParameters() const
Returns a reference to the user's parameter list.
void debug(MessageOutputLevel level, const char *msg) const
Send a message to the debug output manager.
bool doTiming() const
Return true if timing was requested, even if this process is not printing out timing messages.
Comm_t comm_
communicator for environment
const Teuchos::ParameterList & getUnvalidatedParameters() const
Returns a const reference to the user's original list.
int numProcs_
number of processes (relative to comm_)
bool doStatus() const
Return true if debug output was requested, even if this process is not printing out debug messages.
void memory(const char *msg) const
Print a message and the kilobytes in use by this process.
AssertionLevel errorCheckLevel_
level of error checking to do
bool doMemoryProfiling() const
Return true if memory usage output was requested, even if this process is not printing out memory use...
static const std::string fail
@ BASIC_STATUS
the status at each high level step
@ DETAILED_STATUS
sub-steps, each method's entry and exit
@ BASIC_ASSERTION
fast typical checks for valid arguments
@ DEBUG_MODE_ASSERTION
checks for logic errors