cpu-cache

How to produce the cpu cache effect in C and java?

笑着哭i 提交于 2019-12-03 07:04:35
In Ulrich Drepper's paper What every programmer should know about memory , the 3rd part: CPU Caches, he shows a graph that shows the relationship between "working set" size and the cpu cycle consuming per operation (in this case, sequential reading). And there are two jumps in the graph which indicate the size of L1 cache and L2 cache. I wrote my own program to reproduce the effect in c. It just simply read a int[] array sequentially from head to tail, and I've tried different size of the array(from 1KB to 1MB). I plot the data into a graph and there is no jump, the graph is a straight line.

Why is linear read-shuffled write not faster than shuffled read-linear write?

烂漫一生 提交于 2019-12-03 06:11:55
问题 I'm currently trying to get a better understanding of memory/cache related performance issues. I read somewhere that memory locality is more important for reading than for writing, because in the former case the CPU has to actually wait for the data whereas in the latter case it can just ship them out and forget about them. With that in mind, I did the following quick-and-dirty test: I wrote a script that creates an array of N random floats and a permutation, i.e. an array containing the

Optimising Java objects for CPU cache line efficiency

本秂侑毒 提交于 2019-12-03 05:44:37
I'm writing a library where: It will need to run on a wide range of different platforms / Java implementations (the common case is likely to be OpenJDK or Oracle Java on Intel 64 bit machines with Windows or Linux) Achieving high performance is a priority , to the extent that I care about CPU cache line efficiency in object access In some areas, quite large graphs of small objects will be traversed / processed (let's say around 1GB scale) The main workload is almost exclusively reads Reads will be scattered across the object graph, but not totally randomly (i.e. there will be significant

What cache invalidation algorithms are used in actual CPU caches?

 ̄綄美尐妖づ 提交于 2019-12-03 05:32:32
I came to the topic caching and mapping and cache misses and how the cache blocks get replaced in what order when all blocks are already full. There is the least recently used algorithm or the fifo algorithm or the least frequently algorithm and random replacement, ... But what algorithms are used on actual cpu caches? Or can you use all and the... operating system decides what the best algorithm is? Edit: Even when i chose an answer, any further information is welcome ;) As hivert said - it's hard to get a clear picture on the specific algorithm, but one can deduce some of the information

Why does my 8M L3 cache not provide any benefit for arrays larger than 1M?

徘徊边缘 提交于 2019-12-03 04:50:42
问题 I was inspired by this question to write a simple program to test my machine's memory bandwidth in each cache level: Why vectorizing the loop does not have performance improvement My code uses memset to write to a buffer (or buffers) over and over and measures the speed. It also saves the address of every buffer to print at the end. Here's the listing: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/time.h> #define SIZE_KB {8, 16, 24, 28, 32, 36, 40, 48, 64, 128, 256,

Design code to fit in CPU Cache?

百般思念 提交于 2019-12-03 04:44:27
问题 When writing simulations my buddy says he likes to try to write the program small enough to fit into cache. Does this have any real meaning? I understand that cache is faster than RAM and the main memory. Is it possible to specify that you want the program to run from cache or at least load the variables into cache? We are writing simulations so any performance/optimization gain is a huge benefit. If you know of any good links explaining CPU caching, then point me in that direction. 回答1: At

clflush not flushing the instruction cache

一个人想着一个人 提交于 2019-12-03 03:56:06
Consider the following code segment: #include <stdio.h> #include <stdlib.h> #include <stdint.h> #define ARRAYSIZE(arr) (sizeof(arr)/sizeof(arr[0])) inline void clflush(volatile void *p) { asm volatile ("clflush (%0)" :: "r"(p)); } inline uint64_t rdtsc() { unsigned long a, d; asm volatile ("cpuid; rdtsc" : "=a" (a), "=d" (d) : : "ebx", "ecx"); return a | ((uint64_t)d << 32); } inline int func() { return 5;} inline void test() { uint64_t start, end; char c; start = rdtsc(); func(); end = rdtsc(); printf("%ld ticks\n", end - start); } void flushFuncCache() { // Assuming function to be not

Write a program to get CPU cache sizes and levels

血红的双手。 提交于 2019-12-03 03:00:40
I want to write a program to get my cache size(L1, L2, L3). I know the general idea of it. Allocate a big array Access part of it of different size each time. So I wrote a little program. Here's my code: #include <cstdio> #include <time.h> #include <sys/mman.h> const int KB = 1024; const int MB = 1024 * KB; const int data_size = 32 * MB; const int repeats = 64 * MB; const int steps = 8 * MB; const int times = 8; long long clock_time() { struct timespec tp; clock_gettime(CLOCK_REALTIME, &tp); return (long long)(tp.tv_nsec + (long long)tp.tv_sec * 1000000000ll); } int main() { // allocate memory

How does CLFLUSH work for an address that is not in cache yet?

烂漫一生 提交于 2019-12-03 02:12:26
We are trying to use the Intel CLFLUSH instruction to flush the cache content of a process in Linux at the userspace. We create a very simple C program that first access a large array and then call the CLFLUSH to flush the virtual address space of the whole array. We measure the latency it takes for CLFLUSH to flush the whole array. The size of the array in the program is an input and we vary the input from 1MB to 40MB with a step of 2MB. In our understanding, the CLFLUSH should flush the content in the cache . So we expect to see the latency of flushing the whole array first increase linearly

What is meant by data cache and instruction cache?

走远了吗. 提交于 2019-12-03 01:43:44
问题 From here: Instructions and data have different access patterns, and access different regions of memory. Thus, having the same cache for both instructions and data may not always work out. Thus, it's rather common to have two caches: an instruction cache that only stores instructions, and a data cache that only stores data. It's intuitive to know the distinction between instructions and data, but now I'm not show sure of the difference in this context? What constitutes as data and gets put