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  //Gather blocks (contiguous groups of blockSize rows)
115  //X_out and X_in are point indexed, but perm uses block indices.
116  //So X_out.getLocalLength() / blockSize gives the number of blocks.
117  void
118  gatherBlock (
119  MV_out& X_out,
120  const MV_in& X_in,
121  const Teuchos::ArrayView<const LO> perm,
122  LO blockSize) const
123  {
124  using Teuchos::ArrayRCP;
125  const size_t numBlocks = X_out.getLocalLength() / blockSize;
126  const size_t numVecs = X_in.getNumVectors ();
127  for (size_t j = 0; j < numVecs; ++j) {
128  ArrayRCP<const InScalar> X_in_j = X_in.getData(j);
129  ArrayRCP<OutScalar> X_out_j = X_out.getDataNonConst(j);
130  for (size_t i = 0; i < numBlocks; ++i) {
131  const size_t i_perm = perm[i];
132  for (LO k = 0; k < blockSize; k++) {
133  X_out_j[i * blockSize + k] = X_in_j[i_perm * blockSize + k];
134  }
135  }
136  }
137  }
138 
139  void
140  scatter (MV_in& X_in,
141  const MV_out& X_out,
142  const Teuchos::ArrayView<const LO> perm) const
143  {
144  using Teuchos::ArrayRCP;
145  const size_t numRows = X_out.getLocalLength();
146  const size_t numVecs = X_in.getNumVectors();
147  for (size_t j = 0; j < numVecs; ++j) {
148  ArrayRCP<InScalar> X_in_j = X_in.getDataNonConst(j);
149  ArrayRCP<const OutScalar> X_out_j = X_out.getData(j);
150  for (size_t i = 0; i < numRows; ++i) {
151  const size_t i_perm = perm[i];
152  X_in_j[i_perm] = X_out_j[i];
153  }
154  }
155  }
156 
157  void
158  scatterBlock (
159  MV_in& X_in,
160  const MV_out& X_out,
161  const Teuchos::ArrayView<const LO> perm,
162  LO blockSize) const
163  {
164  using Teuchos::ArrayRCP;
165  const size_t numBlocks = X_out.getLocalLength() / blockSize;
166  const size_t numVecs = X_in.getNumVectors ();
167  for (size_t j = 0; j < numVecs; ++j) {
168  ArrayRCP<const InScalar> X_in_j = X_in.getData(j);
169  ArrayRCP<OutScalar> X_out_j = X_out.getDataNonConst(j);
170  for (size_t i = 0; i < numBlocks; ++i) {
171  const size_t i_perm = perm[i];
172  for (LO k = 0; k < blockSize; k++) {
173  X_in_j[i_perm * blockSize + k] = X_out_j[i * blockSize + k];
174  }
175  }
176  }
177  }
178 
179  /******************/
180  /* View <==> View */
181  /******************/
182  template<typename InView, typename OutView>
183  void gatherViewToView(OutView X_out,
184  const InView X_in,
185  const Teuchos::ArrayView<const LO> perm) const
186  {
187  //note: j is col, i is row
188  for(size_t j = 0; j < X_out.extent(1); ++j) {
189  for(size_t i = 0; i < X_out.extent(0); ++i) {
190  const LO i_perm = perm[i];
191  X_out(i, j) = X_in(i_perm, j);
192  }
193  }
194  }
195 
196  template<typename InView, typename OutView>
197  void scatterViewToView(InView X_in,
198  const OutView X_out,
199  const Teuchos::ArrayView<const LO> perm) const
200  {
201  for(size_t j = 0; j < X_out.extent(1); ++j) {
202  for(size_t i = 0; i < X_out.extent(0); ++i) {
203  const LO i_perm = perm[i];
204  X_in(i_perm, j) = X_out(i, j);
205  }
206  }
207  }
208 
209  template<typename InView, typename OutView>
210  void gatherViewToViewBlock(OutView X_out,
211  const InView X_in,
212  const Teuchos::ArrayView<const LO> perm,
213  LO blockSize) const
214  {
215  //note: j is col, i is row
216  size_t numBlocks = X_out.extent(0) / blockSize;
217  for(size_t j = 0; j < X_out.extent(1); ++j) {
218  for(size_t i = 0; i < numBlocks; ++i) {
219  const LO i_perm = perm[i];
220  for(LO k = 0; k < blockSize; k++) {
221  X_out(i * blockSize + k, j) = X_in(i_perm * blockSize + k, j);
222  }
223  }
224  }
225  }
226 
227  template<typename InView, typename OutView>
228  void scatterViewToViewBlock(InView X_in,
229  const OutView X_out,
230  const Teuchos::ArrayView<const LO> perm,
231  LO blockSize) const
232  {
233  //note: j is col, i is row
234  size_t numBlocks = X_out.extent(0) / blockSize;
235  for(size_t j = 0; j < X_out.extent(1); ++j) {
236  for(size_t i = 0; i < numBlocks; ++i) {
237  const LO i_perm = perm[i];
238  for(LO k = 0; k < blockSize; k++) {
239  X_in(i_perm * blockSize + k, j) = X_out(i * blockSize + k, j);
240  }
241  }
242  }
243  }
244 
245  /*******************************/
246  /* MV <==> View specialization */
247  /*******************************/
248  template<typename InView>
249  void gatherMVtoView(MV_out X_out,
250  InView X_in,
251  const Teuchos::ArrayView<const LO> perm) const
252  {
253  //note: j is col, i is row
254  size_t numRows = X_out.getLocalLength();
255  for(size_t j = 0; j < X_out.getNumVectors(); ++j) {
256  Teuchos::ArrayRCP<OutScalar> X_out_j = X_out.getDataNonConst(j);
257  for(size_t i = 0; i < numRows; ++i) {
258  const LO i_perm = perm[i];
259  X_out_j[i] = X_in(i_perm, j);
260  }
261  }
262  }
263 
264  template<typename InView>
265  void scatterMVtoView(InView X_in,
266  MV_out X_out,
267  const Teuchos::ArrayView<const LO> perm) const
268  {
269  size_t numRows = X_out.getLocalLength();
270  for(size_t j = 0; j < X_in.extent(1); ++j) {
271  Teuchos::ArrayRCP<const OutScalar> X_out_j = X_out.getData(j);
272  for(size_t i = 0; i < numRows; ++i) {
273  const LO i_perm = perm[i];
274  X_in(i_perm, j) = X_out_j[i];
275  }
276  }
277  }
278 
279  template<typename InView>
280  void gatherMVtoViewBlock(MV_out X_out,
281  InView X_in,
282  const Teuchos::ArrayView<const LO> perm,
283  LO blockSize) const
284  {
285  //note: j is col, i is row
286  size_t numBlocks = X_out.getLocalLength() / blockSize;
287  for(size_t j = 0; j < X_out.getNumVectors(); ++j) {
288  Teuchos::ArrayRCP<OutScalar> X_out_j = X_out.getDataNonConst(j);
289  for(size_t i = 0; i < numBlocks; ++i) {
290  const LO i_perm = perm[i];
291  for(LO k = 0; k < blockSize; k++) {
292  X_out_j[i * blockSize + k] = X_in(i_perm * blockSize + k, j);
293  }
294  }
295  }
296  }
297 
298  template<typename InView>
299  void scatterMVtoViewBlock(InView X_in,
300  MV_out X_out,
301  const Teuchos::ArrayView<const LO> perm,
302  LO blockSize) const
303  {
304  size_t numBlocks = X_out.getLocalLength() / blockSize;
305  for(size_t j = 0; j < X_in.extent(1); ++j) {
306  Teuchos::ArrayRCP<const OutScalar> X_out_j = X_out.getData(j);
307  for(size_t i = 0; i < numBlocks; ++i) {
308  const LO i_perm = perm[i];
309  for(LO k = 0; k < blockSize; k++) {
310  X_in(i_perm * blockSize + k, j) = X_out_j[i * blockSize + k];
311  }
312  }
313  }
314  }
315 };
316 
317 } // namespace Details
318 } // namespace Ifpack2
319 
320 #endif // IFPACK2_DETAILS_MULTIVECTORLOCALGATHERSCATTER_HPP
Implementation detail of Ifpack2::Container subclasses.
Definition: Ifpack2_Details_MultiVectorLocalGatherScatter.hpp:85