42 #ifndef STOKHOS_SIMPLE_TILED_CRS_PRODUCT_TENSOR_HPP
43 #define STOKHOS_SIMPLE_TILED_CRS_PRODUCT_TENSOR_HPP
45 #include "Kokkos_Core.hpp"
60 template<
typename ValueType,
class ExecutionSpace >
72 #elif defined(__MIC__)
81 #if defined( KOKKOS_ENABLE_CUDA )
82 Kokkos::Impl::is_same<ExecutionSpace,Kokkos::Cuda>::value;
93 typedef Kokkos::View< value_type[], execution_space >
vec_type;
96 typedef Kokkos::View< size_type[][2], Kokkos::LayoutLeft, execution_space >
coord2_array_type;
98 typedef Kokkos::View< size_type*, execution_space >
i_size_type;
99 typedef Kokkos::View< size_type*, execution_space >
num_j_type;
102 typedef Kokkos::View< size_type**, execution_space >
num_k_type;
104 typedef Kokkos::View< size_type***, execution_space >
k_size_type;
106 typedef Kokkos::View< size_type****, Kokkos::LayoutRight, execution_space >
row_map_type;
107 typedef Kokkos::View< size_type****, Kokkos::LayoutRight, execution_space >
num_entry_type;
134 template <
typename coord_t>
220 KOKKOS_INLINE_FUNCTION
224 KOKKOS_INLINE_FUNCTION
228 KOKKOS_INLINE_FUNCTION
232 KOKKOS_INLINE_FUNCTION
236 KOKKOS_INLINE_FUNCTION
240 KOKKOS_INLINE_FUNCTION
244 KOKKOS_INLINE_FUNCTION
250 KOKKOS_INLINE_FUNCTION
256 KOKKOS_INLINE_FUNCTION
261 KOKKOS_INLINE_FUNCTION
268 KOKKOS_INLINE_FUNCTION
275 KOKKOS_INLINE_FUNCTION
282 KOKKOS_INLINE_FUNCTION
289 KOKKOS_INLINE_FUNCTION
296 KOKKOS_INLINE_FUNCTION
302 KOKKOS_INLINE_FUNCTION
308 KOKKOS_INLINE_FUNCTION
314 KOKKOS_INLINE_FUNCTION
319 KOKKOS_INLINE_FUNCTION
324 KOKKOS_INLINE_FUNCTION
328 KOKKOS_INLINE_FUNCTION
331 template <
typename OrdinalType>
343 typedef typename Cijk_type::i_iterator i_iterator;
344 typedef typename Cijk_type::ik_iterator ik_iterator;
345 typedef typename Cijk_type::ikj_iterator ikj_iterator;
347 const size_type i_tile_size = params.
get<OrdinalType>(
"Tile Size");
352 i_iterator i_end = Cijk.
i_end();
353 for (i_iterator i_it=
i_begin; i_it!=i_end; ++i_it) {
354 OrdinalType i = index(i_it);
356 ik_iterator k_end = Cijk.
k_end(i_it);
357 for (ik_iterator k_it =
k_begin; k_it != k_end; ++k_it) {
358 OrdinalType k = index(k_it);
360 ikj_iterator j_end = Cijk.
j_end(k_it);
361 for (ikj_iterator j_it =
j_begin; j_it != j_end; ++j_it) {
362 OrdinalType
j = index(j_it);
364 ValueType c = Stokhos::value(j_it);
365 Cijk_sym.add_term(i,
j, k, c);
370 Cijk_sym.fillComplete();
375 size_type num_i_parts = (basis_size + i_tile_size-1) / i_tile_size;
379 for (
size_type i=0; i<num_i_parts; ++i) {
380 i_tiles[i].lower = i*its;
381 i_tiles[i].upper =
std::min(basis_size, (i+1)*its);
382 i_tiles[i].parts.
resize(1);
383 i_tiles[i].parts[0].lower = basis_size;
384 i_tiles[i].parts[0].upper = 0;
389 for (i_iterator i_it=Cijk_sym.i_begin(); i_it!=Cijk_sym.i_end(); ++i_it) {
390 OrdinalType i = index(i_it);
394 while (idx < num_i_parts && i >= i_tiles[idx].lower) ++idx;
398 ik_iterator
k_begin = Cijk_sym.k_begin(i_it);
399 ik_iterator k_end = Cijk_sym.k_end(i_it);
400 for (ik_iterator k_it =
k_begin; k_it != k_end; ++k_it) {
401 OrdinalType
j = index(k_it);
403 if (
j < i_tiles[idx].parts[0].lower)
404 i_tiles[idx].parts[0].lower =
j;
405 if (
j > i_tiles[idx].parts[0].upper)
406 i_tiles[idx].parts[0].upper =
j;
409 for (
size_type idx=0; idx<num_i_parts; ++idx) {
410 size_type lower = i_tiles[idx].parts[0].lower;
411 size_type upper = i_tiles[idx].parts[0].upper;
413 size_type num_j_parts = (range + j_tile_size-1) / j_tile_size;
419 j_tiles[
j].lower = lower +
j*jts;
420 j_tiles[
j].upper =
std::min(upper+1, lower + (
j+1)*jts);
422 j_tiles[
j].parts[0].lower = basis_size;
423 j_tiles[
j].parts[0].upper = 0;
425 i_tiles[idx].parts.
swap(j_tiles);
429 for (i_iterator i_it=Cijk_sym.i_begin(); i_it!=Cijk_sym.i_end(); ++i_it) {
430 OrdinalType i = index(i_it);
434 while (idx < num_i_parts && i >= i_tiles[idx].lower) ++idx;
438 ik_iterator
k_begin = Cijk_sym.k_begin(i_it);
439 ik_iterator k_end = Cijk_sym.k_end(i_it);
440 for (ik_iterator k_it =
k_begin; k_it != k_end; ++k_it) {
441 OrdinalType
j = index(k_it);
446 while (jdx < num_j_parts && j >= i_tiles[idx].parts[jdx].lower) ++jdx;
450 ikj_iterator
j_begin = Cijk_sym.j_begin(k_it);
451 ikj_iterator j_end = Cijk_sym.j_end(k_it);
452 for (ikj_iterator j_it =
j_begin; j_it != j_end; ++j_it) {
453 OrdinalType k = index(j_it);
454 ValueType
cijk = Stokhos::value(j_it);
459 if (k < i_tiles[idx].parts[jdx].parts[0].lower)
460 i_tiles[idx].parts[jdx].parts[0].lower = k;
461 if (k > i_tiles[idx].parts[jdx].parts[0].upper)
462 i_tiles[idx].parts[jdx].parts[0].upper = k;
470 for (
size_type idx=0; idx<num_i_parts; ++idx) {
472 for (
size_type jdx=0; jdx<num_j_parts; ++jdx) {
473 size_type lower = i_tiles[idx].parts[jdx].parts[0].lower;
474 size_type upper = i_tiles[idx].parts[jdx].parts[0].upper;
476 size_type num_k_parts = (range + j_tile_size-1) / j_tile_size;
481 for (
size_type k=0; k<num_k_parts; ++k) {
482 k_tiles[k].lower = lower + k*kts;
483 k_tiles[k].upper =
std::min(upper+1, lower + (k+1)*kts);
485 size_type num_k = i_tiles[idx].parts[jdx].parts[0].parts.
size();
487 size_type i = i_tiles[idx].parts[jdx].parts[0].parts[l].i;
488 size_type j = i_tiles[idx].parts[jdx].parts[0].parts[l].j;
489 size_type k = i_tiles[idx].parts[jdx].parts[0].parts[l].k;
494 while (kdx < num_k_parts && k >= k_tiles[kdx].lower) ++kdx;
502 if (
j != k) ++num_coord;
507 for (
size_type k=0; k<num_k_parts; ++k) {
508 if (k_tiles[k].parts.
size() > 0)
511 i_tiles[idx].parts[jdx].parts.
swap(k_tiles2);
518 size_type max_num_j_parts = 0, max_num_k_parts = 0;
520 for (
size_type idx=0; idx<num_i_parts; ++idx) {
522 max_num_j_parts =
std::max(max_num_j_parts, num_j_parts);
523 coord_work[idx].
resize(num_j_parts);
524 for (
size_type jdx=0; jdx<num_j_parts; ++jdx) {
525 size_type num_k_parts = i_tiles[idx].parts[jdx].parts.
size();
526 max_num_k_parts =
std::max(max_num_k_parts, num_k_parts);
527 coord_work[idx][jdx].
resize(num_k_parts);
528 for (
size_type kdx=0; kdx<num_k_parts; ++kdx) {
529 size_type num_rows = i_tiles[idx].upper - i_tiles[idx].lower + 1;
530 total_num_rows += num_rows;
531 max_num_rows =
std::max(max_num_rows, num_rows);
532 coord_work[idx][jdx][kdx].
resize(num_rows, 0);
534 size_type nc = i_tiles[idx].parts[jdx].parts[kdx].parts.
size();
536 size_type i = i_tiles[idx].parts[jdx].parts[kdx].parts[c].i;
538 ++(coord_work[idx][jdx][kdx][i-
i_begin]);
546 for (
size_type idx=0; idx<num_i_parts; ++idx) {
548 for (
size_type jdx=0; jdx<num_j_parts; ++jdx) {
549 size_type num_k_parts = i_tiles[idx].parts[jdx].parts.
size();
550 for (
size_type kdx=0; kdx<num_k_parts; ++kdx) {
553 const size_t rem = coord_work[idx][jdx][kdx][i] %
tensor_align;
556 coord_work[idx][jdx][kdx][i] += pad;
580 max_num_j_parts, max_num_k_parts,
583 max_num_j_parts, max_num_k_parts,
591 typename value_array_type::HostMirror host_value =
593 typename coord_array_type::HostMirror host_coord =
595 typename coord2_array_type::HostMirror host_coord2 =
597 typename i_begin_type::HostMirror host_i_begin =
599 typename i_size_type::HostMirror host_i_size =
601 typename num_j_type::HostMirror host_num_j =
603 typename j_begin_type::HostMirror host_j_begin =
605 typename j_size_type::HostMirror host_j_size =
607 typename num_k_type::HostMirror host_num_k =
609 typename k_begin_type::HostMirror host_k_begin =
611 typename k_size_type::HostMirror host_k_size =
613 typename row_map_type::HostMirror host_row_map =
615 typename num_entry_type::HostMirror host_num_entry =
620 for (
size_type idx=0; idx<num_i_parts; ++idx) {
622 for (
size_type jdx=0; jdx<num_j_parts; ++jdx) {
623 size_type num_k_parts = i_tiles[idx].parts[jdx].parts.
size();
624 for (
size_type kdx=0; kdx<num_k_parts; ++kdx) {
626 host_row_map(idx,jdx,kdx,0) =
sum;
628 sum += coord_work[idx][jdx][kdx][t];
629 host_row_map(idx,jdx,kdx,t+1) =
sum;
630 host_num_entry(idx,jdx,kdx,t) = 0;
637 for (
size_type idx=0; idx<num_i_parts; ++idx) {
639 for (
size_type jdx=0; jdx<num_j_parts; ++jdx) {
640 size_type num_k_parts = i_tiles[idx].parts[jdx].parts.
size();
641 for (
size_type kdx=0; kdx<num_k_parts; ++kdx) {
644 coord_work[idx][jdx][kdx][t] = host_row_map(idx,jdx,kdx,t);
651 for (
size_type idx=0; idx<num_i_parts; ++idx) {
652 host_i_begin(idx) = i_tiles[idx].lower;
653 host_i_size(idx) = i_tiles[idx].upper - i_tiles[idx].lower;
656 host_num_j(idx) = num_j_parts;
657 for (
size_type jdx=0; jdx<num_j_parts; ++jdx) {
658 host_j_begin(idx,jdx) = i_tiles[idx].parts[jdx].lower;
659 host_j_size(idx,jdx) = i_tiles[idx].parts[jdx].upper -
660 i_tiles[idx].parts[jdx].lower;
662 size_type num_k_parts = i_tiles[idx].parts[jdx].parts.
size();
663 host_num_k(idx,jdx) = num_k_parts;
664 for (
size_type kdx=0; kdx<num_k_parts; ++kdx) {
665 host_k_begin(idx,jdx,kdx) = i_tiles[idx].parts[jdx].parts[kdx].lower;
666 host_k_size(idx,jdx,kdx) = i_tiles[idx].parts[jdx].parts[kdx].upper -
667 i_tiles[idx].parts[jdx].parts[kdx].lower;
670 size_type nc = i_tiles[idx].parts[jdx].parts[kdx].parts.
size();
672 Coord s = i_tiles[idx].parts[jdx].parts[kdx].parts[t];
678 const size_type row = i - host_i_begin(idx);
679 const size_type n = coord_work[idx][jdx][kdx][row];
680 ++coord_work[idx][jdx][kdx][row];
682 host_value(
n) = (
j != k) ? c : 0.5*c;
683 host_coord2(
n,0) =
j - host_j_begin(idx,jdx);
684 host_coord2(
n,1) = k - host_k_begin(idx,jdx,kdx);
685 host_coord(
n) = (host_coord2(
n,1) << 16) | host_coord2(
n,0);
687 ++host_num_entry(idx,jdx,kdx,row);
710 for (
size_type idx=0; idx<num_i_parts; ++idx) {
712 for (
size_type jdx=0; jdx<num_j_parts; ++jdx) {
713 size_type num_k_parts = i_tiles[idx].parts[jdx].parts.
size();
714 for (
size_type kdx=0; kdx<num_k_parts; ++kdx) {
715 for (
size_type i = 0; i < host_i_size(idx); ++i) {
716 tensor.
m_flops += 5*host_num_entry(idx,jdx,kdx,i) + 1;
726 template<
class Device,
typename OrdinalType,
typename ValueType >
727 SimpleTiledCrsProductTensor<ValueType, Device>
734 basis, Cijk, params);
737 template <
typename ValueType,
typename Device >
745 template<
typename MatrixValue ,
typename VectorValue >
746 KOKKOS_INLINE_FUNCTION
748 const MatrixValue *
const a ,
749 const VectorValue *
const x ,
750 VectorValue *
const y )
756 for (
size_type i_tile = 0; i_tile<n_i_tile; ++i_tile) {
761 for (
size_type j_tile = 0; j_tile<n_j_tile; ++j_tile) {
766 for (
size_type k_tile = 0; k_tile<n_k_tile; ++k_tile) {
773 tensor.
num_entry(i_tile,j_tile,k_tile,i);
776 const size_type iEntryEnd = iEntryBeg + nEntry;
779 VectorValue ytmp = 0 ;
782 if (block_size > 1) {
783 const size_type nBlock = nEntry / block_size;
784 const size_type nEntryB = nBlock * block_size;
785 const size_type iEnd = iEntryBeg + nEntryB;
789 int j[block_size], k[block_size];
791 for ( ; iEntry < iEnd ; iEntry += block_size ) {
793 for (
size_type ii=0; ii<block_size; ++ii) {
794 j[ii] = tensor.
coord(iEntry+ii,0) + j_begin;
795 k[ii] = tensor.
coord(iEntry+ii,1) + k_begin;
797 TV aj(a,
j), ak(a, k), xj(
x,
j), xk(
x, k),
798 c(&(tensor.
value(iEntry)));
813 for ( ; iEntry<iEntryEnd; ++iEntry) {
817 ytmp += tensor.
value(iEntry) * ( a[
j] *
x[k] + a[k] *
x[
j] );
820 y[i+i_begin] += ytmp;
828 KOKKOS_INLINE_FUNCTION
832 KOKKOS_INLINE_FUNCTION