P2812: P1673R11 LEWG presentation

Document #: P2812
Date: 2023/02/10
Project: Programming Language C++
LEWG
Reply-to: Mark Hoemmen
<>
Christian Trott
<>
Damien Lebrun-Grandie
<>
Nevin Liber
<>

1 P1673R11 LEWG presentation

1.1 Context

This paper is nonnormative. It is the presentation that P1673R11 coauthors gave to LEWG at the Issaquah WG21 meeting on 2023/02/10.

1.2 Motivation

1.2.1 Previous WG21 talks and proposals

1.2.2 Very brief summary

1.2.3 Example: matrix-vector product

using vector_t = mdspan<double, dextents<int, 1>>;
using matrix_t = mdspan<double, dextents<int, 2>>;

// ... allocate and fill ...
vector_t x = /* ... */;
vector_t y = /* ... */;
matrix_t A = /* ... */;

// compute y = Ax
matrix_vector_multiply(y, A, x);

// compute norm2 of y
double val = vector_norm2(y);

// mixed precision, different layout 
mdspan<float, dextents<int, 2>, layout_left> A_f =
  /* allocate and fill */;
matrix_vector_multiply(y, A_f, x);
double val2 = vector_norm2(y);

1.3 Previous reviews

1.4 Implemented Feedback from Kona

1.4.1 Specification of requirements on mdspan parameters

Implements LEWG’s Kona 2022/11/10 request to

explore expressing constraints with concepts instead of named type requirements.

1.4.1.1 Before

template<class in_vector_t,
         class in_matrix_t,
         class out_vector_t>
void matrix_vector_product(in_matrix_t A,
                           in_vector_t x,
                           out_vector_t y);

1.4.1.2 After

template<in-matrix InMat,
         in-vector InVec,
         out-vector OutVec>
void matrix_vector_product(InMat A,
                           InVec x,
                           OutVec y);

1.4.1.3 Exposition-only concept example

template<class T>
struct is-mdspan : false_type {}; // exposition only

template<class ElementType, class Extents, class Layout, class Accessor>
struct is-mdspan<mdspan<ElementType, Extents, Layout, Accessor>> // exposition only
  : true_type {};

template<class T>
concept in-vector = // exposition only
  is-mdspan<T>::value &&
  T::rank() == 1;

template<class T>
concept out-vector = // exposition only
  is-mdspan<T>::value &&
  T::rank() == 1 &&
  is_same_v<remove_const_t<typename T::element_type>, typename T::element_type> &&
  T::is_always_unique();

// ...

template<class T>
concept in-matrix = // exposition only
  is-mdspan<T>::value &&
  T::rank() == 2;

Entirely syntactic, except for:

2 If an algorithm in [linalg.algs] accesses the elements of an in-vector, in-matrix, or in-object, it will do so in read-only fashion.

1.4.2 Describe the algorithms mathematically

Follows LEWG guidance to simplify Effects and Constraints.

This is our interpretation of the “hand wavy do math” poll option that received a majority of votes at Kona on 2022/11/10.

1.4.2.1 P1673 takes any “number-like” types

1.4.2.2 This imposes a wording challenge

Before After
For i in the domain of y and N equal to A.extent(1), the mathematical expression for the algorithm is y[i] = the sum of A[i,j] * x[j] for all j such that i,j is in the domain of A. Effects: Computes y such that y = Ax.

P1673R10 defined “Mathematical expression for the algorithm” as:

Each algorithm or method using the type has one or more associated mathematical expressions that defines the algorithm’s or method’s behavior. For each algorithm or method, its mathematical expression(s) are either explicitly stated as such, or are implicitly stated in the algorithm’s or method’s description. The requirements below will refer to those mathematical expression(s).

P1673R11’s Effects reference the mathematical definition of matrix-vector multiplication. Compare to wording for the mathematical special functions [sf.cmath], which uses math-font integrals, derivatives, and infinite sums. [linalg.reqs.val] is much shorter.

1.4.3 Linear algebra value types

1.5 Notes