Question
Assume that both the arrays (a & b) are stored in a row-major fashion, which means all the element of a row are stored contiguously
Assume that both the arrays (a & b) are stored in a row-major fashion, which means all the element of a row are stored contiguously (a[i][0], a[i][1], a[i][2] etc. are stored in consecutive memory locations). i, j and sum are scalar variables. In the two codes given below identify variables which exhibit temporal locality and variables which exhibit spatial locality. What kind of locality is exhibited by the code itself? Provide adequate justifications by tracing the access pattern for your answer to get full credit.
Code-1: sum = 0.0; for(i=0; i<5000; i++){ for(j=0; j<100; j++){ sum =sum + a[i][j] * b[i][j]; } }
Code-2: sum = 0.0; for(j=0; j<100; j++){ for(i=0; i<5000; i++){ sum =sum + a[i][j] * b[i][j]; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started