22#ifndef NDA_HAVE_DEVICE
42 auto get_transpose_vector(
auto &&v) {
43 auto v_t = std::vector<std::decay_t<
decltype(
transpose(v[0]))>>{};
44 v_t.reserve(v.size());
45 std::transform(v.begin(), v.end(), std::back_inserter(v_t), [](
auto &x) { return transpose(x); });
50 template <
bool is_vbatch, nda::mem::AddressSpace vec_addr_spc>
51 auto get_ptr_vector(
auto &&v) {
52 EXPECTS(std::ranges::all_of(v, [&v](
auto &A) {
return is_vbatch or A.shape() == v[0].shape(); }));
53 EXPECTS(std::ranges::all_of(v, [](
auto &A) {
return get_array(A).indexmap().min_stride() == 1; }));
56 std::transform(v.begin(), v.end(), v_ptrs.begin(), [](
auto &z) { return get_array(z).data(); });
83 template <
bool is_vbatch = false, Matrix A, Matrix B, MemoryMatrix C>
88 EXPECTS(va.size() == vb.size() and va.size() == vc.size());
89 if (va.empty())
return;
90 auto const batch_count = va.size();
94 auto vcT = detail::get_transpose_vector(vc);
95 return gemm_batch<is_vbatch>(alpha, detail::get_transpose_vector(vb), detail::get_transpose_vector(va), beta, vcT);
98 auto constexpr vec_addr_spc = []() {
return mem::on_host<C> ? mem::Host : mem::Unified; }();
101 auto a_ptrs = detail::get_ptr_vector<is_vbatch, vec_addr_spc>(va);
102 auto b_ptrs = detail::get_ptr_vector<is_vbatch, vec_addr_spc>(vb);
103 auto c_ptrs = detail::get_ptr_vector<is_vbatch, vec_addr_spc>(vc);
106 if constexpr (is_vbatch) {
109 vldb(batch_count + 1), vldc(batch_count + 1);
111 for (
auto i : range(batch_count)) {
117 auto const [m, k] = mat_a.shape();
118 auto const [l, n] = mat_b.shape();
120 EXPECTS(m == mat_c.extent(0));
121 EXPECTS(n == mat_c.extent(1));
134#if defined(NDA_HAVE_DEVICE)
135 device::gemm_vbatch(
get_op<A>,
get_op<B>, vm.data(), vn.data(), vk.data(), alpha, a_ptrs.data(), vlda.data(), b_ptrs.data(), vldb.data(),
136 beta, c_ptrs.data(), vldc.
data(), batch_count);
141 f77::gemm_vbatch(
get_op<A>,
get_op<B>, vm.data(), vn.data(), vk.data(), alpha, a_ptrs.data(), vlda.data(), b_ptrs.data(), vldb.data(), beta,
142 c_ptrs.data(), vldc.
data(), batch_count);
150 auto const [m, k] = mat_a.shape();
151 auto const [l, n] = mat_b.shape();
153 EXPECTS(m == mat_c.extent(0));
154 EXPECTS(n == mat_c.extent(1));
158#if defined(NDA_HAVE_DEVICE)
159 device::gemm_batch(
get_op<A>,
get_op<B>, m, n, k, alpha, a_ptrs.data(),
get_ld(mat_a), b_ptrs.data(),
get_ld(mat_b), beta, c_ptrs.data(),
160 get_ld(mat_c), batch_count);
165 f77::gemm_batch(
get_op<A>,
get_op<B>, m, n, k, alpha, a_ptrs.data(),
get_ld(mat_a), b_ptrs.data(),
get_ld(mat_b), beta, c_ptrs.data(),
166 get_ld(mat_c), batch_count);
186 template <Matrix A, Matrix B, MemoryMatrix C>
206 template <ArrayOfRank<3> A, ArrayOfRank<3> B, MemoryArrayOfRank<3> C>
211 EXPECTS(a.shape()[0] == b.shape()[0] and a.shape()[0] == c.shape()[0]);
212 if (a.size() == 0)
return;
213 auto const batch_count = a.shape()[0];
225 auto a0 = arr_a(0, nda::range::all, nda::range::all);
226 auto b0 = arr_b(0, nda::range::all, nda::range::all);
227 auto c0 = c(0, nda::range::all, nda::range::all);
230 auto const [m, k] = a0.shape();
231 auto const [l, n] = b0.shape();
233 EXPECTS(m == c0.extent(0));
234 EXPECTS(n == c0.extent(1));
237 EXPECTS(arr_a.indexmap().min_stride() == 1);
238 EXPECTS(arr_b.indexmap().min_stride() == 1);
239 EXPECTS(c.indexmap().min_stride() == 1);
243#if defined(NDA_HAVE_DEVICE)
244 device::gemm_batch_strided(
get_op<A>,
get_op<B>, m, n, k, alpha, arr_a.data(),
get_ld(a0), arr_a.strides()[0], arr_b.data(),
get_ld(b0),
245 arr_b.strides()[0], beta, c.data(),
get_ld(c0), c.strides()[0], batch_count);
250 f77::gemm_batch_strided(
get_op<A>,
get_op<B>, m, n, k, alpha, arr_a.data(),
get_ld(a0), arr_a.strides()[0], arr_b.data(),
get_ld(b0),
251 arr_b.strides()[0], beta, c.data(),
get_ld(c0), c.strides()[0], batch_count);
Provides definitions and type traits involving the different memory address spaces supported by nda.
Provides a C++ interface for various BLAS routines.
ValueType const * data() const noexcept
Get a pointer to the actual data (in general this is not the beginning of the memory block for a view...
Check if a given type is an nda::MemoryArray of a certain rank.
Provides concepts for the nda library.
Provides various convenient aliases and helper functions for nda::basic_array and nda::basic_array_vi...
Provides GPU and non-GPU specific functionality.
auto transposed_view(A &&a)
Transpose two indices/dimensions of an nda::basic_array or nda::basic_array_view.
auto transpose(A &&a)
Transpose the memory layout of an nda::MemoryArray or an nda::expr_call.
basic_array< ValueType, 1, C_layout, 'V', ContainerPolicy > vector
Alias template of an nda::basic_array with rank 1 and a 'V' algebra.
decltype(auto) get_first_element(A &&a)
Get the first element of an array/view or simply return the scalar if a scalar is given.
constexpr bool have_same_value_type_v
Constexpr variable that is true if all types in As have the same value type as A0.
std::decay_t< decltype(get_first_element(std::declval< A const >()))> get_value_t
Get the value type of an array/view or a scalar type.
static constexpr bool has_C_layout
Constexpr variable that is true if the given nda::Array type has nda::C_layout.
int get_ld(A const &a)
Get the leading dimension of an nda::MemoryArray with rank 1 or 2 for BLAS/LAPACK calls.
static constexpr bool is_conj_array_expr
Constexpr variable that is true if the given type is a conjugate lazy expression.
static constexpr char get_op
Variable template that determines the BLAS matrix operation tag ('N','T','C') based on the given bool...
MemoryArray decltype(auto) get_array(A &&a)
Get the underlying array of a conjugate lazy expression or return the array itself in case it is an n...
void gemm_batch_strided(get_value_t< A > alpha, A const &a, B const &b, get_value_t< A > beta, C &&c)
Interface to MKL's/CUDA's gemm_batch_strided routine.
void gemm_vbatch(get_value_t< A > alpha, std::vector< A > const &va, std::vector< B > const &vb, get_value_t< A > beta, std::vector< C > &vc)
Interface to MKL's/Magma's gemm_vbatch routine.
void gemm_batch(get_value_t< A > alpha, std::vector< A > const &va, std::vector< B > const &vb, get_value_t< A > beta, std::vector< C > &vc)
Interface to MKL's/CUDA's gemm_batch and gemm_vbatch routines.
static constexpr bool have_compatible_addr_space
Constexpr variable that is true if all given types have compatible address spaces.
static constexpr bool have_device_compatible_addr_space
Constexpr variable that is true if all given types have an address space compatible with Device.
static constexpr bool on_host
Constexpr variable that is true if all given types have a Host address space.
void compile_error_no_gpu()
Trigger a compilation error in case GPU specific functionality is used without configuring the projec...
constexpr bool is_blas_lapack_v
Alias for nda::is_double_or_complex_v.
Macros used in the nda library.
Provides type traits for the nda library.