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 σ of shape (500,800,3,3) in C order, so σ[p,q] is the 3×3 stress tensor at grid cell (p,q).
((a)) Is diag=σ[…,0,0] (the xx-component field) a view or a copy? Give its shape and byte strides.
((b)) Using the 64B cache line, 8B-element model, predict the ratio of the memory traffic of diag.sum() to that of summing a contiguous array of the same 500×800 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 2000 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 σ each step.
((d)) Measurement: summing a contiguous array of 500×800=4×105 float64 values takes 0.5ms; summing diag takes 3.9ms. Over the full 2000-step loop the original code takes 25.3s and the preallocated in-place version takes 25.0s. 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).