Clamped View

My experiences is that boundary checks inside inner loops are always going to be slow, and are inflexible as well. What if you want to change to periodic boundary conditions, for example? Better to use padding/ghost elements, which allows you to completely decouple the boundary conditions from the inner loops.

See also Arrays with periodic boundaries - #4 by stevengj or Seemingly unnecessary allocation within for loop - #8 by stevengj

The shift in indices for views is not conditional — it’s just a constant offset. Indeed, the compiler can probably pull the offset completely out of an inner loop by simply offsetting the data pointer.

2 Likes