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 // Stokhos Package
4 //
5 // Copyright 2009 NTESS and the Stokhos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 // Host-specific tests
11 TEUCHOS_UNIT_TEST_TEMPLATE_2_DECL( Kokkos_SG_SpMv, CrsProductTensorCijk, Scalar, Device ) {
12  success = true;
13 
14  typedef Scalar value_type;
16 
17  tensor_type tensor =
18  Stokhos::create_product_tensor<Device>( *setup.basis, *setup.Cijk );
19 
20  for (int i=0; i<setup.stoch_length; ++i) {
21  const int iEntryBeg = tensor.entry_begin(i);
22  const int iEntryEnd = tensor.entry_end(i);
23  for (int iEntry = iEntryBeg ; iEntry < iEntryEnd ; ++iEntry ) {
24  const int kj = tensor.coord( iEntry );
25  const int j = kj & 0x0ffff;
26  const int k = kj >> 16;
27  // const int j = tensor.coord(iEntry,0);
28  // const int k = tensor.coord(iEntry,1);
29  value_type c2 = tensor.value(iEntry);
30  if (j == k) c2 *= 2.0;
31 
32  int ii = setup.inv_perm[i];
33  int jj = setup.inv_perm[j];
34  int kk = setup.inv_perm[k];
35  value_type c = setup.Cijk->getValue(ii,jj,kk);
36 
37  if (std::abs(c-c2) > std::abs(c)*setup.rel_tol + setup.abs_tol) {
38  out << "(" << ii << "," << jj << "," << kk << "): " << c
39  << " == " << c2 << " failed!" << std::endl;
40  success = false;
41  }
42  }
43  }
44 }
45 
46 TEUCHOS_UNIT_TEST_TEMPLATE_2_DECL( Kokkos_SG_SpMv, TiledCrsProductTensorCijk, Scalar, Device ) {
47  success = true;
48 
49  typedef Scalar value_type;
51 
53  params.set("Tile Size",10);
54  params.set("Max Tiles",10000);
55 
56  tensor_type tensor =
57  Stokhos::create_tiled_product_tensor<Device>( *setup.basis, *setup.Cijk,
58  params );
59 
60  // This is a valid test only with no symmetry
61  // TEUCHOS_TEST_EQUALITY( tensor.entry_count(), setup.Cijk->num_entries(),
62  // out, success );
63 
64  const size_t n_tile = tensor.num_tiles();
65  for ( size_t tile = 0 ; tile < n_tile ; ++tile ) {
66  const size_t i_offset = tensor.offset(tile, 0);
67  const size_t j_offset = tensor.offset(tile, 1);
68  const size_t k_offset = tensor.offset(tile, 2);
69  const size_t n_row = tensor.num_rows(tile);
70 
71  for (size_t i=0; i<n_row; ++i) {
72  const size_t iEntryBeg = tensor.entry_begin(tile,i);
73  const size_t iEntryEnd = tensor.entry_end(tile,i);
74  for (size_t iEntry = iEntryBeg ; iEntry < iEntryEnd ; ++iEntry ) {
75  const size_t j = tensor.coord(iEntry,0);
76  const size_t k = tensor.coord(iEntry,1);
77  value_type c2 = tensor.value(iEntry);
78  int ii = i + i_offset;
79  int jj = j + j_offset;
80  int kk = k + k_offset;
81  if (jj == kk)
82  c2 *= 2.0;
83  value_type c = setup.Cijk->getValue(ii,jj,kk);
84 
85  if (std::abs(c-c2) > std::abs(c)*setup.rel_tol + setup.abs_tol) {
86  out << "(" << ii << "," << jj << "," << kk << "): " << c
87  << " == " << c2 << " failed!" << std::endl;
88  success = false;
89  }
90  }
91  }
92  }
93 }
94 
95 TEUCHOS_UNIT_TEST_TEMPLATE_2_DECL( Kokkos_SG_SpMv, SimpleTiledCrsProductTensorCijk, Scalar, Device ) {
96  success = true;
97 
98  typedef Scalar value_type;
100 
101  Teuchos::ParameterList params;
102  params.set("Tile Size",10);
103 
104  tensor_type tensor =
105  Stokhos::create_simple_tiled_product_tensor<Device>(
106  *setup.basis, *setup.Cijk, params);
107 
108  int num_entry = 0;
109  const size_t n_i_tile = tensor.num_i_tiles();
110  for (size_t i_tile = 0; i_tile<n_i_tile; ++i_tile) {
111  const size_t i_begin = tensor.i_begin(i_tile);
112  const size_t i_size = tensor.i_size(i_tile);
113 
114  const size_t n_j_tile = tensor.num_j_tiles(i_tile);
115  for (size_t j_tile = 0; j_tile<n_j_tile; ++j_tile) {
116  const size_t j_begin = tensor.j_begin(i_tile, j_tile);
117  //const size_t j_size = tensor.j_size(i_tile, j_tile);
118 
119  const size_t n_k_tile = tensor.num_k_tiles(i_tile, j_tile);
120  for (size_t k_tile = 0; k_tile<n_k_tile; ++k_tile) {
121  const size_t k_begin = tensor.k_begin(i_tile, j_tile, k_tile);
122  //const size_t k_size = tensor.k_size(i_tile, j_tile, k_tile);
123 
124  for (size_t i=0; i<i_size; ++i) {
125  const size_t iEntryBeg = tensor.entry_begin(i_tile,j_tile,k_tile,i);
126  const size_t iEntryEnd = tensor.entry_end(i_tile,j_tile,k_tile,i);
127  for (size_t iEntry = iEntryBeg ; iEntry < iEntryEnd ; ++iEntry ) {
128  const size_t j = tensor.coord(iEntry,0);
129  const size_t k = tensor.coord(iEntry,1);
130  value_type c2 = tensor.value(iEntry);
131  int ii = i + i_begin;
132  int jj = j + j_begin;
133  int kk = k + k_begin;
134  ++num_entry;
135  if (jj == kk)
136  c2 *= 2.0;
137  else
138  ++num_entry;
139  value_type c = setup.Cijk->getValue(ii,jj,kk);
140 
141  if (std::abs(c-c2) > std::abs(c)*setup.rel_tol + setup.abs_tol) {
142  out << "(" << ii << "," << jj << "," << kk << "): " << c
143  << " == " << c2 << " failed!" << std::endl;
144  success = false;
145  }
146  }
147  }
148  }
149  }
150  }
151  TEUCHOS_TEST_EQUALITY( num_entry, setup.Cijk->num_entries(), out, success );
152 }
153 
154 template <typename Scalar, typename Device, bool Pack>
157  Teuchos::FancyOStream& out) {
158  bool success = true;
159 
160  typedef Scalar value_type;
162 
163  tensor_type tensor =
164  Stokhos::create_coo_product_tensor<Device, Pack>(
165  *setup.basis, *setup.Cijk );
166 
167  const size_t nEntry = tensor.entry_count();
168  size_t i, j, k;
169  for ( size_t entry = 0 ; entry < nEntry ; ++entry ) {
170  tensor.coord(entry, i, j, k);
171  value_type c2 = tensor.value(entry);
172  if (j == k) c2 *= 2.0;
173  value_type c = setup.Cijk->getValue(i,j,k);
174 
175  if (std::abs(c-c2) > std::abs(c)*setup.rel_tol + setup.abs_tol) {
176  out << "(" << i << "," << j << "," << k << "): " << c
177  << " == " << c2 << " failed!" << std::endl;
178  success = false;
179  }
180  }
181 
182  return success;
183 }
184 
185 TEUCHOS_UNIT_TEST_TEMPLATE_2_DECL( Kokkos_SG_SpMv, CooProductTensorCijk_Packed, Scalar, Device ) {
186  success = test_coo_product_tensor_cijk<Scalar,Device,true>(setup, out);
187 }
188 
189 TEUCHOS_UNIT_TEST_TEMPLATE_2_DECL( Kokkos_SG_SpMv, CooProductTensorCijk_Unpacked, Scalar, Device ) {
190  success = test_coo_product_tensor_cijk<Scalar,Device,false>(setup, out);
191 }
192 
193 TEUCHOS_UNIT_TEST_TEMPLATE_2_DECL( Kokkos_SG_SpMv, FlatSparseCijk, Scalar, Device ) {
194  success = true;
195 
196  typedef Scalar value_type;
198  typedef size_t size_type;
199 
200  tensor_type tensor =
201  Stokhos::create_flat_sparse_3_tensor<Device>( *setup.basis, *setup.Cijk );
202 
203  for (int i=0; i<setup.stoch_length; ++i) {
204  const size_type nk = tensor.num_k(i);
205  const size_type kBeg = tensor.k_begin(i);
206  const size_type kEnd = kBeg + nk;
207  for (size_type kEntry = kBeg; kEntry < kEnd; ++kEntry) {
208  const size_type k = tensor.k_coord(kEntry);
209  const size_type nj = tensor.num_j(kEntry);
210  const size_type jBeg = tensor.j_begin(kEntry);
211  const size_type jEnd = jBeg + nj;
212  for (size_type jEntry = jBeg; jEntry < jEnd; ++jEntry) {
213  const size_type j = tensor.j_coord(jEntry);
214  value_type c2 = tensor.value(jEntry);
215  if (j == k) c2 *= 2.0;
216  value_type c = setup.Cijk->getValue(i,j,k);
217  if (std::abs(c-c2) > std::abs(c)*setup.rel_tol + setup.abs_tol) {
218  out << "(" << i << "," << j << "," << k << "): " << c
219  << " == " << c2 << " failed!" << std::endl;
220  success = false;
221  }
222  }
223  }
224  }
225 }
226 
227 TEUCHOS_UNIT_TEST_TEMPLATE_2_DECL( Kokkos_SG_SpMv, FlatSparseCijk_kji, Scalar, Device ) {
228  success = true;
229 
230  typedef Scalar value_type;
232  typedef size_t size_type;
233 
234  tensor_type tensor =
235  Stokhos::create_flat_sparse_3_tensor_kji<Device>(*setup.basis, *setup.Cijk);
236  const size_type nk = tensor.num_k();
237 
238  for ( size_type k = 0; k < nk; ++k) {
239  const size_type nj = tensor.num_j(k);
240  const size_type jBeg = tensor.j_begin(k);
241  const size_type jEnd = jBeg + nj;
242  for (size_type jEntry = jBeg; jEntry < jEnd; ++jEntry) {
243  const size_type j = tensor.j_coord(jEntry);
244  const size_type ni = tensor.num_i(jEntry);
245  const size_type iBeg = tensor.i_begin(jEntry);
246  const size_type iEnd = iBeg + ni;
247  for (size_type iEntry = iBeg; iEntry < iEnd; ++iEntry) {
248  const size_type i = tensor.i_coord(iEntry);
249  value_type c2 = tensor.value(iEntry);
250  if (j == k) c2 *= 2.0;
251  value_type c = setup.Cijk->getValue(i,j,k);
252  if (std::abs(c-c2) > std::abs(c)*setup.rel_tol + setup.abs_tol) {
253  out << "(" << i << "," << j << "," << k << "): " << c
254  << " == " << c2 << " failed!" << std::endl;
255  success = false;
256  }
257  }
258  }
259  }
260 }
261 
262 #define UNIT_TEST_GROUP_SCALAR_HOST_DEVICE( SCALAR, DEVICE ) \
263  TEUCHOS_UNIT_TEST_TEMPLATE_2_INSTANT( Kokkos_SG_SpMv, CrsProductTensorCijk, SCALAR, DEVICE ) \
264  TEUCHOS_UNIT_TEST_TEMPLATE_2_INSTANT( Kokkos_SG_SpMv, TiledCrsProductTensorCijk, SCALAR, DEVICE ) \
265  TEUCHOS_UNIT_TEST_TEMPLATE_2_INSTANT( Kokkos_SG_SpMv, SimpleTiledCrsProductTensorCijk, SCALAR, DEVICE ) \
266  TEUCHOS_UNIT_TEST_TEMPLATE_2_INSTANT( Kokkos_SG_SpMv, CooProductTensorCijk_Packed, SCALAR, DEVICE ) \
267  TEUCHOS_UNIT_TEST_TEMPLATE_2_INSTANT( Kokkos_SG_SpMv, CooProductTensorCijk_Unpacked, SCALAR, DEVICE ) \
268  TEUCHOS_UNIT_TEST_TEMPLATE_2_INSTANT( Kokkos_SG_SpMv, FlatSparseCijk, SCALAR, DEVICE ) \
269  TEUCHOS_UNIT_TEST_TEMPLATE_2_INSTANT( Kokkos_SG_SpMv, FlatSparseCijk_kji, SCALAR, DEVICE )
bool test_coo_product_tensor_cijk(const KokkosKernelsUnitTest::UnitTestSetup< Device > &setup, Teuchos::FancyOStream &out)
ParameterList & set(std::string const &name, T &&value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
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.