Sacado Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
vector_blas_example.cpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Sacado Package
4 //
5 // Copyright 2006 NTESS and the Sacado contributors.
6 // SPDX-License-Identifier: LGPL-2.1-or-later
7 // *****************************************************************************
8 // @HEADER
9 
10 // vector_blas_example
11 //
12 // usage:
13 // vector_blas_example
14 //
15 // output:
16 // prints the results of differentiating a BLAS routine with forward
17 // mode AD using the Sacado::Fad::DVFad class (uses dynamic memory
18 // allocation for number of derivative components stored in a contiguous
19 // array).
20 
21 #include <iostream>
22 #include <iomanip>
23 
24 #include "Sacado_No_Kokkos.hpp"
25 #include "Teuchos_BLAS.hpp"
26 #include "Sacado_Fad_BLAS.hpp"
27 
29 
30 int main(int argc, char **argv)
31 {
32  const unsigned int n = 5;
34  for (unsigned int i=0; i<n; i++) {
35  for (unsigned int j=0; j<n; j++)
38  for (unsigned int j=0; j<n; j++)
40  C[i] = 0.0;
41  }
42 
43  double *a = A.vals();
44  double *b = B.vals();
45  double *bdx = B.dx();
46  std::vector<double> c(n), cdx(n*n);
47 
49  blas.GEMV(Teuchos::NO_TRANS, n, n, 1.0, &a[0], n, &b[0], 1, 0.0, &c[0], 1);
50  blas.GEMM(Teuchos::NO_TRANS, Teuchos::NO_TRANS, n, n, n, 1.0, &a[0], n, &bdx[0], n, 0.0, &cdx[0], n);
51 
52  // Teuchos::BLAS<int,FadType> blas_fad;
53  // blas_fad.GEMV(Teuchos::NO_TRANS, n, n, 1.0, &A[0], n, &B[0], 1, 0.0, &C[0], 1);
54 
55  Teuchos::BLAS<int,FadType> sacado_fad_blas(false);
56  sacado_fad_blas.GEMV(Teuchos::NO_TRANS, n, n, 1.0, &A[0], n, &B[0], 1, 0.0, &C[0], 1);
57 
58  // Print the results
59  int p = 4;
60  int w = p+7;
61  std::cout.setf(std::ios::scientific);
62  std::cout.precision(p);
63 
64  std::cout << "BLAS GEMV calculation:" << std::endl;
65  std::cout << "a = " << std::endl;
66  for (unsigned int i=0; i<n; i++) {
67  for (unsigned int j=0; j<n; j++)
68  std::cout << " " << std::setw(w) << a[i+j*n];
69  std::cout << std::endl;
70  }
71  std::cout << "b = " << std::endl;
72  for (unsigned int i=0; i<n; i++) {
73  std::cout << " " << std::setw(w) << b[i];
74  }
75  std::cout << std::endl;
76  std::cout << "bdot = " << std::endl;
77  for (unsigned int i=0; i<n; i++) {
78  for (unsigned int j=0; j<n; j++)
79  std::cout << " " << std::setw(w) << bdx[i+j*n];
80  std::cout << std::endl;
81  }
82  std::cout << "c = " << std::endl;
83  for (unsigned int i=0; i<n; i++) {
84  std::cout << " " << std::setw(w) << c[i];
85  }
86  std::cout << std::endl;
87  std::cout << "cdot = " << std::endl;
88  for (unsigned int i=0; i<n; i++) {
89  for (unsigned int j=0; j<n; j++)
90  std::cout << " " << std::setw(w) << cdx[i+j*n];
91  std::cout << std::endl;
92  }
93  std::cout << std::endl << std::endl;
94 
95  std::cout << "FAD BLAS GEMV calculation:" << std::endl;
96  std::cout << "A.val() (should = a) = " << std::endl;
97  for (unsigned int i=0; i<n; i++) {
98  for (unsigned int j=0; j<n; j++)
99  std::cout << " " << std::setw(w) << A[i+j*n].val();
100  std::cout << std::endl;
101  }
102  std::cout << "B.val() (should = b) = " << std::endl;
103  for (unsigned int i=0; i<n; i++) {
104  std::cout << " " << std::setw(w) << B[i].val();
105  }
106  std::cout << std::endl;
107  std::cout << "B.dx() (should = bdot) = " << std::endl;
108  double *Bdx = B.dx();
109  for (unsigned int i=0; i<n; i++) {
110  for (unsigned int j=0; j<n; j++)
111  std::cout << " " << std::setw(w) << Bdx[i+j*n];
112  std::cout << std::endl;
113  }
114  std::cout << "C.val() (should = c) = " << std::endl;
115  for (unsigned int i=0; i<n; i++) {
116  std::cout << " " << std::setw(w) << C[i].val();
117  }
118  std::cout << std::endl;
119  std::cout << "C.dx() (should = cdot) = " << std::endl;
120  double *Cdx = C.dx();
121  for (unsigned int i=0; i<n; i++) {
122  for (unsigned int j=0; j<n; j++)
123  std::cout << " " << std::setw(w) << Cdx[i+j*n];
124  std::cout << std::endl;
125  }
126 
127  double tol = 1.0e-14;
128  bool failed = false;
129  for (unsigned int i=0; i<n; i++) {
130  if (std::fabs(C[i].val() - c[i]) > tol)
131  failed = true;
132  for (unsigned int j=0; j<n; j++) {
133  if (std::fabs(C[i].dx(j) - cdx[i+j*n]) > tol)
134  failed = true;
135  }
136  }
137  if (!failed) {
138  std::cout << "\nExample passed!" << std::endl;
139  return 0;
140  }
141  else {
142  std::cout <<"\nSomething is wrong, example failed!" << std::endl;
143  return 1;
144  }
145 }
const char * p
expr expr dx(i)
void GEMV(ETransp trans, const OrdinalType &m, const OrdinalType &n, const alpha_type alpha, const A_type *A, const OrdinalType &lda, const x_type *x, const OrdinalType &incx, const beta_type beta, ScalarType *y, const OrdinalType &incy) const
Sacado::Fad::DFad< double > FadType
A class for storing a contiguously allocated array of Fad objects. This is a general definition that ...
expr val()
#define C(x)
expr expr1 expr1 expr1 c expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr1 c expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr1 c *expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr1 c expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr2 expr1 expr2 expr1 expr1 expr1 c
#define A
Definition: Sacado_rad.hpp:552
int main()
Definition: ad_example.cpp:171
Forward-mode AD class using dynamic memory allocation and expression templates.
void GEMM(ETransp transa, ETransp transb, const int &m, const int &n, const int &k, const double &alpha, const double *A, const int &lda, const double *B, const int &ldb, const double &beta, double *C, const int &ldc) const
expr expr expr fastAccessDx(i)) FAD_UNARYOP_MACRO(exp
const double tol
void GEMV(ETransp trans, const int &m, const int &n, const double &alpha, const double *A, const int &lda, const double *x, const int &incx, const double &beta, double *y, const int &incy) const
fabs(expr.val())
int n