- Programming languages such as Java, C#, C and C++ use the Row major technique to
store arrays while programs such as Matlab etc. which perform scientific computing use
Column major.
Affect of Cache on memory read/write:
-When reading/writing data, data goes from RAM → Cache → CPU or RAM Cache
CPU.
-Data transfer between the CPU and Cache takes place in words. Meaning that either 2, 4 or
8 bytes depending on the type of operating system.
-Data transfer between Cache and memory takes place in blocks of multiple words.
-Cache hit is when the desired data is found inside the cache. In this case we don’t have to
look inside the memory. Cache miss is when the desired data isn’t found inside the cache and
needs to be transferred from the RAM first into the Cache and then into the CPU.
Suppose that transferring one word from
Cache <-> CPU takes 1 unit of time and
one block from RAM <-> CPU takes 3
units of time. In this case, we would save 2
units of time if we already had our desired
data stored in the Cache.
Suppose we write code for a loop to access all the
elements of the array stored in Row major:
When the data is accessed through the loop,
the first element won’t be found inside the Cache
so, it’ll be a cache miss. The first block will then be
loaded into the cache
This initial element will take 3 units of time to be loaded into the CPU. The next 3 elements (2,
3 and 4) will already be in the Cache and will only take 1 unit of time to be loaded into the
CPU. (Note: We are considering one block size to be of 4 elements)
The cycle for the next 4 elements will be same and it will continue until the loop ends.
In Row major the total time to access all the elements with Cache is = (3+1+1+1) * 4 = 24
units of time.
Without Cache it would take = 3 * 16 = 48 units of time.
Now if we use the same code for the
loop but our array is stored in Column major:
For every element there will be a Cache miss because
We are still trying to access the elements row by row
Whereas our data is stored column by column.