00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #ifndef __deal2__chunk_sparsity_pattern_h
00013 #define __deal2__chunk_sparsity_pattern_h
00014
00015
00016 #include <deal.II/base/config.h>
00017 #include <deal.II/base/exceptions.h>
00018 #include <deal.II/base/subscriptor.h>
00019 #include <deal.II/base/vector_slice.h>
00020
00021 #include <deal.II/lac/sparsity_pattern.h>
00022
00023 #include <vector>
00024 #include <iostream>
00025
00026 DEAL_II_NAMESPACE_OPEN
00027
00028
00029 template <typename> class ChunkSparseMatrix;
00030
00031
00046 class ChunkSparsityPattern : public Subscriptor
00047 {
00048 public:
00049
00074 static const unsigned int invalid_entry = SparsityPattern::invalid_entry;
00075
00086 ChunkSparsityPattern ();
00087
00119 ChunkSparsityPattern (const ChunkSparsityPattern &);
00120
00136 ChunkSparsityPattern (const unsigned int m,
00137 const unsigned int n,
00138 const unsigned int max_chunks_per_row,
00139 const unsigned int chunk_size,
00140 const bool optimize_diagonal = true);
00141
00160 ChunkSparsityPattern (const unsigned int m,
00161 const unsigned int n,
00162 const std::vector<unsigned int>& row_lengths,
00163 const unsigned int chunk_size,
00164 const bool optimize_diagonal = true);
00165
00179 ChunkSparsityPattern (const unsigned int n,
00180 const unsigned int max_per_row,
00181 const unsigned int chunk_size);
00182
00197 ChunkSparsityPattern (const unsigned int m,
00198 const std::vector<unsigned int>& row_lengths,
00199 const unsigned int chunk_size,
00200 const bool optimize_diagonal = true);
00201
00205 ~ChunkSparsityPattern ();
00206
00215 ChunkSparsityPattern & operator = (const ChunkSparsityPattern &);
00216
00228 void reinit (const unsigned int m,
00229 const unsigned int n,
00230 const unsigned int max_per_row,
00231 const unsigned int chunk_size,
00232 const bool optimize_diagonal = true);
00233
00259 void reinit (const unsigned int m,
00260 const unsigned int n,
00261 const std::vector<unsigned int> &row_lengths,
00262 const unsigned int chunk_size,
00263 const bool optimize_diagonal = true);
00264
00269 void reinit (const unsigned int m,
00270 const unsigned int n,
00271 const VectorSlice<const std::vector<unsigned int> > &row_lengths,
00272 const unsigned int chunk_size,
00273 const bool optimize_diagonal = true);
00274
00295 void compress ();
00296
00435 template <typename ForwardIterator>
00436 void copy_from (const unsigned int n_rows,
00437 const unsigned int n_cols,
00438 const ForwardIterator begin,
00439 const ForwardIterator end,
00440 const unsigned int chunk_size,
00441 const bool optimize_diagonal = true);
00442
00452 template <typename SparsityType>
00453 void copy_from (const SparsityType &csp,
00454 const unsigned int chunk_size,
00455 const bool optimize_diagonal = true);
00456
00468 template <typename number>
00469 void copy_from (const FullMatrix<number> &matrix,
00470 const unsigned int chunk_size,
00471 const bool optimize_diagonal = true);
00472
00479 bool empty () const;
00480
00486 unsigned int get_chunk_size () const;
00487
00496 unsigned int max_entries_per_row () const;
00497
00506 void add (const unsigned int i,
00507 const unsigned int j);
00508
00520 void symmetrize ();
00521
00527 inline unsigned int n_rows () const;
00528
00534 inline unsigned int n_cols () const;
00535
00540 bool exists (const unsigned int i,
00541 const unsigned int j) const;
00542
00546 unsigned int row_length (const unsigned int row) const;
00547
00558 unsigned int bandwidth () const;
00559
00572 unsigned int n_nonzero_elements () const;
00573
00578 bool is_compressed () const;
00579
00614 bool optimize_diagonal () const;
00615
00639 bool stores_only_added_elements () const;
00640
00661 void block_write (std::ostream &out) const;
00662
00686 void block_read (std::istream &in);
00687
00697 void print (std::ostream &out) const;
00698
00723 void print_gnuplot (std::ostream &out) const;
00724
00731 std::size_t memory_consumption () const;
00732
00738 DeclException1 (ExcInvalidNumber,
00739 int,
00740 << "The provided number is invalid here: " << arg1);
00744 DeclException2 (ExcInvalidIndex,
00745 int, int,
00746 << "The given index " << arg1
00747 << " should be less than " << arg2 << ".");
00751 DeclException2 (ExcNotEnoughSpace,
00752 int, int,
00753 << "Upon entering a new entry to row " << arg1
00754 << ": there was no free entry any more. " << std::endl
00755 << "(Maximum number of entries for this row: "
00756 << arg2 << "; maybe the matrix is already compressed?)");
00760 DeclException0 (ExcNotCompressed);
00764 DeclException0 (ExcMatrixIsCompressed);
00768 DeclException0 (ExcEmptyObject);
00772 DeclException0 (ExcInvalidConstructorCall);
00782 DeclException0 (ExcDiagonalNotOptimized);
00786 DeclException2 (ExcIteratorRange,
00787 int, int,
00788 << "The iterators denote a range of " << arg1
00789 << " elements, but the given number of rows was " << arg2);
00793 DeclException0 (ExcMETISNotInstalled);
00797 DeclException1 (ExcInvalidNumberOfPartitions,
00798 int,
00799 << "The number of partitions you gave is " << arg1
00800 << ", but must be greater than zero.");
00804 DeclException2 (ExcInvalidArraySize,
00805 int, int,
00806 << "The array has size " << arg1 << " but should have size "
00807 << arg2);
00809 private:
00814 unsigned int rows;
00815
00820 unsigned int cols;
00821
00825 unsigned int chunk_size;
00826
00833 SparsityPattern sparsity_pattern;
00834
00839 template <typename> friend class ChunkSparseMatrix;
00840 };
00841
00842
00844
00845
00846 #ifndef DOXYGEN
00847
00848
00849 inline
00850 unsigned int
00851 ChunkSparsityPattern::n_rows () const
00852 {
00853 return rows;
00854 }
00855
00856
00857 inline
00858 unsigned int
00859 ChunkSparsityPattern::n_cols () const
00860 {
00861 return cols;
00862 }
00863
00864
00865
00866 inline
00867 unsigned int
00868 ChunkSparsityPattern::get_chunk_size () const
00869 {
00870 return chunk_size;
00871 }
00872
00873
00874
00875 inline
00876 bool
00877 ChunkSparsityPattern::is_compressed () const
00878 {
00879 return sparsity_pattern.compressed;
00880 }
00881
00882
00883 inline
00884 bool
00885 ChunkSparsityPattern::optimize_diagonal () const
00886 {
00887 return sparsity_pattern.diagonal_optimized;
00888 }
00889
00890
00891
00892 template <typename ForwardIterator>
00893 void
00894 ChunkSparsityPattern::copy_from (const unsigned int n_rows,
00895 const unsigned int n_cols,
00896 const ForwardIterator begin,
00897 const ForwardIterator end,
00898 const unsigned int chunk_size,
00899 const bool optimize_diag)
00900 {
00901 Assert (static_cast<unsigned int>(std::distance (begin, end)) == n_rows,
00902 ExcIteratorRange (std::distance (begin, end), n_rows));
00903
00904
00905
00906
00907
00908
00909
00910
00911
00912
00913
00914 const bool is_square = optimize_diag && (n_rows == n_cols);
00915 std::vector<unsigned int> row_lengths;
00916 row_lengths.reserve(n_rows);
00917 for (ForwardIterator i=begin; i!=end; ++i)
00918 row_lengths.push_back (std::distance (i->begin(), i->end())
00919 +
00920 (is_square ? 1 : 0));
00921 reinit (n_rows, n_cols, row_lengths, chunk_size, is_square);
00922
00923
00924
00925 unsigned int row = 0;
00926 typedef typename std::iterator_traits<ForwardIterator>::value_type::const_iterator inner_iterator;
00927 for (ForwardIterator i=begin; i!=end; ++i, ++row)
00928 {
00929 const inner_iterator end_of_row = i->end();
00930 for (inner_iterator j=i->begin(); j!=end_of_row; ++j)
00931 {
00932 const unsigned int col
00933 = internal::SparsityPatternTools::get_column_index_from_iterator(*j);
00934 Assert (col < n_cols, ExcInvalidIndex(col,n_cols));
00935
00936 add (row, col);
00937 }
00938 }
00939
00940
00941
00942
00943 compress ();
00944 }
00945
00946
00947 #endif // DOXYGEN
00948
00949 DEAL_II_NAMESPACE_CLOSE
00950
00951 #endif
00952