# Dense matrix-vector multiplication

Primary authors of this description: A.V.Frolov, Vad.V.Voevodin (Section 2.2), A.M.Teplov (Section 2.4)

## 1 Properties and structure of the algorithm

### 1.1 General description of the algorithm

Matrix-vector multiplication is one of the basic procedures in algorithmic linear algebra, which is widely used in a number of different methods. Here, we consider the product $y = Ax$ of a dense real matrix and a vector (serial real version), that is, the version in which neither a special form of the matrix nor the associative properties of the addition operation are used.

### 1.2 Mathematical description of the algorithm

Input data: dense matrix $A$ of size m-by-n (with entries $a_{ij}$), its vector cofactor $x$ of dimension n (with components $x_{i}$).

Output data: vector $y$ of dimension m (with components $y_{i}$).

Formulas of the method:

\begin{align} y_{i} = \sum_{j = 1}^{n} a_{ij} x_{j}, \quad i \in [1, m]. \end{align}

There also exists a block version of the method; however, this description treats only the pointwise version.

### 1.3 Computational kernel of the algorithm

The computational kernel of the dense matrix-vector multiplication can be compiled of repeated dot products of the rows of $A$ and the vector $x$ (there are on the whole $m$ such products):

$\sum_{j = 1}^{n} a_{ij} x_{j}$

Depending on the problem requirements, these sums are calculated with or without using the accumulation mode.

### 1.4 Macro structure of the algorithm

As already noted in the description of the computational kernel, the basic part of the dense matrix-vector multiplication is compiled of repeated dot products of the rows of $A$ and the vector $x$ (there are on the whole $m$ such products):

$\sum_{j = 1}^{n} a_{ij} x_{j}$

These calculations are performed with or without using the accumulation mode.

### 1.5 Implementation scheme of the serial algorithm

For all $i$ from $1$ to $m$, do

$y_{i} = \sum_{j = 1}^{n} a_{ij} x_{j}$

We emphasize that sums of the form $\sum_{j = 1}^{n} a_{ij} x_{j}$ are calculated in the accumulation mode by adding the products $a_{ij} x_{j}$ to the current (temporary) value of the component $y_{i}$. The index $j$ increases from $1$ to $n$. All the sums are initialized to zero. The general scheme is virtually the same if summations are performed in the decreasing order of $j$; therefore, this case is not considered. Other orders of summation change the parallel properties of the algorithm and are considered in separate descriptions.

### 1.6 Serial complexity of the algorithm

The multiplication of a square matrix of order $n$ (that is, $m=n$) by a vector of the same dimension in the (fastest) serial version requires

• $n^2$ multiplications and the same number of additions.

The multiplication of an $m$-by-$n$ matrix by a vector of dimension $n$ in the (fastest) serial version requires

• $mn$ multiplications and the same number of additions.

The use of accumulation requires that multiplications and additions be done in the double precision mode (or such procedures as the Fortran function DPROD be used). This increases the computation time for performing the matrix-vector multiplication.

In terms of serial complexity, the matrix-vector multiplication is qualified as a quadratic complexity algorithm (or a bilinear complexity algorithm if the matrix is rectangular).

### 1.7 Information graph

We describe the algorithm graph both analytically and graphically.

Figure 1. Graph of the serial multiplication of a dense matrix by a vector with demonstration of the input and output data

The algorithm graph of multiplying a dense matrix by a vector consists of a single group of vertices placed at integer nodes of a two-dimensional domain. The corresponding operation is $a+bc$.

The natural coordinates of this domain are as follows:

• $i$ varies from $1$ to $m$, taking all the integer values in this range;
• $j$ varies from $1$ to $n$, taking all the integer values in this range.

The arguments of the operation are as follows:

• $a$ is:
• the constant $0$ if $j = 1$;
• the result of performing the operation corresponding to the vertex with coordinates $i, j -1$ if $k \gt 1$;
• $b$ is the element $a_{ij}$ of the input data;
• $c$ is the element $x_{j}$ of the input data.

The result of performing the operation is:

• an intermediate data item if $j \lt n$;
• an output data item $c_{ij}$ if $j = n$.

The graph described can be seen in the figure corresponding to the case $m = 4, n = 5$. The vertices are depicted in blue. Only the supplies of the input data from the vector $x$ are shown. The supplies of the entries of $A$ to all the vertices are not shown.

### 1.8 Parallelization resource of the algorithm

The parallel version of the algorithm for multiplying a square matrix of order n by a vector requires that the following layers be successively performed:

• $n$ layers of multiplications and the same number of layers for addition (there are $n$ operations in each layer).

The multiplication of an $m$-by-$n$ matrix by a vector of dimension $n$ in the (fastest) serial version requires

• $n$ layers of multiplications and the same number of layers for addition (there are $m$ operations in each layer).

The use of accumulation requires that multiplications and additions be done in the double precision mode. For the parallel version, this implies that virtually all the intermediate calculations in the algorithm must be performed in double precision if the accumulation option is used. Unlike the situation with the serial version, this results in a certain increase in the required memory size.

In terms of the parallel form height, the dense matrix-vector multiplication is qualified as a linear complexity algorithm. In terms of the parallel form width, its complexity is also linear.

### 1.9 Input and output data of the algorithm

Input data: matrix $A$ (with entries $a_{ij}$), vector $x$ (with components $x_{i}$).

Size of the input data : $mn+n$ .

Output data : vector $y$ (with components $y_{i}$).

Size of the output data: $m$.

### 1.10 Properties of the algorithm

It is clear that, in the case of unlimited resources, the ratio of the serial to parallel complexity is linear (since this is the ratio of the quadratic or bilinear complexity to linear one).

The computational power of the dense matrix-vector multiplication, understood as the ratio of the number of operations to the total size of the input and output data, is only a constant.

The algorithm of dense matrix-vector multiplication is completely determined. We do not consider any other order of performing associative operations in this version.