← Back to team overview

dolfin team mailing list archive

[noreply@xxxxxxxxxxxxx: [Branch ~dolfin-core/dolfin/main] Rev 5309: Simplify STLMatrix and fix small bug.]

 

Did you compare the speed of find vs linear search? I think I remember
that the simple linear search was faster in some test.

--
Anders
--- Begin Message ---
------------------------------------------------------------
revno: 5309
committer: Garth N. Wells <gnw20@xxxxxxxxx>
branch nick: dolfin-all
timestamp: Mon 2010-11-22 13:47:34 +0000
message:
  Simplify STLMatrix and fix small bug.
modified:
  dolfin/la/STLMatrix.cpp
  dolfin/la/STLMatrix.h


--
lp:dolfin
https://code.launchpad.net/~dolfin-core/dolfin/main

Your team DOLFIN Core Team is subscribed to branch lp:dolfin.
To unsubscribe from this branch go to https://code.launchpad.net/~dolfin-core/dolfin/main/+edit-subscription
=== modified file 'dolfin/la/STLMatrix.cpp'
--- dolfin/la/STLMatrix.cpp	2010-11-08 12:14:42 +0000
+++ dolfin/la/STLMatrix.cpp	2010-11-22 13:47:34 +0000
@@ -8,8 +8,9 @@
 // First added:  2007-01-17
 // Last changed: 2010-11-08
 
+#include <algorithm>
+#include <iomanip>
 #include <sstream>
-#include <iomanip>
 
 #include "STLFactory.h"
 #include "STLMatrix.h"
@@ -17,6 +18,37 @@
 using namespace dolfin;
 
 //-----------------------------------------------------------------------------
+void STLMatrix::add(const double* block, uint m, const uint* rows, uint n, const uint* cols)
+{
+  // Perform a simple linear search along each column. Otherwise,
+  // append the value (calling push_back).
+
+  // Iterate over rows
+  uint pos = 0;
+  for (uint i = 0; i < m; i++)
+  {
+    const uint I = rows[i];
+    std::vector<uint>& rcols = this->cols[I];
+    std::vector<double>& rvals = this->vals[I];
+
+    // Iterate over columns
+    for (uint j = 0; j < n; j++)
+    {
+      const uint J = cols[j];
+
+      // Check if column entry exists and insert
+      const std::vector<uint>::const_iterator column = std::find(rcols.begin(), rcols.end(), J);
+      if (column != rcols.end())
+        rvals[column - rcols.begin()] += block[pos++];
+      else
+      {
+        rcols.push_back(J);
+        rvals.push_back(block[pos++]);
+      }
+    }
+  }
+}
+//-----------------------------------------------------------------------------
 std::string STLMatrix::str(bool verbose) const
 {
   std::stringstream s;

=== modified file 'dolfin/la/STLMatrix.h'
--- dolfin/la/STLMatrix.h	2010-11-08 22:42:14 +0000
+++ dolfin/la/STLMatrix.h	2010-11-22 13:47:34 +0000
@@ -83,7 +83,7 @@
       cols.clear();
       vals.clear();
       cols.resize(M);
-      vals.resize(N);
+      vals.resize(M);
       dims[0] = M;
       dims[1] = N;
     }
@@ -97,45 +97,7 @@
     { dolfin_not_implemented(); }
 
     /// Add block of values
-    virtual void add(const double* block, uint m, const uint* rows, uint n, const uint* cols)
-    {
-      // Perform a simple linear search along each column. Otherwise,
-      // append the value (calling push_back).
-
-      // Iterate over rows
-      uint pos = 0;
-      for (uint i = 0; i < m; i++)
-      {
-        const uint I = rows[i];
-        std::vector<uint>& rcols = this->cols[I];
-        std::vector<double>& rvals = this->vals[I];
-
-        // Iterate over columns
-        for (uint j = 0; j < n; j++)
-        {
-          const uint J = cols[j];
-
-          // Try to find column
-          bool found = false;
-          for (uint k = 0; k < rcols.size(); k++)
-          {
-            if (rcols[k] == J)
-            {
-              rvals[k] += block[pos++];
-              found = true;
-              break;
-            }
-          }
-
-          // Append if not found
-          if (!found)
-          {
-            rcols.push_back(J);
-            rvals.push_back(block[pos++]);
-          }
-        }
-      }
-    }
+    virtual void add(const double* block, uint m, const uint* rows, uint n, const uint* cols);
 
     /// Add multiple of given matrix (AXPY operation)
     virtual void axpy(double a, const GenericMatrix& A, bool same_nonzero_pattern)


--- End Message ---

Follow ups