We have a 1024*1024 matrix with 32 bit numbers that is going to be normalized. Suppose that the size of the page in the virtual memory is 4KB and we allocate 1 MB of main memory to save the matrix while we are working. Suppose that we need 10 ms to upload a page from the disc.
a) Suppose that we work with the matrix one column at a time. How many page faults will be caused to traverse all the matrix elements, if they are saved in the virtual memory by column?
The answer is 1024, but I don't understand why this is?
b) What if we work by row not by column?
The answer for this is 1024 page faults*2*1024
How do we get both of these answers,can you explain these to me?
Since an entry of the matrix is of size 32 bit, which is 4 Bytes, an entire row or column can be stored in a virtual memory of 4 Bytes * 1024 = 4KB. Since the memory is filled using columns, we can fit exactly one column in the memory.
Say we walk over the elements column by column. Getting the first entry we see this one is not present in the virtual memory so we have to load it (i.e. page fault). Now the entire column is stored so the next 1023 elements do not yield a page fault (they are all present in the memory). The next fault appears when accessing the first element of the second column. In general we have one page fault per column, which results in 1024 page faults.
Now we traverse the matrix row wise, every time we access the an element it will not be contained in the memory. This is clear since we have one column on the memory at all time and we never access elements of the same column sequentially. So each entry gives a page fault, resulting in 1024*1024 page faults.