MOAB: Mesh Oriented datABase  (version 5.2.1)
SpatialLocatorTimes.hpp
Go to the documentation of this file.
00001 /**\file SpatialLocatorTimes.hpp
00002  * \class moab::SpatialLocatorTimes
00003  * \brief Statistics for spatial location
00004  *
00005  * Class to accumulate statistics on performance of spatial location.  This structure stores
00006  * only local (single proc) statistics, but provides functions for accumulating max/min/avg
00007  * time properties for performance reporting.
00008  *
00009  * Similar to TreeStats, this class is very lightweight, with most variables publicly accessible.
00010  *
00011  */
00012 
00013 #ifndef SPATIALLOCATORTIMES_HPP
00014 #define SPATIALLOCATORTIMES_HPP
00015 
00016 #include "moab/Interface.hpp"
00017 
00018 #include <iostream>
00019 
00020 #ifdef MOAB_HAVE_MPI
00021 #include "moab_mpi.h"
00022 #endif
00023 
00024 namespace moab
00025 {
00026 class SpatialLocatorTimes
00027 {
00028   public:
00029     /* constructor
00030      */
00031     SpatialLocatorTimes()
00032     {
00033         reset();
00034     }
00035 
00036     /* \brief Reset all stats defined here
00037      */
00038     void reset();
00039 
00040     /* \brief Output header for stats names
00041      */
00042     void output_header( bool print_endl = false ) const;
00043 
00044     /* \brief Output stats all on one line
00045      */
00046     void output( bool print_head = false, bool print_endl = false ) const;
00047 
00048 #ifdef MOAB_HAVE_MPI
00049     /* \brief Accumulate times over all processors into provided array
00050      * Max and min is accumulated over all processors, onto root, for each stat.  If avg_stats is
00051      non-NULL,
00052      * all stats are gathered to root so average can be taken too.
00053      *
00054      * This function must be called collectively on the communicator
00055      * \param comm MPI communictor over which this accumulation is done
00056      * \param max_times Array of size NUM_STATS into which max times are inserted
00057      * \param min_times Array of size NUM_STATS into which min times are inserted
00058      * \param avg_times Array of size NUM_STATS into which avg times are inserted; if NULL, no avg
00059      times are computed.
00060 
00061      */
00062     ErrorCode accumulate_times( MPI_Comm comm, double* max_times, double* min_times, double* avg_times = NULL );
00063 #endif
00064 
00065     /* \brief Enumeration to identify fields in performance data
00066      */
00067     enum
00068     {
00069         INTMED_INIT = 0,  // time to compute intermediate partition, incl global bounding box
00070         INTMED_SEND,      // time to send search points from target to intermediate parts
00071         INTMED_SEARCH,    // time to find candidate src boxes for search points on intermidiate procs
00072         SRC_SEND,         // time to send search points to src procs
00073         SRC_SEARCH,       // time to search local box/elements on src procs
00074         TARG_RETURN,      // time to return point location data to target procs
00075         TARG_STORE,       // time to store point location into local SpatialLocator object
00076         NUM_STATS         // number of stats, useful for array sizing and terminating loops over stats
00077     };
00078 
00079     double slTimes[NUM_STATS];
00080 };
00081 
00082 inline void SpatialLocatorTimes::reset()
00083 {
00084     for( int i = 0; i < NUM_STATS; i++ )
00085         slTimes[i] = 0.0;
00086 }
00087 
00088 #ifdef MOAB_HAVE_MPI
00089 inline ErrorCode SpatialLocatorTimes::accumulate_times( MPI_Comm comm, double* min_times, double* max_times,
00090                                                         double* avg_times )
00091 {
00092     ErrorCode rval = MB_SUCCESS;
00093     int success    = MPI_Reduce( slTimes, min_times, NUM_STATS, MPI_DOUBLE, MPI_MIN, 0, comm );
00094     if( !success ) rval = MB_FAILURE;
00095 
00096     success = MPI_Reduce( slTimes, max_times, NUM_STATS, MPI_DOUBLE, MPI_MAX, 0, comm );
00097     if( !success ) rval = MB_FAILURE;
00098 
00099     if( avg_times )
00100     {
00101         int sz, rank;
00102         MPI_Comm_size( comm, &sz );
00103         MPI_Comm_rank( comm, &rank );
00104         std::vector< double > all_times;
00105         if( !rank ) all_times.resize( NUM_STATS * sz + NUM_STATS );
00106         success = MPI_Gather( slTimes, NUM_STATS, MPI_DOUBLE, ( rank ? NULL : &all_times[0] ), NUM_STATS, MPI_DOUBLE, 0,
00107                               comm );
00108         if( !success ) rval = MB_FAILURE;
00109         if( !rank )
00110         {
00111             std::fill( avg_times, avg_times + NUM_STATS, 0.0 );
00112             for( int p = 0; p < sz; p++ )
00113             {
00114                 for( int s = 0; s < NUM_STATS; s++ )
00115                     avg_times[s] += all_times[p * NUM_STATS + s];
00116             }
00117 
00118             for( int s = 0; s <= NUM_STATS; s++ )
00119                 avg_times[s] /= (double)sz;
00120         }
00121     }
00122 
00123     return rval;
00124 }
00125 #endif
00126 
00127 /* \brief Output stats all on one line
00128  */
00129 inline void SpatialLocatorTimes::output_header( bool print_endl ) const
00130 {
00131     std::cout << "Intmed_init Intmed_send Intmed_search src_send src_search targ_return targ_store";
00132     if( print_endl ) std::cout << std::endl;
00133 }
00134 
00135 /* \brief Output stats all on one line
00136  */
00137 inline void SpatialLocatorTimes::output( bool print_head, bool print_endl ) const
00138 {
00139     if( print_head ) output_header( true );
00140     for( int i = 0; i < NUM_STATS; i++ )
00141         std::cout << slTimes[i] << " ";
00142 
00143     if( print_endl ) std::cout << std::endl;
00144 }
00145 
00146 }  // namespace moab
00147 
00148 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines