Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
TestSpMv.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Stokhos Package
5 // Copyright (2009) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Eric T. Phipps (etphipp@sandia.gov).
38 //
39 // ***********************************************************************
40 // @HEADER
41 #include <iostream>
42 
43 // MP::Vector and Matrix
45 #include "Kokkos_Sparse_CrsMatrix.hpp"
47 
48 // Compile-time loops
49 #include "Sacado_mpl_range_c.hpp"
50 #include "Sacado_mpl_for_each.hpp"
51 #include "Sacado_mpl_integral_c.hpp"
52 
53 // Utilities
54 #include "impl/Kokkos_Timer.hpp"
55 
56 template< typename IntType >
57 inline
58 IntType map_fem_graph_coord( const IntType & N ,
59  const IntType & i ,
60  const IntType & j ,
61  const IntType & k )
62 {
63  return k + N * ( j + N * i );
64 }
65 
66 inline
67 size_t generate_fem_graph( size_t N ,
68  std::vector< std::vector<size_t> > & graph )
69 {
70  graph.resize( N * N * N , std::vector<size_t>() );
71 
72  size_t total = 0 ;
73 
74  for ( int i = 0 ; i < (int) N ; ++i ) {
75  for ( int j = 0 ; j < (int) N ; ++j ) {
76  for ( int k = 0 ; k < (int) N ; ++k ) {
77 
78  const size_t row = map_fem_graph_coord((int)N,i,j,k);
79 
80  graph[row].reserve(27);
81 
82  for ( int ii = -1 ; ii < 2 ; ++ii ) {
83  for ( int jj = -1 ; jj < 2 ; ++jj ) {
84  for ( int kk = -1 ; kk < 2 ; ++kk ) {
85  if ( 0 <= i + ii && i + ii < (int) N &&
86  0 <= j + jj && j + jj < (int) N &&
87  0 <= k + kk && k + kk < (int) N ) {
88  size_t col = map_fem_graph_coord((int)N,i+ii,j+jj,k+kk);
89 
90  graph[row].push_back(col);
91  }
92  }}}
93  total += graph[row].size();
94  }}}
95 
96  return total ;
97 }
98 
99 template <typename StorageType, typename MultiplyTag>
100 std::vector<double>
101 test_mpvector_spmv(const int ensemble_length,
102  const int nGrid,
103  const int iterCount,
104  Kokkos::DeviceConfig dev_config,
105  MultiplyTag tag)
106 {
107  typedef StorageType storage_type;
108  typedef typename storage_type::value_type value_type;
109  typedef typename storage_type::ordinal_type ordinal_type;
111  typedef Sacado::MP::Vector<StorageType> VectorType;
112  typedef Kokkos::LayoutRight Layout;
113  typedef Kokkos::View< VectorType*, Layout, execution_space > vector_type;
114  typedef Kokkos::CrsMatrix< VectorType, ordinal_type, execution_space > matrix_type;
115  typedef typename matrix_type::StaticCrsGraphType matrix_graph_type;
116  typedef typename matrix_type::values_type matrix_values_type;
117 
118  //------------------------------
119  // Generate graph for "FEM" box structure:
120 
121  std::vector< std::vector<size_t> > fem_graph;
122  const size_t fem_length = nGrid * nGrid * nGrid;
123  const size_t graph_length = generate_fem_graph( nGrid , fem_graph );
124 
125  //------------------------------
126  // Generate input multivector:
127 
128  vector_type x =
129  vector_type(Kokkos::ViewAllocateWithoutInitializing("x"), fem_length, ensemble_length);
130  vector_type y =
131  vector_type(Kokkos::ViewAllocateWithoutInitializing("y"), fem_length, ensemble_length);
132 
133  //------------------------------
134 
135  matrix_graph_type matrix_graph =
136  Kokkos::create_staticcrsgraph<matrix_graph_type>(
137  std::string("test crs graph"), fem_graph);
138  matrix_values_type matrix_values =
139  matrix_values_type(Kokkos::ViewAllocateWithoutInitializing("matrix"), graph_length, ensemble_length);
140  matrix_type matrix("block_matrix", fem_length, matrix_values, matrix_graph);
141  matrix.dev_config = dev_config;
142 
143  //------------------------------
144  // Fill:
145 
146  {
147  // The VectorType may be dynamic (with allocated memory)
148  // so cannot pass a VectorType value to the device.
149  // Get an array-of-intrinsic View and fill that view.
150  typename vector_type::array_type xx( x );
151  typename vector_type::array_type yy( y );
152  typename matrix_values_type::array_type mm( matrix_values );
153 
154  Kokkos::deep_copy( xx , value_type(1.0) );
155  Kokkos::deep_copy( yy , value_type(1.0) );
156  Kokkos::deep_copy( mm , value_type(1.0) );
157  }
158 
159  //------------------------------
160 
161  // One iteration to warm up
162  Stokhos::multiply( matrix, x, y, tag );
163 
164  execution_space::fence();
165  Kokkos::Impl::Timer clock ;
166  for (int iter = 0; iter < iterCount; ++iter) {
167  Stokhos::multiply( matrix, x, y, tag );
168  }
169  execution_space::fence();
170 
171  const double seconds_per_iter = clock.seconds() / ((double) iterCount );
172  const double flops = 1.0e-9 * 2.0 * graph_length * ensemble_length;
173 
174  std::vector<double> perf(5);
175  perf[0] = fem_length;
176  perf[1] = ensemble_length;
177  perf[2] = graph_length;
178  perf[3] = seconds_per_iter;
179  perf[4] = flops / seconds_per_iter;
180  return perf;
181 }
182 
183 template <typename ScalarType, typename OrdinalType, typename Device>
184 std::vector<double>
185 test_scalar_spmv(const int ensemble_length,
186  const int nGrid,
187  const int iterCount,
188  Kokkos::DeviceConfig dev_config)
189 {
190  typedef ScalarType value_type;
191  typedef OrdinalType ordinal_type;
192  typedef Device execution_space;
193  typedef Kokkos::View< value_type*, execution_space > vector_type;
194  typedef Kokkos::CrsMatrix< value_type, ordinal_type, execution_space > matrix_type;
195  typedef typename matrix_type::StaticCrsGraphType matrix_graph_type;
196  typedef typename matrix_type::values_type matrix_values_type;
197 
198  //------------------------------
199  // Generate graph for "FEM" box structure:
200 
201  std::vector< std::vector<size_t> > fem_graph;
202  const size_t fem_length = nGrid * nGrid * nGrid;
203  const size_t graph_length = generate_fem_graph( nGrid , fem_graph );
204 
205  //------------------------------
206  // Generate input multivector:
207 
208  std::vector<vector_type> x(ensemble_length);
209  std::vector<vector_type> y(ensemble_length);
210  for (int e=0; e<ensemble_length; ++e) {
211  x[e] = vector_type(Kokkos::ViewAllocateWithoutInitializing("x"), fem_length);
212  y[e] = vector_type(Kokkos::ViewAllocateWithoutInitializing("y"), fem_length);
213 
214  Kokkos::deep_copy( x[e] , value_type(1.0) );
215  Kokkos::deep_copy( y[e] , value_type(0.0) );
216  }
217 
218  //------------------------------
219 
220  std::vector<matrix_type> matrix(ensemble_length);
221  for (int e=0; e<ensemble_length; ++e) {
222  matrix_graph_type matrix_graph =
223  Kokkos::create_staticcrsgraph<matrix_graph_type>(
224  std::string("test crs graph"), fem_graph);
225  matrix_values_type matrix_values =
226  matrix_values_type(Kokkos::ViewAllocateWithoutInitializing("matrix"), graph_length);
227  matrix[e] = matrix_type("matrix", fem_length, matrix_values, matrix_graph);
228 
229  Kokkos::deep_copy( matrix[e].values , value_type(1.0) );
230  }
231 
232  //------------------------------
233 
234  // One iteration to warm up
235  for (int iter = 0; iter < iterCount; ++iter) {
236  for (int e=0; e<ensemble_length; ++e) {
237  KokkosSparse::spmv( "N" , value_type(1.0), matrix[e], x[e] , value_type(0.0), y[e]);
238  }
239  }
240 
241  execution_space::fence();
242  Kokkos::Impl::Timer clock ;
243  for (int iter = 0; iter < iterCount; ++iter) {
244  for (int e=0; e<ensemble_length; ++e) {
245  KokkosSparse::spmv( "N" , value_type(1.0), matrix[e], x[e] , value_type(0.0), y[e]);
246  }
247  }
248  execution_space::fence();
249 
250  const double seconds_per_iter = clock.seconds() / ((double) iterCount );
251  const double flops = 1.0e-9 * 2.0 * graph_length * ensemble_length;
252 
253  std::vector<double> perf(5);
254  perf[0] = fem_length;
255  perf[1] = ensemble_length;
256  perf[2] = graph_length;
257  perf[3] = seconds_per_iter;
258  perf[4] = flops / seconds_per_iter;
259  return perf;
260 }
261 
262 template <class Storage>
263 struct PerformanceDriverOp {
264  typedef typename Storage::value_type Scalar;
265  typedef typename Storage::ordinal_type Ordinal;
267  const int nGrid, nIter;
268  Kokkos::DeviceConfig dev_config;
269 
270  PerformanceDriverOp(const int nGrid_, const int nIter_,
271  Kokkos::DeviceConfig dev_config_) :
272  nGrid(nGrid_), nIter(nIter_), dev_config(dev_config_) {}
273 
274  template <typename ArgT>
275  void operator() (ArgT arg) const {
276  const int ensemble = ArgT::value;
277  typedef typename Storage::template apply_N<ensemble> NewStorageApply;
278  typedef typename NewStorageApply::type storage_type;
279 
280  const std::vector<double> perf_scalar =
281  test_scalar_spmv<Scalar,Ordinal,Device>(
282  ensemble, nGrid, nIter, dev_config );
283 
284  const std::vector<double> perf_mpvector =
285  test_mpvector_spmv<storage_type>(
287 
288  std::cout << nGrid << " , "
289  << perf_scalar[0] << " , "
290  << perf_scalar[2] << " , "
291  << perf_scalar[1] << " , "
292  << perf_scalar[3] << " , "
293  << perf_scalar[4] / perf_scalar[4] << " , "
294  << perf_scalar[4] << " , "
295  << perf_mpvector[4]/ perf_scalar[4] << " , "
296  << perf_mpvector[4] << " , "
297  << std::endl;
298  }
299 };
300 
301 template <class Storage, int entry_min, int entry_max, int entry_step>
302 void performance_test_driver( const int nGrid,
303  const int nIter,
304  Kokkos::DeviceConfig dev_config)
305 {
306  std::cout.precision(8);
307  std::cout << std::endl
308  << "\"Grid Size\" , "
309  << "\"FEM Size\" , "
310  << "\"FEM Graph Size\" , "
311  << "\"Ensemble Size\" , "
312  << "\"Scalar SpMv Time\" , "
313  << "\"Scalar SpMv Speedup\" , "
314  << "\"Scalar SpMv GFLOPS\" , "
315  << "\"MPVector SpMv Speedup\" , "
316  << "\"MPVector SpMv GFLOPS\" , "
317  << std::endl;
318 
319  // Loop over [entry_min, entry_max] vector entries per thread
320  typedef Sacado::mpl::range_c< int, entry_min, entry_max+1, entry_step > Range;
321  PerformanceDriverOp<Storage> op(nGrid, nIter, dev_config);
322  Sacado::mpl::for_each_no_kokkos<Range> f(op);
323 }
Stokhos::StandardStorage< int, double > storage_type
Stokhos_MV_Multiply_Op< Stokhos::DefaultMultiply > DefaultMultiply
Kokkos::DefaultExecutionSpace execution_space
void multiply(const CrsMatrix< MatrixValue, Device, Layout > &A, const InputMultiVectorType &x, OutputMultiVectorType &y, const std::vector< OrdinalType > &col_indices, SingleColumnMultivectorMultiply)
const IndexType const IndexType const IndexType const IndexType const ValueType const ValueType * x
Definition: csr_vector.h:260
IntType map_fem_graph_coord(const IntType &N, const IntType &i, const IntType &j, const IntType &k)
Definition: TestSpMv.hpp:58
void performance_test_driver(const int nGrid, const int nIter, Kokkos::DeviceConfig dev_config)
Definition: TestSpMv.hpp:302
Storage::execution_space Device
Definition: TestSpMv.hpp:266
Storage::value_type Scalar
Definition: TestSpMv.hpp:264
size_t generate_fem_graph(size_t N, std::vector< std::vector< size_t > > &graph)
Definition: TestSpMv.hpp:67
Storage::ordinal_type Ordinal
Definition: TestSpMv.hpp:265
void deep_copy(const Stokhos::CrsMatrix< ValueType, DstDevice, Layout > &dst, const Stokhos::CrsMatrix< ValueType, SrcDevice, Layout > &src)
expr expr expr expr j
std::vector< double > test_scalar_spmv(const int ensemble_length, const int nGrid, const int iterCount, Kokkos::DeviceConfig dev_config)
Definition: TestSpMv.hpp:185
Kokkos::DeviceConfig dev_config
Definition: TestSpMv.hpp:268
Kokkos::Example::FENL::DeviceConfig dev_config
ScalarType f(const Teuchos::Array< ScalarType > &x, double a, double b)
std::vector< double > test_mpvector_spmv(const int ensemble_length, const int nGrid, const int iterCount, Kokkos::DeviceConfig dev_config, MultiplyTag tag)
Definition: TestSpMv.hpp:101
PerformanceDriverOp(const int nGrid_, const int nIter_, Kokkos::DeviceConfig dev_config_)
Definition: TestSpMv.hpp:270
const IndexType const IndexType const IndexType const IndexType const ValueType const ValueType ValueType * y
Definition: csr_vector.h:267
std::enable_if< Kokkos::is_view_uq_pce< Kokkos::View< InputType, InputP... > >::value &&Kokkos::is_view_uq_pce< Kokkos::View< OutputType, OutputP... > >::value >::type spmv(const char mode[], const AlphaType &a, const MatrixType &A, const Kokkos::View< InputType, InputP... > &x, const BetaType &b, const Kokkos::View< OutputType, OutputP... > &y, const RANK_ONE)