Ifpack2 Templated Preconditioning Package  Version 1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Ifpack2_Details_MultiVectorLocalGatherScatter.hpp
Go to the documentation of this file.
1 /*@HEADER
2 // ***********************************************************************
3 //
4 // Ifpack2: Templated Object-Oriented Algebraic Preconditioner Package
5 // Copyright (2009) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ***********************************************************************
40 //@HEADER
41 */
42 
43 #ifndef IFPACK2_DETAILS_MULTIVECTORLOCALGATHERSCATTER_HPP
44 #define IFPACK2_DETAILS_MULTIVECTORLOCALGATHERSCATTER_HPP
45 
49 
50 #include "Tpetra_MultiVector.hpp"
51 #include "Tpetra_Map.hpp"
52 
53 namespace Ifpack2 {
54 namespace Details {
55 
84 template<class MV_in, class MV_out>
86 public:
87  typedef typename MV_in::scalar_type InScalar;
88  typedef typename MV_out::scalar_type OutScalar;
89  typedef typename MV_in::local_ordinal_type LO;
90  typedef typename MV_in::global_ordinal_type GO;
91  typedef typename MV_in::node_type NO;
92 
93  /**************/
94  /* MV <==> MV */
95  /**************/
96  void
97  gather (MV_out& X_out,
98  const MV_in& X_in,
99  const Teuchos::ArrayView<const LO>& perm) const
100  {
101  using Teuchos::ArrayRCP;
102  const size_t numRows = X_out.getLocalLength ();
103  const size_t numVecs = X_in.getNumVectors ();
104  for (size_t j = 0; j < numVecs; ++j) {
105  ArrayRCP<const InScalar> X_in_j = X_in.getData(j);
106  ArrayRCP<OutScalar> X_out_j = X_out.getDataNonConst(j);
107  for (size_t i = 0; i < numRows; ++i) {
108  const size_t i_perm = perm[i];
109  X_out_j[i] = X_in_j[i_perm];
110  }
111  }
112  }
113 
114  void
115  scatter (MV_in& X_in,
116  const MV_out& X_out,
117  const Teuchos::ArrayView<const LO>& perm) const
118  {
119  using Teuchos::ArrayRCP;
120  const size_t numRows = X_out.getLocalLength();
121  const size_t numVecs = X_in.getNumVectors();
122  for (size_t j = 0; j < numVecs; ++j) {
123  ArrayRCP<InScalar> X_in_j = X_in.getDataNonConst(j);
124  ArrayRCP<const OutScalar> X_out_j = X_out.getData(j);
125  for (size_t i = 0; i < numRows; ++i) {
126  const size_t i_perm = perm[i];
127  X_in_j[i_perm] = X_out_j[i];
128  }
129  }
130  }
131 
132  /******************/
133  /* View <==> View */
134  /******************/
135  template<typename InView, typename OutView>
136  void gatherViewToView(OutView& X_out,
137  InView& X_in,
138  const Teuchos::ArrayView<const LO>& perm) const
139  {
140  //note: j is col, i is row
141  for(size_t j = 0; j < X_out.extent(1); ++j) {
142  for(size_t i = 0; i < X_out.extent(0); ++i) {
143  const LO i_perm = perm[i];
144  X_out(i, j) = X_in(i_perm, j);
145  }
146  }
147  }
148 
149  template<typename InView, typename OutView>
150  void scatterViewToView(InView& X_in,
151  OutView& X_out,
152  const Teuchos::ArrayView<const LO>& perm) const
153  {
154  for(size_t j = 0; j < X_out.extent(1); ++j) {
155  for(size_t i = 0; i < X_out.extent(0); ++i) {
156  const LO i_perm = perm[i];
157  X_in(i_perm, j) = X_out(i, j);
158  }
159  }
160  }
161 
162  /*******************************/
163  /* MV <==> View specialization */
164  /*******************************/
165  template<typename InView>
166  void gatherMVtoView(MV_out& X_out,
167  InView& X_in,
168  const Teuchos::ArrayView<const LO>& perm) const
169  {
170  //note: j is col, i is row
171  for(size_t j = 0; j < X_out.getNumVectors(); ++j) {
172  Teuchos::ArrayRCP<OutScalar> X_out_j = X_out.getDataNonConst(j);
173  for(size_t i = 0; i < X_out.getLocalLength(); ++i) {
174  const LO i_perm = perm[i];
175  X_out_j[i] = X_in(i_perm, j);
176  }
177  }
178  }
179 
180  template<typename InView>
181  void scatterMVtoView(InView& X_in,
182  MV_out& X_out,
183  const Teuchos::ArrayView<const LO>& perm) const
184  {
185  for(size_t j = 0; j < X_in.extent(1); ++j) {
186  Teuchos::ArrayRCP<const OutScalar> X_out_j = X_out.getData(j);
187  for(size_t i = 0; i < X_out.getLocalLength(); ++i) {
188  const LO i_perm = perm[i];
189  X_in(i_perm, j) = X_out_j[i];
190  }
191  }
192  }
193 };
194 
195 } // namespace Details
196 } // namespace Ifpack2
197 
198 #endif // IFPACK2_DETAILS_MULTIVECTORLOCALGATHERSCATTER_HPP
Implementation detail of Ifpack2::Container subclasses.
Definition: Ifpack2_Details_MultiVectorLocalGatherScatter.hpp:85