cuIBM
A GPU-based Immersed Boundary Method code
preconditioner.h
Go to the documentation of this file.
1 
7 #pragma once
8 
9 #include <cusp/linear_operator.h>
10 #include <cusp/csr_matrix.h>
11 #include <cusp/precond/diagonal.h>
12 #include <cusp/precond/aggregation/smoothed_aggregation.h>
13 #include <cusp/precond/ainv.h>
14 #include <cusp/detail/format.h>
15 
16 #include "types.h"
17 
18 
23 template <typename Matrix>
25 {
27 
28  cusp::linear_operator<typename Matrix::value_type,
29  typename Matrix::memory_space,
30  typename Matrix::index_type>* LO;
31 
32 public:
33  typedef typename Matrix::index_type index_type;
34  typedef typename Matrix::value_type value_type;
35  typedef typename Matrix::memory_space memory_space;
36  typedef typename cusp::unknown_format format;
37 
38  // constructor
40 
41  // constructor overloading
42  preconditioner(const Matrix &A, preconditionerType _type);
43 
44  // destructor
46 
47  // update the preconditioner of the system
48  void update(const Matrix &A);
49 
50  // overload the operator ()
51  template <typename VectorType1, typename VectorType2>
52  void operator()(const VectorType1 &x, VectorType2 &y) const;
53 }; // preconditioner
54 
55 
63 template <class Matrix>
65 {
66  typedef typename Matrix::value_type ValueType;
67  typedef typename Matrix::index_type IndexType;
68  typedef typename Matrix::memory_space MemorySpace;
69 
70  type = _type;
71 
72  // generate an instance of linear_operator with the required derived class
73  // depending on the second parameter
74  /*if (type == NONE)
75  LO = new cusp::identity_operator<ValueType, MemorySpace>(A.num_rows, A.num_cols);
76  else*/
77  if (type == DIAGONAL)
78  LO = new cusp::precond::diagonal<ValueType, MemorySpace>(A);
79  else
81  LO = new cusp::precond::aggregation::smoothed_aggregation<IndexType, ValueType, MemorySpace>(A);
82  else
83  if (type == AINV)
84  LO = new cusp::precond::nonsym_bridson_ainv<ValueType, MemorySpace>(A);
85  else
86  {
87  printf("Error: Unknown preconditionerType!\n");
88  exit(-1);
89  }
90 } // preconditioner
91 
92 
96 template <typename Matrix>
98 {
99  delete LO;
100 }
101 
102 
108 template <class Matrix>
109 void preconditioner<Matrix>::update(const Matrix &A)
110 {
111  typedef typename Matrix::value_type ValueType;
112  typedef typename Matrix::index_type IndexType;
113  typedef typename Matrix::memory_space MemorySpace;
114 
115  /*if (type == NONE)
116  LO = cusp::identity_operator<ValueType, MemorySpace>(A.num_rows, A.num_cols);
117  else*/
118  if (type == DIAGONAL)
119  *LO = cusp::precond::diagonal<ValueType, MemorySpace>(A);
120  else
121  if (type == SMOOTHED_AGGREGATION)
122  *LO = cusp::precond::aggregation::smoothed_aggregation<IndexType, ValueType, MemorySpace>(A);
123  else
124  if (type == AINV)
125  *LO = cusp::precond::nonsym_bridson_ainv<ValueType, MemorySpace>(A);
126  else
127  {
128  printf("Error: Unknown preconditionerType!\n");
129  exit(-1);
130  }
131 } // update
132 
133 
138 template <typename Matrix>
139 template <typename VectorType1, typename VectorType2>
140 void preconditioner<Matrix>::operator()(const VectorType1 &x, VectorType2 &y) const
141 {
142  /*if (type == NONE)
143  {
144  cusp::identity_operator<value_type, memory_space> *identity =
145  static_cast<cusp::identity_operator<value_type, memory_space> *>(LO);
146  printf("dispatching identity->operator()\n");
147  identity->operator()(x,b);
148  }
149  else*/
150  if (type == DIAGONAL)
151  {
152  cusp::precond::diagonal<value_type, memory_space> *diag =
153  static_cast<cusp::precond::diagonal<value_type, memory_space> *>(LO);
154  diag->operator()(x,y);
155  }
156  else if (type == SMOOTHED_AGGREGATION)
157  {
158  cusp::precond::aggregation::smoothed_aggregation<index_type, value_type, memory_space> *SA =
159  static_cast<cusp::precond::aggregation::smoothed_aggregation<index_type, value_type, memory_space> *>(LO);
160  SA->operator()(x,y);
161  }
162  else if (type == AINV)
163  {
164  cusp::precond::nonsym_bridson_ainv<value_type, memory_space> *AI =
165  static_cast<cusp::precond::nonsym_bridson_ainv<value_type, memory_space> *>(LO);
166  AI->operator()(x,y);
167  }
168  else
169  {
170  printf("Error: Unknown preconditionerType!\n");
171  exit(-1);
172  }
173 } // operator()
Definition of custom types required by the code.
void operator()(const VectorType1 &x, VectorType2 &y) const
Overloads the operator (). This is required due to the way preconditioners are implemented in Cusp - ...
Stores the preconditioner for a given system.
preconditionerType type
type of preconditioner
Matrix::value_type value_type
cusp::unknown_format format
Matrix::memory_space memory_space
cusp::linear_operator<typename Matrix::value_type, typename Matrix::memory_space, typename Matrix::index_type > * LO
linear operator
preconditionerType
Specifies the type of preconditioner.
Definition: types.h:104
smoothed aggregation preconditioner
Definition: types.h:108
Matrix::index_type index_type
approximate inverse preconditioner
Definition: types.h:109
~preconditioner()
Destructor. Deletes the preconditioner.
diagonal preconditioner
Definition: types.h:107
void update(const Matrix &A)
Updates the preconditioner of the system.