[cig-commits] commit: Use a priority queue instead of sorting everything all the time.

Mercurial hg at geodynamics.org
Fri Mar 30 06:48:36 PDT 2012


changeset:   138:4746e93fd167
user:        Walter Landry <wlandry at caltech.edu>
date:        Thu Mar 29 20:11:29 2012 -0700
files:       compute_v_on_interface/Valid.hxx compute_v_on_interface/compute_dv_dtt.cxx
description:
Use a priority queue instead of sorting everything all the time.
Reduces run time by 30%.


diff -r c42bf414a732 -r 4746e93fd167 compute_v_on_interface/Valid.hxx
--- a/compute_v_on_interface/Valid.hxx	Thu Mar 29 20:08:33 2012 -0700
+++ b/compute_v_on_interface/Valid.hxx	Thu Mar 29 20:11:29 2012 -0700
@@ -20,6 +20,10 @@ public:
   {
     return r<v.r;
   }
+  bool operator>(const Valid &v) const
+  {
+    return r>v.r;
+  }
 };
 
 
diff -r c42bf414a732 -r 4746e93fd167 compute_v_on_interface/compute_dv_dtt.cxx
--- a/compute_v_on_interface/compute_dv_dtt.cxx	Thu Mar 29 20:08:33 2012 -0700
+++ b/compute_v_on_interface/compute_dv_dtt.cxx	Thu Mar 29 20:11:29 2012 -0700
@@ -9,7 +9,7 @@
 #include "compute_values.hxx"
 #include <tuple>
 #include <algorithm>
-#include <iostream>
+#include <queue>
 
 void compute_dv_dtt(const double zx[Nx+1][Ny],
                     const double zy[Nx][Ny+1],
@@ -230,24 +230,32 @@ void compute_dv_dtt(const double zx[Nx+1
               }
           }
 
-      /* Sort valid vectors */
-
-      std::sort(dd_valid(0,0).begin(),dd_valid(0,0).end());
-      std::sort(dd_valid(0,1).begin(),dd_valid(0,1).end());
-      std::sort(dd_valid(1,1).begin(),dd_valid(1,1).end());
-      std::sort(d_valid(0).begin(),d_valid(0).end());
-      std::sort(d_valid(1).begin(),d_valid(1).end());
-      std::sort(valid.begin(),valid.end());
+      /* Put the valid vectors into a priority queue so that we can
+         take the sorted versions out as needed.  A bit messier than
+         just sorting the vector, but also significantly faster. */
 
       FTensor::Tensor3_christof<bool,2,2> is_set(false,false,false,false,
                                                  false,false);
 
+      typedef std::priority_queue<Valid, std::vector<Valid>, std::greater<Valid> > mpq;
+      mpq ddp_valid[]={{dd_valid(0,0).begin(),dd_valid(0,0).end()},
+                       {dd_valid(0,1).begin(),dd_valid(0,1).end()},
+                       {dd_valid(1,1).begin(),dd_valid(1,1).end()}};
+
+      mpq dp_valid[]={{d_valid(0).begin(),d_valid(0).end()},
+                      {d_valid(1).begin(),d_valid(1).end()}};
+      std::priority_queue<Valid, std::vector<Valid>, std::greater<Valid> >
+        p_valid(valid.begin(),valid.end());
+
       /* Compute everything */
       /* v */
-      
+
       std::vector<Valid> values[2];
-      for(auto &V: valid)
+      for(auto V=p_valid.top(); !p_valid.empty(); p_valid.pop(), V=p_valid.top())
         {
+          if(values[0].size()==6 && values[1].size()==6)
+            break;
+
           if(V.sign==0)
             continue;
           const int pm=(V.sign==-1 ? 0 : 1);
@@ -298,8 +306,12 @@ void compute_dv_dtt(const double zx[Nx+1
         {
           /* First derivative dv */
           std::vector<Valid> derivs[2];
-          for(auto &V: d_valid(d0))
+          for(auto V=dp_valid[d0].top(); !dp_valid[d0].empty();
+              dp_valid[d0].pop(), V=dp_valid[d0].top())
             {
+              if(derivs[0].size()==3 && derivs[1].size()==3)
+                break;
+
               if(V.sign==0)
                 continue;
               if(V.valid)
@@ -334,8 +346,12 @@ void compute_dv_dtt(const double zx[Nx+1
           /* Second derivative ddv */
           for(int d1=d0;d1<2;++d1)
             {
-              for(auto &V: dd_valid(d0,d1))
+              for(auto V=ddp_valid[2*d0+d1].top(); !ddp_valid[2*d0+d1].empty();
+                  ddp_valid[2*d0+d1].pop(), V=ddp_valid[2*d0+d1].top())
                 {
+                  if(is_set(0,d0,d1) && is_set(1,d0,d1))
+                    break;
+
                   for(int pm=0;pm<2;++pm)
                     {
                       if(V.sign==(pm==0 ? -1 : 1) && V.valid && !is_set(pm,d0,d1))



More information about the CIG-COMMITS mailing list