Zoltan2  Version of the Day
Zoltan2_XpetraTraits.hpp
Go to the documentation of this file.
1 // @HEADER
2 //
3 // ***********************************************************************
4 //
5 // Zoltan2: A package of combinatorial algorithms for scientific computing
6 // Copyright 2012 Sandia Corporation
7 //
8 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Karen Devine (kddevin@sandia.gov)
39 // Erik Boman (egboman@sandia.gov)
40 // Siva Rajamanickam (srajama@sandia.gov)
41 //
42 // ***********************************************************************
43 //
44 // @HEADER
45 
50 #ifndef _ZOLTAN2_XPETRATRAITS_HPP_
51 #define _ZOLTAN2_XPETRATRAITS_HPP_
52 
53 #include <Zoltan2_InputTraits.hpp>
54 #include <Zoltan2_Standards.hpp>
55 
56 #include <Xpetra_EpetraCrsMatrix.hpp>
57 #include <Xpetra_TpetraCrsMatrix.hpp>
58 #include <Xpetra_TpetraRowMatrix.hpp>
59 #include <Xpetra_EpetraVector.hpp>
60 #include <Xpetra_TpetraVector.hpp>
61 #include <Xpetra_EpetraUtils.hpp>
62 #include <Tpetra_Vector.hpp>
63 
64 namespace Zoltan2 {
65 
67 // Extra traits needed only for Xpetra matrices and graphs
68 
91 template <typename User>
93 {
96  static inline RCP<const User> convertToXpetra(const RCP<const User> &a)
97  {
98  return a;
99  }
100 
103  typedef long gno_t;
104 
107  typedef int lno_t;
108 
114  static RCP<const User> doMigration(const RCP<const User> &from,
115  lno_t numLocalRows, const gno_t *myNewRows)
116  {
117  return from;
118  }
119 };
120 
121 #ifndef DOXYGEN_SHOULD_SKIP_THIS
122 
124 // Tpetra::CrsMatrix
125 template <typename scalar_t,
126  typename lno_t,
127  typename gno_t,
128  typename node_t>
129 struct XpetraTraits<Tpetra::CrsMatrix<scalar_t, lno_t, gno_t, node_t> >
130 {
131  typedef typename Xpetra::CrsMatrix<scalar_t,lno_t,gno_t,node_t>
132  xmatrix_t;
133  typedef typename Xpetra::TpetraCrsMatrix<scalar_t,lno_t,gno_t,node_t>
134  xtmatrix_t;
135  typedef typename Tpetra::CrsMatrix<scalar_t,lno_t,gno_t,node_t>
136  tmatrix_t;
137 
138  static inline RCP<const xmatrix_t> convertToXpetra(
139  const RCP<const tmatrix_t> &a)
140  {
141  return rcp(new xtmatrix_t(rcp_const_cast<tmatrix_t>(a)));
142  }
143 
144  static RCP<const tmatrix_t> doMigration(const RCP<const tmatrix_t> &from,
145  lno_t numLocalRows, const gno_t *myNewRows)
146  {
147  typedef Tpetra::Map<lno_t, gno_t, node_t> map_t;
148  lno_t base = 0;
149 
150  // source map
151  const RCP<const map_t> &smap = from->getRowMap();
152  int oldNumElts = smap->getNodeNumElements();
153  gno_t numGlobalRows = smap->getGlobalNumElements();
154 
155  // target map
156  ArrayView<const gno_t> rowList(myNewRows, numLocalRows);
157  const RCP<const Teuchos::Comm<int> > &comm = from->getComm();
158  RCP<const map_t> tmap = rcp(
159  new map_t(numGlobalRows, rowList, base, comm));
160  int newNumElts = numLocalRows;
161 
162  // importer
163  Tpetra::Import<lno_t, gno_t, node_t> importer(smap, tmap);
164 
165  // number of non zeros in my new rows
166  typedef Tpetra::Vector<scalar_t, lno_t, gno_t, node_t> vector_t;
167  vector_t numOld(smap); // TODO These vectors should have scalar = size_t,
168  vector_t numNew(tmap); // but explicit instantiation does not yet support that.
169  for (int lid=0; lid < oldNumElts; lid++){
170  numOld.replaceGlobalValue(smap->getGlobalElement(lid),
171  scalar_t(from->getNumEntriesInLocalRow(lid)));
172  }
173  numNew.doImport(numOld, importer, Tpetra::INSERT);
174 
175  // TODO Could skip this copy if could declare vector with scalar = size_t.
176  ArrayRCP<size_t> nnz(newNumElts);
177  if (newNumElts > 0){
178  ArrayRCP<scalar_t> ptr = numNew.getDataNonConst(0);
179  for (int lid=0; lid < newNumElts; lid++){
180  nnz[lid] = static_cast<size_t>(ptr[lid]);
181  }
182  }
183 
184  // target matrix
185  RCP<tmatrix_t> M = rcp(new tmatrix_t(tmap, nnz, Tpetra::StaticProfile));
186  M->doImport(*from, importer, Tpetra::INSERT);
187  M->fillComplete();
188 
189  return rcp_const_cast<const tmatrix_t>(M);
190  }
191 };
192 
194 // Epetra_CrsMatrix
195 
196 template <>
197 struct XpetraTraits<Epetra_CrsMatrix>
198 {
203 
204  static inline RCP<const Xpetra::CrsMatrix<scalar_t,lno_t,gno_t,node_t> >
205  convertToXpetra(const RCP<const Epetra_CrsMatrix> &a)
206  {
207  return rcp(new Xpetra::EpetraCrsMatrix(
208  rcp_const_cast<Epetra_CrsMatrix>(a)));
209  }
210 
211 
212  static RCP<Epetra_CrsMatrix> doMigration(
213  const RCP<const Epetra_CrsMatrix> &from,
214  lno_t numLocalRows, const gno_t *myNewRows)
215  {
216  lno_t base = 0;
217 
218  // source map
219  const Epetra_Map &smap = from->RowMap();
220  int oldNumElts = smap.NumMyElements();
221  gno_t numGlobalRows = smap.NumGlobalElements();
222 
223  // target map
224  const Epetra_Comm &comm = from->Comm();
225  Epetra_Map tmap(numGlobalRows, numLocalRows, myNewRows, base, comm);
226  int newNumElts = numLocalRows;
227 
228  // importer
229  Epetra_Import importer(tmap, smap);
230 
231  // number of non zeros in my new rows
232  Epetra_Vector numOld(smap);
233  Epetra_Vector numNew(tmap);
234 
235  for (int lid=0; lid < oldNumElts; lid++){
236  numOld[lid] = from->NumMyEntries(lid);
237  }
238  numNew.Import(numOld, importer, Insert);
239 
240  Array<int> nnz(newNumElts);
241  for (int lid=0; lid < newNumElts; lid++){
242  nnz[lid] = static_cast<int>(numNew[lid]);
243  }
244 
245  // target matrix
246  RCP<Epetra_CrsMatrix> M = rcp(
247  new Epetra_CrsMatrix(Copy, tmap, nnz.getRawPtr(), true));
248  M->Import(*from, importer, Insert);
249  M->FillComplete();
250 
251  return M;
252  }
253 };
254 
256 // Xpetra::CrsMatrix
257 // KDDKDD: Do we need specializations for Xpetra::EpetraCrsMatrix and
258 // KDDKDD: Xpetra::TpetraCrsMatrix
259 template <typename scalar_t,
260  typename lno_t,
261  typename gno_t,
262  typename node_t>
263 struct XpetraTraits<Xpetra::CrsMatrix<scalar_t, lno_t, gno_t, node_t> >
264 {
265  typedef Xpetra::CrsMatrix<scalar_t, lno_t, gno_t, node_t> x_matrix_t;
266  typedef Xpetra::TpetraCrsMatrix<scalar_t, lno_t, gno_t, node_t> xt_matrix_t;
267  typedef Tpetra::CrsMatrix<scalar_t,lno_t,gno_t,node_t> t_matrix_t;
268 
269  static inline RCP<const x_matrix_t>
270  convertToXpetra( const RCP<const x_matrix_t > &a)
271  {
272  return a;
273  }
274 
275  static RCP<const x_matrix_t> doMigration(const RCP<const x_matrix_t> &from,
276  lno_t numLocalRows, const gno_t *myNewRows)
277  {
278  Xpetra::UnderlyingLib lib = from->getRowMap()->lib();
279 
280  if (lib == Xpetra::UseEpetra){
281  throw logic_error("compiler should have used specialization");
282  } else{
283  // Do the import with the Tpetra::CrsMatrix traits object
284  const x_matrix_t *xm = from.get();
285  const xt_matrix_t *xtm = dynamic_cast<const xt_matrix_t *>(xm);
286  RCP<const t_matrix_t> tm = xtm->getTpetra_CrsMatrix();
287 
288  RCP<const t_matrix_t> tmnew = XpetraTraits<t_matrix_t>::doMigration(
289  tm, numLocalRows, myNewRows);
290 
291  RCP<const x_matrix_t> xmnew =
293 
294  return xmnew;
295  }
296  }
297 };
298 
300 // Xpetra::CrsMatrix specialization
301 
302 template <typename node_t>
303 struct XpetraTraits<Xpetra::CrsMatrix<double, int, int, node_t> >
304 {
305  typedef double scalar_t;
306  typedef int lno_t;
307  typedef int gno_t;
308  typedef Xpetra::CrsMatrix<scalar_t, lno_t, gno_t, node_t> x_matrix_t;
309  typedef Xpetra::TpetraCrsMatrix<scalar_t, lno_t, gno_t, node_t> xt_matrix_t;
310  typedef Tpetra::CrsMatrix<scalar_t,lno_t,gno_t,node_t> t_matrix_t;
311  typedef Xpetra::EpetraCrsMatrix xe_matrix_t;
312  typedef Epetra_CrsMatrix e_matrix_t;
313 
314  static inline RCP<const x_matrix_t>
315  convertToXpetra( const RCP<const x_matrix_t > &a)
316  {
317  return a;
318  }
319 
320  static RCP<const x_matrix_t> doMigration(const RCP<const x_matrix_t> &from,
321  lno_t numLocalRows, const gno_t *myNewRows)
322  {
323  Xpetra::UnderlyingLib lib = from->getRowMap()->lib();
324  const x_matrix_t *xm = from.get();
325 
326  if (lib == Xpetra::UseEpetra){
327  // Do the import with the Epetra_CrsMatrix traits object
328  const xe_matrix_t *xem = dynamic_cast<const xe_matrix_t *>(xm);
329  RCP<const e_matrix_t> em = xem->getEpetra_CrsMatrix();
330 
331  RCP<const e_matrix_t> emnew = XpetraTraits<e_matrix_t>::doMigration(
332  em, numLocalRows, myNewRows);
333 
334  RCP<const x_matrix_t> xmnew =
336 
337  return xmnew;
338  } else{
339  // Do the import with the Tpetra::CrsMatrix traits object
340  const xt_matrix_t *xtm = dynamic_cast<const xt_matrix_t *>(xm);
341  RCP<const t_matrix_t> tm = xtm->getTpetra_CrsMatrix();
342 
343  RCP<const t_matrix_t> tmnew = XpetraTraits<t_matrix_t>::doMigration(
344  tm, numLocalRows, myNewRows);
345 
346  RCP<const x_matrix_t> xmnew =
348 
349  return xmnew;
350  }
351  }
352 };
353 
354 
356 // Tpetra::CrsGraph
357 template <typename lno_t,
358  typename gno_t,
359  typename node_t>
360 struct XpetraTraits<Tpetra::CrsGraph<lno_t, gno_t, node_t> >
361 {
362  typedef typename Xpetra::CrsGraph<lno_t, gno_t, node_t> xgraph_t;
363  typedef typename Xpetra::TpetraCrsGraph<lno_t, gno_t, node_t> xtgraph_t;
364  typedef typename Tpetra::CrsGraph<lno_t, gno_t, node_t> tgraph_t;
365 
366  static inline RCP<const xgraph_t> convertToXpetra(
367  const RCP<const tgraph_t> &a)
368  {
369  return rcp(new xtgraph_t(rcp_const_cast<tgraph_t>(a)));
370  }
371 
372  static RCP<const tgraph_t> doMigration(const RCP<const tgraph_t> &from,
373  lno_t numLocalRows, const gno_t *myNewRows)
374  {
375  typedef Tpetra::Map<lno_t, gno_t, node_t> map_t;
376  lno_t base = 0;
377 
378  // source map
379  const RCP<const map_t> &smap = from->getRowMap();
380  int oldNumElts = smap->getNodeNumElements();
381  gno_t numGlobalRows = smap->getGlobalNumElements();
382 
383  // target map
384  ArrayView<const gno_t> rowList(myNewRows, numLocalRows);
385  const RCP<const Teuchos::Comm<int> > &comm = from->getComm();
386  RCP<const map_t> tmap = rcp(
387  new map_t(numGlobalRows, rowList, base, comm));
388 
389  // importer
390  Tpetra::Import<lno_t, gno_t, node_t> importer(smap, tmap);
391 
392  // number of entries in my new rows
393  typedef Tpetra::Vector<gno_t, lno_t, gno_t, node_t> vector_t;
394  vector_t numOld(smap);
395  vector_t numNew(tmap);
396  for (int lid=0; lid < oldNumElts; lid++){
397  numOld.replaceGlobalValue(smap->getGlobalElement(lid),
398  from->getNumEntriesInLocalRow(lid));
399  }
400  numNew.doImport(numOld, importer, Tpetra::INSERT);
401 
402  size_t numElts = tmap->getNodeNumElements();
403  ArrayRCP<const gno_t> nnz;
404  if (numElts > 0)
405  nnz = numNew.getData(0); // hangs if vector len == 0
406 
407  ArrayRCP<const size_t> nnz_size_t;
408 
409  if (numElts && sizeof(gno_t) != sizeof(size_t)){
410  size_t *vals = new size_t [numElts];
411  nnz_size_t = arcp(vals, 0, numElts, true);
412  for (size_t i=0; i < numElts; i++){
413  vals[i] = static_cast<size_t>(nnz[i]);
414  }
415  }
416  else{
417  nnz_size_t = arcp_reinterpret_cast<const size_t>(nnz);
418  }
419 
420  // target graph
421  RCP<tgraph_t> G = rcp(new tgraph_t(tmap, nnz_size_t, Tpetra::StaticProfile));
422 
423  G->doImport(*from, importer, Tpetra::INSERT);
424  G->fillComplete();
425 
426  return G;
427  }
428 
429 };
430 
432 // Epetra_CrsGraph
433 template < >
434 struct XpetraTraits<Epetra_CrsGraph>
435 {
439  static inline RCP<const Xpetra::CrsGraph<lno_t,gno_t,node_t> >
440  convertToXpetra(const RCP<const Epetra_CrsGraph> &a)
441  {
442  return rcp(new Xpetra::EpetraCrsGraph(
443  rcp_const_cast<Epetra_CrsGraph>(a)));
444  }
445 
446  static RCP<const Epetra_CrsGraph> doMigration(
447  const RCP<const Epetra_CrsGraph> &from,
448  lno_t numLocalRows, const gno_t *myNewRows)
449  {
450  lno_t base = 0;
451 
452  // source map
453  const Epetra_BlockMap &smap = from->RowMap();
454  gno_t numGlobalRows = smap.NumGlobalElements();
455  lno_t oldNumElts = smap.NumMyElements();
456 
457  // target map
458  const Epetra_Comm &comm = from->Comm();
459  Epetra_BlockMap tmap(numGlobalRows, numLocalRows,
460  myNewRows, 1, base, comm);
461  lno_t newNumElts = tmap.NumMyElements();
462 
463  // importer
464  Epetra_Import importer(tmap, smap);
465 
466  // number of non zeros in my new rows
467  Epetra_Vector numOld(smap);
468  Epetra_Vector numNew(tmap);
469 
470  for (int lid=0; lid < oldNumElts; lid++){
471  numOld[lid] = from->NumMyIndices(lid);
472  }
473  numNew.Import(numOld, importer, Insert);
474 
475  Array<int> nnz(newNumElts);
476  for (int lid=0; lid < newNumElts; lid++){
477  nnz[lid] = static_cast<int>(numNew[lid]);
478  }
479 
480  // target graph
481  RCP<Epetra_CrsGraph> G = rcp(
482  new Epetra_CrsGraph(Copy, tmap, nnz.getRawPtr(), true));
483  G->Import(*from, importer, Insert);
484  G->FillComplete();
485 
486  return G;
487  }
488 
489 };
490 
492 // Xpetra::CrsGraph
493 // KDDKDD Do we need specializations for Xpetra::TpetraCrsGraph and
494 // KDDKDD Xpetra::EpetraCrsGraph?
495 template <typename lno_t,
496  typename gno_t,
497  typename node_t>
498 struct XpetraTraits<Xpetra::CrsGraph<lno_t, gno_t, node_t> >
499 {
500  typedef Xpetra::CrsGraph<lno_t, gno_t, node_t> x_graph_t;
501  typedef Xpetra::TpetraCrsGraph<lno_t, gno_t, node_t> xt_graph_t;
502  typedef Tpetra::CrsGraph<lno_t,gno_t,node_t> t_graph_t;
503 
504  static inline RCP<const x_graph_t>
505  convertToXpetra(const RCP<const x_graph_t> &a)
506  {
507  return a;
508  }
509 
510  static RCP<const x_graph_t> doMigration(const RCP<const x_graph_t> &from,
511  lno_t numLocalRows, const gno_t *myNewRows)
512  {
513  Xpetra::UnderlyingLib lib = from->getRowMap()->lib();
514 
515  if (lib == Xpetra::UseEpetra){
516  throw logic_error("compiler should have used specialization");
517  } else{
518  // Do the import with the Tpetra::CrsGraph traits object
519  const x_graph_t *xg = from.get();
520  const xt_graph_t *xtg = dynamic_cast<const xt_graph_t *>(xg);
521  RCP<const t_graph_t> tg = xtg->getTpetra_CrsGraph();
522 
523  RCP<const t_graph_t> tgnew = XpetraTraits<t_graph_t>::doMigration(
524  tg, numLocalRows, myNewRows);
525 
526  RCP<const x_graph_t> xgnew =
528  return xgnew;
529  }
530  }
531 };
532 
533 
534 
536 // Xpetra::RowMatrix
537 template <typename scalar_t,
538  typename lno_t,
539  typename gno_t,
540  typename node_t>
541 struct XpetraTraits<Xpetra::RowMatrix<scalar_t, lno_t, gno_t, node_t> >
542 {
543  typedef Xpetra::RowMatrix<scalar_t, lno_t, gno_t, node_t> x_matrix_t;
544  typedef Xpetra::TpetraRowMatrix<scalar_t, lno_t, gno_t, node_t> xt_matrix_t;
545  typedef Tpetra::RowMatrix<scalar_t,lno_t,gno_t,node_t> t_matrix_t;
546 
547  static inline RCP<const x_matrix_t>
548  convertToXpetra( const RCP<const x_matrix_t > &a)
549  {
550  return a;
551  }
552 
553  static RCP<const x_matrix_t> doMigration(const RCP<const x_matrix_t> &from,
554  lno_t numLocalRows, const gno_t *myNewRows)
555  {
556  Xpetra::UnderlyingLib lib = from->getRowMap()->lib();
557 
558  if (lib == Xpetra::UseEpetra){
559  throw logic_error("compiler should have used specialization");
560  } else{
561  // Do the import with the Tpetra::CrsMatrix traits object
562  const x_matrix_t *xm = from.get();
563  const xt_matrix_t *xtm = dynamic_cast<const xt_matrix_t *>(xm);
564  RCP<const t_matrix_t> tm = xtm->getTpetra_CrsMatrix();
565 
566  RCP<const t_matrix_t> tmnew = XpetraTraits<t_matrix_t>::doMigration(
567  tm, numLocalRows, myNewRows);
568 
569  RCP<const x_matrix_t> xmnew =
571 
572  return xmnew;
573  }
574  }
575 };
576 
578 // Xpetra::CrsGraph specialization
579 template < typename node_t>
580 struct XpetraTraits<Xpetra::CrsGraph<int, int, node_t> >
581 {
582  typedef int lno_t;
583  typedef int gno_t;
584  typedef Xpetra::CrsGraph<lno_t, gno_t, node_t> x_graph_t;
585  typedef Xpetra::TpetraCrsGraph<lno_t, gno_t, node_t> xt_graph_t;
586  typedef Tpetra::CrsGraph<lno_t,gno_t,node_t> t_graph_t;
587  typedef Xpetra::EpetraCrsGraph xe_graph_t;
588  typedef Epetra_CrsGraph e_graph_t;
589 
590  static inline RCP<const x_graph_t>
591  convertToXpetra(const RCP<const x_graph_t> &a)
592  {
593  return a;
594  }
595 
596  static RCP<const x_graph_t> doMigration(const RCP<const x_graph_t> &from,
597  lno_t numLocalRows, const gno_t *myNewRows)
598  {
599  Xpetra::UnderlyingLib lib = from->getRowMap()->lib();
600  const x_graph_t *xg = from.get();
601 
602  if (lib == Xpetra::UseEpetra){
603  // Do the import with the Epetra_CrsGraph traits object
604  const xe_graph_t *xeg = dynamic_cast<const xe_graph_t *>(xg);
605  RCP<const e_graph_t> eg = xeg->getEpetra_CrsGraph();
606 
607  RCP<const e_graph_t> egnew = XpetraTraits<e_graph_t>::doMigration(
608  eg, numLocalRows, myNewRows);
609 
610  RCP<const x_graph_t> xgnew =
612 
613  return xgnew;
614  } else{
615  // Do the import with the Tpetra::CrsGraph traits object
616  const xt_graph_t *xtg = dynamic_cast<const xt_graph_t *>(xg);
617  RCP<const t_graph_t> tg = xtg->getTpetra_CrsGraph();
618 
619  RCP<const t_graph_t> tgnew = XpetraTraits<t_graph_t>::doMigration(
620  tg, numLocalRows, myNewRows);
621 
622  RCP<const x_graph_t> xgnew =
624 
625  return xgnew;
626  }
627  }
628 };
629 
631 // Tpetra::Vector
632 template <typename scalar_t,
633  typename lno_t,
634  typename gno_t,
635  typename node_t>
636 struct XpetraTraits<Tpetra::Vector<scalar_t, lno_t, gno_t, node_t> >
637 {
638  typedef Tpetra::Vector<scalar_t, lno_t, gno_t, node_t> t_vector_t;
639  typedef Xpetra::TpetraVector<scalar_t, lno_t, gno_t, node_t> xt_vector_t;
640  typedef Xpetra::Vector<scalar_t, lno_t, gno_t, node_t> x_vector_t;
641 
642  static inline RCP<const x_vector_t>
643  convertToXpetra(const RCP<const t_vector_t> &a)
644  {
645  return rcp(new xt_vector_t(rcp_const_cast<t_vector_t>(a)));
646  }
647 
648  static RCP<const t_vector_t> doMigration(const RCP<const t_vector_t> &from,
649  lno_t numLocalElts, const gno_t *myNewElts)
650  {
651  lno_t base = 0;
652  typedef Tpetra::Map<lno_t, gno_t, node_t> map_t;
653 
654  // source map
655  const RCP<const map_t> &smap = from->getMap();
656  gno_t numGlobalElts = smap->getGlobalNumElements();
657 
658  // target map
659  ArrayView<const gno_t> eltList(myNewElts, numLocalElts);
660  const RCP<const Teuchos::Comm<int> > comm = from->getMap()->getComm();
661  RCP<const map_t> tmap = rcp(
662  new map_t(numGlobalElts, eltList, base, comm));
663 
664  // importer
665  Tpetra::Import<lno_t, gno_t, node_t> importer(smap, tmap);
666 
667  // target vector
668  RCP<t_vector_t> V =
669  Tpetra::createVector<scalar_t,lno_t,gno_t,node_t>(tmap);
670  V->doImport(*from, importer, Tpetra::INSERT);
671 
672  return V;
673  }
674 };
675 
677 // Epetra_Vector
678 template < >
679 struct XpetraTraits<Epetra_Vector>
680 {
685 
686  typedef Xpetra::Vector<scalar_t, lno_t, gno_t, node_t> x_vector_t;
687 
688  static inline RCP<const x_vector_t>
689  convertToXpetra(const RCP<const Epetra_Vector> &a)
690  {
691  RCP<Xpetra::EpetraVector> xev = rcp(
692  new Xpetra::EpetraVector(rcp_const_cast<Epetra_Vector>(a)));
693  return rcp_implicit_cast<x_vector_t>(xev);
694  }
695 
696  static RCP<Epetra_Vector> doMigration(const RCP<const Epetra_Vector> &from,
697  lno_t numLocalElts, const gno_t *myNewElts)
698  {
699  lno_t base = 0;
700  // source map
701  const Epetra_BlockMap &smap = from->Map();
702  gno_t numGlobalElts = smap.NumGlobalElements();
703 
704  // target map
705  const Epetra_Comm &comm = from->Comm();
706  const Epetra_BlockMap tmap(numGlobalElts, numLocalElts, myNewElts,
707  1, base, comm);
708 
709  // importer
710  Epetra_Import importer(tmap, smap);
711 
712  // target vector
713  RCP<Epetra_Vector> V = rcp(new Epetra_Vector(tmap, true));
714  Epetra_CombineMode c = Insert;
715  V->Import(*from, importer, c);
716 
717  return V;
718  }
719 };
720 
722 // Xpetra::Vector
723 template <typename scalar_t,
724  typename lno_t,
725  typename gno_t,
726  typename node_t>
727 struct XpetraTraits<Xpetra::Vector<scalar_t, lno_t, gno_t, node_t> >
728 {
729  typedef Xpetra::Vector<scalar_t, lno_t, gno_t, node_t> x_vector_t;
730  typedef Xpetra::TpetraVector<scalar_t, lno_t, gno_t, node_t> xt_vector_t;
731  typedef Tpetra::Vector<scalar_t, lno_t, gno_t, node_t> t_vector_t;
732 
733  static inline RCP<const x_vector_t>
734  convertToXpetra(const RCP<const x_vector_t> &a)
735  {
736  return a;
737  }
738 
739  static RCP<const x_vector_t> doMigration(const RCP<const x_vector_t> &from,
740  lno_t numLocalRows, const gno_t *myNewRows)
741  {
742  Xpetra::UnderlyingLib lib = from->getMap()->lib();
743 
744  if (lib == Xpetra::UseEpetra){
745  throw logic_error("compiler should have used specialization");
746  } else{
747  // Do the import with the Tpetra::Vector traits object
748  const x_vector_t *xv = from.get();
749  const xt_vector_t *xtv = dynamic_cast<const xt_vector_t *>(xv);
750  RCP<const t_vector_t> tv = xtv->getTpetra_Vector();
751 
752  RCP<const t_vector_t> tvnew = XpetraTraits<t_vector_t>::doMigration(
753  tv, numLocalRows, myNewRows);
754 
755  RCP<const x_vector_t> xvnew =
757 
758  return xvnew;
759  }
760  }
761 };
762 
764 // Xpetra::Vector specialization
765 template <typename node_t>
766 struct XpetraTraits<Xpetra::Vector<double, int, int, node_t> >
767 {
768  typedef double scalar_t;
769  typedef int lno_t;
770  typedef int gno_t;
771  typedef Xpetra::Vector<scalar_t, lno_t, gno_t, node_t> x_vector_t;
772  typedef Xpetra::TpetraVector<scalar_t, lno_t, gno_t, node_t> xt_vector_t;
773  typedef Tpetra::Vector<scalar_t, lno_t, gno_t, node_t> t_vector_t;
774  typedef Xpetra::EpetraVector xe_vector_t;
775  typedef Epetra_Vector e_vector_t;
776 
777  static inline RCP<const x_vector_t>
778  convertToXpetra(const RCP<const x_vector_t> &a)
779  {
780  return a;
781  }
782 
783  static RCP<const x_vector_t> doMigration(const RCP<const x_vector_t> &from,
784  lno_t numLocalRows, const gno_t *myNewRows)
785  {
786  Xpetra::UnderlyingLib lib = from->getMap()->lib();
787  const x_vector_t *vec = from.get();
788 
789  if (lib == Xpetra::UseEpetra){
790  // Do the import with the Epetra_Vector traits object
791  const xe_vector_t *xev = dynamic_cast<const xe_vector_t *>(vec);
792  RCP<const e_vector_t> ev = rcp(xev->getEpetra_Vector());
793 
794  RCP<const e_vector_t> evnew = XpetraTraits<e_vector_t>::doMigration(
795  ev, numLocalRows, myNewRows);
796 
797  RCP<const x_vector_t> xvnew =
799 
800  return xvnew;
801  } else{
802  // Do the import with the Tpetra::Vector traits object
803  const xt_vector_t *xtv = dynamic_cast<const xt_vector_t *>(vec);
804  RCP<t_vector_t> tv = xtv->getTpetra_Vector();
805  RCP<const t_vector_t> ctv = rcp_const_cast<const t_vector_t>(tv);
806 
807  RCP<const t_vector_t> tvnew = XpetraTraits<t_vector_t>::doMigration(
808  ctv, numLocalRows, myNewRows);
809 
810  RCP<const x_vector_t> xvnew =
812 
813  return xvnew;
814  }
815  }
816 };
817 
819 // Tpetra::MultiVector
820 template <typename scalar_t,
821  typename lno_t,
822  typename gno_t,
823  typename node_t>
824 struct XpetraTraits<Tpetra::MultiVector<scalar_t, lno_t, gno_t, node_t> >
825 {
826  typedef Tpetra::MultiVector<scalar_t, lno_t, gno_t, node_t> t_vector_t;
827  typedef Xpetra::TpetraMultiVector<scalar_t, lno_t, gno_t, node_t> xt_vector_t;
828  typedef Xpetra::MultiVector<scalar_t, lno_t, gno_t, node_t> x_vector_t;
829 
830  static inline RCP<const x_vector_t>
831  convertToXpetra(const RCP<const t_vector_t> &a)
832  {
833  return rcp(new xt_vector_t(rcp_const_cast<t_vector_t>(a)));
834  }
835 
836  static RCP<const t_vector_t> doMigration(const RCP<const t_vector_t> &from,
837  lno_t numLocalElts, const gno_t *myNewElts)
838  {
839  typedef Tpetra::Map<lno_t, gno_t, node_t> map_t;
840  lno_t base = 0;
841 
842  // source map
843  const RCP<const map_t> &smap = from->getMap();
844  gno_t numGlobalElts = smap->getGlobalNumElements();
845 
846  // target map
847  ArrayView<const gno_t> eltList(myNewElts, numLocalElts);
848  const RCP<const Teuchos::Comm<int> > comm = from->getMap()->getComm();
849  RCP<const map_t> tmap = rcp(
850  new map_t(numGlobalElts, eltList, base, comm));
851 
852  // importer
853  Tpetra::Import<lno_t, gno_t, node_t> importer(smap, tmap);
854 
855  // target vector
856  RCP<t_vector_t> MV = rcp(
857  new t_vector_t(tmap, from->getNumVectors(), true));
858  MV->doImport(*from, importer, Tpetra::INSERT);
859 
860  return MV;
861  }
862 };
863 
865 // Epetra_MultiVector
866 template < >
867 struct XpetraTraits<Epetra_MultiVector>
868 {
873  typedef Xpetra::MultiVector<scalar_t, lno_t, gno_t, node_t> x_mvector_t;
874 
875  static inline RCP<const x_mvector_t>
876  convertToXpetra(const RCP<const Epetra_MultiVector> &a)
877  {
878  RCP<Xpetra::EpetraMultiVector> xemv = rcp(
879  new Xpetra::EpetraMultiVector(
880  rcp_const_cast<Epetra_MultiVector>(a)));
881  return rcp_implicit_cast<x_mvector_t>(xemv);
882  }
883 
884  static RCP<Epetra_MultiVector> doMigration(
885  const RCP<const Epetra_MultiVector> &from,
886  lno_t numLocalElts, const gno_t *myNewElts)
887  {
888  lno_t base = 0;
889  // source map
890  const Epetra_BlockMap &smap = from->Map();
891  gno_t numGlobalElts = smap.NumGlobalElements();
892 
893  // target map
894  const Epetra_Comm &comm = from->Comm();
895  const Epetra_BlockMap tmap(numGlobalElts, numLocalElts, myNewElts,
896  1, base, comm);
897 
898  // importer
899  Epetra_Import importer(tmap, smap);
900 
901  // target vector
902  RCP<Epetra_MultiVector> MV = rcp(
903  new Epetra_MultiVector(tmap, from->NumVectors(), true));
904  Epetra_CombineMode c = Insert;
905  MV->Import(*from, importer, c);
906 
907  return MV;
908  }
909 };
910 
912 // Xpetra::MultiVector
913 template <typename scalar_t,
914  typename lno_t,
915  typename gno_t,
916  typename node_t>
917 struct XpetraTraits<Xpetra::MultiVector<scalar_t, lno_t, gno_t, node_t> >
918 {
919  typedef Xpetra::MultiVector<scalar_t, lno_t, gno_t, node_t> x_mvector_t;
920  typedef Xpetra::TpetraMultiVector<scalar_t, lno_t, gno_t, node_t> xt_mvector_t;
921  typedef Tpetra::MultiVector<scalar_t, lno_t, gno_t, node_t> t_mvector_t;
922 
923  static inline RCP<const x_mvector_t>
924  convertToXpetra(const RCP<const x_mvector_t> &a)
925  {
926  return a;
927  }
928 
929  static RCP<const x_mvector_t> doMigration(const RCP<const x_mvector_t> &from,
930  lno_t numLocalRows, const gno_t *myNewRows)
931  {
932  Xpetra::UnderlyingLib lib = from->getMap()->lib();
933 
934  if (lib == Xpetra::UseEpetra){
935  throw logic_error("compiler should have used specialization");
936  } else{
937  // Do the import with the Tpetra::MultiVector traits object
938  const x_mvector_t *xmv = from.get();
939  const xt_mvector_t *xtv = dynamic_cast<const xt_mvector_t *>(xmv);
940  RCP<t_mvector_t> tv = xtv->getTpetra_MultiVector();
941  RCP<const t_mvector_t> ctv = rcp_const_cast<const t_mvector_t>(tv);
942 
943  RCP<const t_mvector_t> tvnew = XpetraTraits<t_mvector_t>::doMigration(
944  ctv, numLocalRows, myNewRows);
945 
946  RCP<const x_mvector_t> xvnew =
948 
949  return xvnew;
950  }
951  }
952 };
953 
955 // Xpetra::MultiVector specialization
956 template <typename node_t>
957 struct XpetraTraits<Xpetra::MultiVector<double, int, int, node_t> >
958 {
959  typedef double scalar_t;
960  typedef int lno_t;
961  typedef int gno_t;
962  typedef Xpetra::MultiVector<scalar_t, lno_t, gno_t, node_t> x_mvector_t;
963  typedef Xpetra::TpetraMultiVector<scalar_t, lno_t, gno_t, node_t> xt_mvector_t;
964  typedef Tpetra::MultiVector<scalar_t, lno_t, gno_t, node_t> t_mvector_t;
965  typedef Xpetra::EpetraMultiVector xe_mvector_t;
966  typedef Epetra_MultiVector e_mvector_t;
967 
968  static inline RCP<const x_mvector_t>
969  convertToXpetra(const RCP<const x_mvector_t> &a)
970  {
971  return a;
972  }
973 
974  static RCP<const x_mvector_t> doMigration(const RCP<const x_mvector_t> &from,
975  lno_t numLocalRows, const gno_t *myNewRows)
976  {
977  Xpetra::UnderlyingLib lib = from->getMap()->lib();
978  const x_mvector_t *xmv = from.get();
979 
980  if (lib == Xpetra::UseEpetra){
981  // Do the import with the Epetra_MultiVector traits object
982  const xe_mvector_t *xev = dynamic_cast<const xe_mvector_t *>(xmv);
983  RCP<e_mvector_t> ev = xev->getEpetra_MultiVector();
984  RCP<const e_mvector_t> cev = rcp_const_cast<const e_mvector_t>(ev);
985 
986  RCP<const e_mvector_t> evnew = XpetraTraits<e_mvector_t>::doMigration(
987  cev, numLocalRows, myNewRows);
988 
989  RCP<const x_mvector_t> xvnew =
991 
992  return xvnew;
993 
994  } else{
995  // Do the import with the Tpetra::MultiVector traits object
996  const xt_mvector_t *xtv = dynamic_cast<const xt_mvector_t *>(xmv);
997  RCP<t_mvector_t> tv = xtv->getTpetra_MultiVector();
998  RCP<const t_mvector_t> ctv = rcp_const_cast<const t_mvector_t>(tv);
999 
1000  RCP<const t_mvector_t> tvnew = XpetraTraits<t_mvector_t>::doMigration(
1001  ctv, numLocalRows, myNewRows);
1002 
1003  RCP<const x_mvector_t> xvnew =
1005 
1006  return xvnew;
1007  }
1008  }
1009 };
1010 
1011 #endif // DOXYGEN_SHOULD_SKIP_THIS
1012 
1013 } //namespace Zoltan2
1014 
1015 #endif // _ZOLTAN2_XPETRATRAITS_HPP_
Defines the traits required for Tpetra, Eptra and Xpetra objects.
Xpetra::CrsGraph< lno_t, gno_t, node_t > xgraph_t
int lno_t
The objects local ordinal data type.
dictionary vals
Definition: xml2dox.py:186
default_lno_t lno_t
The ordinal type (e.g., int, long, int64_t) that represents local counts and local indices...
long gno_t
The objects global ordinal data type.
static RCP< const User > doMigration(const RCP< const User > &from, lno_t numLocalRows, const gno_t *myNewRows)
Migrate the object Given a user object and a new row distribution, create and return a new user objec...
static RCP< const User > convertToXpetra(const RCP< const User > &a)
Convert the object to its Xpetra wrapped version.
Traits for application input objects.
default_gno_t gno_t
The ordinal type (e.g., int, long, int64_t) that can represent global counts and identifiers.
double scalar_t
Tpetra::CrsMatrix< scalar_t, lno_t, gno_t, node_t > tmatrix_t
Gathering definitions used in software development.
Tpetra::CrsGraph< lno_t, gno_t, node_t > tgraph_t
Kokkos::DefaultNode::DefaultNodeType node_t
The Kokkos node type. This is only meaningful for users of Tpetra objects.
Xpetra::CrsMatrix< scalar_t, lno_t, gno_t, node_t > xmatrix_t
default_scalar_t scalar_t
The data type for weights and coordinates.
Kokkos::DefaultNode::DefaultNodeType node_t