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_zoltan.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_Epetra.hpp"
13 #include "Teuchos_toString.hpp"
14 
15 #include <fstream>
16 #include <iostream>
17 
18 extern "C" {
19 #include "zoltan.h"
20 }
21 
22 // Growth policies
23 const int num_growth_types = 2;
26 const char *growth_type_names[] = { "slow", "moderate" };
27 
28 // Product Basis types
30 const int num_prod_basis_types = 4;
33 const char *prod_basis_type_names[] = {
34  "complete", "tensor", "total", "smolyak" };
35 
36 // Ordering types
38 const int num_ordering_types = 2;
41 const char *ordering_type_names[] = {
42  "total", "lexicographic" };
43 
44 // Partitioning types
46 const int num_partitioning_types = 2;
48  RCB, HG_FLAT_J };
49 const char *partitioning_type_names[] = {
50  "rcb", "hg_flat_j" };
51 
52 using Teuchos::rcp;
53 using Teuchos::RCP;
55 using Teuchos::Array;
56 using Teuchos::toString;
57 
58 struct TensorData {
62 };
63 
64 // Functions implementing hypergraph for 1-D i-wise decomposition
65 // with flattened j. For this hypergraph model
66 // * the n vertices are the i-indices (n = basis size)
67 // * the n_k hyperedges are the flattened j-k planes:
68 // hyperedge k contains vertex i if C_ijk \neq 0 for any j
69 namespace HG_1D_Flat_J {
70 
71  // Return number of vertices
72  int get_number_of_vertices(void *data, int *ierr) {
73  TensorData *td = static_cast<TensorData*>(data);
74  *ierr = ZOLTAN_OK;
75 
76  return td->basis->size();
77  }
78 
79  // Compute IDs and weights of each vertex
80  void get_vertex_list(void *data, int sizeGID, int sizeLID,
81  ZOLTAN_ID_PTR globalID, ZOLTAN_ID_PTR localID,
82  int wgt_dim, float *obj_wgts, int *ierr) {
83  TensorData *td = static_cast<TensorData*>(data);
84  *ierr = ZOLTAN_OK;
85 
86  int n = td->basis->size();
87  for (int i=0; i<n; ++i) {
88  globalID[i] = i;
89  localID[i] = i;
90  }
91 
92  // Do not set weights so Zoltan assumes equally weighted vertices
93  }
94 
95  // Compute number of hyperedges and pins
96  void get_hypergraph_size(void *data, int *num_lists, int *num_nonzeroes,
97  int *format, int *ierr) {
98  TensorData *td = static_cast<TensorData*>(data);
99  *ierr = ZOLTAN_OK;
100 
101  // Number of hyperedges
102  *num_lists = td->Cijk->num_k();
103 
104  // Number of pins == number of i's for all k's computing using
105  // the i-j symmetry
106  int num_pins = 0;
109  for (TensorData::Cijk_type::k_iterator k_it=k_begin; k_it!=k_end; ++k_it)
110  num_pins += td->Cijk->num_j(k_it);
111  *num_nonzeroes = num_pins;
112 
113  // hypergraph will be stored in compressed-edge format
114  *format = ZOLTAN_COMPRESSED_EDGE;
115  }
116 
117  // Compute hypergraph
118  void get_hypergraph(void *data, int sizeGID, int num_edges, int num_nonzeroes,
119  int format, ZOLTAN_ID_PTR edgeGID, int *vtxPtr,
120  ZOLTAN_ID_PTR vtxGID, int *ierr) {
121  TensorData *td = static_cast<TensorData*>(data);
122  *ierr = ZOLTAN_OK;
123 
124  // Compute pins in each hyperedge. For each hyperedge (k), these are
125  // all of the vertices (i) such that Cijk \neq 0 for any j. Due to i-j
126  // symmetry, this is all of the j's for each k such that Cijk \neq 0 for
127  // any i.
128  int kdx = 0, jdx = 0;
129  int num_pins = 0;
132  for (TensorData::Cijk_type::k_iterator k_it=k_begin; k_it!=k_end;
133  ++k_it, ++kdx) {
134  int k = index(k_it);
135  edgeGID[kdx] = k;
136  vtxPtr[kdx] = num_pins;
137  num_pins += td->Cijk->num_j(k_it);
138  TensorData::Cijk_type::kj_iterator j_begin = td->Cijk->j_begin(k_it);
139  TensorData::Cijk_type::kj_iterator j_end = td->Cijk->j_end(k_it);
140  for (TensorData::Cijk_type::kj_iterator j_it = j_begin; j_it != j_end;
141  ++j_it) {
142  int j = index(j_it);
143  vtxGID[jdx++] = j;
144  }
145  }
146  }
147 }
148 
149 
150 int main(int argc, char **argv)
151 {
152  try {
153 
154  // Initialize Zoltan
155  float version;
156  int rc = Zoltan_Initialize(argc,argv,&version);
157  TEUCHOS_ASSERT(rc == 0);
158 
159  // Setup command line options
161  CLP.setDocString(
162  "This example generates the sparsity pattern for the block stochastic Galerkin matrix.\n");
163  int d = 5;
164  CLP.setOption("dimension", &d, "Stochastic dimension");
165  int p = 3;
166  CLP.setOption("order", &p, "Polynomial order");
167  double drop = 1.0e-12;
168  CLP.setOption("drop", &drop, "Drop tolerance");
169  bool symmetric = true;
170  CLP.setOption("symmetric", "asymmetric", &symmetric, "Use basis polynomials with symmetric PDF");
172  CLP.setOption("growth", &growth_type,
174  "Growth type");
175  ProductBasisType prod_basis_type = TOTAL;
176  CLP.setOption("product_basis", &prod_basis_type,
179  "Product basis type");
180  OrderingType ordering_type = LEXICOGRAPHIC_ORDERING;
181  CLP.setOption("ordering", &ordering_type,
184  "Product basis ordering");
185  PartitioningType partitioning_type = RCB;
186  CLP.setOption("partitioning", &partitioning_type,
189  "Partitioning Method");
190  double imbalance_tol = 1.2;
191  CLP.setOption("imbalance", &imbalance_tol, "Imbalance tolerance");
192  bool full = true;
193  CLP.setOption("full", "linear", &full, "Use full or linear expansion");
194  int tile_size = 32;
195  CLP.setOption("tile_size", &tile_size, "Tile size");
196  bool save_3tensor = false;
197  CLP.setOption("save_3tensor", "no-save_3tensor", &save_3tensor,
198  "Save full 3tensor to file");
199  std::string file_3tensor = "Cijk.dat";
200  CLP.setOption("filename_3tensor", &file_3tensor,
201  "Filename to store full 3-tensor");
202 
203  // Parse arguments
204  CLP.parse( argc, argv );
205 
206  // Basis
208  const double alpha = 1.0;
209  const double beta = symmetric ? 1.0 : 2.0 ;
210  for (int i=0; i<d; i++) {
211  bases[i] = rcp(new Stokhos::JacobiBasis<int,double>(
212  p, alpha, beta, true, growth_type));
213  }
217  if (prod_basis_type == COMPLETE)
218  basis =
220  bases, drop));
221  else if (prod_basis_type == TENSOR) {
222  if (ordering_type == TOTAL_ORDERING)
223  basis =
225  bases, drop));
226  else if (ordering_type == LEXICOGRAPHIC_ORDERING)
227  basis =
229  bases, drop));
230  }
231  else if (prod_basis_type == TOTAL) {
232  if (ordering_type == TOTAL_ORDERING)
233  basis =
235  bases, drop));
236  else if (ordering_type == LEXICOGRAPHIC_ORDERING)
237  basis =
239  bases, drop));
240  }
241  else if (prod_basis_type == SMOLYAK) {
242  Stokhos::TotalOrderIndexSet<int> index_set(d, p);
243  if (ordering_type == TOTAL_ORDERING)
244  basis =
246  bases, index_set, drop));
247  else if (ordering_type == LEXICOGRAPHIC_ORDERING)
248  basis =
250  bases, index_set, drop));
251  }
252 
253  // Triple product tensor
255  RCP<Cijk_type> Cijk;
256  if (full)
257  Cijk = basis->computeTripleProductTensor();
258  else
259  Cijk = basis->computeLinearTripleProductTensor();
260 
261  int basis_size = basis->size();
262  std::cout << "basis size = " << basis_size
263  << " num nonzero Cijk entries = " << Cijk->num_entries()
264  << std::endl;
265 
266  // File for saving sparse Cijk tensor and parts
267  std::ofstream cijk_file;
268  if (save_3tensor) {
269  cijk_file.open(file_3tensor.c_str());
270  cijk_file.precision(14);
271  cijk_file.setf(std::ios::scientific);
272  cijk_file << "i, j, k, part" << std::endl;
273  }
274 
275  // Create zoltan
276  Zoltan_Struct *zz = Zoltan_Create(MPI_COMM_WORLD);
277 
278  // Setup Zoltan parameters
279  Zoltan_Set_Param(zz, "DEBUG_LEVEL", "2");
280 
281  // partitioning method
282  Zoltan_Set_Param(zz, "LB_METHOD", "HYPERGRAPH");
283  Zoltan_Set_Param(zz, "HYPERGRAPH_PACKAGE", "PHG"); // version of method
284  Zoltan_Set_Param(zz, "NUM_GID_ENTRIES", "1");// global IDs are integers
285  Zoltan_Set_Param(zz, "NUM_LID_ENTRIES", "1");// local IDs are integers
286  //Zoltan_Set_Param(zz, "RETURN_LISTS", "ALL"); // export AND import lists
287  Zoltan_Set_Param(zz, "RETURN_LISTS", "PARTS");
288  Zoltan_Set_Param(zz, "OBJ_WEIGHT_DIM", "0"); // use Zoltan default vertex weights
289  Zoltan_Set_Param(zz, "EDGE_WEIGHT_DIM", "0");// use Zoltan default hyperedge weights
290  int num_parts = basis_size / tile_size;
291  Zoltan_Set_Param(zz, "NUM_GLOBAL_PARTS", toString(num_parts).c_str());
292  Zoltan_Set_Param(zz, "NUM_LOCAL_PARTS", toString(num_parts).c_str());
293  Zoltan_Set_Param(zz, "IMBALANCE_TOL", toString(imbalance_tol).c_str());
294 
295  // Set query functions
296  TensorData td; td.basis = basis; td.Cijk = Cijk;
297  Zoltan_Set_Num_Obj_Fn(zz, HG_1D_Flat_J::get_number_of_vertices, &td);
298  Zoltan_Set_Obj_List_Fn(zz, HG_1D_Flat_J::get_vertex_list, &td);
299  Zoltan_Set_HG_Size_CS_Fn(zz, HG_1D_Flat_J::get_hypergraph_size, &td);
300  Zoltan_Set_HG_CS_Fn(zz, HG_1D_Flat_J::get_hypergraph, &td);
301 
302  // Partition
303  int changes, numGidEntries, numLidEntries, numImport, numExport;
304  ZOLTAN_ID_PTR importGlobalGids, importLocalGids, exportGlobalGids, exportLocalGids;
305  int *importProcs, *importToPart, *exportProcs, *exportToPart;
306  rc =
307  Zoltan_LB_Partition(
308  zz, // input (all remaining fields are output)
309  &changes, // 1 if partitioning was changed, 0 otherwise
310  &numGidEntries, // Number of integers used for a global ID
311  &numLidEntries, // Number of integers used for a local ID
312  &numImport, // Number of vertices to be sent to me
313  &importGlobalGids, // Global IDs of vertices to be sent to me
314  &importLocalGids, // Local IDs of vertices to be sent to me
315  &importProcs, // Process rank for source of each incoming vertex
316  &importToPart, // New partition for each incoming vertex
317  &numExport, // Number of vertices I must send to other processes*/
318  &exportGlobalGids, // Global IDs of the vertices I must send
319  &exportLocalGids, // Local IDs of the vertices I must send
320  &exportProcs, // Process to which I send each of the vertices
321  &exportToPart); // Partition to which each vertex will belong
322  TEUCHOS_ASSERT(rc == 0);
323 
324  std::cout << "num parts requested = " << num_parts
325  << " changes= " << changes
326  << " num import = " << numImport
327  << " num export = " << numExport << std::endl;
328 
329  // for (int i=0; i<numExport; ++i)
330  // std::cout << exportGlobalGids[i] << " " << exportToPart[i] << std::endl;
331 
332  // Build list of rows that belong to each part
333  Array< Array<int> > part_map(num_parts);
334  for (int i=0; i<numExport; ++i) {
335  part_map[ exportToPart[i] ].push_back( exportGlobalGids[i] );
336  }
337 
338  // Build permuation array mapping reoredered to original
339  Array<int> perm_new_to_old;
340  for (int part=0; part<num_parts; ++part) {
341  int num_vtx = part_map[part].size();
342  for (int i=0; i<num_vtx; ++i)
343  perm_new_to_old.push_back(part_map[part][i]);
344  }
345  TEUCHOS_ASSERT(perm_new_to_old.size() == basis_size);
346 
347  // Build permuation array mapping original to reordered
348  Array<int> perm_old_to_new(basis_size);
349  for (int i=0; i<basis_size; ++i)
350  perm_old_to_new[ perm_new_to_old[i] ] = i;
351 
352  if (save_3tensor) {
353  Cijk_type::k_iterator k_begin = Cijk->k_begin();
354  Cijk_type::k_iterator k_end = Cijk->k_end();
355  for (Cijk_type::k_iterator k_it=k_begin; k_it!=k_end; ++k_it) {
356  int k = index(k_it);
357  Cijk_type::kj_iterator j_begin = Cijk->j_begin(k_it);
358  Cijk_type::kj_iterator j_end = Cijk->j_end(k_it);
359  for (Cijk_type::kj_iterator j_it = j_begin; j_it != j_end; ++j_it) {
360  int j = index(j_it);
361  Cijk_type::kji_iterator i_begin = Cijk->i_begin(j_it);
362  Cijk_type::kji_iterator i_end = Cijk->i_end(j_it);
363  for (Cijk_type::kji_iterator i_it = i_begin; i_it != i_end; ++i_it) {
364  int i = index(i_it);
365  cijk_file << perm_old_to_new[i] << ", "
366  << perm_old_to_new[j] << ", "
367  << perm_old_to_new[k] << ", "
368  << exportToPart[i] << std::endl;
369  }
370  }
371  }
372  cijk_file.close();
373  }
374 
375  // Clean-up
376  Zoltan_LB_Free_Part(&importGlobalGids, &importLocalGids,
377  &importProcs, &importToPart);
378  Zoltan_LB_Free_Part(&exportGlobalGids, &exportLocalGids,
379  &exportProcs, &exportToPart);
380  Zoltan_Destroy(&zz);
381 
382  //Teuchos::TimeMonitor::summarize(std::cout);
383 
384  }
385  catch (std::exception& e) {
386  std::cout << e.what() << std::endl;
387  }
388 
389  return 0;
390 }
const ProductBasisType prod_basis_type_values[]
PartitioningType
k_iterator k_begin() const
Iterator pointing to first k entry.
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...
ordinal_type num_j(const k_iterator &k) const
Number of j entries in C(i,j,k) for given k.
void get_vertex_list(void *data, int sizeGID, int sizeLID, ZOLTAN_ID_PTR globalID, ZOLTAN_ID_PTR localID, int wgt_dim, float *obj_wgts, int *ierr)
ordinal_type num_k() const
Number of k entries in C(i,j,k)
kj_iterator j_begin(const k_iterator &k) const
Iterator pointing to first j entry for given k.
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 char * partitioning_type_names[]
const char * toString(const EReductionType reductType)
const int num_ordering_types
A comparison functor implementing a strict weak ordering based total-order ordering, recursive on the dimension.
kj_iterator j_end(const k_iterator &k) const
Iterator pointing to last j entry for given k.
Bi-directional iterator for traversing a sparse array.
RCP< const Stokhos::ProductBasis< int, double > > basis
OrderingType
ProductBasisType
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
void setOption(const char option_true[], const char option_false[], bool *option_val, const char documentation[]=NULL)
const int num_growth_types
std::string toString(const HashSet< Key > &h)
Jacobi polynomial basis.
EParseCommandLineReturn parse(int argc, char *argv[], std::ostream *errout=&std::cerr) const
int get_number_of_vertices(void *data, int *ierr)
const int num_partitioning_types
const Stokhos::GrowthPolicy growth_type_values[]
Multivariate orthogonal polynomial basis generated from a Smolyak sparse grid.
k_iterator k_end() const
Iterator pointing to last k entry.
Multivariate orthogonal polynomial basis generated from a tensor product of univariate polynomials...
Stokhos::Sparse3Tensor< int, double > Cijk_type
int main(int argc, char **argv)
void push_back(const value_type &x)
An isotropic total order index set.
void setDocString(const char doc_string[])
size_type size() const
A comparison functor implementing a strict weak ordering based lexographic ordering.
Stokhos::Sparse3Tensor< int, double > Cijk_type
void get_hypergraph_size(void *data, int *num_lists, int *num_nonzeroes, int *format, int *ierr)
#define TEUCHOS_ASSERT(assertion_test)
RCP< const Stokhos::Sparse3Tensor< int, double > > Cijk
const char * ordering_type_names[]
int n
const char * prod_basis_type_names[]
virtual ordinal_type size() const =0
Return total size of basis.
void get_hypergraph(void *data, int sizeGID, int num_edges, int num_nonzeroes, int format, ZOLTAN_ID_PTR edgeGID, int *vtxPtr, ZOLTAN_ID_PTR vtxGID, int *ierr)
const PartitioningType partitioning_type_values[]