tags:

views:

168

answers:

5

Hi, I just had a phone interview where I was asked this question. I am aware of ways to store in register or heap or stack, but cache specifically?

+1  A: 

In C, as in as defined by the C standard? No.

In C, as in some specific implementation on a specific platform? Maybe.

Matti Virkkunen
which platform could that be? What I know is, that the CPU is managing the L1/l2/.. cache and the only thing you can do is writing smart code that the chance is higher that a variable and/or code is stored in L1 cache. But I'm not a kernel developer, just asking and curios
Arthur
@Arthur: I'm not exactly an experienced kernel developer either, hence the "Maybe". At most I've read about some things (such as [coreboot](http://en.wikipedia.org/wiki/Coreboot)) using the CPU cache as RAM when the actual RAM controller hasn't been initialized yet.
Matti Virkkunen
@Matti: as far as I understand it, Cache-as-RAM is a CPU mode where CPU cache memory is used instead of DRAM. But it's not related to C, it needs hardware support. There is also compilers like romcc that uses only registers (or cache as RAM) and never DRAM.
kriss
Err, no, at the C level, the most you can expect is to force a variable to be stored at a TCM (which is not a cache) using gcc's section attributes. Accessing directly the system coprocessor (using provileged assembler instructions) on some CPUs you may be able to lockdown part of the cache.
ninjalj
@kriss, @ninjalj: *sigh* hence the "Maybe!" It means it's possibly doable in a program written in C. That's all.
Matti Virkkunen
@Matti: you would usually need some asm extension or target specific pragma to achieve it. Can you really still say it's C ? From my point of view I would say no because it's very unlikely your program will still be portable. But however in projects like writing BIOS you usually don't care much of portability...
kriss
+7  A: 

Not in C as a language. In GCC as a compiler - look for __builtin_prefetch.

You might be interested in reading What every programmer should know about memory.

Edit:

Just to clear some confusion - caches are physically separate memories in hardware, but not in software abstraction of the machine. A word in a cache is always associated with address in main memory. This is different from the CPU registers, which are named/addressed separately from the RAM.

Nikolai N Fetissov
great article! can't stop reading.
Arthur
@nikolai: maybe the __builtin_prefetch was what he meant, but I guess i'll only know if I get called for an onsite interview, maybe i'll ask him then! Great article though!
Shivram
.. What a great article .. Really can't stop reading. Thank Nikolai !
Poni
+1  A: 

As cache is a CPU concept and is meaningless for C language (and C language has targets processors that have no cache, unlikely today, but quite common in old days) definitely No.

Trying to optimize such things by hand is also usually a quite bad idea.

What you can do is keep the job easy for the compiler keeping loops very short and doing only one thing (good for instruction cache), iterate on memory blocks in the right order (prefer accesses to consecutive cells in memory to sparse accesses), avoid reusing the same variables for different uses (it introduces read-after-write dependencies), etc. If you are attentive to such details the program is more likely to be efficiently optimized by compiler and memory accesses be cached.

But it will still depend on actual hardware and even compiler may not guarantee it.

kriss
A: 

If you are trying to force something to be stored in the CPU cache, I would recommend that you avoid trying to do so unless you have an overwhelmingly good reason. Manually manipulating the CPU cache can have all sorts of unintended consequences, not the least among them being coherency in multi-core or multi-CPU applications. This is something that is done by the CPU at run-time and is generally transparent to the programmer and the compiler for a good reason.

The specific answer will depend on your compiler and platform. If you are targeting a MIPS architecture, there is a CACHE instruction (assembly) which allows you to do CPU cache manipulations.

bta
+1  A: 

It depends on the platform, so if you were speaking to a company targetting current generation consoles, you would need to know the PowerPC data cache intrinsics/instructions. On various platforms, you would also need to know the false sharing rules. Also, you can't cache from memory marked explicitly as uncached.

Without more context about the actual job or company or question, this would probably be best answered by talking about what not to do to keep memory references in the data cache.

MSN