SCIRun  5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ParallelLinearAlgebra.h
Go to the documentation of this file.
1 /*
2  For more information, please see: http://software.sci.utah.edu
3 
4  The MIT License
5 
6  Copyright (c) 2009 Scientific Computing and Imaging Institute,
7  University of Utah.
8 
9 
10  Permission is hereby granted, free of charge, to any person obtaining a
11  copy of this software and associated documentation files (the "Software"),
12  to deal in the Software without restriction, including without limitation
13  the rights to use, copy, modify, merge, publish, distribute, sublicense,
14  and/or sell copies of the Software, and to permit persons to whom the
15  Software is furnished to do so, subject to the following conditions:
16 
17  The above copyright notice and this permission notice shall be included
18  in all copies or substantial portions of the Software.
19 
20  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26  DEALINGS IN THE SOFTWARE.
27 */
28 
29 ///////////////////////////
30 // PORTED SCIRUN v4 CODE //
31 ///////////////////////////
32 
33 #ifndef CORE_ALGORITHMS_MATH_PARALLELALGEBRA_PARALLELLINEARALGEBRA_H
34 #define CORE_ALGORITHMS_MATH_PARALLELALGEBRA_PARALLELLINEARALGEBRA_H
35 
36 #include <vector>
37 #include <list>
38 #include <boost/noncopyable.hpp>
40 #include <Core/Thread/Barrier.h>
43 
44 namespace SCIRun {
45 namespace Core {
46 namespace Algorithms {
47 namespace Math {
48 
49  class ParallelLinearAlgebra;
50 
52  {
57 
58  void clear()
59  {
60  A.reset();
61  b.reset();
62  x0.reset();
63  x.reset();
64  }
65  };
66 
67  class SCISHARE ParallelLinearAlgebraSharedData : boost::noncopyable
68  {
69  public:
70  explicit ParallelLinearAlgebraSharedData(const SolverInputs& inputs, int numProcs);
71  size_t getSize() const { return size_; }
72  Datatypes::DenseColumnMatrixHandle getCurrentMatrix() const { return current_matrix_; }
73  void setCurrentMatrix(Datatypes::DenseColumnMatrixHandle mat) { current_matrix_ = mat; }
74  void addVector(Datatypes::DenseColumnMatrixHandle mat) { vectors_.push_back(mat); }
75  void setFlag(size_t i, bool b) { success_[i] = b; }
76  void setSuccess(size_t i) { success_[i] = true; }
77  void setFail(size_t i) { success_[i] = false; }
78  bool isSuccess(size_t i) const { return success_[i]; }
79 
80  void wait() { barrier_.wait(); }
81  int numProcs() const { return numProcs_; }
82 
83  bool success() const
84  {
85  //return std::all_of(success_.begin(), success_.end(), [](bool b) {return b;});
86  for (size_t j = 0; j < success_.size(); ++j)
87  if (!success_[j])
88  {
89  return false;
90  }
91  return true;
92  }
93 
94  SolverInputs& inputs() { return imatrices_; }
95 
96  double* reduceBuffer1() { return &reduce1_[0]; }
97  double* reduceBuffer2() { return &reduce2_[0]; }
98 
99  private:
100  size_t size_;
101  Datatypes::DenseColumnMatrixHandle current_matrix_;
102  std::list<Datatypes::DenseColumnMatrixHandle> vectors_;
103  std::vector<bool> success_;
104  SolverInputs imatrices_;
106  int numProcs_;
107  /// classes for communication
108  std::vector<double> reduce1_;
109  std::vector<double> reduce2_;
110  };
111 
112 // The algorithm that uses this should derive from this class
113 class SCISHARE ParallelLinearAlgebraBase : boost::noncopyable
114 {
115 public:
117  virtual ~ParallelLinearAlgebraBase();
118 
119  bool start_parallel(SolverInputs& matrices, int nproc = -1) const;
120 
121  virtual bool parallel(ParallelLinearAlgebra& PLA, SolverInputs& matrices) const = 0;
122 
123 private:
124  void run_parallel(ParallelLinearAlgebraSharedData& data, int proc) const;
125  SolverInputs imatrices_;
126 };
127 
128 
129 
130 class SCISHARE ParallelLinearAlgebra : boost::noncopyable
131 {
132 public:
134  public:
135  double* data_;
136  size_t size_;
137  };
138 
140  public:
143  double* data_;
144 
145  size_t m_;
146  size_t n_;
147  size_t nnz_;
148  };
149 
150  // Constructor
152 
153  bool add_vector(Datatypes::DenseColumnMatrixHandle mat, ParallelVector& V);
154  bool new_vector(ParallelVector& V);
155  bool add_matrix(Datatypes::SparseRowMatrixHandle mat, ParallelMatrix& M);
156 
157  void mult(const ParallelVector& a, const ParallelVector& b, ParallelVector& r);
158  void sub(const ParallelVector& a, const ParallelVector& b, ParallelVector& r);
159  void copy(const ParallelVector& a, ParallelVector& r);
160 
161  // r = s*a + b;
162  void scale_add(double s, const ParallelVector& a, const ParallelVector& b, ParallelVector& r);
163 
164  void add(const ParallelVector& a, const ParallelVector& b, ParallelVector& r);
165 
166  void scale(double s, ParallelVector& a, ParallelVector& r);
167  void invert(ParallelVector& a, ParallelVector& r);
168  void threshold_invert(ParallelVector& a, ParallelVector& r, double threshold);
169 
170  double min(const ParallelVector& a);
171 
172  double absmin(const ParallelVector& a);
173  double absmax(const ParallelVector& a);
174 
175  void mult_trans(ParallelMatrix& a, ParallelVector& b, ParallelVector& r);
176 
177  void diag(ParallelMatrix& a, ParallelVector& r);
178  void zeros(ParallelVector& r);
179 
180  void absthreshold_invert(const ParallelVector& a, ParallelVector& r, double threshold);
181 
182  double dot(const ParallelVector& a, const ParallelVector& b);
183  double norm(const ParallelVector& a);
184  double max(const ParallelVector& a);
185 
186  void mult(const ParallelMatrix& a, const ParallelVector& b, ParallelVector& r);
187 
188  void absdiag(const ParallelMatrix& a, ParallelVector& r);
189 
190  void ones(ParallelVector& r);
191 
192  int proc() { return proc_; }
193  int nproc() { return nproc_; }
194 
195  bool first() { return proc_ == 0; }
196  void wait();
197 
198 private:
199  double reduce_sum(double val);
200  double reduce_min(double val);
201  double reduce_max(double val);
202 
204 
205  int proc_; // process number
206  int nproc_; // number of processes
207 
208  size_t size_;
209  size_t local_size_;
210  size_t local_size16_;
211  size_t start_;
212  size_t end_;
213 
214  double* reduce_[2];
215  int reduce_buffer_;
216 
217 
218 };
219 
220 
221 }}}}
222 
223 #endif
double * reduceBuffer2()
Definition: ParallelLinearAlgebra.h:97
bool first()
Definition: ParallelLinearAlgebra.h:195
index_type * columns_
Definition: ParallelLinearAlgebra.h:142
int proc()
Definition: ParallelLinearAlgebra.h:192
Definition: ParallelLinearAlgebra.h:113
void setSuccess(size_t i)
Definition: ParallelLinearAlgebra.h:76
Datatypes::DenseColumnMatrixHandle x
Definition: ParallelLinearAlgebra.h:56
int nproc()
Definition: ParallelLinearAlgebra.h:193
Datatypes::DenseColumnMatrixHandle getCurrentMatrix() const
Definition: ParallelLinearAlgebra.h:72
void addVector(Datatypes::DenseColumnMatrixHandle mat)
Definition: ParallelLinearAlgebra.h:74
Definition: ParallelLinearAlgebra.h:51
Datatypes::SparseRowMatrixHandle A
Definition: ParallelLinearAlgebra.h:53
#define SCISHARE
Definition: share.h:39
Definition: ParallelLinearAlgebra.h:130
boost::shared_ptr< SparseRowMatrix > SparseRowMatrixHandle
Definition: MatrixFwd.h:68
double * data_
Definition: ParallelLinearAlgebra.h:143
Definition: ParallelLinearAlgebraTests.cc:304
double * reduceBuffer1()
Definition: ParallelLinearAlgebra.h:96
SolverInputs & inputs()
Definition: ParallelLinearAlgebra.h:94
Definition: ParallelLinearAlgebraTests.cc:358
Datatypes::DenseColumnMatrixHandle b
Definition: ParallelLinearAlgebra.h:54
void setFlag(size_t i, bool b)
Definition: ParallelLinearAlgebra.h:75
dictionary data
Definition: eabLatVolData.py:11
boost::shared_ptr< DenseColumnMatrix > DenseColumnMatrixHandle
Definition: MatrixFwd.h:60
Datatypes::DenseColumnMatrixHandle x0
Definition: ParallelLinearAlgebra.h:55
void setCurrentMatrix(Datatypes::DenseColumnMatrixHandle mat)
Definition: ParallelLinearAlgebra.h:73
void clear()
Definition: ParallelLinearAlgebra.h:58
int numProcs() const
Definition: ParallelLinearAlgebra.h:81
size_t nnz_
Definition: ParallelLinearAlgebra.h:147
Definition: Barrier.h:42
bool success() const
Definition: ParallelLinearAlgebra.h:83
void wait()
Definition: ParallelLinearAlgebra.h:80
double * data_
Definition: ParallelLinearAlgebra.h:135
size_t getSize() const
Definition: ParallelLinearAlgebra.h:71
long long index_type
Definition: Types.h:39
Definition: ParallelLinearAlgebraTests.cc:751
size_t size_
Definition: ParallelLinearAlgebra.h:136
bool isSuccess(size_t i) const
Definition: ParallelLinearAlgebra.h:78
index_type * rows_
Definition: ParallelLinearAlgebra.h:141
void setFail(size_t i)
Definition: ParallelLinearAlgebra.h:77