aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTianhao Wang <wth@riseup.net>2023-11-02 01:14:25 +0100
committerTianhao Wang <wth@riseup.net>2023-11-02 01:14:25 +0100
commitf3ba79298a26169d6563c42bbd437b903623c599 (patch)
treee5b1e91b0a01ac7e99efd1f6424fa7c44af8b3d4
init
-rw-r--r--.gitignore1
-rw-r--r--Makefile25
-rw-r--r--readme.txt19
-rw-r--r--testall.sh28
-rw-r--r--testn.c42
5 files changed, 115 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..145f5d7
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+test_*
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..7a077b5
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,25 @@
+targets = test_0
+targets += test_1
+targets += test_2
+targets += test_3
+targets += test_4
+targets += test_5
+targets += test_6
+targets += test_7
+targets += test_8
+targets += test_9
+targets += test_10
+targets += test_11
+targets += test_12
+targets += test_13
+targets += test_14
+targets += test_15
+targets += test_16
+
+all: $(targets)
+
+test_%: testn.c
+ cc -static -DOFF=$* -O0 -o $@ $^
+
+clean:
+ rm -f test_*
diff --git a/readme.txt b/readme.txt
new file mode 100644
index 0000000..d9646d9
--- /dev/null
+++ b/readme.txt
@@ -0,0 +1,19 @@
+This is a toy program to play with cache line alignment.
+
+The program iterates over a 2D array and modify the elements, and record
+the time it takes for the traversal.
+Each array row has a tailing padding of OFF*4 bytes (i.e. OFF x sizeof(int)).
+The padding elements are NOT traversed, they are only there to make the array
+misaligned.
+
+The makefile builds 17x binaries with OFF = 0...16. The testall.sh script
+runs each of them 100 times and print the averaged output.
+
+Build:
+$make
+
+run:
+$sh testall.sh
+
+
+Play around: change the demensions of the array and the loop operations.
diff --git a/testall.sh b/testall.sh
new file mode 100644
index 0000000..922e1bd
--- /dev/null
+++ b/testall.sh
@@ -0,0 +1,28 @@
+runs=100
+
+# warm up the machine
+for i in $(seq 0 1 $runs)
+do
+ ./test_0 > /dev/null
+done
+
+sum=0
+for i in $(seq 0 1 $runs)
+do
+ sum=$((sum+$(./test_0)))
+done
+
+ref_avg=$((sum / runs))
+echo offset=0 time used: $ref_avg \( 1 \)
+
+for p in $(seq 1 1 16)
+do
+ sum=0
+ for i in $(seq 0 1 $runs)
+ do
+ sum=$((sum+$(./test_$p)))
+ done
+ avg=$((sum / runs))
+ ratio=$(echo $(( 100 * $avg / $ref_avg )) | sed -e 's/..$/.&/;t' -e 's/.$/.0&/')
+ echo offset=$p time used: $avg \( $ratio \)
+done
diff --git a/testn.c b/testn.c
new file mode 100644
index 0000000..84815aa
--- /dev/null
+++ b/testn.c
@@ -0,0 +1,42 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#define ROWS 1024
+#define COLS 64
+#define VFL 6291456
+#ifndef OFF
+#define OFF 0
+#endif
+
+char arr[ROWS*4][(COLS+OFF)*4] __attribute__((aligned(4096)));
+char arr2[VFL];
+
+int main() {
+ int i, j, k;
+ time_t t;
+ volatile int xor;
+ srand(0x42069);
+ // init array with random values
+ for (i = 0; i < ROWS; i++) {
+ for (j = 0; j < COLS; j++) {
+ arr[i][j] = rand()%255;
+ }
+ }
+
+ // "flush" 6MiB cache
+ // for(i = 0; i < VFL; i++)arr2[i] = i*i;
+ // for(i = 0; i < VFL; i++)arr2[i] += 1;
+
+ // mem barrier
+ __sync_synchronize();
+ t = clock();
+ for (i = 1; i < ROWS; i++) {
+ for (j = 0; j < COLS; j++) {
+ arr[i][j] *= arr[i-1][j];
+ xor ^= arr[i][j];
+ }
+ }
+ t = clock() - t;
+ printf("%ld",t);
+}
+