Jump to content

cache line problem

- - - - -

  • Please log in to reply
5 replies to this topic

#1
P98

P98

    Newbie

  • Members
  • PipPip
  • 14 posts
Array is

A[10][10]

And assume that the cache line is 10 int's long and also the memory is in row major format. If i try to access A[2][2] then will the whole row A[2] be cached in cache memory ?

#2
AKMafia001

AKMafia001

    Programmer

  • Members
  • PipPipPipPip
  • 119 posts
Nop! It will be only a single cell. i.e. The cell on the intersection of row 2 and column 2.
I think i'm able to write a code for printing "Hello, World!". Proud of that!

#3
mebob

mebob

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 490 posts
@AKMafia I don't think that was what he was asking.

@P98 Is this a homework question?
Latinamne loqueris?

#4
P98

P98

    Newbie

  • Members
  • PipPip
  • 14 posts
This is not a homework question. I was just curious to know what happens. Well why isnt the entire row written into the cache ?

#5
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
Array accessors are a convenience. A[2][2] is really something more like *(*(A + 2) + 2) for the sake of memory.

P98 said:

Well why isnt the entire row written into the cache ?

What is "cache"? A[2] is likely in the memory stack if local or memory heap if allocated with something such as malloc, if you call that cache (it is a little ambiguous)
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#6
mebob

mebob

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 490 posts
By cache do you mean the CPU cache (i.e. L1, L2, L3 caches)?

If you do, that question is very hard to answer. How the caching is done isn't really something they document (publicly at least). It would totally depend on what CPU you are using. If you really want to find an answer, you may want to contact your CPU manufacturer (probably Intel or AMD) and ask them about your particular CPU model.

Aside from that, the cache would probably not cache the entire row, as the CPU has no way of knowing that what you are accessing is a pointer. In fact, a pointer residing in the memory looks exactly the same as, say, an int (on a 32 bit system at least).
Latinamne loqueris?




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users