PEXSI
 All Classes Namespaces Files Functions Variables Typedefs Pages
sparse_matrix_impl.hpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 2012 The Regents of the University of California,
3  through Lawrence Berkeley National Laboratory.
4 
5  Author: Mathias Jacquelin and Lin Lin
6 
7  This file is part of PEXSI. All rights reserved.
8 
9  Redistribution and use in source and binary forms, with or without
10  modification, are permitted provided that the following conditions are met:
11 
12  (1) Redistributions of source code must retain the above copyright notice, this
13  list of conditions and the following disclaimer.
14  (2) Redistributions in binary form must reproduce the above copyright notice,
15  this list of conditions and the following disclaimer in the documentation
16  and/or other materials provided with the distribution.
17  (3) Neither the name of the University of California, Lawrence Berkeley
18  National Laboratory, U.S. Dept. of Energy nor the names of its contributors may
19  be used to endorse or promote products derived from this software without
20  specific prior written permission.
21 
22  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
23  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
26  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
29  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 
33  You are under no obligation whatsoever to provide any bug fixes, patches, or
34  upgrades to the features, functionality or performance of the source code
35  ("Enhancements") to anyone; however, if you choose to make your Enhancements
36  available either publicly, or directly to Lawrence Berkeley National
37  Laboratory, without imposing a separate written license agreement for such
38  Enhancements, then you hereby grant the following license: a non-exclusive,
39  royalty-free perpetual license to install, use, modify, prepare derivative
40  works, incorporate into other computer software, distribute, and sublicense
41  such enhancements or derivative works thereof, in binary and source code form.
42 */
46 #ifndef _PEXSI_SPARSE_MATRIX_IMPL_HPP_
47 #define _PEXSI_SPARSE_MATRIX_IMPL_HPP_
48 
49 #include <iostream>
50 #include "pexsi/environment.hpp"
51 
52 using std::ifstream;
53 using std::ofstream;
54 
55 #include "pexsi/utility.hpp"
56 
57 
58 namespace PEXSI{
59 
60  struct IndexComparator {
61  Int * lookup;
62  IndexComparator(Int * v):lookup(v){}
63  bool operator() (int i,int j) { return (lookup[i]<lookup[j]);}
64  };
65 
66 
67 
68 template <typename F> inline void DistSparseMatrix<F>::SortIndices()
69 {
70 
71 #ifndef _RELEASE_
72  PushCallStack("DistSparseMatrix<F>::SortIndices");
73 #endif
74 
75  for(Int col = 0; col<colptrLocal.m()-1;++col){
76  Int colbeg = colptrLocal[col]-1;
77  Int colend = colptrLocal[col+1]-1;
78 
79  Int numRow = colend - colbeg;
80 
81  std::vector<Int> rowsPerm(numRow);
82  std::vector<Int> rowsSorted(numRow);
83  std::vector<F> valsSorted(numRow);
84  Int * rows = &rowindLocal[colbeg];
85  F * vals = &nzvalLocal[colbeg];
86 
87  for(Int i = 0; i<rowsPerm.size(); ++i){ rowsPerm[i] = i;}
88  IndexComparator cmp(rows);
89  //sort the row indices
90  std::sort(rowsPerm.begin(),rowsPerm.end(),cmp);
91 
92  for(Int i = 0; i<numRow; ++i){ rowsSorted[i] = rows[rowsPerm[i]]; }
93  std::copy(rowsSorted.begin(),rowsSorted.end(),rows);
94 
95  for(Int i = 0; i<numRow; ++i){ valsSorted[i] = vals[rowsPerm[i]]; }
96  std::copy(valsSorted.begin(),valsSorted.end(),vals);
97  }
98 
99 
100 #ifndef _RELEASE_
101  PopCallStack();
102 #endif
103 
104 
105 }
106 
107 template <class F> inline LongInt DistSparseMatrix<F>::Nnz ( )
108 {
109 #ifndef _RELEASE_
110  PushCallStack("DistSparseMatrix<F>::Nnz");
111 #endif
112  LongInt nnzLocalLong = nnzLocal;
113  LongInt nnz;
114 
115  MPI_Allreduce( &nnzLocalLong, &nnz, 1, MPI_LONG_LONG, MPI_SUM,
116  comm );
117 
118 #ifndef _RELEASE_
119  PopCallStack();
120 #endif
121 
122  return nnz;
123 } // ----- end of method DistSparseMatrix<F>::Nnz -----
124 
125 template <typename F> inline void DistSparseMatrix<F>::Clear()
126 {
127 
128 #ifndef _RELEASE_
129  PushCallStack("DistSparseMatrix<F>::Clear");
130 #endif
131 
132  size = 0;
133  nnzLocal = 0;
134  nnz = 0;
135  colptrLocal.Clear();
136  rowindLocal.Clear();
137  nzvalLocal.Clear();
138 
139 
140 
141 
142 
143 #ifndef _RELEASE_
144  PopCallStack();
145 #endif
146 
147 
148 }
149 
150 
151 
152 
153 
154 } // namespace PEXSI
155 
156 #endif // _PEXSI_SPARSE_MATRIX_IMPL_HPP_
Environmental variables.
Definition: sparse_matrix_impl.hpp:60
void SortIndices()
Locally sorts the row indices within every column.
Definition: sparse_matrix_impl.hpp:68
Various utility subroutines.
LongInt Nnz()
Compute the total number of nonzeros through MPI_Allreduce.
Definition: sparse_matrix_impl.hpp:107
void Clear()
Clear all memory (The matrix becomes a 0-by-0 matrix)
Definition: sparse_matrix_impl.hpp:125