N
N
newuser88882020-09-23 20:20:26
C++ / C#
newuser8888, 2020-09-23 20:20:26

How to break the first level cache using the example code and am I thinking correctly about alignment, cache and word cpu?

The cache of the first level is 32 kilobytes (well, I have it), the cache line is 64 bytes, the cache lines of the first level are 512
. all data is 8 bytes (doubles) - alignment is not required.
The word cpu is a pointer? The x64 pointer is 8 bytes.
Alignment occurs for fast memory access by multiplying an 8byte pointer * by a number.
Here, I've created a 64 byte structure. 8 bytes * 8 - cache line size.
The CPU directs the pointer to the memory, a piece of memory is taken into the second-level cache line, then from the second-level cache to the first? Or from the first cache, if there are few cache lines and it is deleted, is it transferred to the second level cache?
How can I break the cache using this code example? To create a lot of L1 cache misses.
On the first pointer request, a 64-byte structure is placed in a 64-byte cache line, after that all subsequent requests go to the cache, and not to memory?
And how to create the perfect code based on this example? Am I thinking right?
I'm trying to figure this all out.

5f6b78e97b5c1498737926.png

#include <iostream>
#include <vector>
using namespace std;

struct Car {
  double one = 8;
  double two = 8;
  double three = 8;
  double four = 8;
  double five = 8;
  double six = 8;
  double seven = 8;
  double eight = 8;
};

int main() {
  vector<Car*> cars;
  for (int i = 0; i < 512; ++i)
    cars.push_back(new Car());

  for (int i = 0; i < 512; ++i) {
    cout << cars[i]->one;
    cout << cars[i]->two;
    cout << cars[i]->three;
    cout << cars[i]->four;
    cout << cars[i]->five;
    cout << cars[i]->six;
    cout << cars[i]->seven;
    cout << cars[i]->eight;
  }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
jcmvbkbc, 2020-09-23
@jcmvbkbc

An ageless classic with answers to many of your questions: https://akkadia.org/drepper/cpumemory.pdf

Or from the first cache, if there are few cache lines and it is deleted, is it transferred to the second level cache?

Depends on whether the cache is exclusive or inclusive.

A
Anton Zhilin, 2020-09-23
@Anton3

First, the output to cout outweighs any cache misses, so it should be replaced with, for example, a sum calculation.
Secondly, why is there a vector of pointers here? Especially raw pointers and new/delete?! (By the way, there is no delete...) Simply replace with a vector from Car.
Thirdly, cache misses are best demonstrated not by sequential, but by random access to memory. That is, in a loop for a million (or as many as you need) iterations, drag an element from a random place in the array and add it to the sum. By the way, think about using random faster, otherwise it will kill the whole idea in the same way as cout.
Well, yes, the main idea is to change the size of the array and measure the speed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question