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 vv of shape (3000,4000)(3000, 4000) in C order. ((a)) Is w=v[500:2500, 1000:3000]w = v[500{:}2500,\,1000{:}3000] a view or a copy of vv's memory? Give the byte strides of ww. ((b)) Using the model of a 64 B64\,\mathrm{B} cache line and 8 B8\,\mathrm{B} per float64 element, predict the ratio of the memory traffic of v[:, ::4].sum() (every fourth value of every row, 3×1063\times10^6 elements at stride four) to that of v[:750].sum() (the first 750750 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 1000010000-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.2 ms1.2\,\mathrm{ms} and the stride-four sum takes 4.5 ms4.5\,\mathrm{ms} (single runs). Over the full 1000010000-step loop the original code takes 240 s240\,\mathrm{s} in total and the in-place rewrite takes 233 s233\,\mathrm{s}. 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).

Browse the problem library