Previous | Next --- Slide 27 of 63
Back to Lecture Thumbnails
kaiang

In situations like our saxpy question where we allocate memory to the heap, do the cost of reading and writing differ? Or maybe my questions is more related to how caches work...

The scenarios I'm imagining are that (a) we want to use the value of X[i] multiple times, in which case we can probably load it once, cache it / store it in a register, and quickly retrieve it the second time, versus (b) we want to write the value of result[i] multiple times -- is caching a possibility here? Relatedly, how do we know that no other thread has messed with X[i], allowing us to exploit the cache?

viklassic

I'm not 100% sure, but I believe in scenario (a), we can indeed load X[i] once and quickly retrieve it as long as the subsequent accesses are recent enough and there haven't been so many other loads in between that the X[i] cache entry has been overwritten. I think caching is also useful in (b) in that we would cache result[i]'s value in the cache and keep writing to the cache instead of result[i]'s memory location until the OS does a cycle through its cache, checking which cache entries are "dirty" in that they've been written to, and writes those cache entries back to memory. So, instead of 6 writes to result[i], maybe we'd do six writes to cache and then one final write to result[i]. For the last question, I think it depends on the way the OS does caching but maybe it maps X[i]'s address to its value and you know X[i] is no longer in the cache if X[i]'s address isn't there anymore.

Please log in to leave a comment.