]> https://gitweb.dealii.org/ - dealii-svn.git/commitdiff
SparsityPattern::block_write/block_read
authorwolf <wolf@0785d39b-7218-0410-832d-ea1e28bc413d>
Mon, 9 Sep 2002 22:41:08 +0000 (22:41 +0000)
committerwolf <wolf@0785d39b-7218-0410-832d-ea1e28bc413d>
Mon, 9 Sep 2002 22:41:08 +0000 (22:41 +0000)
git-svn-id: https://svn.dealii.org/trunk@6377 0785d39b-7218-0410-832d-ea1e28bc413d

deal.II/doc/news/2002/c-3-4.html
deal.II/lac/include/lac/sparsity_pattern.h
deal.II/lac/source/sparsity_pattern.cc
tests/lac/sparsity_pattern.cc

index 7801e6758c4723e6f3db231b00a866d5e6ddae46..bbea7bf7574f77a631f2ca8ce19a4ee5bd529385 100644 (file)
@@ -207,7 +207,16 @@ contributor's names are abbreviated by WB (Wolfgang Bangerth), GK
 
 <ol>
   <li> <p>
-       New:<code class="class">Vector</code> has a function <code
+       New: <code class="class">SparsityPattern</code> now has functions <code
+       class="member">block_write/block_read</code>, allowing to dump the data
+       of this object in to a file in binary format, and later to re-read it
+       without much need for much parsing.
+       <br>
+       (WB 2002/09/09)
+       </p>
+
+  <li> <p>
+       New: <code class="class">Vector</code> has a function <code
        class="member">lp_norm</code>, computing the <i>l<sub>p</sub></i>-norm
        of a vector for arbitrary <i>p</i>.
        <br>
index f32db1fa57d8bed9cda753875fa8ed95a08544c0..e5b3d33a943851012d542cc368d88bf4150b57ba 100644 (file)
@@ -19,6 +19,7 @@
 #include <base/subscriptor.h>
 
 #include <vector>
+#include <iostream>
 
 
 template <typename number> class FullMatrix;
@@ -741,6 +742,54 @@ class SparsityPattern : public Subscriptor
                                      */
     const unsigned int * get_column_numbers () const;
 
+                                    /**
+                                     * Write the data of this object
+                                     * en bloc to a file. This is
+                                     * done in a binary mode, so the
+                                     * output is neither readable by
+                                     * humans nor (probably) by other
+                                     * computers using a different
+                                     * operating system of number
+                                     * format.
+                                     *
+                                     * The purpose of this function
+                                     * is that you can swap out
+                                     * matrices and sparsity pattern
+                                     * if you are short of memory,
+                                     * want to communicate between
+                                     * different programs, or allow
+                                     * objects to be persistent
+                                     * across different runs of the
+                                     * program.
+                                     */
+    void block_write (ostream &out) const;
+
+                                    /**
+                                     * Read data that has previously
+                                     * been written by
+                                     * @p{block_write} en block from
+                                     * a file. This is done using the
+                                     * inverse operations to the
+                                     * above function, so it is
+                                     * reasonably fast because the
+                                     * bitstream is not interpreted
+                                     * except for a few numbers up
+                                     * front.
+                                     *
+                                     * The object is resized on this
+                                     * operation, and all previous
+                                     * contents are lost.
+                                     *
+                                     * A primitive form of error
+                                     * checking is performed which
+                                     * will recognize the bluntest
+                                     * attempts to interpret some
+                                     * data as a vector stored
+                                     * bitwise to a file, but not
+                                     * more.
+                                     */
+    void block_read (std::istream &in);
+    
                                     /**
                                      * Determine an estimate for the
                                      * memory consumption (in bytes)
index 37911fbbe65fec066a0ada24bbfdb3f63610d4e1..fb401a7684ebe46d533cb4905c4cfb32caa67dd5 100644 (file)
@@ -775,6 +775,74 @@ SparsityPattern::bandwidth () const
 
 
 
+void
+SparsityPattern::block_write (ostream &out) const 
+{
+  AssertThrow (out, ExcIO());
+
+                                   // first the simple objects,
+                                   // bracketed in [...]
+  out << '[' << max_dim << ' '
+      << rows << ' '
+      << max_vec_len << ' '
+      << max_row_length << ' '
+      << compressed << "][";
+                                   // then write out real data
+  out.write (reinterpret_cast<const char*>(&rowstart[0]),
+            reinterpret_cast<const char*>(&rowstart[max_dim])
+            - reinterpret_cast<const char*>(&rowstart[0]));
+  out << "][";
+  out.write (reinterpret_cast<const char*>(&colnums[0]),
+            reinterpret_cast<const char*>(&colnums[max_vec_len])
+            - reinterpret_cast<const char*>(&colnums[0]));
+  out << ']';
+  
+  AssertThrow (out, ExcIO());
+};
+
+
+
+void
+SparsityPattern::block_read (istream &in)
+{
+  AssertThrow (in, ExcIO());
+
+  char c;
+
+                                   // first read in simple data
+  in >> c;
+  AssertThrow (c == '[', ExcIO());
+  in >> max_dim >> rows >> max_vec_len >> max_row_length >> compressed;
+
+  in >> c;
+  AssertThrow (c == ']', ExcIO());
+  in >> c;
+  AssertThrow (c == '[', ExcIO());
+
+                                   // reallocate space
+  delete[] rowstart;
+  delete[] colnums;
+
+  rowstart = new unsigned int[max_dim];
+  colnums  = new unsigned int[max_vec_len];
+  
+                                   // then read data
+  in.read (reinterpret_cast<char*>(&rowstart[0]),
+           reinterpret_cast<char*>(&rowstart[max_dim])
+           - reinterpret_cast<char*>(&rowstart[0]));
+  in >> c;
+  AssertThrow (c == ']', ExcIO());
+  in >> c;
+  AssertThrow (c == '[', ExcIO());
+  in.read (reinterpret_cast<char*>(&colnums[0]),
+           reinterpret_cast<char*>(&colnums[max_vec_len])
+           - reinterpret_cast<char*>(&colnums[0]));
+  in >> c;
+  AssertThrow (c == ']', ExcIO());
+};
+
+
+
 unsigned int
 SparsityPattern::memory_consumption () const
 {
index bc688c6083c4156f30bf9cea99809473c8f73e44..567ec62038a3b399a94b8b544106f7835118d8d7 100644 (file)
@@ -19,6 +19,7 @@
 #include <iostream>
 #include <list>
 #include <set>
+#include <cstdio>
 
 
 int
@@ -91,9 +92,13 @@ main ()
                                   // now check for equivalence of sp3 and sp4
   for (unsigned int row=0; row<sp3.n_rows(); ++row)
     {
-      const unsigned int *sp3_p=sp3.get_column_numbers()+sp3.get_rowstart_indices()[row];
-      const unsigned int *sp4_p=sp4.get_column_numbers()+sp4.get_rowstart_indices()[row];
-      for (; sp3_p != sp3.get_column_numbers()+sp3.get_rowstart_indices()[row+1]; ++sp3_p, ++sp4_p)
+      const unsigned int
+        *sp3_p=sp3.get_column_numbers()+sp3.get_rowstart_indices()[row];
+      const unsigned int
+        *sp4_p=sp4.get_column_numbers()+sp4.get_rowstart_indices()[row];
+      for (; sp3_p != (sp3.get_column_numbers() +
+                       sp3.get_rowstart_indices()[row+1]);
+           ++sp3_p, ++sp4_p)
        Assert (*sp3_p == *sp4_p, ExcInternalError());
     };
 
@@ -121,6 +126,35 @@ main ()
                    std::make_pair(row,col),
                    ExcInternalError());
     };
+
+  
+                                   // check block_write/block_read by
+                                   // dumping a sparsity pattern and
+                                   // checking whether the
+                                   // read-back-in pattern is the same
+  std::ofstream tmp_write("sparsity_pattern.tmp");
+  sp3.block_write (tmp_write);
+  tmp_write.close ();
+  
+  std::ifstream tmp_read("sparsity_pattern.tmp");
+  sp4.block_read (tmp_read);
+  tmp_read.close ();
+
+                                   // delete temporary file
+  remove ("sparsity_pattern.tmp");
+  
+                                  // now check for equivalence of sp3 and sp4
+  for (unsigned int row=0; row<sp3.n_rows(); ++row)
+    {
+      const unsigned int
+        *sp3_p=sp3.get_column_numbers()+sp3.get_rowstart_indices()[row];
+      const unsigned int
+        *sp4_p=sp4.get_column_numbers()+sp4.get_rowstart_indices()[row];
+      for (; sp3_p != (sp3.get_column_numbers() +
+                       sp3.get_rowstart_indices()[row+1]);
+           ++sp3_p, ++sp4_p)
+       Assert (*sp3_p == *sp4_p, ExcInternalError());
+    };
 };
 
   

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.