Tensorium
Loading...
Searching...
No Matches
CacheInfo.hpp
Go to the documentation of this file.
1
2#pragma once
3#include <cstdint>
4#include <array>
5#include <iostream>
6
7namespace tensorium {
8
9 class CacheInfo {
10 public:
11 static int getL1CacheSize();
12 static int getL2CacheSize();
13 static int getL3CacheSize();
14 static int getCacheLineSize();
15 };
16
17#if defined(__x86_64__) || defined(_M_X64)
18
19#include <cpuid.h>
20
21 inline void cpuid(int info[4], int function_id, int subfunction_id = 0) {
23 }
24
25
26 inline int CacheInfo::getCacheLineSize() {
27 int info[4];
28 cpuid(info, 0x4, 0);
29 int line_size = (info[1] & 0xFFF) + 1;
30 return line_size;
31 }
32
33
34 inline int CacheInfo::getL1CacheSize() {
35 int info[4];
36 cpuid(info, 0x4, 1);
37 int ways = ((info[1] >> 22) & 0x3FF) + 1;
38 int partitions = ((info[1] >> 12) & 0x3FF) + 1;
39 int line_size = (info[1] & 0xFFF) + 1;
40 int sets = info[2] + 1;
41 return ways * partitions * line_size * sets;
42 }
43
44 inline int CacheInfo::getL2CacheSize() {
45 int info[4];
46 cpuid(info, 0x4, 2);
47 int ways = ((info[1] >> 22) & 0x3FF) + 1;
48 int partitions = ((info[1] >> 12) & 0x3FF) + 1;
49 int line_size = (info[1] & 0xFFF) + 1;
50 int sets = info[2] + 1;
51 return ways * partitions * line_size * sets;
52 }
53
54 inline int CacheInfo::getL3CacheSize() {
55 int info[4];
56 cpuid(info, 0x4, 3);
57 int ways = ((info[1] >> 22) & 0x3FF) + 1;
58 int partitions = ((info[1] >> 12) & 0x3FF) + 1;
59 int line_size = (info[1] & 0xFFF) + 1;
60 int sets = info[2] + 1;
61 return ways * partitions * line_size * sets;
62 }
63
64#else
65
66 inline int CacheInfo::getCacheLineSize() { return 64; }
67 inline int CacheInfo::getL1CacheSize() { return 32 * 1024; }
68 inline int CacheInfo::getL2CacheSize() { return 256 * 1024; }
69 inline int CacheInfo::getL3CacheSize() { return 8 * 1024 * 1024; }
70
71#endif
72
73}
Definition CacheInfo.hpp:9
static int getCacheLineSize()
Definition CacheInfo.hpp:66
static int getL3CacheSize()
Definition CacheInfo.hpp:69
static int getL1CacheSize()
Definition CacheInfo.hpp:67
static int getL2CacheSize()
Definition CacheInfo.hpp:68
Multi-dimensional tensor class with fixed rank and SIMD support.
Definition Tensor.hpp:25
Definition Derivate.hpp:24