Zoltan2
MetricOutputManager.cpp
Go to the documentation of this file.
1 // @HEADER
2 //
3 // ***********************************************************************
4 //
5 // Zoltan2: A package of combinatorial algorithms for scientific computing
6 // Copyright 2012 Sandia Corporation
7 //
8 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Karen Devine (kddevin@sandia.gov)
39 // Erik Boman (egboman@sandia.gov)
40 // Siva Rajamanickam (srajama@sandia.gov)
41 //
42 // ***********************************************************************
43 //
44 // @HEADER
45 //
46 // Testing the MetricOutputManager object.
47 //
48 // Verbosity levels are
49 // NO_STATUS,
50 // BASIC_STATUS,
51 // DETAILED_STATUS,
52 // VERBOSE_DETAILED_STATUS
53 // NUM_STATUS_OUTPUT_LEVELS
54 //
55 // This test can only really be verified by reading the output.
56 // So we are testing that MetricOutputManager doesn't crash.
57 
58 
60 #include <Zoltan2_Parameters.hpp>
61 #include <Zoltan2_TestHelpers.hpp>
62 
63 #include <Teuchos_DefaultComm.hpp>
64 
65 #include <set>
66 #include <iostream>
67 #include <string>
68 #include <ostream>
69 
70 using std::string;
72 
73 int main(int narg, char *arg[])
74 {
75  Tpetra::ScopeGuard tscope(&narg, &arg);
76  Teuchos::RCP<const Teuchos::Comm<int> > comm = Tpetra::getDefaultComm();
77 
78  int rank = comm->getRank();
79  int nprocs = comm->getSize();
80  bool fail = false;
81 
82  MetricOutputManager<int> *intmom = NULL;
83  MetricOutputManager<float> *floatmom = NULL;
84  MetricOutputManager<double> *doublemom = NULL;
85 
86  // Even ranks print to std::cout
87 
88  bool iPrint = (rank%2 == 0);
89  bool someOnePrints = true;
90 
91  comm->barrier();
92 
93  try {
94  intmom = new MetricOutputManager<int>(
95  rank, iPrint, std::cout, someOnePrints, string("units"), 10);
96  }
97  catch(std::exception &e){
98  fail=true;
99  }
100 
101  TEST_FAIL_AND_EXIT(*comm, !fail, "constructor", 1);
102 
103  if (intmom->getMetricsOn() != true)
104  fail = true;
105 
106  TEST_FAIL_AND_EXIT(*comm, !fail, "getMetricsOn", 1);
107 
108  if (rank==0){
109  std::cout << "\nThere are " << nprocs << " processes. ";
110  std::cout << "Even ranks only participate." << std::endl;
111  }
112 
113  try{
114  intmom->print(string("number of things"), 10);
115  intmom->print(string("number of other things"), 5);
116  }
117  catch(std::exception &e){
118  fail=true;
119  }
120 
121  TEST_FAIL_AND_EXIT(*comm, !fail, "print to standard output", 1);
122 
123  delete intmom;
124 
125  // All print to std::cout
126 
127  iPrint = true;
128  someOnePrints = true;
129  comm->barrier();
130 
131  try {
132  floatmom = new MetricOutputManager<float>(
133 
134  rank, iPrint, std::cout, someOnePrints, string("dollars"), 10);
135  }
136  catch(std::exception &e){
137  fail=true;
138  }
139 
140  TEST_FAIL_AND_EXIT(*comm, !fail, "constructor", 1);
141 
142  if (floatmom->getMetricsOn() != true)
143  fail = true;
144 
145  TEST_FAIL_AND_EXIT(*comm, !fail, "getMetricsOn", 1);
146 
147  if (rank==0){
148  std::cout << "\nThere are " << nprocs << " processes. ";
149  std::cout << "All ranks participate." << std::endl;
150  }
151 
152  try{
153  floatmom->print(string("Price of things"), 10.10);
154  floatmom->print(string("Price of other things"), 25.25);
155  }
156  catch(std::exception &e){
157  fail=true;
158  }
159 
160  TEST_FAIL_AND_EXIT(*comm, !fail, "all print to standard output", 1);
161 
162  delete floatmom;
163 
164  // Node zero prints to a file.
165 
166  iPrint = (rank == 0);
167  someOnePrints = true;
168  comm->barrier();
169 
170  std::ios_base::openmode flags = std::ios_base::out & std::ios_base::trunc;
171 
172  std::ofstream outF("testMetricFile.txt", flags);
173 
174  try {
175  doublemom = new MetricOutputManager<double>( rank, iPrint, outF, someOnePrints, string("microseconds"), 10);
176  }
177  catch(std::exception &e){
178  fail=true;
179  }
180 
181  TEST_FAIL_AND_EXIT(*comm, !fail, "constructor", 1);
182 
183  if (rank==0){
184  std::cout << "\nThere are " << nprocs << " processes. ";
185  std::cout << "Rank zero only participates" << std::endl;
186  }
187 
188  try{
189  doublemom->print(string("Time to do something"),
190  10.101012345);
191  doublemom->print(string("Time to do something else"),
192  25.2500024);
193  }
194  catch(std::exception &e){
195  fail=true;
196  }
197 
198  TEST_FAIL_AND_EXIT(*comm, !fail, "printing to a file", 1);
199 
200  outF.close();
201 
202  comm->barrier();
203 
204  if (rank == 0){
205  std::ifstream inF("testMetricFile.txt");
206  string s;
207  while (getline(inF, s)){
208  std::cout << s << std::endl;
209  }
210  inF.close();
211  system("rm testMetricFile.txt"); // \todo fix for windows
212  }
213 
214  comm->barrier();
215 
216  delete doublemom;
217 
218  if (rank==0)
219  std::cout << "PASS" << std::endl;
220 }
#define TEST_FAIL_AND_EXIT(comm, ok, s, code)
int main(int narg, char *arg[])
Defines the MetricOutputManager class.
Defines Parameter related enumerators, declares functions.
common code used by tests
MetricOutputManager handles output of profiling messages.
bool getMetricsOn() const
Return true if any process outputs metrics.
void print(const std::string &msg, const T val)
Print a line of information.
static const std::string fail