TRIQS/nda 2.0.0
Multi-dimensional array library for C++
Loading...
Searching...
No Matches
dot.hpp
Go to the documentation of this file.
1// Copyright (c) 2024--present, The Simons Foundation
2// This file is part of TRIQS/nda and is licensed under the Apache License, Version 2.0.
3// SPDX-License-Identifier: Apache-2.0
4// See LICENSE in the root of this distribution for details.
5
10
11#pragma once
12
15#include "./tools.hpp"
16#include "../algorithms.hpp"
17#include "../basic_array.hpp"
18#include "../declarations.hpp"
21#include "../mem/policies.hpp"
22#include "../traits.hpp"
23
24#include <string_view>
25
26namespace nda::tensor {
27
32
67 template <BlasArrayOrConj A, BlasArrayOrConjFor<A> B>
68 get_value_t<A> dot(A const &a, std::string_view idx_a, B const &b, std::string_view idx_b) {
69 // compile-time checks
70 constexpr bool run_on_device = mem::have_device_compatible_addr_space<A, B>;
71 static_assert(!run_on_device || have_cutensor, "nda::tensor::dot: cuTENSOR support is required");
72 static_assert(run_on_device || have_tblis || get_rank<A> == get_rank<B>, "nda::tensor::dot: nda host fallback requires identical ranks");
73
74 // dispatch to backends
75 if constexpr (run_on_device) {
77 device::contract(get_value_t<A>{1}, a, idx_a, b, idx_b, get_value_t<A>{0}, z.data(), "", z.data());
78 return nda::to_host(z)(0);
79 } else if constexpr (have_tblis) {
80 return tblis::dot(a, idx_a, b, idx_b);
81 } else {
82 require_equal_indices(idx_a, idx_b, get_rank<A>, "dot");
83 return nda::sum(nda::hadamard(a, b));
84 }
85 }
86
88 template <BlasArrayOrConj A, BlasArrayOrConjFor<A, get_rank<A>> B>
89 get_value_t<A> dot(A const &a, B const &b) {
90 auto idx = default_index<get_rank<A>>();
91 return dot(a, idx, b, idx);
92 }
93
95
96} // namespace nda::tensor
Provides definitions and type traits involving the different memory address spaces supported by nda.
Provides various algorithms to be used with nda::Array objects.
Provides the generic class for arrays.
A generic multi-dimensional array.
Provides a C++ interface for various cuTENSOR routines.
Provides various convenient aliases and helper functions for nda::basic_array and nda::basic_array_vi...
auto sum(A const &a)
Sum all the elements of an nda::Array object.
constexpr auto hadamard(A &&a, B &&b)
Hadamard product of two nda::Array objects.
auto zeros(std::array< Int, Rank > const &shape)
Make an array of the given shape on the given address space and zero-initialize it.
decltype(auto) to_host(A &&a)
Convert an nda::MemoryArray to its regular type on host memory.
constexpr int get_rank
Constexpr variable that specifies the rank of an nda::Array or of a contiguous 1-dimensional range.
Definition traits.hpp:147
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.
Definition traits.hpp:212
static constexpr bool have_device_compatible_addr_space
Constexpr variable that is true if all given types have an address space compatible with Device.
heap_basic< mem::mallocator< AdrSp > > heap
Alias template of the nda::heap_basic policy using an nda::mem::mallocator.
Definition policies.hpp:52
get_value_t< A > dot(A const &a, std::string_view idx_a, B const &b, std::string_view idx_b)
Full tensor dot product with cuTENSOR/TBLIS/nda dispatch.
Definition dot.hpp:68
static constexpr bool have_tblis
Constexpr variable that is true if nda is configured with TBLIS support.
Definition tools.hpp:47
static constexpr bool have_cutensor
Constexpr variable that is true if nda is configured cuTENSOR support.
Definition tools.hpp:40
void require_equal_indices(std::string_view idx_a, std::string_view idx_b, int rank, std::string_view op_name)
Check if two index strings are equal and have a specified length.
Definition tools.hpp:247
std::string_view default_index()
Generate a default index string ("abc...") of a given length.
Definition tools.hpp:265
Provides definitions of various layout policies.
Defines various memory handling policies.
Contiguous layout policy with C-order (row-major order).
Definition policies.hpp:36
Provides a C++ interface for various TBLIS tensor routines.
Provides various traits and utilities for the tensor interface.
Provides type traits for the nda library.