Page error for code snippet

This is not a homework question. Today he came to my semester exam.

This code fragment calculates the average value for each column of the table t[i][j] 0<=i<18 ; 0<=j<1024 t[i][j] 0<=i<18 ; 0<=j<1024

 for (j = 0; j < 1024; i++) { temp = 0; for (i = 0; i < 18; i++) { temp += temp + t[i][j]; } cout << temp/18; } 

Variables are 32-bit floating point values.

Variables i , j , temp are stored in the processor register (therefore, there is no need to refer to memory to access temp). The main memory is the address and paged page containing 17 frames, each 1024 words in size and one word is 4 bytes. Page Replacement Policy - LRU.

Determine the number of page errors for the execution of this fragment of the program? Ans: 18432

How to calculate it?

+4
source share
1 answer
 int array[3][3] = {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}}; 

The layout of this array in memory is [0, 1, 2, 3, 4, 5, 6, 7, 8] , which

 array[0][0] array[0][1] array[0][2] array[1][0] array[1][1] array[1][2] array[2][0] array[2][1] array[2][2] 

Here the difference of memory addresses between array[1][0] and array[2][0] is 3;

So, the array a[18][1024] . The difference between a[i][j] and a[i+1][j] is 1024 bytes (page error size). Thus, every time your inner loop fires, it causes a page error. Your inner loop runs 18 * 1024 times (18432).

+5
source

Source: https://habr.com/ru/post/1446994/


All Articles