298 m_env = SYCLEnv::instance();
301 m_max_num_groups = m_env->maxNumGroups() ;
302 m_max_work_group_size = m_env->maxWorkGroupSize() ;
303 m_total_threads = m_env->maxNumThreads() ;
307 virtual ~KernelInternal() {}
309 void setDotAlgo(
int dot_algo)
311 m_dot_algo = dot_algo;
314 template <
typename T>
315 void assign(T
const a,
318 m_env->internal()->queue().submit(
319 [&](sycl::handler& cgh)
321 auto acc = y.template get_access<sycl::access::mode::discard_write>(cgh);
343 template <
typename T,
typename Lambda>
344 void apply(Lambda
const& lambda,
347 sycl::range<1> work_items{ m_total_threads };
350 m_env->internal()->queue().submit( [&](sycl::handler& cgh)
352 auto access_y = y.template get_access<sycl::access::mode::read_write>(cgh);
353 auto y_length = y.size() ;
354 cgh.parallel_for<
class vector_apply>(sycl::range<1>{m_total_threads}, [=] (sycl::item<1> itemId)
356 auto id = itemId.get_id(0);
357 for (
auto i =
id; i < y_length; i += itemId.get_range()[0])
358 access_y[i] = lambda(i);
366 template <
typename T>
370 sycl::range<1> work_items{ m_total_threads };
373 m_env->internal()->queue().submit([&](sycl::handler& cgh)
375 auto access_y = y.template get_access<sycl::access::mode::read_write>(cgh);
376 auto y_length = y.size() ;
377 cgh.parallel_for<
class vector_scal>(sycl::range<1>{m_total_threads}, [=] (sycl::item<1> itemId)
379 auto id = itemId.get_id(0);
380 for (
auto i =
id; i < y_length; i += itemId.get_range()[0])
381 access_y[i] = a*access_y[i];
388 template <
typename T>
393 sycl::range<1> work_items{ m_total_threads };
396 m_env->internal()->queue().submit([&](sycl::handler& cgh)
398 auto access_x = x.template get_access<sycl::access::mode::read>(cgh);
399 auto access_y = y.template get_access<sycl::access::mode::read_write>(cgh);
400 auto y_length = y.size() ;
401 cgh.parallel_for<
class vector_axpy>(sycl::range<1>{m_total_threads}, [=] (sycl::item<1> itemId)
403 auto id = itemId.get_id(0);
404 for (
auto i =
id; i < y_length; i += itemId.get_range()[0])
405 access_y[i] += a * access_x[i];
412 template <
typename T>
416 constexpr int W = (16 /
sizeof(T)) > 0 ?
int(16 /
sizeof(T)) : 1;
417 using VecT = sycl::vec<T, W>;
419 auto& queue = m_env->internal()->queue();
420 const std::size_t n = y.size();
424 const std::size_t nv = n / W;
426 constexpr std::size_t WG = 256;
427 const std::size_t groups =
428 std::max<std::size_t>(1,std::min((nv + WG - 1) / WG,
429 size_t(m_max_num_groups) * 8));
430 const std::size_t total = groups * WG;
433 [&](sycl::handler& cgh)
435 sycl::accessor ay{ y, cgh, sycl::read_write };
437 cgh.parallel_for<
class vector_xcal_vec>(
438 sycl::nd_range<1>{ {total}, {WG} },
439 [=](sycl::nd_item<1> item)
442 const size_t id = item.get_global_id(0);
443 const size_t stride = item.get_global_range()[0];
444 if (
reinterpret_cast<uintptr_t
>(p) %
alignof(VecT) == 0)
446 VecT* pv =
reinterpret_cast<VecT*
>(p);
447 for (
size_t i =
id; i < nv; i += stride)
451 if ((n & 1) &&
id == 0) p[n-1] *= a;
455 for (
size_t i =
id; i < n; i += stride)
463 template <
typename T>
469 constexpr int W = (16 /
sizeof(T)) > 0 ?
int(16 /
sizeof(T)) : 1;
470 using VecT = sycl::vec<T, W>;
472 auto& queue = m_env->internal()->queue();
473 const std::size_t n = y.size();
477 const std::size_t nv = n / W;
479 constexpr std::size_t WG = 256;
480 const std::size_t groups = std::max<size_t>(1,
481 std::min((nv + WG - 1) / WG,
482 size_t(m_max_num_groups) * 8));
483 const std::size_t total = groups * WG;
486 queue.submit([&](sycl::handler& cgh)
488 sycl::accessor ax{ x, cgh, sycl::read_only };
489 sycl::accessor ay{ y, cgh, sycl::read_write };
491 cgh.parallel_for<
class vector_axpy_buffer_vec>(
492 sycl::nd_range<1>{ sycl::range<1>{ total }, sycl::range<1>{ WG } },
493 [=](sycl::nd_item<1> it) {
494 const T* px = &ax[0];
497 const size_t gid = it.get_global_id(0);
498 const size_t stride = it.get_global_range(0);
501 (
reinterpret_cast<uintptr_t
>(px) %
alignof(VecT) == 0) &&
502 (
reinterpret_cast<uintptr_t
>(py) %
alignof(VecT) == 0);
505 const VecT* vx =
reinterpret_cast<const VecT*
>(px);
506 VecT* vy =
reinterpret_cast<VecT*
>(py);
509 for (
size_t i = gid; i < nv; i += stride)
510 vy[i] = vy[i] + a * vx[i];
513 for (
size_t i = nv * W + gid; i < n; i += stride)
519 for (
size_t i = gid; i < n; i += stride)
527 template <
typename T>
534 sycl::range<1> work_items{ m_total_threads };
537 m_env->internal()->queue().submit([&](sycl::handler& cgh)
539 auto access_x = x.template get_access<sycl::access::mode::read>(cgh);
540 auto access_y = y.template get_access<sycl::access::mode::read_write>(cgh);
541 auto x_length = x.size()/stride_x ;
542 cgh.parallel_for<
class vector_axpy>(sycl::range<1>{m_total_threads}, [=] (sycl::item<1> itemId)
544 auto id = itemId.get_id(0);
545 for (
auto i =
id; i < x_length; i += itemId.get_range()[0])
546 access_y[i*stride_y] += a * access_x[i*stride_x];
553 template <
typename T>
557 ConstArrayView<Integer> stride_y)
559 sycl::range<1> work_items{ m_total_threads };
561 auto x_length = x.size() ;
562 sycl::buffer<Integer, 1> s(stride_y.unguardedBasePointer(),sycl::range<1>(x_length));
565 m_env->internal()->queue().submit([&](sycl::handler& cgh)
567 auto access_x = x.template get_access<sycl::access::mode::read>(cgh);
568 auto access_s = s.template get_access<sycl::access::mode::read>(cgh);
569 auto access_y = y.template get_access<sycl::access::mode::read_write>(cgh);
570 cgh.parallel_for<
class vector_axpy_s>(sycl::range<1>{m_total_threads}, [=] (sycl::item<1> itemId)
572 auto id = itemId.get_id(0);
573 for (
auto i =
id; i < x_length; i += itemId.get_range()[0])
574 access_y[access_s[i]] += a * access_x[i];
581 template <
typename T>
582 void pointwiseMult(sycl::buffer<T>& x,
587 sycl::range<1> work_items{ m_total_threads };
590 m_env->internal()->queue().submit([&](sycl::handler& cgh)
592 auto access_x = x.template get_access<sycl::access::mode::read>(cgh);
593 auto access_y = y.template get_access<sycl::access::mode::read>(cgh);
594 auto access_z = z.template get_access<sycl::access::mode::read_write>(cgh);
595 auto y_length = y.size() ;
596 cgh.parallel_for<
class vector_pointwizemult>(sycl::range<1>{m_total_threads}, [=] (sycl::item<1> itemId)
598 auto id = itemId.get_id(0);
599 for (
auto i =
id; i < y_length; i += itemId.get_range()[0])
600 access_z[i] = access_x[i] * access_y[i];
606 using VecT = sycl::vec<T, 2>;
608 auto& queue = m_env->internal()->queue();
609 const size_t n = y.size();
610 const size_t n2 = n / 2;
611 const size_t tail = n % 2;
613 static constexpr size_t WG_SIZE = 256;
614 const size_t blocks = std::max((n2 + WG_SIZE - 1) / WG_SIZE,
615 m_max_num_groups * 4UL);
616 const size_t total = blocks * WG_SIZE;
621 sycl::buffer<VecT> x2{ x.template reinterpret<VecT>(sycl::range<1>{n2}) };
622 sycl::buffer<VecT> y2{ y.template reinterpret<VecT>(sycl::range<1>{n2}) };
623 sycl::buffer<VecT> z2{ z.template reinterpret<VecT>(sycl::range<1>{n2}) };
625 queue.submit([&](sycl::handler& cgh) {
626 auto ax = x2.template get_access<sycl::access::mode::read>(cgh);
627 auto ay = y2.template get_access<sycl::access::mode::read>(cgh);
628 auto az = z2.template get_access<sycl::access::mode::read_write>(cgh);
630 cgh.parallel_for<
class vector_pointwizemult>(
631 sycl::nd_range<1>{ {total}, {WG_SIZE} },
632 [=](sycl::nd_item<1> item) {
633 const size_t stride = item.get_global_range()[0];
634 for (
size_t i = item.get_global_id(0); i < n2; i += stride) {
636 az[i] = ax[i] * ay[i];
643 sycl::buffer<T> xt{ x.template reinterpret<T>(sycl::range<1>{n}) };
644 sycl::buffer<T> yt{ y.template reinterpret<T>(sycl::range<1>{n}) };
645 sycl::buffer<T> zt{ z.template reinterpret<T>(sycl::range<1>{n}) };
646 queue.submit([&](sycl::handler& cgh) {
647 auto ax = xt.template get_access<sycl::access::mode::read>(cgh);
648 auto ay = yt.template get_access<sycl::access::mode::read>(cgh);
649 auto az = zt.template get_access<sycl::access::mode::read_write>(cgh);
650 cgh.single_task([=]{ az[n-1] = ax[n-1] * ay[n-1]; });
654#ifdef PRINT_DEBUG_INFO
656 sycl::host_accessor<T, 1, sycl::access::mode::read> x_acc(x);
657 sycl::host_accessor<T, 1, sycl::access::mode::read> y_acc(y);
658 sycl::host_accessor<T, 1, sycl::access::mode::read> z_acc(z);
659 for(
int il=0;il<x.size();++il)
661 std::cout<<
"X Y Z ["<<il<<
"] : "<<x_acc[il]<<
"*"<<y_acc[il]<<
"="<<z_acc[il]<<std::endl ;
667 template <
typename T>
668 void copy(sycl::buffer<T>& x,
674 m_env->internal()->queue().submit(
675 [&](sycl::handler& cgh)
677 auto access_x = x.template get_access<sycl::access::mode::read>(cgh);
678 auto access_y = y.template get_access<sycl::access::mode::discard_write>(cgh);
679 cgh.copy(access_x,access_y) ;
694 template <
typename T>
695 void copy(sycl::buffer<T>& x,
700 sycl::range<1> work_items{ m_total_threads };
703 m_env->internal()->queue().submit( [&](sycl::handler& cgh)
705 auto access_x = x.template get_access<sycl::access::mode::read>(cgh);
706 auto access_y = y.template get_access<sycl::access::mode::read_write>(cgh);
707 auto x_length = x.size()/stride_x ;
708 cgh.parallel_for<
class vector_copy>(sycl::range<1>{m_total_threads}, [=] (sycl::item<1> itemId)
710 auto id = itemId.get_id(0);
711 for (
auto i =
id; i < x_length; i += itemId.get_range()[0])
712 access_y[i*stride_y] = access_x[i*stride_x];
719 template <
typename T>
720 void copy(sycl::buffer<T>& x,
721 ConstArrayView<Integer> stride_x,
724 auto y_length = y.size();
725 sycl::buffer<Integer, 1> s(stride_x.unguardedBasePointer(),sycl::range<1>(y_length));
726 sycl::range<1> work_items{ m_total_threads };
729 m_env->internal()->queue().submit(
730 [&](sycl::handler& cgh)
732 auto access_x = x.template get_access<sycl::access::mode::read>(cgh);
733 auto access_y = y.template get_access<sycl::access::mode::read_write>(cgh);
734 auto access_s = x.template get_access<sycl::access::mode::read>(cgh);
735 cgh.parallel_for<
class vector_copy_sx>(sycl::range<1>{m_total_threads}, [=] (sycl::item<1> itemId)
737 auto id = itemId.get_id(0);
738 for (
auto i =
id; i < y_length; i += itemId.get_range()[0])
739 access_y[i] = access_x[access_s[i]];
746 template <
typename T>
747 void copy(sycl::buffer<T>& x,
749 ConstArrayView<Integer> stride_y)
751 auto x_length = x.size();
752 sycl::buffer<Integer, 1> s(stride_y.unguardedBasePointer(),sycl::range<1>(x_length));
753 sycl::range<1> work_items{ m_total_threads };
756 m_env->internal()->queue().submit(
757 [&](sycl::handler& cgh)
759 auto access_x = x.template get_access<sycl::access::mode::read>(cgh);
760 auto access_y = y.template get_access<sycl::access::mode::read_write>(cgh);
761 auto access_s = x.template get_access<sycl::access::mode::read>(cgh);
762 cgh.parallel_for<
class vector_copy_sy>(sycl::range<1>{m_total_threads}, [=] (sycl::item<1> itemId)
764 auto id = itemId.get_id(0);
765 for (
auto i =
id; i < x_length; i += itemId.get_range()[0])
766 access_y[access_s[i]] = access_x[i];
773 template <
typename T>
779 template <
typename index_t>
780 inline index_t round_up(
const index_t x,
const index_t y)
782 return ((x + y - 1) / y) * y;
785 template <
typename T>
788 template <
typename T>
789 T reduce_sum(sycl::buffer<T>& x,
793 auto& w = getWorkBuffer<T>(x.size());
796 m_env->internal()->queue().submit( [&](sycl::handler& cgh)
798 auto access_x = x.template get_access<sycl::access::mode::read>(cgh);
799 auto access_y = y.template get_access<sycl::access::mode::read>(cgh);
801 auto access_w = sycl::accessor { w, cgh, sycl::write_only, sycl::property::no_init{}};
803 auto y_length = y.size() ;
804 cgh.parallel_for<
class vector_dot>(sycl::range<1>{m_total_threads}, [=] (sycl::item<1> itemId)
806 auto id = itemId.get_id(0);
807 for (
auto i =
id; i < y_length; i += itemId.get_range()[0])
808 access_w[i] = access_x[i]*access_y[i];
813 std::size_t local = m_max_work_group_size;
814 std::size_t length = x.size();
821 auto round_length = round_up(length, local);
823 auto f = [length, round_length, local, &w](sycl::handler& h)
mutable
825 sycl::nd_range<1> range{sycl::range<1>{round_length},
826 sycl::range<1>{local}};
827 auto access_w = w.template get_access<sycl::access::mode::read_write>(h);
831 sycl::local_accessor<T> scratch{sycl::range<1>(local), h};
835 h.parallel_for<
class sycl_reduction_sum_T>(range,
836 [access_w, scratch, local, length](sycl::nd_item<1> id)
838 std::size_t globalid =
id.get_global_id(0);
839 std::size_t localid =
id.get_local_id(0);
846 scratch[localid] = (globalid < length)? access_w[globalid] : 0. ;
847 id.barrier(sycl::access::fence_space::local_space);
851 if (globalid < length)
854 std::size_t min = local ;
855 for (std::size_t offset = min / 2; offset > 0; offset /= 2)
858 if (localid < offset)
860 scratch[localid] += scratch[localid + offset];
862 id.barrier(sycl::access::fence_space::local_space);
867 access_w[
id.get_group(0)] = scratch[localid];
873 m_env->internal()->queue().submit(f);
877 length = (length + local - 1) / local;
879 }
while (length > 1);
882 auto h_w = w.get_host_access();
887 template <
typename T>
890 template <
typename T>
893 template <
typename T>
894 T map_reduce_sum(sycl::buffer<T>& x,
897 auto& w = getWorkBuffer<T>(x.size());
899 std::size_t local = m_max_work_group_size;
900 std::size_t length = x.size();
908 auto round_length = round_up(length, local);
910 auto f0 = [length, round_length, local, &x,&y, &w](sycl::handler& h)
mutable
912 sycl::nd_range<1> range{sycl::range<1>{round_length},
913 sycl::range<1>{local}};
914 auto access_x = x.template get_access<sycl::access::mode::read>(h);
915 auto access_y = y.template get_access<sycl::access::mode::read>(h);
917 auto access_w = sycl::accessor { w, h, sycl::read_write, sycl::property::no_init{}};
921 sycl::local_accessor<T> scratch{sycl::range<1>(local), h};
925 h.parallel_for<
class sycl_map_reduction_sum0_T>(range,
926 [access_x,access_y,access_w, scratch, local, length](sycl::nd_item<1> id)
928 std::size_t globalid =
id.get_global_id(0);
929 std::size_t localid =
id.get_local_id(0);
936 scratch[localid] = (globalid < length)? access_x[globalid]*access_y[globalid] : 0. ;
938 id.barrier(sycl::access::fence_space::local_space);
942 if (globalid < length)
945 std::size_t min = local ;
946 for (std::size_t offset = min / 2; offset > 0; offset /= 2)
949 if (localid < offset)
951 scratch[localid] += scratch[localid + offset];
953 id.barrier(sycl::access::fence_space::local_space);
958 access_w[
id.get_group(0)] = scratch[localid];
966 auto f1 = [length, round_length, local, &w](sycl::handler& h)
mutable
968 sycl::nd_range<1> range{sycl::range<1>{round_length},
969 sycl::range<1>{local}};
970 auto access_w = w.template get_access<sycl::access::mode::read_write>(h);
974 sycl::local_accessor<T> scratch{sycl::range<1>(local), h};
978 h.parallel_for<
class sycl_map_reduction_sum_T>(range,
979 [access_w, scratch, local, length](sycl::nd_item<1> id)
981 std::size_t globalid =
id.get_global_id(0);
982 std::size_t localid =
id.get_local_id(0);
989 scratch[localid] = (globalid < length)? access_w[globalid] : 0. ;
990 id.barrier(sycl::access::fence_space::local_space);
994 if (globalid < length)
997 std::size_t min = local ;
998 for (std::size_t offset = min / 2; offset > 0; offset /= 2)
1001 if (localid < offset)
1003 scratch[localid] += scratch[localid + offset];
1005 id.barrier(sycl::access::fence_space::local_space);
1010 access_w[
id.get_group(0)] = scratch[localid];
1017 m_env->internal()->queue().submit(f0);
1019 m_env->internal()->queue().submit(f1);
1023 length = (length + local - 1) / local;
1027 auto h_x = w.get_host_access();
1030 for (std::size_t i = 0; i < length; ++i)
1035 template <
typename T>
1038 template <
typename T>
1039 T map2_reduce_sum(sycl::buffer<T>& x,
1042 std::size_t local = m_max_work_group_size;
1043 std::size_t total_threads = m_total_threads;
1044 std::size_t length = x.size();
1047 sycl::buffer<T> sum{ &sum_init, 1 };
1053 auto round_length = round_up(length, local);
1055 auto f0 = [length, round_length,total_threads, local, &x, &y, &sum](sycl::handler& h)
mutable
1057 sycl::nd_range<1> range{sycl::range<1>{std::min(total_threads,round_length)},
1058 sycl::range<1>{local}};
1059 auto access_x = x.template get_access<sycl::access::mode::read>(h);
1060 auto access_y = y.template get_access<sycl::access::mode::read>(h);
1062 sycl::accessor access_sum {sum, h};
1064 auto sumReduction = sycl::reduction(access_sum, sycl::plus<T>());
1067 auto sumReduction = sycl::reduction(sum, h, sycl::plus<T>());
1070 auto sumReduction = sycl::reduction(sum, h, sycl::plus<T>());
1075 sycl::local_accessor<T> scratch{sycl::range<1>(local), h};
1079 h.parallel_for<
class sycl_map2_reduction_sum0_T>(range,
1081 [access_x,access_y, scratch, local,total_threads,length](sycl::nd_item<1> id, auto &sum)
1083 std::size_t globalid =
id.get_global_id(0);
1084 std::size_t localid =
id.get_local_id(0);
1091 scratch[localid] = (globalid < length)? access_x[globalid]*access_y[globalid] : 0. ;
1092 for (
auto i = globalid+total_threads; i < length; i += total_threads)
1093 scratch[localid] += access_x[i]*access_y[i];
1095 id.barrier(sycl::access::fence_space::local_space);
1102 std::size_t min = local ;
1103 for (std::size_t offset = min / 2; offset > 0; offset /= 2)
1106 if (localid < offset)
1108 scratch[localid] += scratch[localid + offset];
1110 id.barrier(sycl::access::fence_space::local_space);
1116 sum += scratch[localid];
1122 m_env->internal()->queue().submit(f0);
1125 auto h_sum = sum.get_host_access();
1129 template <
typename T>
1132 template <
typename T>
1133 T map3_reduce_sum(sycl::buffer<T>& x,
1136 std::size_t local = m_max_work_group_size;
1137 std::size_t total_threads = m_total_threads;
1138 std::size_t length = x.size();
1142 auto& group_sum = getWorkBuffer<T>(m_max_num_groups);
1147 auto round_length = round_up(length, local);
1154 auto f0 = [total_threads,round_length, length, local, &x, &y,&group_sum](sycl::handler& h)
mutable
1156 sycl::nd_range<1> range{sycl::range<1>{std::min(total_threads,round_length)},
1157 sycl::range<1>{local}};
1158 auto access_x = x.template get_access<sycl::access::mode::read>(h);
1159 auto access_y = y.template get_access<sycl::access::mode::read>(h);
1161 auto access_sum = sycl::accessor { group_sum, h, sycl::read_write, sycl::property::no_init{}};
1168 sycl::local_accessor<T> scratch{sycl::range<1>(local), h};
1173 h.parallel_for<
class sycl_map3_reduction_sum0_T>(range,
1176 [access_x,access_y,access_sum,scratch,local,length,total_threads] (sycl::nd_item<1> id)
1178 std::size_t globalid =
id.get_global_id(0);
1179 std::size_t localid =
id.get_local_id(0);
1181 scratch[localid] = (globalid < length)? access_x[globalid]*access_y[globalid] : 0. ;
1183 for (
auto i = globalid+total_threads; i < length; i += total_threads)
1184 scratch[localid] += access_x[i]*access_y[i];
1186 id.barrier(sycl::access::fence_space::local_space);
1192 std::size_t min = local ;
1193 for (std::size_t offset = min / 2; offset > 0; offset /= 2)
1195 if (localid < offset)
1197 scratch[localid] += scratch[localid + offset];
1199 id.barrier(sycl::access::fence_space::local_space);
1205 access_sum[
id.get_group(0)] = scratch[localid];
1212 m_env->internal()->queue().submit(f0);
1218 auto h_sum = group_sum.get_host_access();
1220 for (std::size_t i = 0; i < std::min(total_threads, round_length) / local; ++i)
1225 template <
typename T>
1228 template <
typename T>
1229 void asynch_map4_reduce_sum(sycl::buffer<T>& x,
1231 sycl::buffer<T>& res,
1234 std::size_t local = m_max_work_group_size;
1235 std::size_t total_threads = m_total_threads;
1236 std::size_t length = x.size();
1245 auto round_length = round_up(length, local);
1247 auto f0 = [total_threads, round_length, length, local, &x, &y,&res](sycl::handler& h)
mutable
1249 sycl::nd_range<1> range{sycl::range<1>{std::min(total_threads,round_length)},
1250 sycl::range<1>{local}};
1251 auto access_x = x.template get_access<sycl::access::mode::read>(h);
1252 auto access_y = y.template get_access<sycl::access::mode::read>(h);
1254 sycl::accessor access_sum {res, h};
1255 auto sumReduction = sycl::reduction(access_sum, sycl::plus<T>());
1258 auto sumReduction = sycl::reduction(res, h, sycl::plus<T>());
1261 auto sumReduction = sycl::reduction(res, h, sycl::plus<T>());
1271 sycl::local_accessor<T> scratch{sycl::range<1>(local), h};
1276 h.parallel_for<
class map4_reduction_sum_T>(range,
1278 [access_x,access_y,scratch,local,length,total_threads] (sycl::nd_item<1> id, auto& sum)
1280 std::size_t globalid =
id.get_global_id(0);
1281 std::size_t localid =
id.get_local_id(0);
1283 scratch[localid] = (globalid < length)? access_x[globalid]*access_y[globalid] : 0. ;
1285 for (
auto i = globalid+total_threads; i < length; i += total_threads)
1286 scratch[localid] += access_x[i]*access_y[i];
1288 id.barrier(sycl::access::fence_space::local_space);
1295 std::size_t min = local ;
1296 for (std::size_t offset = min / 2; offset > 0; offset /= 2)
1299 if (localid < offset)
1301 scratch[localid] += scratch[localid + offset];
1303 id.barrier(sycl::access::fence_space::local_space);
1310 sum += scratch[localid] ;
1316 event = m_env->internal()->queue().submit(f0);
1321 template <
typename T>
1322 T end_map4_reduce_sum(sycl::event& event,
1323 sycl::buffer<T>& res,
1324 [[maybe_unused]] std::size_t num_blocks)
1327 auto h_access = res.get_host_access();
1331 template <
typename T>
1334 template <
typename T>
1337 template <
typename T>
1338 void asynch_map5_reduce_sum(sycl::buffer<T>& x,
1340 sycl::buffer<T>& res,
1343 std::size_t local = m_max_work_group_size;
1344 std::size_t total_threads = m_total_threads;
1345 std::size_t length = x.size();
1353 auto f0 = [total_threads, length, local, &x, &y,&res](sycl::handler& h)
mutable
1355 sycl::nd_range<1> range{sycl::range<1>{total_threads},
1356 sycl::range<1>{local}};
1357 auto access_x = x.template get_access<sycl::access::mode::read>(h);
1358 auto access_y = y.template get_access<sycl::access::mode::read>(h);
1359 auto access_sum = sycl::accessor { res, h, sycl::read_write, sycl::property::no_init{}};
1363 sycl::local_accessor<T> scratch{sycl::range<1>(local), h};
1368 h.parallel_for<
class map5_reduction_sum_T>(range,
1369 [access_x,access_y,access_sum,scratch,local,length,total_threads] (sycl::nd_item<1> id)
1371 std::size_t globalid =
id.get_global_id(0);
1372 std::size_t localid =
id.get_local_id(0);
1374 scratch[localid] = (globalid < length)? access_x[globalid]*access_y[globalid] : 0. ;
1376 for (
auto i = globalid+total_threads; i < length; i += total_threads)
1377 scratch[localid] += access_x[i]*access_y[i];
1379 id.barrier(sycl::access::fence_space::local_space);
1383 if (globalid < length)
1386 for (std::size_t offset = local / 2; offset > 0; offset /= 2)
1389 if (localid < offset)
1391 scratch[localid] += scratch[localid + offset];
1393 id.barrier(sycl::access::fence_space::local_space);
1399 access_sum[
id.get_group(0)] = scratch[localid];
1406 auto f1 = [length, local, &res](sycl::handler& h)
mutable {
1407 sycl::nd_range<1> range{ sycl::range<1>{ local },
1408 sycl::range<1>{ local } };
1409 auto access_sum = res.template get_access<sycl::access::mode::read_write>(h);
1413 sycl::local_accessor<T> scratch{sycl::range<1>(local), h};
1417 h.parallel_for<
class map5_reduction_sum1_T>(range,
1418 [access_sum, scratch, local, length](sycl::nd_item<1> id) {
1420 std::size_t globalid =
id.get_global_id(0);
1421 std::size_t localid =
id.get_local_id(0);
1428 scratch[localid] = (localid < length) ? access_sum[localid] : 0.;
1429 id.barrier(sycl::access::fence_space::local_space);
1433 if (localid < length) {
1435 std::size_t min = local;
1436 for (std::size_t offset = min / 2; offset > 0; offset /= 2)
1439 if (localid < offset) {
1440 scratch[localid] += scratch[localid + offset];
1442 id.barrier(sycl::access::fence_space::local_space);
1446 access_sum[0] = scratch[localid];
1453 event = m_env->internal()->queue().submit(f0);
1455 event = m_env->internal()->queue().submit(f1);
1461 length = (std::min(total_threads, length) + local - 1) / local;
1463 }
while (length > 1);
1474 template <
typename T>
1475 T end_map5_reduce_sum(sycl::event& event,
1476 sycl::buffer<T>& res,
1477 [[maybe_unused]] std::size_t num_blocks)
1480 auto h_access = res.get_host_access();
1484 template <
typename T>
1485 T reduce_sum2(
const std::vector<T>& x)
1493 auto num_groups = m_env->internal()->queue().get_device().get_info<sycl::info::device::max_compute_units>();
1495 auto work_group_size = m_env->internal()->queue().get_device().get_info<sycl::info::device::max_work_group_size>();
1497 auto total_threads = num_groups * work_group_size;
1508 auto device = m_env->internal()->queue().get_device();
1510 std::size_t local = device.get_info<sycl::info::device::max_work_group_size>();
1512 std::size_t length = x.size();
1514 sycl::buffer<T, 1> xbuf(x.data(), sycl::range<1>(x.size()));
1515 xbuf.set_final_data(
nullptr);
1523 auto round_length = round_up(length, local);
1524 std::cout <<
"LENGTH :" << level <<
" " << length <<
" " << round_length <<
" " << local << std::endl;
1526 auto f = [length,round_length,local, &xbuf](sycl::handler& h)
mutable
1530 sycl::nd_range<1> r{sycl::range<1>{round_length},
1531 sycl::range<1>{local}};
1534 auto x_access = xbuf.template get_access<sycl::access::mode::read_write>(h);
1538 sycl::local_accessor<T> scratch{sycl::range<1>(local), h};
1542 h.parallel_for<
class reduce_sum2>(r, [x_access, scratch, local, length](sycl::nd_item<1> id)
1544 std::size_t globalid =
id.get_global_id(0);
1545 std::size_t localid =
id.get_local_id(0);
1552 scratch[localid] = (globalid < length)? x_access[globalid] : 0. ;
1553 id.barrier(sycl::access::fence_space::local_space);
1558 if (globalid < length)
1562 for (std::size_t offset = min / 2; offset > 0; offset /= 2)
1565 if (localid < offset)
1567 scratch[localid] += scratch[localid + offset];
1569 id.barrier(sycl::access::fence_space::local_space);
1574 x_access[
id.get_group(0)] = scratch[localid];
1581 m_env->internal()->queue().submit(f);
1585 length = (length + local - 1) / local;
1586 std::cout <<
"AFTER LENGTH :" << level <<
" new length" << length <<
" " << local << std::endl;
1588 }
while (length > 1);
1593 auto hI = xbuf.get_host_acces();
1600 template <
typename T>
1601 T sycl_reduce_sum(sycl::buffer<T>& x,
1605 sycl::buffer<T> sum_buff{ &sum_init, 1 };
1608 m_env->internal()->queue().submit([&](sycl::handler &cgh)
1610 auto access_x = x.template get_access<sycl::access::mode::read>(cgh);
1611 auto access_y = y.template get_access<sycl::access::mode::read>(cgh);
1613 sycl::accessor sum_acc {sum_buff, cgh};
1614 auto sumReduction = sycl::reduction(sum_acc, sycl::plus<T>());
1617 auto sumReduction = sycl::reduction(sum_buff, cgh, sycl::plus<T>());
1620 auto sumReduction = sycl::reduction(sum_buff, cgh, sycl::plus<T>());
1622 cgh.parallel_for(sycl::range<1>{x.size()},
1624 [=](sycl::id<1> idx,
auto &sum)
1626 sum += access_x[idx]*access_y[idx];
1631 return sum_buff.get_host_access()[0];
1821 template <
typename T>
1822 inline T dot_product_h100(sycl::buffer<T>& buf_x,
1823 sycl::buffer<T>& buf_y)
1825 using namespace sycl;
1826 auto& q = m_env->internal()->queue();
1827 const size_t N = buf_x.size();
1828 assert(buf_y.size() == N);
1830 auto dev = q.get_device();
1835 const size_t blocks_needed = (N + WG_SIZE * ITEMS_PER_WI - 1)
1836 / (WG_SIZE * ITEMS_PER_WI);
1837 const size_t num_blocks = std::max(blocks_needed,
1838 m_max_num_groups * TARGET_WAVES);
1839 const size_t total_threads = num_blocks * WG_SIZE;
1843 sycl::buffer<T> partials{num_blocks};
1849 auto ax = buf_x.template get_access<access::mode::read>(cgh);
1850 auto ay = buf_y.template get_access<access::mode::read>(cgh);
1851 auto ap = partials.template get_access<access::mode::read_write>(cgh);
1853 local_accessor<T,1> lds{WG_SIZE / WARP_SIZE, cgh};
1860 cgh.parallel_for<
class dot_h100_phase1>(
1861 nd_range<1>{{total_threads}, {WG_SIZE}},
1862 [=](nd_item<1> item)
1863 [[intel::reqd_sub_group_size(WARP_SIZE)]]
1865 h100::dot_kernel_h100(item, ax, ay, ap, n, lds);
1876 auto h_partials = partials.get_host_access();
1879 T sum = 0.0, c = 0.0;
1880 for (
size_t b = 0; b < num_blocks; ++b) {
1881 T z = h_partials[b] - c;
1889 template <
typename T>
1890 inline std::size_t asynch_dot_product_h100(sycl::buffer<T>& buf_x,
1891 sycl::buffer<T>& buf_y,
1892 sycl::buffer<T>& res,
1895 using namespace sycl;
1896 auto& q = m_env->internal()->queue();
1897 const size_t N = buf_x.size();
1898 assert(buf_y.size() == N);
1905 const size_t blocks_needed = (N + WG_SIZE * ITEMS_PER_WI - 1)
1906 / (WG_SIZE * ITEMS_PER_WI);
1907 const size_t num_blocks = std::max(blocks_needed,m_max_num_groups * TARGET_WAVES);
1908 const size_t total_threads = num_blocks * WG_SIZE;
1917 auto ax = buf_x.template get_access<access::mode::read>(cgh);
1918 auto ay = buf_y.template get_access<access::mode::read>(cgh);
1919 auto ap = res.template get_access<sycl::access::mode::read_write>(cgh);
1921 local_accessor<T,1> lds{WG_SIZE / WARP_SIZE, cgh};
1928 cgh.parallel_for<
class dot_h100_phase1>(
1929 nd_range<1>{{total_threads}, {WG_SIZE}},
1930 [=](nd_item<1> item)
1931 [[intel::reqd_sub_group_size(WARP_SIZE)]]
1933 h100::dot_kernel_h100(item, ax, ay, ap, n, lds);
1940 template <
typename T>
1941 inline T end_dot_product_h100(sycl::event& event,
1942 sycl::buffer<T>& res,
1943 std::size_t num_blocks)
1953 auto h_partials = res.get_host_access();
1956 T sum = 0.0, c = 0.0;
1957 for (
size_t b = 0; b < num_blocks; ++b) {
1958 T z = h_partials[b] - c;
1971 template<
typename T>
1972 inline T dot_product_mi300(sycl::buffer<T, 1>& x_buf,
1973 sycl::buffer<T, 1>& y_buf)
1975 using namespace mi300 ;
1976 std::cout<<
" DOT PROD MI300 : "<<WG_SIZE<<
" "<<ITEMS_PER_WI<<std::endl ;
1978 bool use_doublebuf =
false;
1979 auto& q = m_env->internal()->queue();
2010 std::size_t N = x_buf.size() ;
2012 const size_t blocks_needed = (N + WG_SIZE * ITEMS_PER_WI - 1)
2013 / (WG_SIZE * ITEMS_PER_WI);
2016 const size_t num_blocks = std::max(blocks_needed,
2017 m_max_num_groups * TARGET_WAVES
2018 * (WG_SIZE / WAVEFRONT));
2019 const size_t total_threads = num_blocks * WG_SIZE;
2022 sycl::buffer<T, 1> partials(num_blocks);
2024 q.submit([&](sycl::handler& cgh)
2026 auto ax = x_buf.template get_access<sycl::access::mode::read>(cgh);
2027 auto ay = y_buf.template get_access<sycl::access::mode::read>(cgh);
2028 auto ap = partials.template get_access<sycl::access::mode::read_write>(cgh);
2029 sycl::local_accessor<double,1> lds{N_WARPS_BLOC, cgh};
2035 const bool db = use_doublebuf;
2037 cgh.parallel_for<
class dot_mi300_phase1>(
2038 sycl::nd_range<1>{{total_threads}, {WG_SIZE}},
2042 [=](sycl::nd_item<1> item)
2043 [[intel::reqd_sub_group_size(WAVEFRONT)]]
2046 mi300::dot_kernel_mi300_doublebuf(item, ax, ay, ap, n, lds);
2048 mi300::dot_kernel_mi300(item, ax, ay, ap, n, lds);
2056 auto h_partials = partials.get_host_access();
2060 for (std::size_t b = 0; b < num_blocks; ++b)
2062 T z = h_partials[b] - c;
2070 template <
typename T>
2071 inline void asynch_dot_product_mi300(sycl::buffer<T, 1>& x_buf,
2072 sycl::buffer<T, 1>& y_buf,
2073 sycl::buffer<T, 1>& result_buf,
2076 using namespace mi300 ;
2078 std::size_t n = x_buf.size() ;
2079 auto& q = m_env->internal()->queue();
2081 const std::size_t stride = WG_SIZE * ITEMS_PER_WI;
2082 const std::size_t n_padded = ((n + stride - 1) / stride) * stride;
2084 event = q.submit([&](sycl::handler& cgh) {
2085 auto x = x_buf.template get_access<sycl::access::mode::read>(cgh);
2086 auto y = y_buf.template get_access<sycl::access::mode::read>(cgh);
2087 auto red = sycl::reduction(result_buf, cgh, sycl::plus<T>{});
2090 sycl::nd_range<1>{n_padded, WG_SIZE}, red,
2091 [=](sycl::nd_item<1> item,
auto& sum) {
2092 const std::size_t lid = item.get_local_id(0);
2093 const std::size_t base = item.get_group(0) * stride;
2096 for (std::size_t k = 0; k < ITEMS_PER_WI; ++k) {
2097 const std::size_t idx = base + lid + k * WG_SIZE;
2098 if (idx < n) acc += x[idx] * y[idx];
2107 template <
typename T>
2108 T end_dot_product_mi300(sycl::event& event,
2109 sycl::buffer<T>& res,
2110 [[maybe_unused]] std::size_t num_blocks)
2113 auto h_access = res.get_host_access();
2117 template <
typename T>
2118 T dot(sycl::buffer<T>& x,
2121 switch (m_dot_algo) {
2123 return reduce_sum(x, y);
2125 return map_reduce_sum(x, y);
2127 return map2_reduce_sum(x, y);
2129 return map3_reduce_sum(x, y);
2131 return dot_product_h100(x, y);
2133 return dot_product_mi300(x, y);
2135 return sycl_reduce_sum(x, y);
2139 template <
typename T>
2140 void dot(sycl::buffer<T>& x,
2147 asynch_map4_reduce_sum(x, y, res.deviceValue(), res.event());
2148 res.setWaitFunction([=](sycl::event& event, sycl::buffer<double>& res, std::size_t num_blocks)
2150 return this->end_map4_reduce_sum(event,res, num_blocks) ;
2155 std::size_t num_blocks = asynch_dot_product_h100(x, y, res.deviceValue(), res.event());
2156 res.setWaitFunction([=](sycl::event& event, sycl::buffer<double>& res, std::size_t num_blocks)
2158 return this->end_dot_product_h100(event, res, num_blocks) ;
2160 res.setNumBlocks(num_blocks) ;
2164 asynch_dot_product_mi300(x, y, res.deviceValue(), res.event());
2165 res.setWaitFunction([=](sycl::event& event, sycl::buffer<T>& res, std::size_t num_blocks)
2167 return this->end_dot_product_mi300(event,res, num_blocks) ;
2171 asynch_map5_reduce_sum(x, y, res.deviceValue(), res.event());
2172 res.setWaitFunction([=](sycl::event& event, sycl::buffer<T>& res, std::size_t num_blocks)
2174 return this->end_map5_reduce_sum(event,res, num_blocks) ;
2182 SYCLEnv* m_env = nullptr ;
2183 std::size_t m_max_num_groups = 0 ;
2184 std::size_t m_max_work_group_size = 0 ;
2185 std::size_t m_total_threads = 0 ;
2188 template <
typename T>
2189 sycl::buffer<T>& getWorkBuffer(std::size_t size);
2191 mutable sycl::buffer<double>* m_double_work =
nullptr;