Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Stokhos_EpetraSparse3Tensor.cpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Stokhos Package
4 //
5 // Copyright 2009 NTESS and the Stokhos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #include "Stokhos_ConfigDefs.h"
13 #include "Teuchos_Assert.hpp"
14 #ifdef HAVE_STOKHOS_ISORROPIA
15 #include "Isorropia_Epetra.hpp"
16 #endif
17 #include "Epetra_Map.h"
18 
23  const Teuchos::RCP<const EpetraExt::MultiComm>& globalMultiComm_,
24  int k_begin_, int k_end_) :
25  basis(basis_),
26  Cijk(Cijk_),
27  globalMultiComm(globalMultiComm_),
28  k_begin(k_begin_),
29  k_end(k_end_)
30 {
31  // Get stochastic distribution
33  stoch_comm = Teuchos::rcp(&(globalMultiComm->TimeDomainComm()), false);
34  is_parallel = (stoch_comm->NumProc() > 1);
35  if (k_end == -1)
36  k_end = Cijk->num_k();
37 
38  // Build stochastic row map -- stochastic blocks evenly distributed
39  // across stochastic procs
41  *stoch_comm));
42 
43  // Build Cijk tensor parallel over i
44  if (!is_parallel)
46  else
48 
49  // Build stochastic graph
51 
52  // Build stochastic column map
54 }
55 
60  const Teuchos::RCP<const EpetraExt::MultiComm>& globalMultiComm_,
61  const Teuchos::RCP<const Epetra_BlockMap>& stoch_row_map_,
62  const Teuchos::RCP<const Stokhos::Sparse3Tensor<int,double> >& Cijk_parallel_,
63  int k_begin_, int k_end_) :
64  basis(basis_),
65  Cijk(Cijk_),
66  globalMultiComm(globalMultiComm_),
67  k_begin(k_begin_),
68  k_end(k_end_),
69  stoch_row_map(stoch_row_map_),
70  Cijk_parallel(Cijk_parallel_)
71 {
72  // Get stochastic distribution
74  stoch_comm = Teuchos::rcp(&(globalMultiComm->TimeDomainComm()), false);
75  is_parallel = (stoch_comm->NumProc() > 1);
76  if (k_end == -1)
77  k_end = Cijk->num_k();
78 
79  // Build Cijk tensor parallel over i
81  if (!is_parallel)
83  else
85  }
86 
87  // Build stochastic graph
89 
90  // Build stochastic column map
92 }
93 
96  int k_begin_, int k_end_) :
97  basis(epetraCijk.basis),
98  Cijk(epetraCijk.Cijk),
99  globalMultiComm(epetraCijk.globalMultiComm),
100  num_global_stoch_blocks(epetraCijk.num_global_stoch_blocks),
101  k_begin(k_begin_),
102  k_end(k_end_),
103  stoch_comm(epetraCijk.stoch_comm),
104  is_parallel(epetraCijk.is_parallel),
105  stoch_row_map(epetraCijk.stoch_row_map)
106 {
107  if (k_end == -1)
108  k_end = Cijk->num_k();
109 
110  // Build Cijk tensor parallel over i
111  if (!is_parallel)
113  else
115 
116  // Build stochastic graph
118 
119  // Build stochastic column map
121 }
122 
123 void
126 {
127 #ifdef HAVE_STOKHOS_ISORROPIA
128  // Reblance with Isorropia
129  Teuchos::RCP<const Epetra_CrsGraph> rebalanced_graph =
130  Teuchos::rcp(Isorropia::Epetra::createBalancedCopy(
131  *stoch_graph, isorropia_params));
132  stoch_graph = rebalanced_graph;
133 
134  // Get new maps
135  stoch_row_map = Teuchos::rcp(&(stoch_graph->RowMap()), false);
136  stoch_col_map = Teuchos::rcp(&(stoch_graph->ColMap()), false);
137 
138  // Build new parallel Cijk
139  Cijk_parallel = buildParallelCijk();
140 #else
141  TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error,
142  "Error! Stokhos must be configured with " <<
143  "isorropia support to rebalance the stochastic " <<
144  "graph.");
145 #endif
146 }
147 
151 {
153  int *rowIndices = stoch_row_map->MyGlobalElements();
154  int myBlockRows = stoch_row_map->NumMyElements();
155  for (int i=0; i<myBlockRows; i++) {
156  int myRow = rowIndices[i];
157  Cijk_type::i_iterator i_it = Cijk->find_i(myRow);
158  for (Cijk_type::ik_iterator k_it = Cijk->k_begin(i_it);
159  k_it != Cijk->k_end(i_it); ++k_it) {
160  int k = index(k_it);
161  if (k >= k_begin && k < k_end) {
162  for (Cijk_type::ikj_iterator j_it = Cijk->j_begin(k_it);
163  j_it != Cijk->j_end(k_it); ++j_it) {
164  int j = index(j_it);
165  double c = value(j_it);
166  Cijk_tmp->add_term(myRow, j, k, c);
167  }
168  }
169  }
170  }
171  Cijk_tmp->fillComplete();
172 
173  return Cijk_tmp;
174 }
175 
176 void
179 {
181  for (Cijk_type::i_iterator i_it = Cijk_parallel->i_begin();
182  i_it != Cijk_parallel->i_end(); ++i_it) {
183  int i = stoch_row_map->LID(index(i_it));
184  TEUCHOS_TEST_FOR_EXCEPTION(i == -1, std::logic_error,
185  "Error! Global row index " << index(i_it) <<
186  " is not on processor " << globalMultiComm->MyPID());
187  for (Cijk_type::ik_iterator k_it = Cijk_parallel->k_begin(i_it);
188  k_it != Cijk_parallel->k_end(i_it); ++k_it) {
189  int k = index(k_it);
190  for (Cijk_type::ikj_iterator j_it = Cijk_parallel->j_begin(k_it);
191  j_it != Cijk_parallel->j_end(k_it); ++j_it) {
192  int j= stoch_col_map->LID(index(j_it));
193  TEUCHOS_TEST_FOR_EXCEPTION(j == -1, std::logic_error,
194  "Error! Global col index " << index(j_it) <<
195  " is not on processor " << globalMultiComm->MyPID());
196  double c = value(j_it);
197  Cijk_tmp->add_term(i, j, k, c);
198  }
199  }
200  }
201  Cijk_tmp->fillComplete();
202 
203  Cijk_parallel = Cijk_tmp;
204 }
Teuchos::RCP< const Epetra_CrsGraph > stoch_graph
Stochastic operator graph.
void transformToLocal()
Transform Cijk to local i and j indices.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Teuchos::RCP< const Epetra_BlockMap > stoch_col_map
Stochastic col-map.
const Epetra_BlockMap & ColMap() const
Bi-directional iterator for traversing a sparse array.
EpetraSparse3Tensor(const Teuchos::RCP< const Stokhos::OrthogPolyBasis< int, double > > &basis, const Teuchos::RCP< const Stokhos::Sparse3Tensor< int, double > > &Cijk, const Teuchos::RCP< const EpetraExt::MultiComm > &globalMultiComm, int k_begin=0, int k_end=-1)
Constructor from a full Cijk.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Teuchos::RCP< const Cijk_type > Cijk
Triple product.
Teuchos::RCP< const EpetraExt::MultiComm > globalMultiComm
Multi-comm.
Teuchos::RCP< const Cijk_type > Cijk_parallel
Cijk tensor parallel over i.
Teuchos::RCP< const Epetra_BlockMap > stoch_row_map
Stochastic row-map.
void rebalance(Teuchos::ParameterList &isorropia_params)
Rebalance maps and graph using Isorropia.
Teuchos::RCP< const Stokhos::OrthogPolyBasis< int, double > > basis
Basis.
virtual int NumProc() const =0
Teuchos::RCP< Epetra_CrsGraph > sparse3Tensor2CrsGraph(const Stokhos::OrthogPolyBasis< ordinal_type, value_type > &basis, const Stokhos::Sparse3Tensor< ordinal_type, value_type > &Cijk, const Epetra_Comm &comm)
Build an Epetra_CrsGraph from a sparse 3 tensor.
Teuchos::RCP< const Epetra_Comm > stoch_comm
Stochastic comm.
Teuchos::RCP< Cijk_type > buildParallelCijk() const
Build parallel Cijk tensor from a parallel row map.
int num_global_stoch_blocks
Number of global stochastic blocks.
virtual ordinal_type size() const =0
Return total size of basis.
bool is_parallel
Whether stochastic blocks are parallel.