is smaller. The slowest memory is cheaper. Data Locality principle is a phenomenon in which the same values, or related storage locations, are frequently accessed.
… a[1,m-1] … a[n-1,1] … a[n-1,m-1] for i in 0..n for j in 0..m for k in 0..p C[i][j] = C[i][j] + A[i][k] * B[k][j]; a[0,0] a[1,0] a[n-1,0] Can be factored out of the loop by compiler
… a[1,m-1] … a[n-1,1] … a[n-1,m-1] for i in 0..n for j in 0..m for k in 0..p C[i][j] = C[i][j] + A[i][k] * B[k][j]; a[0,0] a[1,0] a[n-1,0] Can be factored out of the loop by compiler
… a[1,m-1] … a[n-1,1] … a[n-1,m-1] for i in 0..n for j in 0..m for k in 0..p C[i][j] = C[i][j] + A[i][k] * B[k][j]; a[0,0] a[1,0] a[n-1,0] Can be factored out of the loop by compiler
… a[1,m-1] … a[n-1,1] … a[n-1,m-1] for i in 0..n for j in 0..m for k in 0..p C[i][j] = C[i][j] + A[i][k] * B[k][j]; a[0,0] a[1,0] a[n-1,0] Can be factored out of the loop by compiler Iterating over the items in the single row. Row data are cached.
… a[1,m-1] … a[n-1,1] … a[n-1,m-1] for i in 0..n for j in 0..m for k in 0..p C[i][j] = C[i][j] + A[i][k] * B[k][j]; a[0,0] a[1,0] a[n-1,0] Can be factored out of the loop by compiler Iterating over the items in the single row. Row data are cached.
… a[1,m-1] … a[n-1,1] … a[n-1,m-1] for i in 0..n for j in 0..m for k in 0..p C[i][j] = C[i][j] + A[i][k] * B[k][j]; a[0,0] a[1,0] a[n-1,0] Can be factored out of the loop by compiler Iterating over the items in the single row. Row data are cached.
… a[1,m-1] … a[n-1,1] … a[n-1,m-1] for i in 0..n for j in 0..m for k in 0..p C[i][j] = C[i][j] + A[i][k] * B[k][j]; a[0,0] a[1,0] a[n-1,0] Can be factored out of the loop by compiler Iterating over the items in the single row. Row data are cached. Iterating over different rows. Cache miss is highly likely.
in 0..m C[i][j] = C[i][j] + A[i][k] * B[k][j]; Example: Matrix multiplication Powered by EPAM a[0,1] … a[0,m-1] a[1,1] … a[1,m-1] … a[n-1,1] … a[n-1,m-1] a[0,0] a[1,0] a[n-1,0] Iterating over the single row. Cached. Factored out of the loop.
in 0..m C[i][j] = C[i][j] + A[i][k] * B[k][j]; Example: Matrix multiplication Powered by EPAM a[0,1] … a[0,m-1] a[1,1] … a[1,m-1] … a[n-1,1] … a[n-1,m-1] a[0,0] a[1,0] a[n-1,0] Iterating over the single row. Cached. Factored out of the loop.
in 0..m C[i][j] = C[i][j] + A[i][k] * B[k][j]; Example: Matrix multiplication Powered by EPAM a[0,1] … a[0,m-1] a[1,1] … a[1,m-1] … a[n-1,1] … a[n-1,m-1] a[0,0] a[1,0] a[n-1,0] Iterating over the single row. Cached. Factored out of the loop. Again iterating over the single row. Cached.
1) for (auto& p : data) { ++p.X; } // Thread 2 (affinity 4) for (auto& p : data) { ++p.Y; } for (auto& p : data) { ++p.X; ++p.Y; } Single-threaded implementation is faster
1) for (auto& p : data) { ++p.X; } // Thread 2 (affinity 4) for (auto& p : data) { ++p.Y; } for (auto& p : data) { ++p.X; ++p.Y; } Single-threaded implementation is faster