42 #ifndef STOKHOS_COO_PRODUCT_TENSOR_HPP
43 #define STOKHOS_COO_PRODUCT_TENSOR_HPP
45 #include "Kokkos_Core.hpp"
52 #include "Kokkos_Core.hpp"
67 template<
typename ValueType,
class ExecutionSpace,
bool PackIndex >
72 template<
typename ValueType,
class ExecutionSpace >
77 typedef typename execution_space::size_type
size_type;
88 typedef Kokkos::View< value_type[], execution_space >
vec_type;
103 size_type ijk = ii | (jj << bits) | (kk << 2*bits);
107 KOKKOS_INLINE_FUNCTION
109 i = ijk & mask; ijk >>= bits;
131 m_coord( rhs.m_coord ),
132 m_value( rhs.m_value ),
134 m_flops( rhs.m_flops ) {}
139 m_coord = rhs.m_coord;
140 m_value = rhs.m_value;
142 m_flops = rhs.m_flops;
147 KOKKOS_INLINE_FUNCTION
151 KOKKOS_INLINE_FUNCTION
155 KOKKOS_INLINE_FUNCTION
158 unpack(m_coord(entry), i, j, k);
162 KOKKOS_INLINE_FUNCTION
164 {
return m_value(entry); }
167 KOKKOS_INLINE_FUNCTION
171 KOKKOS_INLINE_FUNCTION
174 template <
typename OrdinalType>
181 typedef typename Cijk_type::i_iterator i_iterator;
182 typedef typename Cijk_type::ik_iterator k_iterator;
183 typedef typename Cijk_type::ikj_iterator j_iterator;
187 for (i_iterator i_it = Cijk.
i_begin(); i_it!=Cijk.
i_end(); ++i_it) {
188 for (k_iterator k_it = Cijk.
k_begin(i_it); k_it != Cijk.
k_end(i_it);
190 OrdinalType k = index(k_it);
191 for (j_iterator j_it = Cijk.
j_begin(k_it); j_it != Cijk.
j_end(k_it);
193 OrdinalType
j = index(j_it);
202 #if defined( KOKKOS_ENABLE_CUDA )
203 enum { Align = std::is_same<ExecutionSpace,Kokkos::Cuda>::value ? 32 : 1 };
208 entry_count = (entry_count+Align-1) & ~(Align-1);
215 tensor.m_dim = basis.
size();
216 tensor.m_flops = 5*entry_count + tensor.m_dim;
219 typename coord_array_type::HostMirror
221 typename value_array_type::HostMirror
225 OrdinalType i=0,
j=0, k=0;
226 for (i_iterator i_it = Cijk.
i_begin(); i_it!=Cijk.
i_end(); ++i_it) {
228 for (k_iterator k_it = Cijk.
k_begin(i_it); k_it != Cijk.
k_end(i_it);
231 for (j_iterator j_it = Cijk.
j_begin(k_it); j_it != Cijk.
j_end(k_it);
234 ValueType c = Stokhos::value(j_it);
236 host_value(n) = (
j != k) ? c : 0.5*c;
237 host_coord(n) = pack(i,
j,k);
243 for (; n < entry_count; ++n) {
245 host_coord(n) = pack(i,
j,k);
255 void print(std::ostream& os)
const {
257 typename coord_array_type::HostMirror
259 typename value_array_type::HostMirror
264 os <<
"CooProductTensor: dim = "
265 << dimension() <<
", entry_count = "
266 << num_entry << std::endl
267 <<
"Entries: i j k : cijk" << std::endl;
270 unpack(host_coord(l), i, j, k);
271 ValueType
cijk = host_value(l);
272 os <<
"\t " << l <<
" : " << i <<
" " << j <<
" " << k <<
" = " << cijk
274 if (l > 0 && l % 32 == 0) os << std::endl;
281 template<
typename ValueType,
class ExecutionSpace>
291 typedef Kokkos::View< value_type[], execution_space >
vec_type;
314 m_coord( rhs.m_coord ),
315 m_value( rhs.m_value ),
317 m_flops( rhs.m_flops ) {}
322 m_coord = rhs.m_coord;
323 m_value = rhs.m_value;
325 m_flops = rhs.m_flops;
330 KOKKOS_INLINE_FUNCTION
334 KOKKOS_INLINE_FUNCTION
338 KOKKOS_INLINE_FUNCTION
341 i = m_coord(entry,0);
342 j = m_coord(entry,1);
343 k = m_coord(entry,2);
347 KOKKOS_INLINE_FUNCTION
349 {
return m_value( entry ); }
352 KOKKOS_INLINE_FUNCTION
356 KOKKOS_INLINE_FUNCTION
359 template <
typename OrdinalType>
366 typedef typename Cijk_type::i_iterator i_iterator;
367 typedef typename Cijk_type::ik_iterator k_iterator;
368 typedef typename Cijk_type::ikj_iterator j_iterator;
372 for (i_iterator i_it = Cijk.
i_begin(); i_it!=Cijk.
i_end(); ++i_it) {
373 for (k_iterator k_it = Cijk.
k_begin(i_it); k_it != Cijk.
k_end(i_it);
375 OrdinalType k = index(k_it);
376 for (j_iterator j_it = Cijk.
j_begin(k_it); j_it != Cijk.
j_end(k_it);
378 OrdinalType
j = index(j_it);
387 #if defined( KOKKOS_ENABLE_CUDA )
388 enum { Align = std::is_same<ExecutionSpace,Kokkos::Cuda>::value ? 32 : 1 };
393 entry_count = (entry_count+Align-1) & ~(Align-1);
400 tensor.m_dim = basis.
size();
401 tensor.m_flops = 5*entry_count + tensor.m_dim;
404 typename coord_array_type::HostMirror
406 typename value_array_type::HostMirror
411 OrdinalType i=0,
j=0, k=0;
412 for (i_iterator i_it = Cijk.
i_begin(); i_it!=Cijk.
i_end(); ++i_it) {
414 for (k_iterator k_it = Cijk.
k_begin(i_it); k_it != Cijk.
k_end(i_it);
417 for (j_iterator j_it = Cijk.
j_begin(k_it); j_it != Cijk.
j_end(k_it);
420 ValueType c = Stokhos::value(j_it);
422 host_value(n) = (
j != k) ? c : 0.5*c;
431 for (; n < entry_count; ++n) {
445 void print(std::ostream& os)
const {
447 typename coord_array_type::HostMirror
449 typename value_array_type::HostMirror
454 os <<
"CooProductTensor: dim = "
455 << dimension() <<
", entry_count = "
456 << num_entry << std::endl
457 <<
"Entries: i j k : cijk" << std::endl;
462 ValueType
cijk = host_value(l);
463 os <<
"\t " << l <<
" : " << i <<
" " << j <<
" " << k <<
" = " << cijk
465 if (l > 0 && l % 32 == 0) os << std::endl;
470 template<
class Device,
bool Pack,
typename OrdinalType,
typename ValueType >
471 CooProductTensor<ValueType, Device, Pack>
478 basis, Cijk, params );
481 template <
typename ValueType,
typename Device,
bool Pack>
489 template<
typename ValueType,
typename Device,
bool Pack >
497 template<
typename MatrixValue ,
typename VectorValue >
498 KOKKOS_INLINE_FUNCTION
500 const MatrixValue *
const a,
501 const VectorValue *
const x,
502 VectorValue *
const y )
504 const size_type nEntry = tensor.entry_count();
506 VectorValue
val = 0.0, carry_val = 0.0;
507 for (
size_type entry = 0 ; entry < nEntry ; ++entry ) {
508 tensor.coord(entry, i,
j, k);
509 val = tensor.value(entry) * ( a[
j] * x[k] + a[k] * x[
j] );
513 y[i_prev] += carry_val;
521 KOKKOS_INLINE_FUNCTION
523 {
return tensor.dimension(); }
525 KOKKOS_INLINE_FUNCTION
527 {
return tensor.dimension(); }
KOKKOS_INLINE_FUNCTION void coord(const size_type entry, size_type &i, size_type &j, size_type &k) const
Get (i,j,k) coordinates of an entry.
execution_space::size_type size_type
Kokkos::View< value_type[], execution_space > value_array_type
k_iterator k_begin() const
Iterator pointing to first k entry.
KOKKOS_INLINE_FUNCTION size_type dimension() const
Dimension of the tensor.
Data structure storing a sparse 3-tensor C(i,j,k) in a a compressed format.
static KOKKOS_INLINE_FUNCTION size_type matrix_size(const tensor_type &tensor)
KOKKOS_INLINE_FUNCTION size_type dimension() const
Dimension of the tensor.
Kokkos::View< size_type[][3], execution_space > coord_array_type
CooProductTensor(const CooProductTensor &rhs)
kj_iterator j_begin(const k_iterator &k) const
Iterator pointing to first j entry for given k.
KOKKOS_INLINE_FUNCTION size_type entry_count() const
Number of sparse entries.
static CooProductTensor create(const Stokhos::ProductBasis< OrdinalType, ValueType > &basis, const Stokhos::Sparse3Tensor< OrdinalType, ValueType > &Cijk, const Teuchos::ParameterList ¶ms=Teuchos::ParameterList())
kj_iterator j_end(const k_iterator &k) const
Iterator pointing to last j entry for given k.
CooProductTensor(const CooProductTensor &rhs)
KOKKOS_INLINE_FUNCTION size_type num_non_zeros() const
Number of non-zero's.
KOKKOS_INLINE_FUNCTION size_type entry_count() const
Number of sparse entries.
std::ostream & operator<<(std::ostream &os, const ProductContainer< coeff_type > &vec)
Kokkos::View< value_type[], execution_space > vec_type
Kokkos::View< size_type[], execution_space > coord_array_type
static size_type pack(const size_type i, const size_type j, const size_type k)
void deep_copy(const Stokhos::CrsMatrix< ValueType, DstDevice, Layout > &dst, const Stokhos::CrsMatrix< ValueType, SrcDevice, Layout > &src)
KOKKOS_INLINE_FUNCTION size_type num_non_zeros() const
Number of non-zero's.
void print(std::ostream &os) const
k_iterator k_end() const
Iterator pointing to last k entry.
KOKKOS_INLINE_FUNCTION constexpr std::enable_if< is_view_uq_pce< view_type >::value, typename CijkType< view_type >::type >::type cijk(const view_type &view)
Stokhos::Sparse3Tensor< int, double > Cijk_type
execution_space::size_type size_type
void print(std::ostream &os) const
static CooProductTensor create(const Stokhos::ProductBasis< OrdinalType, ValueType > &basis, const Stokhos::Sparse3Tensor< OrdinalType, ValueType > &Cijk, const Teuchos::ParameterList ¶ms=Teuchos::ParameterList())
Kokkos::View< value_type[], execution_space > vec_type
KOKKOS_INLINE_FUNCTION void coord(const size_type entry, size_type &i, size_type &j, size_type &k) const
Get (i,j,k) coordinates of an entry.
KOKKOS_INLINE_FUNCTION size_type num_flops() const
Number flop's per multiply-add.
ExecutionSpace execution_space
KOKKOS_INLINE_FUNCTION size_type num_flops() const
Number flop's per multiply-add.
KOKKOS_INLINE_FUNCTION const value_type & value(const size_type entry) const
Value of an entry.
#define TEUCHOS_ASSERT(assertion_test)
static KOKKOS_INLINE_FUNCTION size_type vector_size(const tensor_type &tensor)
KOKKOS_INLINE_FUNCTION void unpack(size_type ijk, size_type &i, size_type &j, size_type &k) const
kji_iterator i_begin(const kj_iterator &j) const
Iterator pointing to first i entry for given j and k.
Sparse product tensor using 'COO'-like storage format.
Kokkos::View< value_type[], execution_space > value_array_type
kji_iterator i_end(const kj_iterator &j) const
Iterator pointing to last i entry for given j and k.
CooProductTensor< ValueType, Device, Pack > create_coo_product_tensor(const Stokhos::ProductBasis< OrdinalType, ValueType > &basis, const Stokhos::Sparse3Tensor< OrdinalType, ValueType > &Cijk, const Teuchos::ParameterList ¶ms=Teuchos::ParameterList())
CooProductTensor< ValueType, Device, Pack > tensor_type
virtual ordinal_type size() const =0
Return total size of basis.
KOKKOS_INLINE_FUNCTION const value_type & value(const size_type entry) const
Value of an entry.
Device::size_type size_type
Stokhos::CrsMatrix< ValueType, Device, Layout >::HostMirror create_mirror_view(const Stokhos::CrsMatrix< ValueType, Device, Layout > &A)
static KOKKOS_INLINE_FUNCTION void apply(const tensor_type &tensor, const MatrixValue *const a, const VectorValue *const x, VectorValue *const y)
ExecutionSpace execution_space