Is there a way to force a variable to be stored in the cache in C?

前端 未结 5 820
误落风尘
误落风尘 2021-02-05 23:55

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?

相关标签:
5条回答
  • 2021-02-06 00:25

    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.

    0 讨论(0)
  • 2021-02-06 00:36

    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.

    0 讨论(0)
  • 2021-02-06 00:40

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

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

    0 讨论(0)
  • 2021-02-06 00:44

    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.

    0 讨论(0)
  • 2021-02-06 00:45

    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.

    0 讨论(0)
提交回复
热议问题