Locality of Reference - Spatial and Temporal Locality Example: Matrix Multiplication

Spatial and Temporal Locality Example: Matrix Multiplication

A common example is matrix multiplication:

for i in 0..n for j in 0..m for k in 0..p C = C + A * B;

When dealing with large matrices, this algorithm tends to shuffle data around too much. Since memory is pulled up the hierarchy in consecutive address blocks, in the C programming language it would be advantageous to refer to several memory addresses that share the same row (spatial locality). By keeping the row number fixed, the second element changes more rapidly. In C and C++, this means the memory addresses are used more consecutively. One can see that since j affects the column reference of both matrices C and B, it should be iterated in the innermost loop (this will fix the row iterators, i and k, while j moves across each column in the row). This will not change the mathematical result, but it improves efficiency. By switching the looping order for j and k, the speedup in large matrix multiplications becomes dramatic. (In this case, 'large' means, approximately, more than 100,000 elements in each matrix, or enough addressable memory such that the matrices will not fit in L1 and L2 caches.)

Temporal locality can also be improved in the above example by using a technique called blocking. The larger matrix can be divided into evenly-sized sub-matrices, so that the smaller blocks can be referenced (multiplied) several times while in memory.

for (ii = 0; ii < SIZE; ii += BLOCK_SIZE) for (kk = 0; kk < SIZE; kk += BLOCK_SIZE) for (jj = 0; jj < SIZE; jj += BLOCK_SIZE) for (i = ii; i < ii + BLOCK_SIZE && i < SIZE; i++) for (k = kk; k < kk + BLOCK_SIZE && k < SIZE; k++) for (j = jj; j < jj + BLOCK_SIZE && j < SIZE; j++) C = C + A * B;

The temporal locality of the above solution is provided because a block can be used several times before moving on, so that it is moved in and out of memory less often. Spatial locality is improved because elements with consecutive memory addresses tend to be pulled up the memory hierarchy together.

Read more about this topic:  Locality Of Reference

Famous quotes containing the words temporal and/or matrix:

    What’s this, Aurora Leigh,
    You write so of the poets and not laugh?
    Those virtuous liars, dreamers after dark,
    Exaggerators of the sun and moon,
    And soothsayers in a tea-cup? I write so
    Of the only truth-tellers, now left to God,—
    The only speakers of essential truth,
    Opposed to relative, comparative,
    And temporal truths;...
    The only teachers who instruct mankind,
    From just a shadow on a charnel-wall.
    Elizabeth Barrett Browning (1806–1861)

    In all cultures, the family imprints its members with selfhood. Human experience of identity has two elements; a sense of belonging and a sense of being separate. The laboratory in which these ingredients are mixed and dispensed is the family, the matrix of identity.
    Salvador Minuchin (20th century)