MOAB: Mesh Oriented datABase  (version 5.4.1)
TreeStats.hpp
Go to the documentation of this file.
00001 /**\file TreeStats.hpp
00002  * \class moab::TreeStats
00003  * \brief Traversal statistics accumulating and reporting
00004  *
00005  * Class to accumulate statistics on traversal performance. This structure contains the
00006  * count of nodes visited at each level in a tree, and the count of traversals that ended
00007  * at each level.  One TrvStats structure can be used with multiple trees or multiple
00008  * queries, or used on only a single tree or a single query.
00009  *
00010  * Note that these traversal statistics are not related to the stats() query below,
00011  * which calculates static information about a tree.  These statistics relate
00012  * to a tree's dynamic behavior on particular operations.
00013  */
00014 
00015 #ifndef TREESTATS_HPP
00016 #define TREESTATS_HPP
00017 
00018 #include "moab/Interface.hpp"
00019 
00020 #include <vector>
00021 #include <iostream>
00022 #include <string>
00023 
00024 namespace moab
00025 {
00026 class TreeStats
00027 {
00028   public:
00029     //! constructor
00030     TreeStats()
00031     {
00032         reset();
00033     }
00034 
00035     /** \brief Given a root node, compute the stats for a tree
00036      * \param impl MOAB instance pointer
00037      * \param root_node Root entity set for the tree
00038      */
00039     ErrorCode compute_stats( Interface* impl, EntityHandle root_node );
00040 
00041     //! reset traversal counters
00042     void reset_trav_stats();
00043 
00044     //! reset all counters
00045     void reset();
00046 
00047     //! print the contents of this structure
00048     void print() const;
00049 
00050     //! output all the contents of this structure on a single line
00051     void output_all_stats( const bool with_endl = true ) const;
00052 
00053     //! output just the traversal stats of this structure on a single line
00054     void output_trav_stats( const bool with_endl = true ) const;
00055 
00056     // times
00057     double initTime;
00058 
00059     // tree stats that depend only on structure (not traversal)
00060     unsigned int maxDepth;
00061     unsigned int numNodes;
00062     unsigned int numLeaves;
00063     double avgObjPerLeaf;
00064     unsigned int minObjPerLeaf;
00065     unsigned int maxObjPerLeaf;
00066 
00067     // traversal statistics
00068     unsigned int nodesVisited;              // number of tree nodes visited since last reset
00069     unsigned int leavesVisited;             // number of tree leaves visited since last reset
00070     unsigned int numTraversals;             // number of tree traversals since last reset
00071     unsigned int constructLeafObjectTests;  // during construction, number of tests of objects (e.g.
00072                                             // elements)
00073     unsigned int traversalLeafObjectTests;  // during traversals, number of tests of objects (e.g. elements)
00074     unsigned int boxElemTests;              // during construction, number of calls to
00075                                             // GeomUtil::box_elem_overlap (KD tree only)
00076 
00077   private:
00078     ErrorCode traverse( Interface* impl, EntityHandle node, unsigned int& depth );
00079 };
00080 
00081 inline ErrorCode TreeStats::compute_stats( Interface* impl, EntityHandle root_node )
00082 {
00083     maxDepth      = 0;
00084     numNodes      = 0;
00085     numLeaves     = 0;
00086     avgObjPerLeaf = 0.0;
00087     minObjPerLeaf = 0;
00088     maxObjPerLeaf = 0;
00089 
00090     ErrorCode rval = traverse( impl, root_node, maxDepth );
00091     avgObjPerLeaf  = ( avgObjPerLeaf > 0 ? avgObjPerLeaf / (double)numLeaves : 0.0 );
00092     return rval;
00093 }
00094 
00095 inline ErrorCode TreeStats::traverse( Interface* impl, EntityHandle node, unsigned int& depth )
00096 {
00097     depth++;
00098     numNodes++;
00099     std::vector< EntityHandle > children;
00100     children.reserve( 2 );
00101     ErrorCode rval = impl->get_child_meshsets( node, children );
00102     if( MB_SUCCESS != rval ) return rval;
00103     if( children.empty() )
00104     {
00105         numLeaves++;
00106         rval = impl->get_entities_by_handle( node, children );
00107         if( MB_SUCCESS != rval ) return rval;
00108         avgObjPerLeaf += children.size();
00109         minObjPerLeaf = std::min( (unsigned int)children.size(), minObjPerLeaf );
00110         maxObjPerLeaf = std::max( (unsigned int)children.size(), maxObjPerLeaf );
00111         return MB_SUCCESS;
00112     }
00113     else
00114     {
00115         unsigned int right_depth = depth, left_depth = depth;
00116         rval = traverse( impl, children[0], left_depth );
00117         if( MB_SUCCESS != rval ) return rval;
00118         rval = traverse( impl, children[1], right_depth );
00119         if( MB_SUCCESS != rval ) return rval;
00120         depth = std::max( left_depth, right_depth );
00121         return MB_SUCCESS;
00122     }
00123 }
00124 
00125 inline void TreeStats::reset()
00126 {
00127     initTime = 0.0;
00128 
00129     maxDepth                 = 0;
00130     numNodes                 = 0;
00131     numLeaves                = 0;
00132     constructLeafObjectTests = 0;
00133     boxElemTests             = 0;
00134     avgObjPerLeaf            = 0.0;
00135     minObjPerLeaf            = 0.0;
00136     maxObjPerLeaf            = 0.0;
00137 
00138     reset_trav_stats();
00139 }
00140 
00141 inline void TreeStats::reset_trav_stats()
00142 {
00143     nodesVisited             = 0;
00144     leavesVisited            = 0;
00145     numTraversals            = 0;
00146     traversalLeafObjectTests = 0;
00147 }
00148 
00149 inline void TreeStats::print() const
00150 {
00151     std::cout << "Tree initialization time = " << initTime << " seconds" << std::endl;
00152 
00153     std::cout << "Num nodes         = " << numNodes << std::endl;
00154     std::cout << "Num leaves        = " << numLeaves << std::endl;
00155     std::cout << "Max depth         = " << maxDepth << std::endl << std::endl;
00156 
00157     std::cout << "Avg objs per leaf = " << avgObjPerLeaf << std::endl;
00158     std::cout << "Min objs per leaf = " << minObjPerLeaf << std::endl;
00159     std::cout << "Max objs per leaf = " << maxObjPerLeaf << std::endl;
00160 
00161     std::cout << "Construction Leaf Object Tests = " << constructLeafObjectTests << std::endl;
00162     std::cout << "Box-Element Tests = " << boxElemTests << std::endl;
00163 
00164     std::cout << "NodesVisited      = " << nodesVisited << std::endl;
00165     std::cout << "LeavesVisited     = " << leavesVisited << std::endl;
00166     std::cout << "Num Traversals    = " << numTraversals << std::endl;
00167     std::cout << "Traversal Leaf Object Tests = " << traversalLeafObjectTests << std::endl;
00168 }
00169 
00170 inline void TreeStats::output_all_stats( const bool with_endl ) const
00171 {
00172     std::cout << initTime << " " << numNodes << " " << numLeaves << " " << maxDepth << " " << avgObjPerLeaf << " "
00173               << minObjPerLeaf << " " << maxObjPerLeaf << " " << constructLeafObjectTests << " " << boxElemTests << " "
00174               << nodesVisited << " " << leavesVisited << " " << numTraversals << " " << traversalLeafObjectTests << " ";
00175     if( with_endl ) std::cout << std::endl;
00176 }
00177 
00178 inline void TreeStats::output_trav_stats( const bool with_endl ) const
00179 {
00180     std::cout << nodesVisited << " " << leavesVisited << " " << numTraversals << " " << traversalLeafObjectTests << " ";
00181     if( with_endl ) std::cout << std::endl;
00182 }
00183 }  // namespace moab
00184 
00185 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines