# Communication-avoiding algorithm

Communication-avoiding algorithms minimize movement of data within a memory hierarchy for improving its running-time and energy consumption. These minimize the total of two costs (in terms of time and energy): arithmetic and communication. Communication, in this context refers to moving data, either between levels of memory or between multiple processors over a network. It is much more expensive than arithmetic.

## Formal theory
### Two-level memory model

A common computational model in analyzing communication-avoiding algorithms is the two-level memory model:
* There is one processor and two levels of memory.
* Level 1 memory is infinitely large. Level 0 memory ("cache") has size $M$.
* In the beginning, input resides in level 1. In the end, the output resides in level 1.
* Processor can only operate on data in cache.
* The goal is to minimize data transfers between the two levels of memory.

### Matrix multiplication

Corollary 6.2:
Theorem — Given matrices $A,B,C$ of sizes $n\times m,m\times k,n\times k$, then $AB+C$ has communication complexity $\Omega (\max(mkn/M^{1/2},mk+kn+mk))$.
This lower bound is achievable by tiling matrix multiplication.
More general results for other numerical linear algebra operations can be found in. The following proof is from.
We can draw the computation graph of $D=AB+C$ as a cube of lattice points, each point is of form $(i,j,k)$. Since $D[i,k]=\sum _{j}A[i,j]B[j,k]+C[i,k]$, computing $AB+C$ requires the processor to have access to each point within the cube at least once. So the problem becomes covering the $mnk$ lattice points with a minimal amount of communication.
If $M$ is large, then we can simply load all $mn+nk+mk$ entries then write $nk$ entries. This is uninteresting.
If $M$ is small, then we can divide the minimal-communication algorithm into separate segments. During each segment, it performs exactly $M$ reads to cache, and any number of writes from cache.
During each segment, the processor has access to at most $2M$ different points from $A,B,C$.
Let $E$ be the set of lattice points covered during this segment. Then by the Loomis–Whitney inequality,
$|E|\leq {\sqrt {|\pi _{1}(E)||\pi _{2}(E)||\pi _{3}(E)|}}$ with constraint $\sum _{i}|\pi _{i}(E)|\leq 2M$.
By the inequality of arithmetic and geometric means, we have $|E|\leq \left({\frac {2}{3}}M\right)^{3/2}$, with extremum reached when $\pi _{i}(E)={\frac {2}{3}}M$.
Thus the arithmetic intensity is bounded above by $CM^{1/2}$ where $C=(2/3)^{3/2}$, and so the communication is bounded below by ${\frac {nmk}{CM^{1/2}}}$.
Direct computation verifies that the tiling matrix multiplication algorithm reaches the lower bound.

## Motivation

Consider the following running-time model:
* Measure of computation = Time per FLOP = γ
* Measure of communication = No. of words of data moved = β
⇒ Total running time = γ·(no. of FLOPs) + β·(no. of words)
From the fact that β >> γ as measured in time and energy, communication cost dominates computation cost. Technological trends indicate that the relative cost of communication is increasing on a variety of platforms, from cloud computing to supercomputers to mobile devices. The report also predicts that gap between DRAM access time and FLOPs will increase 100× over coming decade to balance power usage between processors and DRAM.
| FLOP rate (γ) | DRAM bandwidth (β) | Network bandwidth (β) |
| 59% / year | 23% / year | 26% / year |
Energy consumption increases by orders of magnitude as we go higher in the memory hierarchy.
United States president Barack Obama cited communication-avoiding algorithms in the FY 2012 Department of Energy budget request to Congress:
   New Algorithm Improves Performance and Accuracy on Extreme-Scale Computing Systems. On modern computer architectures, communication between processors takes longer than the performance of a floating-point arithmetic operation by a given processor. ASCR researchers have developed a new method, derived from commonly used linear algebra methods, to minimize communications between processors and the memory hierarchy, by reformulating the communication patterns specified within the algorithm. This method has been implemented in the TRILINOS framework, a highly-regarded suite of software, which provides functionality for researchers around the world to solve large scale, complex multi-physics problems.

## Objectives

Communication-avoiding algorithms are designed with the following objectives:
* Reorganize algorithms to reduce communication across all memory hierarchies.
* Attain the lower-bound on communication when possible.
The following simple example demonstrates how these are achieved.

### Matrix multiplication example

Let A, B and C be square matrices of order n × n. The following naive algorithm implements C = C + A * B:
Arithmetic cost (time-complexity): n$^2$(2n − 1) for sufficiently large n or O(n$^3$).
Rewriting this algorithm with communication cost labelled at each step
Fast memory may be defined as the local processor memory (CPU cache) of size M and slow memory may be defined as the DRAM.
Communication cost (reads/writes): n$^3$ + 3n$^2$ or O(n$^3$)
Since total running time = γ·O(n$^3$) + β·O(n$^3$) and β >> γ the communication cost is dominant. The blocked (tiled) matrix multiplication algorithm reduces this dominant term:

#### Blocked (tiled) matrix multiplication

Consider A, B and C to be n/b-by-n/b matrices of b-by-b sub-blocks where b is called the block size; assume three b-by-b blocks fit in fast memory.
Communication cost: 2n$^3$/b + 2n$^2$ reads/writes << 2n$^3$ arithmetic cost
Making b as large possible:
    3b$^2$ ≤ M
we achieve the following communication lower bound:
    3$^{1/2}$n$^3$/M$^{1/2}$ + 2n$^2$ or Ω (no. of FLOPs / M$^{1/2}$)

## Previous approaches for reducing communication

Most of the approaches investigated in the past to address this problem rely on scheduling or tuning techniques that aim at overlapping communication with computation. However, this approach can lead to an improvement of at most a factor of two. Ghosting is a different technique for reducing communication, in which a processor stores and computes redundantly data from neighboring processors for future computations. Cache-oblivious algorithms represent a different approach introduced in 1999 for fast Fourier transforms, and then extended to graph algorithms, dynamic programming, etc. They were also applied to several operations in linear algebra as dense LU and QR factorizations. The design of architecture specific algorithms is another approach that can be used for reducing the communication in parallel algorithms, and there are many examples in the literature of algorithms that are adapted to a given communication topology.