1
$\begingroup$

Just a basic question to ask

Does the write through cache copies the whole block or just the byte which is updated?

I went through the following question

Array A contains 256 elements of 4 bytes each. Its first element is stored at physical address 4096. Array B contains 512 elements of 4 bytes each. Its first element is stored at physical address 8192. Assume that only arrays A and B can be cached in an initially empty, physically addressed, physically tagged, direct mapped, 2K-byte cache with an 8 byte block size. The following loop is the executed

for(i=0; i<256; i++)
A[i] = A[i] + B[2*i];

How many bytes will be written to memory if the cache has a write-through policy?

I calculated it as follows:

The cache can store the whole array with 2 elements per block (block size = 8bytes, element size = 4bytes). For every write, the whole block will be copied. For $0^{th}$ element, the block containing $0^{th}$ and $1^{st}$ element would be written. The same would be done for $1^{st}$ element as well.

So, for every iteration the 2 elements would be written. This makes the number of bytes as $256*2* (4bytes / element) = 2048bytes$.

But in the solution, they have just calculated the number of loop iterations ($256$) multiplied by the element size ($4byte$) which makes the answer $1024bytes$. If this is true, then the cache would update only the updated byte (not the whole block).

Which is correct?

$\endgroup$
  • 1
    $\begingroup$ This likely depends on the cache implementation, less so on a scientific principle. Therefore, I think the question is offtopic here. $\endgroup$ – Raphael Jan 28 '13 at 10:19
  • $\begingroup$ I think data transfer always occur in blocks. Isn't it? $\endgroup$ – Shashwat Jan 28 '13 at 10:25
1
$\begingroup$

The caching strategies could be very "domain specific" like efficient data caching for numerical data structure, graph data structures and algorithms, bio-informatics data structure, etc.. This is a nice Ph.D thesis about it: "Cache-efficient Algorithms and Data Structures: Theory and Experimental Evaluation"

For your specific program I think it will cache whole block of memory not only the specific data in array. It uses a general cache strategy. See Cache algorithms on Wikipedia.

$\endgroup$
2
$\begingroup$

I would say that write-through cache always knows which addressable units need to be written, so there is absolutely no reason why it should write the rest of the block. Therefore it does not.

I am intentionally saying "addressable units", because (though I am not sure) it would generally be aligned units of the same width as the bus, i.e. usually 4-byte. But that has no effect here when the elements are 4-byte.

In contrast write-back cache is likely to only keep per-block modified status. However a write-back cache would be delaying the writes and since the consecutive passes through the loop end up overwriting whole blocks, it would still write just 1024 bytes.

$\endgroup$
  • $\begingroup$ You are advocating complicated logic to (a) find out exactly what part of the cache line has changed, (b) address individual bytes in memory, and (c) transfer parts of cache lines to/from memory. The whole point of transferring whole cache lines is to get rid of all this extra complexity. Cache should be fast, complex logic is just out of the question. $\endgroup$ – vonbrand May 6 '14 at 12:41
  • $\begingroup$ @vonbrand: For write through cache it is not complicated, because the command is just passed through as is. And for write back cache I didn't say anything like that. $\endgroup$ – Jan Hudec May 6 '14 at 14:08
  • $\begingroup$ @vonbrand: Of course most caches are write back in practice. But the question did say "write through", so I described write through caches first. $\endgroup$ – Jan Hudec May 6 '14 at 14:21
  • $\begingroup$ @vonbrandt: The thing that you complain about is exactly what a write through cache is supposed to do. If my code stores a single byte, the write through cache is supposed to write just that byte straight to RAM. $\endgroup$ – gnasher729 Nov 16 '17 at 19:22
0
$\begingroup$

The answer really depends on the implementation.

A cache and memory subsystem can be designed to support writes on a byte granularity, but doesn't have to be.

For example, if the memory interface is 8B wide and can support only 1 write at a time (and the cache line size is 8B), there's no point in only writing 1 byte in a write-through scenario (I guess there's slight power savings, but it'd probably be overshadowed by the complexity of tracking byte-enables).

Most caches deal with entire blocks of data (whatever the cache line size is). But then, most caches are not write-through. However, they often support byte-selected writes for non-cacheable/non-temporal data as that data isn't cached.

$\endgroup$
-1
$\begingroup$

A cache works by transferring a block at a time from/to RAM, so it copies a full block.

$\endgroup$
  • $\begingroup$ Not when it is a write through cache. $\endgroup$ – gnasher729 Nov 16 '17 at 19:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.