huseyin tugrul buyukisik
huseyin tugrul buyukisik

Reputation: 11920

How can i know my array is in cache?

Lets say my array is 32KB, L1 is 64 KB. Does Windows use some of it while program is running? Maybe I am not able to use L1 because windows is making other programs work? Should I set priority of my program to use all cache?

for(int i=0;i<8192;i++)
{
  array_3[i]+=clock()*(rand()%256);//clock() and rand in cache too?
  //how many times do I need to use a variable to make it stay in cache?
  //or cache is only for reading? look below plz
  temp_a+=array_x[i]*my_function();
}

The program is in C/C++.

Same thing for L2 too please.

Also are functions kept in cache? Cache is read only? (If I change my array then it loses the cache bond?)

Does the compiler create the asm codes to use cache more yield?

Thanks

Upvotes: 3

Views: 2437

Answers (6)

user730816
user730816

Reputation:

Even though you may not know which data is in cache and which not, you still may get an idea how much of the cache you are utilizing. Modern processor have quite many performance counters and some of them related to cache. Intel's processors may tell you how many L1 and L2 misses there were. Check this for more details of how to do it: How to read performance counters on i5, i7 CPUs

Upvotes: 1

Digital Da
Digital Da

Reputation: 911

As far as I know, you can't control what will be in the cache. You can declare a variable as register var_type a and then access to it will be in a single cycle(or a small number of cycles). Moreover, the amount of cycles it will take you to access a chunk of memory also depends on virtual memory translation and TLB. It should be noted that the register keyword is merely a suggestion and the compiler is perfectly free to ignore it, as was suggested by the comment.

Upvotes: 1

Eric Postpischil
Eric Postpischil

Reputation: 222866

As others have stated, you generally cannot control what is in cache. If you are writing code for high-performance and need to rely on cache for performance, then it is not uncommon to write your code so that you are using about half the space of L1 cache. Methods for doing so involve a great deal of discussion beyond the scope of StackOverflow questions. Essentially, you would want to do as much work as possible on some data before moving on to other data.

As a matter of what works practically, using about half of cache leaves enough space for other things to occur that most of your data will remain in cache. You cannot rely on this without cooperation from the operating system and other aspects of the computing platform, so it may be a useful technique for speeding up research calculations but it cannot be used where real-time performance must be guaranteed, as in operating dangerous machinery.

There are additional caveats besides how much data you use. Using data that maps to the same cache lines can evict data from cache even though there is plenty of cache unused. Matrix transposes are notorious for this, because a matrix whose row length is a multiple of a moderate power of two will have columns in which elements map to a small set of cache lines. So learning to use cache efficiently is a significant job.

Upvotes: 1

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272517

How can i know my array is in cache?

In general, you can't. Generally speaking, the cache is managed directly by hardware, not by Windows. You also can't control whether data resides in the cache (although it is possible to specify that an area of memory shouldn't be cached).

Does windows use some of it while program is running? Maybe i am not able to use L1 because windows is making other programs work? Should i set priority of my program to use all cache?

The L1 and L2 caches are shared by all processes running on a given core. When your process is running, it will use all of cache (if it needs it). When there's a context switch, some or all of the cache will be evicted, depending on what the second process needs. So next time there's a context switch back to your process, the cache may have to be refilled all over again.

But again, this is all done automatically by the hardware.

also functions are kept in cache?

On most modern processors, there is a separate cache for instructions. See e.g. this diagram which shows the arrangement for the Intel Nehalem architecture; note the shared L2 and L3 caches, but the separate L1 caches for instructions and data.

cache is read only?(if i change my array then it loses the cache bond?)

No. Caches can handle modified data, although this is considerably more complex (because of the problem of synchronising multiple caches in a multi-core system.)

does the compiler create the asm codes to use cache more yield?

As cache activity is generally all handled automatically by the hardware, no special instructions are needed.

Upvotes: 14

Kirill Kobelev
Kirill Kobelev

Reputation: 10557

The cache is primarily controlled by the hardware. However, I know that Windows scheduler tends to schedule execution of a thread to the same core as before specifically because of the caches. It understands that it will be necessary to reload them on another core. Windows is using this behavior at least since Windows 2000.

Upvotes: 1

perreal
perreal

Reputation: 97948

  • Cache is not directly controlled by the operating system, it is done in hardware

  • In case of a context switch, another application may modify the cache, but you should not care about this. It is more important to handle cases when your program behaves cache unfriendly.

  • Functions are kept in cache (I-Cahce , instruction cache)

  • Cache is not read only, when you write something it goes to [memory and] the cache.

Upvotes: 1

Related Questions