Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cusp_sa_blockcg.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 <iostream>
11 
12 // CUSP
14 #include <cusp/gallery/poisson.h>
15 #include <cusp/csr_matrix.h>
16 #include <cusp/krylov/blockcg.h>
17 #include <cusp/block_monitor.h>
18 
19 // Utilities
21 #include "Teuchos_TimeMonitor.hpp"
23 
24 template <typename IndexType, typename ValueType, typename MemorySpace,
25  typename Orientation>
26 void cusp_sa_block_cg(IndexType nx, IndexType ny, IndexType nz, IndexType nrhs,
27  IndexType max_its, ValueType tol, bool verbose) {
28  // create an empty sparse matrix structure
29  cusp::csr_matrix<IndexType, ValueType, MemorySpace> A;
30 
31  // create 2D Poisson problem
32  cusp::gallery::poisson27pt(A, nx, ny, nz);
33  std::cout << "N =" << A.num_rows<< std::endl;
34  std::cout << "nnz of A = " << A.num_entries << std::endl;
35 
36  // solve with multiple RHS
37  std::cout << "\nSolving Ax = b with multiple RHS..." << std::endl;
38 
39  // allocate storage for solution (x) and right hand side (b)
40  cusp::array2d<ValueType, MemorySpace, Orientation> x(A.num_rows, nrhs, 0);
41  cusp::array2d<ValueType, MemorySpace, Orientation> b(A.num_rows, nrhs, 1);
42 
43  std::cout << "numRHS = " << nrhs << std::endl;
44 
45  // set stopping criteria
46  cusp::default_block_monitor<ValueType> monitor(b, max_its, tol, 0);
47 
48  // setup preconditioner
51  if (verbose) {
52  // print hierarchy information
53  std::cout << "\nPreconditioner statistics" << std::endl;
54  M.print();
55  }
56 
57  // solve
58  {
59  TEUCHOS_FUNC_TIME_MONITOR("Total Block-CG Solve Time");
60  cusp::krylov::blockcg(A, x, b, monitor, M);
61  }
62 }
63 
64 // Orientation types
65 enum Orient { ROW, COL };
66 const int num_orient = 2;
67 const Orient orient_values[] = { ROW, COL };
68 const char *orient_names[] = { "row", "col" };
69 
70 int main(int argc, char *argv[])
71 {
72  typedef int IndexType;
73  typedef double ValueType;
74  typedef cusp::device_memory MemorySpace;
75  //typedef cusp::row_major Orientation;
76 
77  bool success = true;
78  bool verbose = false;
79  try {
80 
81  // Setup command line options
83  CLP.setDocString(
84  "This example runs AMG-preconditioned block-CG with CUSP.\n");
85 
86  IndexType nx = 32;
87  CLP.setOption("nx", &nx, "Number of mesh points in the x-direction");
88  IndexType ny = 32;
89  CLP.setOption("ny", &ny, "Number of mesh points in the y-direction");
90  IndexType nz = 32;
91  CLP.setOption("nz", &nz, "Number of mesh points in the z-direction");
92  IndexType nrhs = 32;
93  CLP.setOption("nrhs", &nrhs, "Number of right-hand-sides");
94  Orient orient = ROW;
95  CLP.setOption("orient", &orient, num_orient, orient_values, orient_names,
96  "Orientation of block RHS");
97  IndexType max_its = 100;
98  CLP.setOption("max_iterations", &max_its,
99  "Maximum number of CG iterations");
100  double tol = 1e-6; // has to be double
101  CLP.setOption("tolerance", &tol, "Convergence tolerance");
102  int device_id = 0;
103  CLP.setOption("device", &device_id, "CUDA device ID");
104  CLP.setOption("verbose", "quiet", &verbose, "Verbose output");
105  CLP.parse( argc, argv );
106 
107  // Set CUDA device
108  cudaSetDevice(device_id);
109  cudaDeviceSetSharedMemConfig(cudaSharedMemBankSizeEightByte);
110 
111  if (orient == ROW)
112  cusp_sa_block_cg<IndexType,ValueType,MemorySpace,cusp::row_major>(
113  nx, ny, nz, nrhs, max_its, tol, verbose);
114  else if (orient == COL)
115  cusp_sa_block_cg<IndexType,ValueType,MemorySpace,cusp::column_major>(
116  nx, ny, nz, nrhs, max_its, tol, verbose);
117 
120  }
121  TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
122 
123  if (success)
124  return 0;
125  return -1;
126 }
#define TEUCHOS_FUNC_TIME_MONITOR(FUNCNAME)
void blockcg(LinearOperator &A, Vector &x, Vector &b)
const Orient orient_values[]
static void summarize(Ptr< const Comm< int > > comm, std::ostream &out=std::cout, const bool alwaysWriteLocal=false, const bool writeGlobalStats=true, const bool writeZeroTimers=true, const ECounterSetOp setOp=Intersection, const std::string &filter="", const bool ignoreZeroTimers=false)
void setOption(const char option_true[], const char option_false[], bool *option_val, const char documentation[]=NULL)
#define TEUCHOS_STANDARD_CATCH_STATEMENTS(VERBOSE, ERR_STREAM, SUCCESS_FLAG)
void cusp_sa_block_cg(IndexType nx, IndexType ny, IndexType nz, IndexType nrhs, IndexType max_its, ValueType tol, bool verbose)
EParseCommandLineReturn parse(int argc, char *argv[], std::ostream *errout=&std::cerr) const
int main(int argc, char **argv)
const char * orient_names[]
void setDocString(const char doc_string[])
const int num_orient
static void zeroOutTimers()