MOAB: Mesh Oriented datABase  (version 5.4.0)
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,
00090                                                         double* min_times,
00091                                                         double* max_times,
00092                                                         double* avg_times )
00093 {
00094     ErrorCode rval = MB_SUCCESS;
00095     int success    = MPI_Reduce( slTimes, min_times, NUM_STATS, MPI_DOUBLE, MPI_MIN, 0, comm );
00096     if( !success ) rval = MB_FAILURE;
00097 
00098     success = MPI_Reduce( slTimes, max_times, NUM_STATS, MPI_DOUBLE, MPI_MAX, 0, comm );
00099     if( !success ) rval = MB_FAILURE;
00100 
00101     if( avg_times )
00102     {
00103         int sz, rank;
00104         MPI_Comm_size( comm, &sz );
00105         MPI_Comm_rank( comm, &rank );
00106         std::vector< double > all_times;
00107         if( !rank ) all_times.resize( NUM_STATS * sz + NUM_STATS );
00108         success = MPI_Gather( slTimes, NUM_STATS, MPI_DOUBLE, ( rank ? NULL : &all_times[0] ), NUM_STATS, MPI_DOUBLE, 0,
00109                               comm );
00110         if( !success ) rval = MB_FAILURE;
00111         if( !rank )
00112         {
00113             std::fill( avg_times, avg_times + NUM_STATS, 0.0 );
00114             for( int p = 0; p < sz; p++ )
00115             {
00116                 for( int s = 0; s < NUM_STATS; s++ )
00117                     avg_times[s] += all_times[p * NUM_STATS + s];
00118             }
00119 
00120             for( int s = 0; s <= NUM_STATS; s++ )
00121                 avg_times[s] /= (double)sz;
00122         }
00123     }
00124 
00125     return rval;
00126 }
00127 #endif
00128 
00129 /* \brief Output stats all on one line
00130  */
00131 inline void SpatialLocatorTimes::output_header( bool print_endl ) const
00132 {
00133     std::cout << "Intmed_init Intmed_send Intmed_search src_send src_search targ_return targ_store";
00134     if( print_endl ) std::cout << std::endl;
00135 }
00136 
00137 /* \brief Output stats all on one line
00138  */
00139 inline void SpatialLocatorTimes::output( bool print_head, bool print_endl ) const
00140 {
00141     if( print_head ) output_header( true );
00142     for( int i = 0; i < NUM_STATS; i++ )
00143         std::cout << slTimes[i] << " ";
00144 
00145     if( print_endl ) std::cout << std::endl;
00146 }
00147 
00148 }  // namespace moab
00149 
00150 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines