TRIQS/nda 2.0.0
Multi-dimensional array library for C++
Loading...
Searching...
No Matches
elementwise.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
14#include "./tools.hpp"
15#include "../exceptions.hpp"
17#include "../traits.hpp"
18
19#include <string_view>
20#include <utility>
21
22namespace nda::tensor {
23
28
63 template <BlasArrayOrConj A, BlasArrayFor<A> B>
64 void elementwise(get_value_t<A> alpha, A const &a, std::string_view idx_a, get_value_t<A> beta, B &&b, std::string_view idx_b, // NOLINT
65 binary_op op = binary_op::SUM) {
66 // compile-time checks
67 constexpr bool run_on_device = mem::have_device_compatible_addr_space<A, B>;
68 static_assert(!run_on_device || have_cutensor, "nda::tensor::elementwise: cuTENSOR support is required");
69 static_assert(run_on_device || get_rank<A> == get_rank<B>, "nda::tensor::elementwise: host fallback requires identical ranks");
70
71 // dispatch to backends
72 if constexpr (run_on_device) {
73 device::elementwise_binary(alpha, a, idx_a, beta, b, idx_b, b, op);
74 } else {
75 require_equal_indices(idx_a, idx_b, get_rank<A>, "elementwise");
76 b = nda::map([alpha, beta, op](auto x, auto y) { return detail::apply_binary(op, alpha * x, beta * y); })(a, b);
77 }
78 }
79
81 template <BlasArrayOrConj A, BlasArrayFor<A> B>
82 void elementwise(A const &a, std::string_view idx_a, B &&b, std::string_view idx_b, binary_op op = binary_op::SUM) { // NOLINT
83 elementwise(get_value_t<A>{1}, a, idx_a, get_value_t<A>{0}, std::forward<B>(b), idx_b, op);
84 }
85
87 template <BlasArrayOrConj A, BlasArrayFor<A> B>
88 void elementwise(get_value_t<A> alpha, A const &a, get_value_t<A> beta, B &&b, binary_op op = binary_op::SUM) { // NOLINT
89 elementwise(alpha, a, default_index<get_rank<A>>(), beta, std::forward<B>(b), default_index<get_rank<B>>(), op);
90 }
91
96 template <BlasArrayOrConj A, BlasArrayFor<A> B>
97 void elementwise(A const &a, B &&b, binary_op op = binary_op::SUM) { // NOLINT
99 }
100
102
103} // namespace nda::tensor
Provides definitions and type traits involving the different memory address spaces supported by nda.
Provides a C++ interface for various cuTENSOR routines.
Provides a custom runtime error class and macros to assert conditions and throw exceptions.
mapped< F > map(F f)
Create a lazy function call expression on arrays/views.
Definition map.hpp:206
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.
void elementwise(get_value_t< A > alpha, A const &a, std::string_view idx_a, get_value_t< A > beta, B &&b, std::string_view idx_b, binary_op op=binary_op::SUM)
In-place elementwise binary tensor operation with cuTENSOR/nda dispatch.
static constexpr bool have_cutensor
Constexpr variable that is true if nda is configured cuTENSOR support.
Definition tools.hpp:40
binary_op
Binary operations for tensor operations.
Definition tools.hpp:67
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 various traits and utilities for the tensor interface.
Provides type traits for the nda library.