[mathicgb] 147/393: Simplified and improved performance for sorting columns and constructing column permutation map in F4-26.

Doug Torrance dtorrance-guest at moszumanska.debian.org
Fri Apr 3 15:58:51 UTC 2015


This is an automated email from the git hooks/post-receive script.

dtorrance-guest pushed a commit to branch upstream
in repository mathicgb.

commit 32eee99f6854a2eadf02d35e1adbd961d1eb4908
Author: Bjarke Hammersholt Roune <bjarkehr.code at gmail.com>
Date:   Mon Feb 4 19:21:22 2013 +0100

    Simplified and improved performance for sorting columns and constructing column permutation map in F4-26.
---
 src/mathicgb/F4MatrixBuilder2.cpp   | 94 ++++++++++---------------------------
 src/mathicgb/FixedSizeMonomialMap.h |  9 +++-
 2 files changed, 32 insertions(+), 71 deletions(-)

diff --git a/src/mathicgb/F4MatrixBuilder2.cpp b/src/mathicgb/F4MatrixBuilder2.cpp
index 948f395..379d516 100755
--- a/src/mathicgb/F4MatrixBuilder2.cpp
+++ b/src/mathicgb/F4MatrixBuilder2.cpp
@@ -285,76 +285,37 @@ namespace {
     ) {
       const auto& ring = map.ring();
 
-      // record which indices go left and which go right
+      // Sort columns by monomial while keeping track of original index.
+      MonomialMap<ColIndex>::Reader reader(map);
+      typedef std::pair<ColIndex, ConstMonomial> IndexMono;
+      std::vector<IndexMono> columns(reader.begin(), reader.end());
+      const auto cmp = [&ring](const IndexMono& a, const IndexMono b) {
+        return ring.monomialLT(b.second, a.second);
+      };
+      tbb::parallel_sort(columns.begin(), columns.end(), cmp);
+
+      // Copy monomials and construct projection mapping.
       MATHICGB_ASSERT
         (isColToLeft.size() <= std::numeric_limits<ColIndex>::max());
       ColIndex colCount = static_cast<ColIndex>(isColToLeft.size());
-      ColIndex leftColCount = 0;
-      ColIndex rightColCount = 0;
+      mProject.resize(isColToLeft.size());
       for (size_t i = 0; i < colCount; ++i) {
-        if (isColToLeft[i]) {
-          Projected p = {leftColCount, true};
-          mProject.push_back(p);
-          ++leftColCount;
+        const auto indexMono = columns[i];
+        monomial mono = ring.allocMonomial();
+        ring.monomialCopy(indexMono.second, mono);
+
+        auto& projected = mProject[indexMono.first];
+        projected.left = isColToLeft[indexMono.first];
+        if (projected.left) {
+          projected.index = static_cast<ColIndex>(mLeftMonomials.size());
+          mLeftMonomials.push_back(mono);
         } else {
-          Projected p = {rightColCount, false};
-          mProject.push_back(p);
-          ++rightColCount;
-        }
-      }
-      MATHICGB_ASSERT(leftColCount + rightColCount == colCount);
-
-      std::vector<std::pair<monomial, ColIndex>> leftColumns(leftColCount);
-      std::vector<std::pair<monomial, ColIndex>> rightColumns(rightColCount);
-      {
-        mLeftMonomials.resize(leftColCount);
-        mRightMonomials.resize(rightColCount);
-        MonomialMap<ColIndex>::Reader reader(map);
-        const auto end = reader.end();
-        for (auto it = reader.begin(); it != end; ++it) {
-          const auto p = *it;
-          monomial copy = ring.allocMonomial();
-          ring.monomialCopy(p.second, copy);
-          const auto index = p.first;
-          auto translated = mProject[index];
-          if (translated.left) {
-            leftColumns[translated.index] = std::make_pair(copy, translated.index);
-          } else {
-            rightColumns[translated.index] = std::make_pair(copy, translated.index);
-          }
+          projected.index = static_cast<ColIndex>(mRightMonomials.size());
+          mRightMonomials.push_back(mono);
         }
       }
-      MATHICGB_ASSERT(leftColumns.size() + rightColumns.size() == colCount);
-
-      const auto sortColumns = [&](
-        std::vector<std::pair<monomial, ColIndex>>& columns,
-        std::vector<monomial>& sortedMonomials,
-        std::vector<ColIndex>& colPermutation
-      ) {
-        std::sort(columns.begin(), columns.end(), ColumnComparer(ring));
-        const auto colCount = columns.size();
-        colPermutation.resize(colCount);
-        sortedMonomials.resize(colCount);
-        for (ColIndex col = 0; col < colCount; ++col) {
-          sortedMonomials[col] = columns[col].first;
-          colPermutation[columns[col].second] = col;
-        }
-      };
-      std::vector<ColIndex> leftPermutation;
-      std::vector<ColIndex> rightPermutation;
-      tbb::parallel_for(0, 2, 1, [&](int i) {
-        if (i == 0)
-          sortColumns(leftColumns, mLeftMonomials, leftPermutation);
-        else
-          sortColumns(rightColumns, mRightMonomials, rightPermutation);
-      });
-
-      for (size_t i = 0; i < mProject.size(); ++i) {
-        if (mProject[i].left)
-          mProject[i].index = leftPermutation[mProject[i].index];
-        else
-          mProject[i].index = rightPermutation[mProject[i].index];
-      }
+      MATHICGB_ASSERT
+        (mLeftMonomials.size() + mRightMonomials.size() == isColToLeft.size());
     }
 
     struct Projected {
@@ -392,8 +353,6 @@ void F4MatrixBuilder2::buildMatrixAndClear(QuadMatrix& quadMatrix) {
     return;
   }
 
-  // todo: prefer sparse/old reducers among the inputs.
-
   // Process pending rows until we are done. Note that the methods
   // we are calling here can add more pending items.
 
@@ -447,12 +406,7 @@ void F4MatrixBuilder2::buildMatrixAndClear(QuadMatrix& quadMatrix) {
       ring().freeMonomial(it->desiredLead);
   mTodo.clear();
 
-  // ***** Sort columns separately left/right and construct index translation.
-  // input: mIsColumnToLeft, map
-  // output: quadMatrix.left/rightColumnMonomials, translate
-
   LeftRightProjection projection(mIsColumnToLeft, mMap);
-
   quadMatrix.leftColumnMonomials = projection.moveLeftMonomials();
   quadMatrix.rightColumnMonomials = projection.moveRightMonomials();
 
diff --git a/src/mathicgb/FixedSizeMonomialMap.h b/src/mathicgb/FixedSizeMonomialMap.h
index 8547a32..cef61f5 100755
--- a/src/mathicgb/FixedSizeMonomialMap.h
+++ b/src/mathicgb/FixedSizeMonomialMap.h
@@ -294,6 +294,13 @@ private:
 public:
   class const_iterator {
   public:
+    typedef std::forward_iterator_tag iterator_category;
+    typedef std::pair<mapped_type, ConstMonomial> value_type;
+    typedef ptrdiff_t difference_type;
+	typedef size_t distance_type;
+	typedef value_type* pointer;
+	typedef value_type& reference;
+
     const_iterator(): mNode(0), mBucket(0), mBucketsEnd(0) {}
 
     const_iterator& operator++() {
@@ -316,7 +323,7 @@ public:
       return !(*this == it);
     }
 
-    const std::pair<mapped_type, ConstMonomial> operator*() const {
+    const value_type operator*() const {
       MATHICGB_ASSERT(mNode != 0);
       return std::make_pair(mNode->value, mNode->mono);
     }

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/mathicgb.git



More information about the debian-science-commits mailing list