1. In the following C code, elements of each row are stored contiguously in memory.
Assume each element in an array is a 32-bit integer.
p = 0;
for (j=0; j<8; ++j)
for(i=500; i>0; --i )
{
A[ i] = 2 * B[j];
++p;
}
a. How many 32-bit integers can be stored in a 32-byte cache block?
b. Which variable references exhibit temporal locality?
c. Which variable references exhibit spatial locality?
a. 1 byte= 8 bits
This implies 32 bits = 32/8 = 4 bytes
No: of 32 bit (4 bytes) integers than can be stored in a 32 - byte cache block = 32/4 = 8
Hence 8 32 bit integers can be stored in a 32 byte cache block
b. p is referenced by the instruction ++p frequently. Hence we can say that the variable p exhibit temporal locality. Similary B[j] (j being any integer) is referenced by the instruction A[i] = 2* B[j] frequently. Hence B[j] also exhibit temporal locality
c. In the inner loop, A[i], i being the variable of the loop, is executed continuously. Hence A[i[ and A[i+] will be saved next to each other in memory. This implies that A[i] exhibits spatial locality.
Get Answers For Free
Most questions answered within 1 hours.