22#ifndef NDA_HAVE_DEVICE
55 template <
bool VBATCH = false, Matrix A, Matrix B, MemoryMatrix C>
60 EXPECTS(va.size() == vb.size() and va.size() == vc.size());
61 if (va.empty())
return;
62 int batch_count = va.size();
65 auto to_mat = []<
typename Z>(Z &z) ->
auto & {
67 return std::get<0>(z.a);
71 auto &a0 = to_mat(va[0]);
72 auto &b0 = to_mat(vb[0]);
76 using mat_a_type =
decltype(a0);
77 using mat_b_type =
decltype(b0);
83 auto map_transpose = [](
auto &v) {
84 auto vt = std::vector<std::decay_t<
decltype(
transpose(v[0]))>>{};
86 std::transform(v.begin(), v.end(), std::back_inserter(vt), [](
auto &x) { return transpose(x); });
89 auto vct = map_transpose(vc);
94 auto constexpr vec_adr_spc = []() {
return mem::on_host<C> ? mem::Host : mem::Unified; }();
97 auto get_ptrs = [&to_mat]<
typename V>(V &v) {
98 EXPECTS(std::all_of(v.begin(), v.end(),
99 [&v, &to_mat](
auto &z) { return (VBATCH or z.shape() == v[0].shape()) and to_mat(z).indexmap().min_stride() == 1; }));
101 using ptr_t = std::conditional_t<std::is_const_v<V>, value_t
const *, value_t *>;
103 std::transform(v.begin(), v.end(), v_ptrs.begin(), [&to_mat](
auto &z) { return to_mat(z).data(); });
106 auto a_ptrs = get_ptrs(va);
107 auto b_ptrs = get_ptrs(vb);
108 auto c_ptrs = get_ptrs(vc);
117 if constexpr (VBATCH) {
120 vldb(batch_count + 1), vldc(batch_count + 1);
122 for (
auto i : range(batch_count)) {
123 auto &ai = to_mat(va[i]);
124 auto &bi = to_mat(vb[i]);
127 EXPECTS(ai.extent(1) == bi.extent(0));
128 EXPECTS(ai.extent(0) == ci.extent(0));
129 EXPECTS(bi.extent(1) == ci.extent(1));
131 vm[i] = ai.extent(0);
132 vk[i] = ai.extent(1);
133 vn[i] = bi.extent(1);
141#if defined(NDA_HAVE_DEVICE)
142 device::gemm_vbatch(op_a, op_b, vm.data(), vn.data(), vk.data(), alpha, a_ptrs.data(), vlda.data(), b_ptrs.data(), vldb.data(), beta,
143 c_ptrs.data(), vldc.
data(), batch_count);
148 f77::gemm_vbatch(op_a, op_b, vm.data(), vn.data(), vk.data(), alpha, a_ptrs.data(), vlda.data(), b_ptrs.data(), vldb.data(), beta,
149 c_ptrs.data(), vldc.
data(), batch_count);
153 EXPECTS(a0.extent(1) == b0.extent(0));
154 EXPECTS(a0.extent(0) == c0.extent(0));
155 EXPECTS(b0.extent(1) == c0.extent(1));
157 auto [m, k] = a0.shape();
158 auto n = b0.extent(1);
161#if defined(NDA_HAVE_DEVICE)
162 device::gemm_batch(op_a, op_b, m, n, k, alpha, a_ptrs.data(),
get_ld(a0), b_ptrs.data(),
get_ld(b0), beta, c_ptrs.data(),
get_ld(c0),
168 f77::gemm_batch(op_a, op_b, m, n, k, alpha, a_ptrs.data(),
get_ld(a0), b_ptrs.data(),
get_ld(b0), beta, c_ptrs.data(),
get_ld(c0),
187 template <Matrix A, Matrix B, MemoryMatrix C>
207 template <ArrayOfRank<3> A, ArrayOfRank<3> B, MemoryArrayOfRank<3> C>
212 EXPECTS(a.shape()[0] == b.shape()[0] and a.shape()[0] == c.shape()[0]);
215 auto to_arr = []<
typename Z>(Z &z) ->
auto & {
217 return std::get<0>(z.a);
221 auto arr_a = to_arr(a);
222 auto arr_b = to_arr(b);
225 using arr_a_type =
decltype(arr_a);
226 using arr_b_type =
decltype(arr_b);
228 "Error in nda::blas::gemm_batch_strided: Incompatible memory address spaces");
231 auto _ = nda::range::all;
232 auto a0 = arr_a(0, _, _);
233 auto b0 = arr_b(0, _, _);
234 auto c0 = c(0, _, _);
235 EXPECTS(a0.extent(1) == b0.extent(0));
236 EXPECTS(a0.extent(0) == c0.extent(0));
237 EXPECTS(b0.extent(1) == c0.extent(1));
238 EXPECTS(arr_a.indexmap().min_stride() == 1);
239 EXPECTS(arr_b.indexmap().min_stride() == 1);
240 EXPECTS(c.indexmap().min_stride() == 1);
251 auto [m, k] = a0.shape();
252 auto n = b0.extent(1);
255#if defined(NDA_HAVE_DEVICE)
256 device::gemm_batch_strided(op_a, op_b, m, n, k, alpha, arr_a.data(),
get_ld(a0), arr_a.strides()[0], arr_b.data(),
get_ld(b0),
257 arr_b.strides()[0], beta, c.data(),
get_ld(c0), c.strides()[0], arr_a.extent(0));
262 f77::gemm_batch_strided(op_a, op_b, m, n, k, alpha, arr_a.data(),
get_ld(a0), arr_a.strides()[0], arr_b.data(),
get_ld(b0),
263 arr_b.strides()[0], beta, c.data(),
get_ld(c0), c.strides()[0], arr_a.extent(0));
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.
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.
int get_ld(A const &a)
Get the leading dimension in LAPACK jargon of an nda::MemoryMatrix.
void gemm_batch_strided(get_value_t< A > alpha, A const &a, B const &b, get_value_t< A > beta, C &&c)
Implements a strided batched version of nda::blas::gemm taking 3-dimensional arrays as arguments.
static constexpr bool has_C_layout
Constexpr variable that is true if the given nda::Array type has a C memory layout.
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)
Wrapper of nda::blas::gemm_batch that allows variable sized matrices.
static constexpr bool is_conj_array_expr
Constexpr variable that is true if the given type is a conjugate lazy expression.
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)
Implements a batched version of nda::blas::gemm taking vectors of matrices as arguments.
const char get_op
Variable template that determines the BLAS matrix operation tag ('N','T','C') based on the given bool...
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.