Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cijk_partition_rcb.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.hpp"
12 
14 
15 #include <fstream>
16 #include <iostream>
17 
18 // Growth policies
19 const int num_growth_types = 2;
22 const char *growth_type_names[] = { "slow", "moderate" };
23 
24 // Product Basis types
26 const int num_prod_basis_types = 4;
29 const char *prod_basis_type_names[] = {
30  "complete", "tensor", "total", "smolyak" };
31 
32 // Ordering types
34 const int num_ordering_types = 2;
37 const char *ordering_type_names[] = {
38  "total", "lexicographic" };
39 
40 // Symmetry types
41 const int num_symmetry_types = 3;
46 };
47 const char *symmetry_type_names[] = {
48  "none", "2-way", "6-way" };
49 
50 using Teuchos::rcp;
51 using Teuchos::RCP;
52 using Teuchos::Array;
53 using Teuchos::ArrayView;
54 using Teuchos::ArrayRCP;
55 
56 int main(int argc, char **argv)
57 {
58  try {
59 
60  // Setup command line options
62  CLP.setDocString(
63  "This example generates the sparsity pattern for the block stochastic Galerkin matrix.\n");
64  int d = 5;
65  CLP.setOption("dimension", &d, "Stochastic dimension");
66  int p = 3;
67  CLP.setOption("order", &p, "Polynomial order");
68  double drop = 1.0e-12;
69  CLP.setOption("drop", &drop, "Drop tolerance");
70  bool symmetric = true;
71  CLP.setOption("symmetric", "asymmetric", &symmetric, "Use basis polynomials with symmetric PDF");
73  CLP.setOption("growth", &growth_type,
75  "Growth type");
76  ProductBasisType prod_basis_type = TOTAL;
77  CLP.setOption("product_basis", &prod_basis_type,
80  "Product basis type");
81  OrderingType ordering_type = LEXICOGRAPHIC_ORDERING;
82  CLP.setOption("ordering", &ordering_type,
85  "Product basis ordering");
87  CLP.setOption("symmetry", &symmetry_type,
90  "Cijk symmetry type");
91  bool full = true;
92  CLP.setOption("full", "linear", &full, "Use full or linear expansion");
93  int tile_size = 32;
94  CLP.setOption("tile_size", &tile_size, "Tile size");
95  bool save_3tensor = false;
96  CLP.setOption("save_3tensor", "no-save_3tensor", &save_3tensor,
97  "Save full 3tensor to file");
98  std::string file_3tensor = "Cijk.dat";
99  CLP.setOption("filename_3tensor", &file_3tensor,
100  "Filename to store full 3-tensor");
101 
102  // Parse arguments
103  CLP.parse( argc, argv );
104 
105  // Basis
107  const double alpha = 1.0;
108  const double beta = symmetric ? 1.0 : 2.0 ;
109  for (int i=0; i<d; i++) {
110  bases[i] = rcp(new Stokhos::JacobiBasis<int,double>(
111  p, alpha, beta, true, growth_type));
112  }
116  if (prod_basis_type == COMPLETE)
117  basis =
119  bases, drop));
120  else if (prod_basis_type == TENSOR) {
121  if (ordering_type == TOTAL_ORDERING)
122  basis =
124  bases, drop));
125  else if (ordering_type == LEXICOGRAPHIC_ORDERING)
126  basis =
128  bases, drop));
129  }
130  else if (prod_basis_type == TOTAL) {
131  if (ordering_type == TOTAL_ORDERING)
132  basis =
134  bases, drop));
135  else if (ordering_type == LEXICOGRAPHIC_ORDERING)
136  basis =
138  bases, drop));
139  }
140  else if (prod_basis_type == SMOLYAK) {
141  Stokhos::TotalOrderIndexSet<int> index_set(d, p);
142  if (ordering_type == TOTAL_ORDERING)
143  basis =
145  bases, index_set, drop));
146  else if (ordering_type == LEXICOGRAPHIC_ORDERING)
147  basis =
149  bases, index_set, drop));
150  }
151 
152  // Triple product tensor
154  RCP<Cijk_type> Cijk;
155  if (full)
156  Cijk = basis->computeTripleProductTensor();
157  else
158  Cijk = basis->computeLinearTripleProductTensor();
159 
160  int basis_size = basis->size();
161  std::cout << "basis size = " << basis_size
162  << " num nonzero Cijk entries = " << Cijk->num_entries()
163  << std::endl;
164 
165  // File for saving sparse Cijk tensor and parts
166  std::ofstream cijk_file;
167  if (save_3tensor) {
168  cijk_file.open(file_3tensor.c_str());
169  cijk_file.precision(14);
170  cijk_file.setf(std::ios::scientific);
171  cijk_file << "i, j, k, part" << std::endl;
172  }
173 
174  // Build tensor data list
176  Stokhos::build_cijk_coordinate_list(*Cijk, symmetry_type);
177 
178  // Partition via RCB
180  rcb_type rcb(tile_size, 10000, coordinate_list());
181  int num_parts = rcb.get_num_parts();
182  std::cout << "num parts = " << num_parts << std::endl;
183 
184  // Print part sizes
185  RCP< Array< RCP<rcb_type::Box> > > parts = rcb.get_parts();
186  for (int i=0; i<num_parts; ++i) {
187  RCP<rcb_type::Box> box = (*parts)[i];
188  std::cout << "part " << i << " bounding box ="
189  << " [ " << box->delta_x << ", " << box->delta_y << ", "
190  << box->delta_z << " ]" << " nnz = "
191  << box->coords.size() << std::endl;
192  }
193 
194  if (save_3tensor) {
195  ArrayRCP<int> part_ids = rcb.get_part_IDs();
196  int idx = 0;
197  Cijk_type::k_iterator k_begin = Cijk->k_begin();
198  Cijk_type::k_iterator k_end = Cijk->k_end();
199  for (Cijk_type::k_iterator k_it=k_begin; k_it!=k_end; ++k_it) {
200  int k = index(k_it);
201  Cijk_type::kj_iterator j_begin = Cijk->j_begin(k_it);
202  Cijk_type::kj_iterator j_end = Cijk->j_end(k_it);
203  for (Cijk_type::kj_iterator j_it = j_begin; j_it != j_end; ++j_it) {
204  int j = index(j_it);
205  Cijk_type::kji_iterator i_begin = Cijk->i_begin(j_it);
206  Cijk_type::kji_iterator i_end = Cijk->i_end(j_it);
207  for (Cijk_type::kji_iterator i_it = i_begin; i_it != i_end; ++i_it) {
208  int i = index(i_it);
209  if ((symmetry_type == Stokhos::CIJK_NO_SYMMETRY) ||
210  (symmetry_type == Stokhos::CIJK_TWO_WAY_SYMMETRY && j >= k) ||
211  (symmetry_type == Stokhos::CIJK_SIX_WAY_SYMMETRY && i >= j && j >= k)) {
212  cijk_file << i << ", " << j << ", " << k << ", "
213  << part_ids[idx++] << std::endl;
214  }
215  }
216  }
217  }
218  cijk_file.close();
219  }
220 
221  //Teuchos::TimeMonitor::summarize(std::cout);
222 
223  }
224  catch (std::exception& e) {
225  std::cout << e.what() << std::endl;
226  }
227 
228  return 0;
229 }
const ProductBasisType prod_basis_type_values[]
SparseArrayIterator< index_iterator, value_iterator >::value_type index(const SparseArrayIterator< index_iterator, value_iterator > &it)
Multivariate orthogonal polynomial basis generated from a total order tensor product of univariate po...
const char * symmetry_type_names[]
const int num_prod_basis_types
GrowthPolicy
Enumerated type for determining Smolyak growth policies.
const char * growth_type_names[]
const OrderingType ordering_type_values[]
const int num_ordering_types
A comparison functor implementing a strict weak ordering based total-order ordering, recursive on the dimension.
OrderingType
const Stokhos::CijkSymmetryType symmetry_type_values[]
ProductBasisType
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
const int num_symmetry_types
void setOption(const char option_true[], const char option_false[], bool *option_val, const char documentation[]=NULL)
const int num_growth_types
Jacobi polynomial basis.
EParseCommandLineReturn parse(int argc, char *argv[], std::ostream *errout=&std::cerr) const
const Stokhos::GrowthPolicy growth_type_values[]
Multivariate orthogonal polynomial basis generated from a Smolyak sparse grid.
Multivariate orthogonal polynomial basis generated from a tensor product of univariate polynomials...
Stokhos::Sparse3Tensor< int, double > Cijk_type
int main(int argc, char **argv)
An isotropic total order index set.
void setDocString(const char doc_string[])
A comparison functor implementing a strict weak ordering based lexographic ordering.
Teuchos::ArrayRCP< CijkData< ordinal_type, scalar_type > > build_cijk_coordinate_list(const Sparse3Tensor< ordinal_type, scalar_type > &Cijk, CijkSymmetryType symmetry_type)
const char * ordering_type_names[]
const char * prod_basis_type_names[]