[MOAB-dev] r2673 - in MOAB/trunk: . tools/dagmc

tautges at mcs.anl.gov tautges at mcs.anl.gov
Mon Mar 2 21:11:25 CST 2009


Author: tautges
Date: 2009-03-02 21:11:25 -0600 (Mon, 02 Mar 2009)
New Revision: 2673

Added:
   MOAB/trunk/tools/dagmc/ray_fire_test.cc
Modified:
   MOAB/trunk/MBOrientedBoxTreeTool.cpp
   MOAB/trunk/MBOrientedBoxTreeTool.hpp
   MOAB/trunk/tools/dagmc/DagMC.hpp
   MOAB/trunk/tools/dagmc/Makefile.am
Log:
Adding a ray fire test for timing calls to ray tracing through dagmc.  
Adding another function to MBOrientedBoxTreeTool for passing back tree 
stats instead of just printing them.



Modified: MOAB/trunk/MBOrientedBoxTreeTool.cpp
===================================================================
--- MOAB/trunk/MBOrientedBoxTreeTool.cpp	2009-03-02 19:45:22 UTC (rev 2672)
+++ MOAB/trunk/MBOrientedBoxTreeTool.cpp	2009-03-03 03:11:25 UTC (rev 2673)
@@ -1665,6 +1665,47 @@
 //#define WW <<std::setw(10)<<std::fixed<<
 #define WE <<std::setw(10)<<
 #define WW WE
+MBErrorCode MBOrientedBoxTreeTool::stats( MBEntityHandle set, 
+                                          unsigned &total_entities,
+                                          double &rv,
+                                          double &tot_node_volume,
+                                          double &tot_to_root_volume,
+                                          unsigned &tree_height,
+                                          unsigned &node_count,
+                                          unsigned &num_leaves)
+{
+  StatData d;
+  MBErrorCode rval;
+  unsigned i;
+  MBCartVect total_dim;
+  
+  rval = recursive_stats( this, instance, set, 0, d, total_entities, total_dim );
+  if (MB_SUCCESS != rval)
+    return rval;
+  
+  tree_height = d.leaf_depth.size();
+  unsigned min_leaf_depth = tree_height;
+  num_leaves = 0;
+  unsigned max_leaf_per_depth = 0;
+  double sum_leaf_depth = 0, sqr_leaf_depth = 0;
+  for (i = 0; i < d.leaf_depth.size(); ++i) {
+    unsigned val = d.leaf_depth[i];
+    num_leaves += val;
+    sum_leaf_depth += (double)val*i;
+    sqr_leaf_depth += (double)val*i*i;
+    if (val && i < min_leaf_depth)
+      min_leaf_depth = i;
+    if (max_leaf_per_depth < val)
+      max_leaf_per_depth = val;
+  }
+  rv = total_dim[0]*total_dim[1]*total_dim[2];
+  tot_node_volume = d.vol.sum;
+  tot_to_root_volume = d.vol.sum/rv;
+  node_count = d.count;
+
+  return MB_SUCCESS;
+}
+
 MBErrorCode MBOrientedBoxTreeTool::stats( MBEntityHandle set, std::ostream& s )
 {
   StatData d;

Modified: MOAB/trunk/MBOrientedBoxTreeTool.hpp
===================================================================
--- MOAB/trunk/MBOrientedBoxTreeTool.hpp	2009-03-02 19:45:22 UTC (rev 2672)
+++ MOAB/trunk/MBOrientedBoxTreeTool.hpp	2009-03-03 03:11:25 UTC (rev 2673)
@@ -280,6 +280,27 @@
      */
     MBErrorCode stats( MBEntityHandle tree_root_set, std::ostream& stream );
   
+    /**\brief Get tree statistics
+     *
+     * Get summary stats. describing tree
+     * \param set Root of tree for which data is requested
+     * \param total_entities Entities in tree
+     * \param root_volume Total volume of root box
+     * \param tot_node_volume Total volume in all nodes
+     * \param tot_to_root_volume Ratio of total / root volume
+     * \param tree_height Maximum height of tree, from root to leaf
+     * \param node_count Number of nodes in tree
+     * \param num_leaves Number of leaf nodes in tree
+     */
+  MBErrorCode stats( MBEntityHandle set, 
+                     unsigned &entities_in_tree,
+                     double &root_volume,
+                     double &tot_node_volume,
+                     double &tot_to_root_volume,
+                     unsigned &tree_height,
+                     unsigned &node_count,
+                     unsigned &num_leaves);
+  
     /** \brief Implement this and pass instance to preorder_traverse
      * 
      * This interface may be implemented and an instance passed to

Modified: MOAB/trunk/tools/dagmc/DagMC.hpp
===================================================================
--- MOAB/trunk/tools/dagmc/DagMC.hpp	2009-03-02 19:45:22 UTC (rev 2672)
+++ MOAB/trunk/tools/dagmc/DagMC.hpp	2009-03-03 03:11:25 UTC (rev 2673)
@@ -140,6 +140,9 @@
   MBErrorCode getobb(MBEntityHandle volume, double center[3], 
                      double axis1[3], double axis2[3], double axis3[3]);
 
+    // get the root of the obbtree for a given entity
+  MBErrorCode get_root(MBEntityHandle vol_or_surf, MBEntityHandle &root);
+
   int get_entity_id(MBEntityHandle this_ent);
 
     // Get the instance of MOAB used by functions in this file.
@@ -297,5 +300,13 @@
   return entHandles[dimension][index];
 }
 
+    // get the root of the obbtree for a given entity
+inline MBErrorCode DagMC::get_root(MBEntityHandle vol_or_surf, MBEntityHandle &root) 
+{
+  unsigned int index = vol_or_surf - setOffset;
+  root = (index < rootSets.size() ? rootSets[index] : 0);
+  return (root ? MB_SUCCESS : MB_INDEX_OUT_OF_RANGE);
+}
+
 #endif
 

Modified: MOAB/trunk/tools/dagmc/Makefile.am
===================================================================
--- MOAB/trunk/tools/dagmc/Makefile.am	2009-03-02 19:45:22 UTC (rev 2672)
+++ MOAB/trunk/tools/dagmc/Makefile.am	2009-03-03 03:11:25 UTC (rev 2673)
@@ -35,7 +35,7 @@
 cfg_DATA = dagmc.make
 
 TESTS = test_geom 
-check_PROGRAMS = $(TESTS) pt_vol_test
+check_PROGRAMS = $(TESTS) pt_vol_test ray_fire_test
 test_geom_SOURCES = test_geom.cc
 cub2h5m_SOURCES = cub2h5m.cc
 test_geom_LDADD = libdagmc.la $(top_builddir)/libMOAB.la $(CGM_LTFLAGS)  $(CGM_LIBS_LINK)
@@ -44,4 +44,7 @@
 pt_vol_test_SOURCES = pt_vol_test.cc
 pt_vol_test_LDADD = libdagmc.la $(top_builddir)/libMOAB.la $(CGM_LTFLAGS)  $(CGM_LIBS_LINK)
 
+ray_fire_test_SOURCES = ray_fire_test.cc
+ray_fire_test_LDADD = libdagmc.la $(top_builddir)/libMOAB.la $(CGM_LTFLAGS)  $(CGM_LIBS_LINK)
 
+

Added: MOAB/trunk/tools/dagmc/ray_fire_test.cc
===================================================================
--- MOAB/trunk/tools/dagmc/ray_fire_test.cc	                        (rev 0)
+++ MOAB/trunk/tools/dagmc/ray_fire_test.cc	2009-03-03 03:11:25 UTC (rev 2673)
@@ -0,0 +1,169 @@
+#include "MBInterface.hpp"
+#include "MBCore.hpp"
+#include "DagMC.hpp"
+#include "MBTagConventions.hpp"
+
+#include <vector>
+#include <iostream>
+#include <math.h>
+#include <unistd.h>
+#include <limits>
+#include <fcntl.h>
+#include <stdio.h>
+#include <iostream>
+#include <cstdlib>
+#include <cfloat>
+#if !defined(_MSC_VER) && !defined(__MINGW32__)
+#include <sys/resource.h>
+#endif
+#ifdef SOLARIS
+extern "C" int getrusage(int, struct rusage *);
+#ifndef RUSAGE_SELF
+#include </usr/ucbinclude/sys/rusage.h>
+#endif
+#endif
+
+#define CHKERR if (MB_SUCCESS != rval) return rval
+
+void get_time_mem(double &tot_time, double &user_time,
+                  double &sys_time, double &tot_mem);
+
+
+int main( int argc, char* argv[] )
+{
+  MBErrorCode rval;
+
+  if (argc < 3) {
+    std::cerr << "Usage: " << argv[0] << " <mesh_filename> "
+              << " <facet_tol> <#calls> " << std::endl;
+    return 1;
+  }
+  
+  char* filename = argv[1];
+  double tol = atof(argv[2]);
+  int ncalls = atoi(argv[3]);
+  
+  
+  DagMC& dagmc = *DagMC::instance();
+  rval = dagmc.load_file_and_init( filename, strlen(filename), 0, 0, tol);
+  if (MB_SUCCESS != rval) {
+    std::cerr << "Failed to initialize DagMC." << std::endl;
+    return 2;
+  }
+
+  MBEntityHandle vol = dagmc.entity_by_index(3, 1);
+  if (0 == vol) {
+    std::cerr << "Problem getting first volume." << std::endl;
+    return 2;
+  }
+  
+  double ttime1, utime1, stime1, tmem1, ttime2, utime2, stime2, tmem2;
+  get_time_mem(ttime1, utime1, stime1, tmem1);
+
+    // initialize random number generator using ttime1
+  srand((unsigned int) ttime1);
+  double denom = 1.0 / ((double) RAND_MAX);
+  double u, v, w, normal, dist;
+  MBEntityHandle nsurf;
+  
+  for (int i = 0; i < ncalls; i++) {
+    u = denom * rand();
+    v = denom * rand();
+    w = denom * rand();
+    normal = 1.0 / (u*u + v*v + w*w);
+    u *= normal;
+    v *= normal;
+    w *= normal;
+    
+    dagmc.ray_fire(vol, 0, 1, u, v, w, 0.0, 0.0, 0.0, DBL_MAX,
+                   dist, nsurf);
+  }
+  get_time_mem(ttime2, utime2, stime2, tmem1);
+  double timewith = ttime2 - ttime1;
+
+    // now without ray fire call, to subtract out overhead
+  for (int i = 0; i < ncalls; i++) {
+    u = denom * rand();
+    v = denom * rand();
+    w = denom * rand();
+    normal = 1.0 / (u*u + v*v + w*w);
+    u *= normal;
+    v *= normal;
+    w *= normal;
+  }
+  
+  get_time_mem(ttime1, utime1, stime1, tmem2);
+  double timewithout = ttime1 - ttime2;
+  
+  MBEntityHandle root;
+  MBErrorCode result = dagmc.get_root(vol, root);
+  if (MB_SUCCESS != result) {
+    std::cerr << "Trouble getting tree stats." << std::endl;
+    return 2;
+  }
+    
+  unsigned int entities_in_tree, tree_height, node_count, num_leaves;
+  double root_volume, tot_node_volume, tot_to_root_volume;
+  dagmc.obb_tree()->stats(root, entities_in_tree, root_volume, tot_node_volume,
+                          tot_to_root_volume, tree_height, node_count, num_leaves);
+
+  std::cout << "Gross time per call, net time per call, memory = " 
+            << timewith/ncalls << " " << (timewith - timewithout)/ncalls
+            << " " << tmem2 << std::endl;
+  std::cout << "Tree stats: facets, tree_height, num_leaves = " 
+            << entities_in_tree << " " << tree_height << " " << num_leaves << std::endl;
+
+  return 0;
+}
+
+void get_time_mem(double &tot_time, double &user_time,
+                  double &sys_time, double &tot_mem) 
+{
+  struct rusage r_usage;
+  getrusage(RUSAGE_SELF, &r_usage);
+  user_time = (double)r_usage.ru_utime.tv_sec +
+    ((double)r_usage.ru_utime.tv_usec/1.e6);
+  sys_time = (double)r_usage.ru_stime.tv_sec +
+    ((double)r_usage.ru_stime.tv_usec/1.e6);
+  tot_time = user_time + sys_time;
+  tot_mem = 0;
+  if (0 != r_usage.ru_maxrss) {
+    tot_mem = r_usage.ru_idrss; 
+  }
+  else {
+      // this machine doesn't return rss - try going to /proc
+      // print the file name to open
+    char file_str[4096], dum_str[4096];
+    int file_ptr = -1, file_len;
+    file_ptr = open("/proc/self/stat", O_RDONLY);
+    file_len = read(file_ptr, file_str, sizeof(file_str)-1);
+    if (file_len == 0) return;
+    
+    close(file_ptr);
+    file_str[file_len] = '\0';
+      // read the preceeding fields and the ones we really want...
+    int dum_int;
+    unsigned int dum_uint, vm_size, rss;
+    int num_fields = sscanf(file_str, 
+                            "%d " // pid
+                            "%s " // comm
+                            "%c " // state
+                            "%d %d %d %d %d " // ppid, pgrp, session, tty, tpgid
+                            "%u %u %u %u %u " // flags, minflt, cminflt, majflt, cmajflt
+                            "%d %d %d %d %d %d " // utime, stime, cutime, cstime, counter, priority
+                            "%u %u " // timeout, itrealvalue
+                            "%d " // starttime
+                            "%u %u", // vsize, rss
+                            &dum_int, 
+                            dum_str, 
+                            dum_str, 
+                            &dum_int, &dum_int, &dum_int, &dum_int, &dum_int, 
+                            &dum_uint, &dum_uint, &dum_uint, &dum_uint, &dum_uint,
+                            &dum_int, &dum_int, &dum_int, &dum_int, &dum_int, &dum_int, 
+                            &dum_uint, &dum_uint, 
+                            &dum_int,
+                            &vm_size, &rss);
+    if (num_fields == 24)
+      tot_mem = ((double)vm_size);
+  }
+}



More information about the moab-dev mailing list