Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Stokhos_KokkosArrayKernelsUnitTest_Host.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 
42 // Host-specific tests
43 TEUCHOS_UNIT_TEST_TEMPLATE_2_DECL( Kokkos_SG_SpMv, CrsProductTensorCijk, Scalar, Device ) {
44  success = true;
45 
46  typedef Scalar value_type;
48 
49  tensor_type tensor =
50  Stokhos::create_product_tensor<Device>( *setup.basis, *setup.Cijk );
51 
52  for (int i=0; i<setup.stoch_length; ++i) {
53  const int iEntryBeg = tensor.entry_begin(i);
54  const int iEntryEnd = tensor.entry_end(i);
55  for (int iEntry = iEntryBeg ; iEntry < iEntryEnd ; ++iEntry ) {
56  const int kj = tensor.coord( iEntry );
57  const int j = kj & 0x0ffff;
58  const int k = kj >> 16;
59  // const int j = tensor.coord(iEntry,0);
60  // const int k = tensor.coord(iEntry,1);
61  value_type c2 = tensor.value(iEntry);
62  if (j == k) c2 *= 2.0;
63 
64  int ii = setup.inv_perm[i];
65  int jj = setup.inv_perm[j];
66  int kk = setup.inv_perm[k];
67  value_type c = setup.Cijk->getValue(ii,jj,kk);
68 
69  if (std::abs(c-c2) > std::abs(c)*setup.rel_tol + setup.abs_tol) {
70  out << "(" << ii << "," << jj << "," << kk << "): " << c
71  << " == " << c2 << " failed!" << std::endl;
72  success = false;
73  }
74  }
75  }
76 }
77 
78 TEUCHOS_UNIT_TEST_TEMPLATE_2_DECL( Kokkos_SG_SpMv, TiledCrsProductTensorCijk, Scalar, Device ) {
79  success = true;
80 
81  typedef Scalar value_type;
83 
85  params.set("Tile Size",10);
86  params.set("Max Tiles",10000);
87 
88  tensor_type tensor =
89  Stokhos::create_tiled_product_tensor<Device>( *setup.basis, *setup.Cijk,
90  params );
91 
92  // This is a valid test only with no symmetry
93  // TEUCHOS_TEST_EQUALITY( tensor.entry_count(), setup.Cijk->num_entries(),
94  // out, success );
95 
96  const size_t n_tile = tensor.num_tiles();
97  for ( size_t tile = 0 ; tile < n_tile ; ++tile ) {
98  const size_t i_offset = tensor.offset(tile, 0);
99  const size_t j_offset = tensor.offset(tile, 1);
100  const size_t k_offset = tensor.offset(tile, 2);
101  const size_t n_row = tensor.num_rows(tile);
102 
103  for (size_t i=0; i<n_row; ++i) {
104  const size_t iEntryBeg = tensor.entry_begin(tile,i);
105  const size_t iEntryEnd = tensor.entry_end(tile,i);
106  for (size_t iEntry = iEntryBeg ; iEntry < iEntryEnd ; ++iEntry ) {
107  const size_t j = tensor.coord(iEntry,0);
108  const size_t k = tensor.coord(iEntry,1);
109  value_type c2 = tensor.value(iEntry);
110  int ii = i + i_offset;
111  int jj = j + j_offset;
112  int kk = k + k_offset;
113  if (jj == kk)
114  c2 *= 2.0;
115  value_type c = setup.Cijk->getValue(ii,jj,kk);
116 
117  if (std::abs(c-c2) > std::abs(c)*setup.rel_tol + setup.abs_tol) {
118  out << "(" << ii << "," << jj << "," << kk << "): " << c
119  << " == " << c2 << " failed!" << std::endl;
120  success = false;
121  }
122  }
123  }
124  }
125 }
126 
127 TEUCHOS_UNIT_TEST_TEMPLATE_2_DECL( Kokkos_SG_SpMv, SimpleTiledCrsProductTensorCijk, Scalar, Device ) {
128  success = true;
129 
130  typedef Scalar value_type;
132 
133  Teuchos::ParameterList params;
134  params.set("Tile Size",10);
135 
136  tensor_type tensor =
137  Stokhos::create_simple_tiled_product_tensor<Device>(
138  *setup.basis, *setup.Cijk, params);
139 
140  int num_entry = 0;
141  const size_t n_i_tile = tensor.num_i_tiles();
142  for (size_t i_tile = 0; i_tile<n_i_tile; ++i_tile) {
143  const size_t i_begin = tensor.i_begin(i_tile);
144  const size_t i_size = tensor.i_size(i_tile);
145 
146  const size_t n_j_tile = tensor.num_j_tiles(i_tile);
147  for (size_t j_tile = 0; j_tile<n_j_tile; ++j_tile) {
148  const size_t j_begin = tensor.j_begin(i_tile, j_tile);
149  //const size_t j_size = tensor.j_size(i_tile, j_tile);
150 
151  const size_t n_k_tile = tensor.num_k_tiles(i_tile, j_tile);
152  for (size_t k_tile = 0; k_tile<n_k_tile; ++k_tile) {
153  const size_t k_begin = tensor.k_begin(i_tile, j_tile, k_tile);
154  //const size_t k_size = tensor.k_size(i_tile, j_tile, k_tile);
155 
156  for (size_t i=0; i<i_size; ++i) {
157  const size_t iEntryBeg = tensor.entry_begin(i_tile,j_tile,k_tile,i);
158  const size_t iEntryEnd = tensor.entry_end(i_tile,j_tile,k_tile,i);
159  for (size_t iEntry = iEntryBeg ; iEntry < iEntryEnd ; ++iEntry ) {
160  const size_t j = tensor.coord(iEntry,0);
161  const size_t k = tensor.coord(iEntry,1);
162  value_type c2 = tensor.value(iEntry);
163  int ii = i + i_begin;
164  int jj = j + j_begin;
165  int kk = k + k_begin;
166  ++num_entry;
167  if (jj == kk)
168  c2 *= 2.0;
169  else
170  ++num_entry;
171  value_type c = setup.Cijk->getValue(ii,jj,kk);
172 
173  if (std::abs(c-c2) > std::abs(c)*setup.rel_tol + setup.abs_tol) {
174  out << "(" << ii << "," << jj << "," << kk << "): " << c
175  << " == " << c2 << " failed!" << std::endl;
176  success = false;
177  }
178  }
179  }
180  }
181  }
182  }
183  TEUCHOS_TEST_EQUALITY( num_entry, setup.Cijk->num_entries(), out, success );
184 }
185 
186 template <typename Scalar, typename Device, bool Pack>
189  Teuchos::FancyOStream& out) {
190  bool success = true;
191 
192  typedef Scalar value_type;
194 
195  tensor_type tensor =
196  Stokhos::create_coo_product_tensor<Device, Pack>(
197  *setup.basis, *setup.Cijk );
198 
199  const size_t nEntry = tensor.entry_count();
200  size_t i, j, k;
201  for ( size_t entry = 0 ; entry < nEntry ; ++entry ) {
202  tensor.coord(entry, i, j, k);
203  value_type c2 = tensor.value(entry);
204  if (j == k) c2 *= 2.0;
205  value_type c = setup.Cijk->getValue(i,j,k);
206 
207  if (std::abs(c-c2) > std::abs(c)*setup.rel_tol + setup.abs_tol) {
208  out << "(" << i << "," << j << "," << k << "): " << c
209  << " == " << c2 << " failed!" << std::endl;
210  success = false;
211  }
212  }
213 
214  return success;
215 }
216 
217 TEUCHOS_UNIT_TEST_TEMPLATE_2_DECL( Kokkos_SG_SpMv, CooProductTensorCijk_Packed, Scalar, Device ) {
218  success = test_coo_product_tensor_cijk<Scalar,Device,true>(setup, out);
219 }
220 
221 TEUCHOS_UNIT_TEST_TEMPLATE_2_DECL( Kokkos_SG_SpMv, CooProductTensorCijk_Unpacked, Scalar, Device ) {
222  success = test_coo_product_tensor_cijk<Scalar,Device,false>(setup, out);
223 }
224 
225 TEUCHOS_UNIT_TEST_TEMPLATE_2_DECL( Kokkos_SG_SpMv, FlatSparseCijk, Scalar, Device ) {
226  success = true;
227 
228  typedef Scalar value_type;
230  typedef size_t size_type;
231 
232  tensor_type tensor =
233  Stokhos::create_flat_sparse_3_tensor<Device>( *setup.basis, *setup.Cijk );
234 
235  for (int i=0; i<setup.stoch_length; ++i) {
236  const size_type nk = tensor.num_k(i);
237  const size_type kBeg = tensor.k_begin(i);
238  const size_type kEnd = kBeg + nk;
239  for (size_type kEntry = kBeg; kEntry < kEnd; ++kEntry) {
240  const size_type k = tensor.k_coord(kEntry);
241  const size_type nj = tensor.num_j(kEntry);
242  const size_type jBeg = tensor.j_begin(kEntry);
243  const size_type jEnd = jBeg + nj;
244  for (size_type jEntry = jBeg; jEntry < jEnd; ++jEntry) {
245  const size_type j = tensor.j_coord(jEntry);
246  value_type c2 = tensor.value(jEntry);
247  if (j == k) c2 *= 2.0;
248  value_type c = setup.Cijk->getValue(i,j,k);
249  if (std::abs(c-c2) > std::abs(c)*setup.rel_tol + setup.abs_tol) {
250  out << "(" << i << "," << j << "," << k << "): " << c
251  << " == " << c2 << " failed!" << std::endl;
252  success = false;
253  }
254  }
255  }
256  }
257 }
258 
259 TEUCHOS_UNIT_TEST_TEMPLATE_2_DECL( Kokkos_SG_SpMv, FlatSparseCijk_kji, Scalar, Device ) {
260  success = true;
261 
262  typedef Scalar value_type;
264  typedef size_t size_type;
265 
266  tensor_type tensor =
267  Stokhos::create_flat_sparse_3_tensor_kji<Device>(*setup.basis, *setup.Cijk);
268  const size_type nk = tensor.num_k();
269 
270  for ( size_type k = 0; k < nk; ++k) {
271  const size_type nj = tensor.num_j(k);
272  const size_type jBeg = tensor.j_begin(k);
273  const size_type jEnd = jBeg + nj;
274  for (size_type jEntry = jBeg; jEntry < jEnd; ++jEntry) {
275  const size_type j = tensor.j_coord(jEntry);
276  const size_type ni = tensor.num_i(jEntry);
277  const size_type iBeg = tensor.i_begin(jEntry);
278  const size_type iEnd = iBeg + ni;
279  for (size_type iEntry = iBeg; iEntry < iEnd; ++iEntry) {
280  const size_type i = tensor.i_coord(iEntry);
281  value_type c2 = tensor.value(iEntry);
282  if (j == k) c2 *= 2.0;
283  value_type c = setup.Cijk->getValue(i,j,k);
284  if (std::abs(c-c2) > std::abs(c)*setup.rel_tol + setup.abs_tol) {
285  out << "(" << i << "," << j << "," << k << "): " << c
286  << " == " << c2 << " failed!" << std::endl;
287  success = false;
288  }
289  }
290  }
291  }
292 }
293 
294 #define UNIT_TEST_GROUP_SCALAR_HOST_DEVICE( SCALAR, DEVICE ) \
295  TEUCHOS_UNIT_TEST_TEMPLATE_2_INSTANT( Kokkos_SG_SpMv, CrsProductTensorCijk, SCALAR, DEVICE ) \
296  TEUCHOS_UNIT_TEST_TEMPLATE_2_INSTANT( Kokkos_SG_SpMv, TiledCrsProductTensorCijk, SCALAR, DEVICE ) \
297  TEUCHOS_UNIT_TEST_TEMPLATE_2_INSTANT( Kokkos_SG_SpMv, SimpleTiledCrsProductTensorCijk, SCALAR, DEVICE ) \
298  TEUCHOS_UNIT_TEST_TEMPLATE_2_INSTANT( Kokkos_SG_SpMv, CooProductTensorCijk_Packed, SCALAR, DEVICE ) \
299  TEUCHOS_UNIT_TEST_TEMPLATE_2_INSTANT( Kokkos_SG_SpMv, CooProductTensorCijk_Unpacked, SCALAR, DEVICE ) \
300  TEUCHOS_UNIT_TEST_TEMPLATE_2_INSTANT( Kokkos_SG_SpMv, FlatSparseCijk, SCALAR, DEVICE ) \
301  TEUCHOS_UNIT_TEST_TEMPLATE_2_INSTANT( Kokkos_SG_SpMv, FlatSparseCijk_kji, SCALAR, DEVICE )
ParameterList & set(std::string const &name, T const &value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
bool test_coo_product_tensor_cijk(const KokkosKernelsUnitTest::UnitTestSetup< Device > &setup, Teuchos::FancyOStream &out)
Sparse product tensor with replicated entries to provide subsets with a given coordinate.
Sparse product tensor with replicated entries to provide subsets with a given coordinate.
TEUCHOS_UNIT_TEST_TEMPLATE_2_DECL(Kokkos_SG_SpMv, CrsProductTensorCijk, Scalar, Device)
KOKKOS_INLINE_FUNCTION size_type num_k() const
Number of k entries.
KOKKOS_INLINE_FUNCTION size_type entry_begin(size_type i) const
Begin entries with a coordinate &#39;i&#39;.
KOKKOS_INLINE_FUNCTION PCE< Storage > abs(const PCE< Storage > &a)
KOKKOS_INLINE_FUNCTION size_type num_k(size_type i) const
Number of k entries with a coordinate &#39;i&#39;.
#define TEUCHOS_TEST_EQUALITY(v1, v2, out, success)
Sparse product tensor with replicated entries to provide subsets with a given coordinate.
Sparse product tensor using &#39;COO&#39;-like storage format.