Zoltan2
Loading...
Searching...
No Matches
MetricOutputManager.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 the MetricOutputManager object.
12//
13// Verbosity levels are
14// NO_STATUS,
15// BASIC_STATUS,
16// DETAILED_STATUS,
17// VERBOSE_DETAILED_STATUS
18// NUM_STATUS_OUTPUT_LEVELS
19//
20// This test can only really be verified by reading the output.
21// So we are testing that MetricOutputManager doesn't crash.
22
23
27
28#include <Teuchos_DefaultComm.hpp>
29
30#include <set>
31#include <iostream>
32#include <string>
33#include <ostream>
34
35using std::string;
37
38int main(int narg, char *arg[])
39{
40 Tpetra::ScopeGuard tscope(&narg, &arg);
41 Teuchos::RCP<const Teuchos::Comm<int> > comm = Tpetra::getDefaultComm();
42
43 int rank = comm->getRank();
44 int nprocs = comm->getSize();
45 bool fail = false;
46
47 MetricOutputManager<int> *intmom = NULL;
48 MetricOutputManager<float> *floatmom = NULL;
49 MetricOutputManager<double> *doublemom = NULL;
50
51 // Even ranks print to std::cout
52
53 bool iPrint = (rank%2 == 0);
54 bool someOnePrints = true;
55
56 comm->barrier();
57
58 try {
59 intmom = new MetricOutputManager<int>(
60 rank, iPrint, std::cout, someOnePrints, string("units"), 10);
61 }
62 catch(std::exception &e){
63 fail=true;
64 }
65
66 TEST_FAIL_AND_EXIT(*comm, !fail, "constructor", 1);
67
68 if (intmom->getMetricsOn() != true)
69 fail = true;
70
71 TEST_FAIL_AND_EXIT(*comm, !fail, "getMetricsOn", 1);
72
73 if (rank==0){
74 std::cout << "\nThere are " << nprocs << " processes. ";
75 std::cout << "Even ranks only participate." << std::endl;
76 }
77
78 try{
79 intmom->print(string("number of things"), 10);
80 intmom->print(string("number of other things"), 5);
81 }
82 catch(std::exception &e){
83 fail=true;
84 }
85
86 TEST_FAIL_AND_EXIT(*comm, !fail, "print to standard output", 1);
87
88 delete intmom;
89
90 // All print to std::cout
91
92 iPrint = true;
93 someOnePrints = true;
94 comm->barrier();
95
96 try {
97 floatmom = new MetricOutputManager<float>(
98
99 rank, iPrint, std::cout, someOnePrints, string("dollars"), 10);
100 }
101 catch(std::exception &e){
102 fail=true;
103 }
104
105 TEST_FAIL_AND_EXIT(*comm, !fail, "constructor", 1);
106
107 if (floatmom->getMetricsOn() != true)
108 fail = true;
109
110 TEST_FAIL_AND_EXIT(*comm, !fail, "getMetricsOn", 1);
111
112 if (rank==0){
113 std::cout << "\nThere are " << nprocs << " processes. ";
114 std::cout << "All ranks participate." << std::endl;
115 }
116
117 try{
118 floatmom->print(string("Price of things"), 10.10);
119 floatmom->print(string("Price of other things"), 25.25);
120 }
121 catch(std::exception &e){
122 fail=true;
123 }
124
125 TEST_FAIL_AND_EXIT(*comm, !fail, "all print to standard output", 1);
126
127 delete floatmom;
128
129 // Node zero prints to a file.
130
131 iPrint = (rank == 0);
132 someOnePrints = true;
133 comm->barrier();
134
135 std::ios_base::openmode flags = std::ios_base::out & std::ios_base::trunc;
136
137 std::ofstream outF("testMetricFile.txt", flags);
138
139 try {
140 doublemom = new MetricOutputManager<double>( rank, iPrint, outF, someOnePrints, string("microseconds"), 10);
141 }
142 catch(std::exception &e){
143 fail=true;
144 }
145
146 TEST_FAIL_AND_EXIT(*comm, !fail, "constructor", 1);
147
148 if (rank==0){
149 std::cout << "\nThere are " << nprocs << " processes. ";
150 std::cout << "Rank zero only participates" << std::endl;
151 }
152
153 try{
154 doublemom->print(string("Time to do something"),
155 10.101012345);
156 doublemom->print(string("Time to do something else"),
157 25.2500024);
158 }
159 catch(std::exception &e){
160 fail=true;
161 }
162
163 TEST_FAIL_AND_EXIT(*comm, !fail, "printing to a file", 1);
164
165 outF.close();
166
167 comm->barrier();
168
169 if (rank == 0){
170 std::ifstream inF("testMetricFile.txt");
171 string s;
172 while (getline(inF, s)){
173 std::cout << s << std::endl;
174 }
175 inF.close();
176 system("rm testMetricFile.txt"); // \todo fix for windows
177 }
178
179 comm->barrier();
180
181 delete doublemom;
182
183 if (rank==0)
184 std::cout << "PASS" << std::endl;
185}
#define TEST_FAIL_AND_EXIT(comm, ok, s, code)
Defines the MetricOutputManager class.
Defines Parameter related enumerators, declares functions.
common code used by tests
int main()
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