假设我的数组是 32KB,L1 是 64KB。 Windows 在程序运行时会使用其中的一些吗?也许我无法使用 L1,因为 Windows 正在让其他程序工作?我应该设置程序的优先级以使用所有缓存吗?
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();
}
该程序是用 C/C++ 编写的。
L2 也一样。
函数也保存在缓存中吗?缓存是只读的? (如果我更改我的数组,那么它会丢失缓存绑定(bind)吗?)
编译器是否创建 asm 代码以使用缓存更多 yield ?
谢谢
最佳答案
How can i know my array is in cache?
一般来说,你不能。一般来说,缓存是由硬件直接管理的,而不是Windows。您也无法控制数据是否驻留在缓存中(尽管可以指定内存区域不应被缓存)。
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?
L1 和 L2 缓存由在给定内核上运行的所有进程共享。当您的进程运行时,它将使用所有缓存(如果需要)。当存在上下文切换时,部分或全部缓存将被逐出,具体取决于第二个进程的需要。因此,下次上下文切换回您的进程时,可能需要重新填充缓存。
但是,这一切都是由硬件自动完成的。
also functions are kept in cache?
在大多数现代处理器上,都有一个单独的指令缓存。参见例如this diagram它显示了英特尔 Nehalem 架构的安排;请注意共享的 L2 和 L3 缓存,但单独的 L1 缓存用于指令和数据。
cache is read only?(if i change my array then it loses the cache bond?)
没有。缓存可以处理修改后的数据,尽管这要复杂得多(因为 the problem of synchronising multiple caches in a multi-core system 。)
does the compiler create the asm codes to use cache more yield?
由于缓存事件通常都由硬件自动处理,因此不需要特殊指令。
关于c++ - 我怎么知道我的数组在缓存中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11492734/