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 namespace std;
71 using std::string;
73 
74 int main(int argc, char *argv[])
75 {
76  Teuchos::GlobalMPISession session(&argc, &argv);
77  Teuchos::RCP<const Teuchos::Comm<int> > comm =
78  Teuchos::DefaultComm<int>::getComm();
79 
80  int rank = comm->getRank();
81  int nprocs = comm->getSize();
82  bool fail = false;
83 
84  MetricOutputManager<int> *intmom = NULL;
85  MetricOutputManager<float> *floatmom = NULL;
86  MetricOutputManager<double> *doublemom = NULL;
87 
88  // Even ranks print to cout
89 
90  bool iPrint = (rank%2 == 0);
91  bool someOnePrints = true;
92 
93  comm->barrier();
94 
95  try {
96  intmom = new MetricOutputManager<int>(
97  rank, iPrint, std::cout, someOnePrints, string("units"), 10);
98  }
99  catch(std::exception &e){
100  fail=true;
101  }
102 
103  TEST_FAIL_AND_EXIT(*comm, !fail, "constructor", 1);
104 
105  if (intmom->getMetricsOn() != true)
106  fail = true;
107 
108  TEST_FAIL_AND_EXIT(*comm, !fail, "getMetricsOn", 1);
109 
110  if (rank==0){
111  std::cout << "\nThere are " << nprocs << " processes. ";
112  std::cout << "Even ranks only participate." << std::endl;
113  }
114 
115  try{
116  intmom->print(string("number of things"), 10);
117  intmom->print(string("number of other things"), 5);
118  }
119  catch(std::exception &e){
120  fail=true;
121  }
122 
123  TEST_FAIL_AND_EXIT(*comm, !fail, "print to standard output", 1);
124 
125  delete intmom;
126 
127  // All print to cout
128 
129  iPrint = true;
130  someOnePrints = true;
131  comm->barrier();
132 
133  try {
134  floatmom = new MetricOutputManager<float>(
135 
136  rank, iPrint, std::cout, someOnePrints, string("dollars"), 10);
137  }
138  catch(std::exception &e){
139  fail=true;
140  }
141 
142  TEST_FAIL_AND_EXIT(*comm, !fail, "constructor", 1);
143 
144  if (floatmom->getMetricsOn() != true)
145  fail = true;
146 
147  TEST_FAIL_AND_EXIT(*comm, !fail, "getMetricsOn", 1);
148 
149  if (rank==0){
150  std::cout << "\nThere are " << nprocs << " processes. ";
151  std::cout << "All ranks participate." << std::endl;
152  }
153 
154  try{
155  floatmom->print(string("Price of things"), 10.10);
156  floatmom->print(string("Price of other things"), 25.25);
157  }
158  catch(std::exception &e){
159  fail=true;
160  }
161 
162  TEST_FAIL_AND_EXIT(*comm, !fail, "all print to standard output", 1);
163 
164  delete floatmom;
165 
166  // Node zero prints to a file.
167 
168  iPrint = (rank == 0);
169  someOnePrints = true;
170  comm->barrier();
171 
172  ios_base::openmode flags = ios_base::out & ios_base::trunc;
173 
174  ofstream outF("testMetricFile.txt", flags);
175 
176  try {
177  doublemom = new MetricOutputManager<double>( rank, iPrint, outF, someOnePrints, string("microseconds"), 10);
178  }
179  catch(std::exception &e){
180  fail=true;
181  }
182 
183  TEST_FAIL_AND_EXIT(*comm, !fail, "constructor", 1);
184 
185  if (rank==0){
186  std::cout << "\nThere are " << nprocs << " processes. ";
187  std::cout << "Rank zero only participates" << std::endl;
188  }
189 
190  try{
191  doublemom->print(string("Time to do something"),
192  10.101012345);
193  doublemom->print(string("Time to do something else"),
194  25.2500024);
195  }
196  catch(std::exception &e){
197  fail=true;
198  }
199 
200  TEST_FAIL_AND_EXIT(*comm, !fail, "printing to a file", 1);
201 
202  outF.close();
203 
204  comm->barrier();
205 
206  if (rank == 0){
207  ifstream inF("testMetricFile.txt");
208  string s;
209  while (getline(inF, s)){
210  std::cout << s << std::endl;
211  }
212  inF.close();
213  system("rm testMetricFile.txt"); // \todo fix for windows
214  }
215 
216  comm->barrier();
217 
218  delete doublemom;
219 
220  if (rank==0)
221  std::cout << "PASS" << std::endl;
222 }
Defines the MetricOutputManager class.
void print(const std::string &msg, const T val)
Print a line of information.
MetricOutputManager handles output of profiling messages.
Defines Parameter related enumerators, declares functions.
#define TEST_FAIL_AND_EXIT(comm, ok, s, code)
bool getMetricsOn() const
Return true if any process outputs metrics.
common code used by tests
int main(int argc, char *argv[])
static const std::string fail