A diffusion field is stored as a float64 array of shape in C order.
PHYSICS · Multi-part · Fermi University Physics · Computational Physics · Scientific programming for physicists · High-performance and data-driven computational physics
Scientific Computing for Physicists, Ch Arrays in memory: layout, cost and the data structures of physical problems
Problem
A diffusion field is stored as a float64 array v of shape (3000,4000) in C order.
((a)) Is w=v[500:2500,1000:3000] a view or a copy of v's memory? Give the byte strides of w.
((b)) Using the model of a 64B cache line and 8B per float64 element, predict the ratio of the memory traffic of v[:, ::4].sum() (every fourth value of every row, 3×106 elements at stride four) to that of v[:750].sum() (the first 750 full rows, the same number of elements, contiguous), and hence the ratio of their times if both sums stream from main memory at the same rate.
((c)) Inside a 10000-step loop the update is currently written v = v + dt*a, which allocates a new array every step. Predict whether rewriting it as v += dt*a changes the number of bytes read from and written to memory each step.
((d)) Measurement: the contiguous sum takes 1.2ms and the stride-four sum takes 4.5ms (single runs). Over the full 10000-step loop the original code takes 240s in total and the in-place rewrite takes 233s. State the measured ratio and the measured percentage reduction in loop time, and account for how each compares with your predictions in (b) and (c).