A stress field on a two-dimensional grid 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 stress field on a two-dimensional grid is stored as a float64 array σ\sigma of shape (500,800,3,3)(500, 800, 3, 3) in C order, so σ[p,q]\sigma[p,q] is the 3×33\times3 stress tensor at grid cell (p,q)(p,q). ((a)) Is diag=σ[…,0,0]\mathrm{diag} = \sigma[\ldots,0,0] (the xxxx-component field) a view or a copy? Give its shape and byte strides. ((b)) Using the 64 B64\,\mathrm{B} cache line, 8 B8\,\mathrm{B}-element model, predict the ratio of the memory traffic of diag.sum()\mathrm{diag}.\mathrm{sum}() to that of summing a contiguous array of the same 500×800500\times800 float64 values, and hence the ratio of their times if both sums stream from main memory at the same rate. ((c)) A stress-invariant loop computes trace = sigma[...,0,0] + sigma[...,1,1] + sigma[...,2,2] at every one of 20002000 time steps, each call allocating a new temporary array. Predict whether accumulating into one preallocated buffer with in-place addition changes the bytes read from σ\sigma each step. ((d)) Measurement: summing a contiguous array of 500×800=4×105500\times800 = 4\times10^{5} float64 values takes 0.5 ms0.5\,\mathrm{ms}; summing diag\mathrm{diag} takes 3.9 ms3.9\,\mathrm{ms}. Over the full 20002000-step loop the original code takes 25.3 s25.3\,\mathrm{s} and the preallocated in-place version takes 25.0 s25.0\,\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