Previous | Next --- Slide 15 of 75
Back to Lecture Thumbnails

I learnt that this would be a cache with cache line size = sizeof(int) and we would not actually pad, as that would waste space. Hopefully I learnt this correctly.


Correct. If the cache line size was sizeof(int), when each per thread counter would be on it's one cache line, and we would not have a false sharing problem.


How do we know the struct would align with the start of a cache line? For example, if the struct is of CACHE_LINE_SIZE, would it be possible to span from the mid point of one cache to the mid point of the next cache line?


As a programmer, are there any automated ways to detect this inefficiency from unpadded struct fields (either through compiler or runtime optimization)? It seems like this is too much of a low-level details for programmer to recognize and prevent.


@laimagineCS149 Nothing guarantees that alignment in general, but there are compiler-specific annotations for that, which you might have encountered (e.g., __attribute__((align(64))) or whatever). I think heap allocations also tend to be aligned in ways that are system-dependent but nonetheless well-documented, and you can always inspect the pointer you get to enforce your own alignment!

That said, even if the struct isn't aligned with cache lines, the code above still guarantees no two integer counters in the array will ever share the same cache line. So it should be okay for our purposes here.


@sirah Checkout

Please log in to leave a comment.