added cache_benchmark

This commit is contained in:
Nils K
2020-04-17 01:50:16 +02:00
parent 2581df87b7
commit e214510956
5 changed files with 278 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@@ -0,0 +1,3 @@
*.exe
*.swp
*.o

2
cache_benchmark/Makefile Normal file
View File

@@ -0,0 +1,2 @@
build: cache.c
gcc cache.c -O2 -o cache.exe

107
cache_benchmark/cache.c Normal file
View File

@@ -0,0 +1,107 @@
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
//prints out usage information
void usage();
//generate dynamically allocated array, initialized with rand()
int* rand_array(size_t n);
//converts clock ticks to nano seconds
double clocks_to_ns(double ct);
//write to /dev/null or nul
//exits if it fails
void int_to_dev_null(int trash);
//calculates the clock ticks rand() probably needs
//from a hot cache
double clock_ticks_rand();
int main(int argc, char* argv[]) {
if(argc != 4) {
usage();
return 1;
}
unsigned n_bytes;
unsigned array_size;
unsigned n_times_access;
unsigned n_loops;
sscanf(argv[1], "%u", &n_bytes);
sscanf(argv[2], "%u", &n_times_access);
sscanf(argv[3], "%u", &n_loops);
srand(time(NULL));
array_size = n_bytes / 4;
int* parry = rand_array(array_size);
clock_t start;
clock_t end;
clock_t overall = 0;
int tmp = 0;
double rc = clock_ticks_rand();
for(size_t n=0; n<n_loops; ++n) {
start = clock();
for(size_t t=0; t<n_times_access; ++t) {
//alternative way(without inner rand): tmp += parry[parry[t % array_size] % array_size];
tmp += parry[rand() % array_size];
}
end = clock();
overall += (end - start);
}
double per_instruct_ticks = overall / ((double) n_times_access * n_loops);
per_instruct_ticks = per_instruct_ticks - rc;
printf("%u, %u, %lf, %lf\n", n_bytes, array_size, clocks_to_ns(per_instruct_ticks), clocks_to_ns(rc));
int_to_dev_null(tmp);
}
double clocks_to_ns(double ct) {
return ct / CLOCKS_PER_SEC * 1000000000.0;
}
void int_to_dev_null(int trash) {
#ifdef _WIN32
const char* dev_null = "nul";
#else
const char* dev_null = "/dev/null";
#endif
FILE* fnull = fopen(dev_null, "w");
if(! fnull) {
perror("unable to open /dev/null or nul");
exit(2);
}
fprintf(fnull, "%d", trash);
fclose(fnull);
}
int* array(size_t n) {
int* p=NULL;
p = malloc(sizeof(int) * n);
if(!p) {
perror("malloc failed");
exit(2);
}
return p;
}
int* rand_array(size_t n) {
int* p = array(n);
for(size_t i=0; i<n; ++i) {
p[i] = rand();
}
return p;
}
void usage() {
printf("<prog> array-size n-times-access n-loops\n");
}
double clock_ticks_rand() {
#define N 1000000
for(size_t i=0; i<N; ++i) {
rand();
}
clock_t start = clock();
for(size_t i=0; i<N; ++i) {
rand();
}
double time_per_rand = (double)(clock() - start) / N;
return time_per_rand;
#undef N
}

View File

@@ -0,0 +1,86 @@
#!/bin/bash
#usage: <script> filename
# filename shouldn't be a path
# the file will be automatically added to the output folder
if [ $# -ne 1 ]
then
echo "missing output-filename"
exit 1
fi
exec >>./output/"$1"
n_times=10000000
n_count=5
echo "#n_times = $n_times"
echo "#n_count = $n_count"
echo "#n_steps = $n_steps"
step_size_bytes=100
last_value_bytes=1000
n_steps=$((last_value_bytes / step_size_bytes))
n_bytes=4
for i in `seq 0 $n_steps`
do
./cache.exe $n_bytes $n_times $n_count
n_bytes=$((n_bytes + step_size_bytes))
done
step_size_bytes=1000
last_value_bytes=10000
n_steps=$((last_value_bytes / step_size_bytes))
n_bytes=4
for i in `seq 0 $n_steps`
do
./cache.exe $n_bytes $n_times $n_count
n_bytes=$((n_bytes + step_size_bytes))
done
step_size_bytes=10000
last_value_bytes=100000
n_steps=$((last_value_bytes / step_size_bytes))
n_bytes=4
for i in `seq 0 $n_steps`
do
./cache.exe $n_bytes $n_times $n_count
n_bytes=$((n_bytes + step_size_bytes))
done
step_size_bytes=100000
last_value_bytes=1000000
n_steps=$((last_value_bytes / step_size_bytes))
n_bytes=4
for i in `seq 0 $n_steps`
do
./cache.exe $n_bytes $n_times $n_count
n_bytes=$((n_bytes + step_size_bytes))
done
step_size_bytes=1000000
last_value_bytes=10000000
n_steps=$((last_value_bytes / step_size_bytes))
n_bytes=4
for i in `seq 0 $n_steps`
do
./cache.exe $n_bytes $n_times $n_count
n_bytes=$((n_bytes + step_size_bytes))
done
step_size_bytes=10000000
last_value_bytes=100000000
n_steps=$((last_value_bytes / step_size_bytes))
n_bytes=4
for i in `seq 0 $n_steps`
do
./cache.exe $n_bytes $n_times $n_count
n_bytes=$((n_bytes + step_size_bytes))
done
step_size_bytes=100000000
last_value_bytes=1000000000
n_steps=$((last_value_bytes / step_size_bytes))
n_bytes=4
for i in `seq 0 $n_steps`
do
./cache.exe $n_bytes $n_times $n_count
n_bytes=$((n_bytes + step_size_bytes))
done

View File

@@ -0,0 +1,80 @@
#n_times = 10000000
#n_count = 5
#n_steps =
4, 1, 1.796940, 11.427000
104, 26, 2.900400, 10.326000
204, 51, 2.884980, 10.322000
304, 76, 2.013720, 11.318000
404, 101, 2.281860, 11.109000
504, 126, 3.361040, 10.419000
604, 151, 2.853020, 10.410000
704, 176, 2.825380, 10.880000
804, 201, 2.450820, 10.867000
904, 226, 2.844100, 10.433000
1004, 251, 2.813580, 10.348000
4, 1, 2.845820, 10.327000
1004, 251, 2.675060, 10.475000
2004, 501, 2.823520, 10.341000
3004, 751, 2.845340, 10.321000
4004, 1001, 2.540120, 10.626000
5004, 1251, 2.811960, 10.348000
6004, 1501, 2.845200, 10.326000
7004, 1751, 2.842140, 10.322000
8004, 2001, 2.852820, 10.322000
9004, 2251, 2.822840, 10.398000
10004, 2501, 2.827560, 10.334000
4, 1, 2.666520, 10.492000
10004, 2501, 2.814660, 10.359000
20004, 5001, 2.840020, 10.326000
30004, 7501, 2.450100, 10.714000
40004, 10001, 2.802600, 10.356000
50004, 12501, 2.802320, 10.362000
60004, 15001, 2.824040, 10.339000
70004, 17501, 2.834120, 10.321000
80004, 20001, 2.823640, 10.339000
90004, 22501, 2.840880, 10.322000
100004, 25001, 2.842940, 10.326000
4, 1, 2.865800, 10.329000
100004, 25001, 2.797480, 10.359000
200004, 50001, 2.825240, 10.342000
300004, 75001, 2.817100, 10.347000
400004, 100001, 2.825280, 10.331000
500004, 125001, 2.900640, 10.350000
600004, 150001, 2.827060, 10.345000
700004, 175001, 2.833460, 10.343000
800004, 200001, 2.812040, 10.346000
900004, 225001, 2.562740, 10.644000
1000004, 250001, 2.820360, 10.343000
4, 1, 2.829680, 10.334000
1000004, 250001, 2.826580, 10.347000
2000004, 500001, 6.918360, 10.342000
3000004, 750001, 12.378520, 10.587000
4000004, 1000001, 16.424660, 10.293000
5000004, 1250001, 21.886400, 10.344000
6000004, 1500001, 22.119560, 10.812000
7000004, 1750001, 26.157120, 10.286000
8000004, 2000001, 25.323320, 10.414000
9000004, 2250001, 27.974520, 10.294000
10000004, 2500001, 26.809840, 10.320000
4, 1, 2.501100, 10.660000
10000004, 2500001, 26.890300, 10.295000
20000004, 5000001, 31.671960, 10.288000
30000004, 7500001, 31.187380, 10.286000
40000004, 10000001, 33.487660, 10.413000
50000004, 12500001, 31.705220, 10.305000
60000004, 15000001, 34.792340, 10.288000
70000004, 17500001, 34.498320, 10.760000
80000004, 20000001, 32.972400, 10.309000
90000004, 22500001, 36.819780, 10.300000
100000004, 25000001, 33.829780, 10.413000
4, 1, 2.820420, 10.378000
100000004, 25000001, 36.639460, 10.292000
200000004, 50000001, 37.877760, 10.387000
300000004, 75000001, 41.470580, 10.338000
400000004, 100000001, 42.162180, 10.371000
500000004, 125000001, 50.617280, 10.766000
600000004, 150000001, 48.964480, 10.322000
700000004, 175000001, 55.075340, 10.292000
800000004, 200000001, 53.195160, 10.285000
900000004, 225000001, 54.293140, 10.611000
1000000004, 250000001, 59.733980, 10.345000