Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TestAMG.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 Orientation,
25  typename IndexType, typename ValueType, typename MemorySpace>
27  const cusp::csr_matrix<IndexType, ValueType, MemorySpace>& A,
28  IndexType nrhs, IndexType max_its, ValueType tol) {
29 
30  // allocate storage for solution (x) and right hand side (b)
31  cusp::array2d<ValueType, MemorySpace, Orientation> x(A.num_rows, nrhs, 0);
32  cusp::array2d<ValueType, MemorySpace, Orientation> b(A.num_rows, nrhs, 1);
33 
34  // set stopping criteria
35  cusp::default_block_monitor<ValueType> monitor(b, max_its, tol, 0.0, false);
36 
37  // setup preconditioner
40 
41  // solve
42  {
43  TEUCHOS_FUNC_TIME_MONITOR("Total Block-CG Solve Time");
44  cusp::krylov::blockcg(A, x, b, monitor, M);
45  }
46 }
47 
48 int main(int argc, char *argv[])
49 {
50  typedef int IndexType;
51  typedef double ValueType;
52  typedef cusp::device_memory MemorySpace;
53  //typedef cusp::row_major Orientation;
54 
55  bool success = true;
56  bool verbose = false;
57  try {
58 
59  // Setup command line options
61  CLP.setDocString("This test performance of block multiply routines.\n");
62  IndexType n = 32;
63  CLP.setOption("n", &n, "Number of mesh points in the each direction");
64  IndexType nrhs_begin = 32;
65  CLP.setOption("begin", &nrhs_begin,
66  "Staring number of right-hand-sides");
67  IndexType nrhs_end = 512;
68  CLP.setOption("end", &nrhs_end,
69  "Ending number of right-hand-sides");
70  IndexType nrhs_step = 32;
71  CLP.setOption("step", &nrhs_step,
72  "Increment in number of right-hand-sides");
73  IndexType max_its = 100;
74  CLP.setOption("max_iterations", &max_its,
75  "Maximum number of CG iterations");
76  double tol = 1e-6; // has to be double
77  CLP.setOption("tolerance", &tol, "Convergence tolerance");
78  int device_id = 0;
79  CLP.setOption("device", &device_id, "CUDA device ID");
80  CLP.parse( argc, argv );
81 
82  // Set CUDA device
83  cudaSetDevice(device_id);
84  cudaDeviceSetSharedMemConfig(cudaSharedMemBankSizeEightByte);
85 
86  // create 3D Poisson problem
87  cusp::csr_matrix<IndexType, ValueType, MemorySpace> A;
88  cusp::gallery::poisson27pt(A, n, n, n);
89 
90  // Create timers
92  Teuchos::TimeMonitor::getNewTimer("Total Block-CG Solve Time");
94  Teuchos::TimeMonitor::getNewTimer("CUSP Block Multilevel Solve");
95  Teuchos::RCP<Teuchos::Time> tm_coarse =
96  Teuchos::TimeMonitor::getNewTimer("CUSP Coarse-grid Solve");
98  Teuchos::TimeMonitor::getNewTimer("CUSP Operator block-apply");
99  Teuchos::RCP<Teuchos::Time> tm_prec_op =
100  Teuchos::TimeMonitor::getNewTimer("CUSP Matrix block-apply");
101 
102  std::cout << "nrhs , num_rows , num_entries , "
103  << "row_cg , row_op , row_prec , row_prec_op , row_coarse , "
104  << "col_cg , col_op , col_prec , col_prec_op , col_coarse"
105  << std::endl;
106 
107  for (IndexType nrhs = nrhs_begin; nrhs <= nrhs_end; nrhs += nrhs_step) {
108 
109  std::cout << nrhs << " , "
110  << A.num_rows << " , " << A.num_entries << " , ";
111 
112  // test row-major storage
114  cusp_sa_block_cg<cusp::row_major>(A, nrhs, max_its, tol);
115 
116  std::cout << tm_cg->totalElapsedTime() << " , "
117  << tm_op->totalElapsedTime() << " , "
118  << tm_prec->totalElapsedTime() << " , "
119  << tm_prec_op->totalElapsedTime() << " , "
120  << tm_coarse->totalElapsedTime() << " , ";
121 
122  // test column-major storage
124  cusp_sa_block_cg<cusp::column_major>(A, nrhs, max_its, tol);
125 
126  std::cout << tm_cg->totalElapsedTime() << " , "
127  << tm_op->totalElapsedTime() << " , "
128  << tm_prec->totalElapsedTime() << " , "
129  << tm_prec_op->totalElapsedTime() << " , "
130  << tm_coarse->totalElapsedTime() << std::endl;
131 
132  }
133 
134  }
135  TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
136 
137  if (success)
138  return 0;
139  return -1;
140 }
#define TEUCHOS_FUNC_TIME_MONITOR(FUNCNAME)
void blockcg(LinearOperator &A, Vector &x, Vector &b)
static RCP< Time > getNewTimer(const std::string &name)
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)
void setDocString(const char doc_string[])
double totalElapsedTime(bool readCurrentTime=false) const
int n
static void zeroOutTimers()