Thyra  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ExampleTridiagSpmdLinearOp.hpp
1 // @HEADER
2 // *****************************************************************************
3 // Thyra: Interfaces and Support for Abstract Numerical Algorithms
4 //
5 // Copyright 2004 NTESS and the Thyra contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef THYRA_EXAMPLE_TRIDIAG_SPMD_LINEAR_OP_HPP
11 #define THYRA_EXAMPLE_TRIDIAG_SPMD_LINEAR_OP_HPP
12 
13 #include "Thyra_LinearOpDefaultBase.hpp"
14 #include "Thyra_DefaultSpmdVectorSpace.hpp"
15 #include "Thyra_DetachedSpmdVectorView.hpp"
16 #include "Teuchos_DefaultComm.hpp"
17 #include "Teuchos_CommHelpers.hpp"
18 
19 
110 template<class Scalar>
112 public:
113 
116 
119  const Teuchos::RCP<const Teuchos::Comm<Thyra::Ordinal> > &comm,
120  const Thyra::Ordinal localDim,
124  )
125  { this->initialize(comm, localDim, lower, diag, upper); }
126 
148  void initialize(
149  const Teuchos::RCP<const Teuchos::Comm<Thyra::Ordinal> > &comm,
150  const Thyra::Ordinal localDim,
154  );
155 
156 protected:
157 
158  // Overridden from LinearOpBase
159 
162  { return space_; }
163 
166  { return space_; }
167 
169  bool opSupportedImpl(Thyra::EOpTransp M_trans) const
170  {
171  return (M_trans == Thyra::NOTRANS);
172  }
173 
175  void applyImpl(
176  const Thyra::EOpTransp M_trans,
177  const Thyra::MultiVectorBase<Scalar> &X_in,
179  const Scalar alpha,
180  const Scalar beta
181  ) const;
182 
183 private:
184 
187  Teuchos::Array<Scalar> lower_; // size = ( procRank == 0 ? localDim - 1 : localDim )
188  Teuchos::Array<Scalar> diag_; // size = localDim
189  Teuchos::Array<Scalar> upper_; // size = ( procRank == numProc-1 ? localDim - 1 : localDim )
190 
191  void communicate( const bool first, const bool last,
193  const Teuchos::Ptr<Scalar> &x_km1,
194  const Teuchos::Ptr<Scalar> &x_kp1 ) const;
195 
196 }; // end class ExampleTridiagSpmdLinearOp
197 
198 
199 template<class Scalar>
201  const Teuchos::RCP<const Teuchos::Comm<Thyra::Ordinal> > &comm,
202  const Thyra::Ordinal localDim,
206  )
207 {
208  TEUCHOS_TEST_FOR_EXCEPT( localDim < 2 );
209  comm_ = comm;
210  space_ = Thyra::defaultSpmdVectorSpace<Scalar>(comm, localDim, -1);
211  lower_ = lower;
212  diag_ = diag;
213  upper_ = upper;
214 }
215 
216 
217 template<class Scalar>
219  const Thyra::EOpTransp M_trans,
220  const Thyra::MultiVectorBase<Scalar> &X_in,
222  const Scalar alpha,
223  const Scalar beta
224  ) const
225 {
226 
228  typedef Thyra::Ordinal Ordinal;
229  using Teuchos::outArg;
230 
231  TEUCHOS_ASSERT(this->opSupported(M_trans));
232 
233  // Get constants
234  const Scalar zero = ST::zero();
235  const Ordinal localDim = space_->localSubDim();
236  const Ordinal procRank = comm_->getRank();
237  const Ordinal numProcs = comm_->getSize();
238  const Ordinal m = X_in.domain()->dim();
239 
240  // Loop over the input columns
241 
242  for (Ordinal col_j = 0; col_j < m; ++col_j) {
243 
244  // Get access the the elements of column j
246  Thyra::DetachedSpmdVectorView<Scalar> y_vec(Y_inout->col(col_j));
247  const Teuchos::ArrayRCP<const Scalar> x = x_vec.sv().values();
248  const Teuchos::ArrayRCP<Scalar> y = y_vec.sv().values();
249 
250  // Determine what process we are
251  const bool first = (procRank == 0), last = ( procRank == numProcs-1 );
252 
253  // Communicate ghost elements
254  Scalar x_km1, x_kp1;
255  communicate( first, last, x(), outArg(x_km1), outArg(x_kp1) );
256 
257  // Perform operation (if beta==0 then we must be careful since y could
258  // be uninitialized on input!)
259  Thyra::Ordinal k = 0, lk = 0;
260  if( beta == zero ) {
261  // First local row
262  y[k] = alpha * ( (first?zero:lower_[lk]*x_km1) + diag_[k]*x[k]
263  + upper_[k]*x[k+1] );
264  if(!first) ++lk;
265  // Middle local rows
266  for( k = 1; k < localDim - 1; ++lk, ++k )
267  y[k] = alpha * ( lower_[lk]*x[k-1] + diag_[k]*x[k] + upper_[k]*x[k+1] );
268  // Last local row
269  y[k] = alpha * ( lower_[lk]*x[k-1] + diag_[k]*x[k]
270  + (last?zero:upper_[k]*x_kp1) );
271  }
272  else {
273  // First local row
274  y[k] = alpha * ( (first?zero:lower_[lk]*x_km1) + diag_[k]*x[k]
275  + upper_[k]*x[k+1] ) + beta*y[k];
276  if(!first) ++lk;
277  // Middle local rows
278  for( k = 1; k < localDim - 1; ++lk, ++k )
279  y[k] = alpha * ( lower_[lk]*x[k-1] + diag_[k]*x[k] + upper_[k]*x[k+1] )
280  + beta*y[k];
281  // Last local row
282  y[k] = alpha * ( lower_[lk]*x[k-1] + diag_[k]*x[k]
283  + (last?zero:upper_[k]*x_kp1) ) + beta*y[k];
284  }
285 
286  }
287 
288 }
289 
290 
291 template<class Scalar>
293  const bool first, const bool last,
295  const Teuchos::Ptr<Scalar> &x_km1,
296  const Teuchos::Ptr<Scalar> &x_kp1
297  ) const
298 {
299  typedef Thyra::Ordinal Ordinal;
300  const Ordinal localDim = space_->localSubDim();
301  const Ordinal procRank = comm_->getRank();
302  const Ordinal numProcs = comm_->getSize();
303  const bool isEven = (procRank % 2 == 0);
304  const bool isOdd = !isEven;
305  // 1) Send x[localDim-1] from each even process forward to the next odd
306  // process where it is received in x_km1.
307  if(isEven && procRank+1 < numProcs) send(*comm_, x[localDim-1], procRank+1);
308  if(isOdd && procRank-1 >= 0) receive(*comm_, procRank-1, &*x_km1);
309  // 2) Send x[0] from each odd process backward to the previous even
310  // process where it is received in x_kp1.
311  if( isOdd && procRank-1 >= 0 ) send(*comm_, x[0], procRank-1);
312  if( isEven && procRank+1 < numProcs ) receive(*comm_, procRank+1, &*x_kp1);
313  // 3) Send x[localDim-1] from each odd process forward to the next even
314  // process where it is received in x_km1.
315  if (isOdd && procRank+1 < numProcs) send(*comm_, x[localDim-1], procRank+1);
316  if (isEven && procRank-1 >= 0) receive(*comm_, procRank-1, &*x_km1);
317  // 4) Send x[0] from each even process backward to the previous odd
318  // process where it is received in x_kp1.
319  if (isEven && procRank-1 >= 0) send(*comm_, x[0], procRank-1);
320  if (isOdd && procRank+1 < numProcs) receive(*comm_, procRank+1, &*x_kp1);
321 }
322 
323 
324 #endif // THYRA_EXAMPLE_TRIDIAG_SPMD_LINEAR_OP_HPP
Create an explicit detached mutable (non-const) view of all of the local elements on this process of ...
EOpTransp
Enumeration for determining how a linear operator is applied. `*.
bool opSupportedImpl(Thyra::EOpTransp M_trans) const
ExampleTridiagSpmdLinearOp(const Teuchos::RCP< const Teuchos::Comm< Thyra::Ordinal > > &comm, const Thyra::Ordinal localDim, const Teuchos::ArrayView< const Scalar > &lower, const Teuchos::ArrayView< const Scalar > &diag, const Teuchos::ArrayView< const Scalar > &upper)
Use the non-transposed operator.
Teuchos::RCP< const Thyra::VectorSpaceBase< Scalar > > range() const
Node subclass that provides a good default implementation for the describe() function.
RCP< const LinearOpBase< Scalar > > zero(const RCP< const VectorSpaceBase< Scalar > > &range, const RCP< const VectorSpaceBase< Scalar > > &domain)
Create a zero linear operator with given range and domain spaces.
Teuchos::RCP< const Thyra::VectorSpaceBase< Scalar > > domain() const
Simple example subclass for Spmd tridiagonal matrices.
Teuchos::Ordinal Ordinal
Type for the dimension of a vector space. `*.
Interface for a collection of column vectors called a multi-vector.
bool opSupported(const LinearOpBase< Scalar > &M, EOpTransp M_trans)
Determines if an operation is supported for a single scalar type.
RCP< const VectorBase< Scalar > > col(Ordinal j) const
Calls colImpl().
virtual RCP< const VectorSpaceBase< Scalar > > domain() const =0
Return a smart pointer for the domain space for this operator.
void initialize(const Teuchos::RCP< const Teuchos::Comm< Thyra::Ordinal > > &comm, const Thyra::Ordinal localDim, const Teuchos::ArrayView< const Scalar > &lower, const Teuchos::ArrayView< const Scalar > &diag, const Teuchos::ArrayView< const Scalar > &upper)
Create an explicit detached non-mutable (const) view of all of the local elements on this process of ...
#define TEUCHOS_ASSERT(assertion_test)
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)
void applyImpl(const Thyra::EOpTransp M_trans, const Thyra::MultiVectorBase< Scalar > &X_in, const Teuchos::Ptr< Thyra::MultiVectorBase< Scalar > > &Y_inout, const Scalar alpha, const Scalar beta) const