Zoltan2
Zoltan2_BaseClassMetrics.hpp
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 
50 #ifndef ZOLTAN2_BASEMETRICVALUES_HPP
51 #define ZOLTAN2_BASEMETRICVALUES_HPP
52 
53 #include <Zoltan2_Standards.hpp>
54 
55 #define UNKNOWN_METRICS_TYPE_NAME "UnknownMetricClass" // this is an error - I kept the base class non virtual so we could work with stl maps for example
56 #define IMBALANCE_METRICS_TYPE_NAME "ImbalanceMetrics" // For the ImbalanceMetrics class
57 #define GRAPH_METRICS_TYPE_NAME "GraphMetrics" // For the GraphMetrics class
58 
59 #define METRICS_UNSET_STRING "unset"
60 
61 namespace Zoltan2{
62 
64 template <typename scalar_t>
66 
67 private:
69 void resetValues( int memCount ){
70  scalar_t *tmp = new scalar_t [memCount];
71  memset(tmp, 0, sizeof(scalar_t) * memCount);
72  values_ = arcp(tmp, 0, memCount, true);
73 }
74 
76 std::string metricName_;
77 
79 ArrayRCP<scalar_t> values_;
80 
81 protected:
82 
84 scalar_t getValue(int enumIndex) const { return values_[enumIndex]; }
85 
87 void setValue(int enumIndex, scalar_t value) { values_[enumIndex] = value; }
88 
89 public:
90 
93  metricName_(METRICS_UNSET_STRING), values_() {
94 
95 }
96 
98 BaseClassMetrics(int memCount, std::string mname) :
99  metricName_(mname), values_() {
100  resetValues(memCount);
101 }
102 
104 BaseClassMetrics(int memCount) :
105  metricName_(METRICS_UNSET_STRING), values_() {
106  resetValues(memCount);
107 }
108 
110 virtual ~BaseClassMetrics() {}
111 
113 virtual void printLine(std::ostream &os) const {};
114 
116 virtual const std::vector<std::string> & getMetrics() const { return static_metricNames_; }
117 
119 virtual const std::string & getMetricType() const { return static_unknown_metricTypeName_; }
120 
122 const std::string &getName() const { return metricName_; }
123 
125 void setName(std::string name) { metricName_ = name;}
126 
128 bool hasMetricValue(const std::string & metric_name) const {
129  return( convertMetricNameToIndex( metric_name ) != getMetrics().size() );
130 }
131 
133 scalar_t getMetricValue(const std::string & metric_name) const
134 {
135  size_t metricIndex = convertMetricNameToIndex( metric_name);
136  if( metricIndex == getMetrics().size() )
137  return 0.0; // throw an error
138  return values_[metricIndex];
139 }
140 
142 void setMetricValue(const std::string & metric_name, scalar_t value) const
143 {
144  size_t metricIndex = convertMetricNameToIndex( metric_name);
145  if( metricIndex != getMetrics().size() )
146  values_[metricIndex] = value; // MDM - I'm doing this for the moment to build the behavior in the utility functions - but we probably want to error handle here for bad names
147 }
148 
150 size_t convertMetricNameToIndex(const std::string & metric_name) const
151 {
152  const std::vector<std::string> & metricNames = getMetrics();
153  size_t metricIndex = std::find(metricNames.begin(), metricNames.end(), metric_name) - metricNames.begin();
154  return metricIndex; // this can return metricNames.size() if not found
155 }
156 
159 
161 static std::vector<std::string> static_metricNames_;
162 
164 static std::vector<std::string> static_allMetricNames_;
165 };
166 
168 template <typename scalar_t>
170 
172 template <typename scalar_t>
173 std::vector<std::string> BaseClassMetrics<scalar_t>::static_metricNames_ = {};
174 
176 template <typename scalar_t>
178 
179 } // namespace Zoltan2
180 #endif
scalar_t getMetricValue(const std::string &metric_name) const
#define IMBALANCE_METRICS_TYPE_NAME
static std::vector< std::string > static_metricNames_
static std::string static_unknown_metricTypeName_
BaseClassMetrics(int memCount, std::string mname)
Constructor.
const std::string & getName() const
Get the name of the item measured.
scalar_t getValue(int enumIndex) const
void setName(std::string name)
Set or reset the name.
void setMetricValue(const std::string &metric_name, scalar_t value) const
void setValue(int enumIndex, scalar_t value)
BaseClassMetrics()
Constructor - for compiling but not used.
size_t convertMetricNameToIndex(const std::string &metric_name) const
bool hasMetricValue(const std::string &metric_name) const
virtual const std::vector< std::string > & getMetrics() const
#define UNKNOWN_METRICS_TYPE_NAME
static std::vector< std::string > static_allMetricNames_
virtual const std::string & getMetricType() const
Get the class type of the metric.
Gathering definitions used in software development.
virtual void printLine(std::ostream &os) const
BaseClassMetrics(int memCount)
Constructor.
#define GRAPH_METRICS_TYPE_NAME
#define METRICS_UNSET_STRING