Skip to content

Commit fe2041c

Browse files
Added more precise benchmark method
1 parent 7f25323 commit fe2041c

17 files changed

+180
-142
lines changed

src/data/Matrix_A.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
2
2-
[ 13465026.000000 51628529.000000 ]
3-
[ 9778574.000000 61218860.000000 ]
2+
[ 3464256.000000 41961928.000000 ]
3+
[ 14050932.000000 45158647.000000 ]

src/data/Matrix_B.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2
2+
[ 47333287.000000 10245261.000000 ]
3+
[ 8857026.000000 48800053.000000 ]

src/data/Matrix_C.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2
2+
[ 65679586.000000 30980945.000000 ]
3+
[ 47825320.000000 16604839.000000 ]

src/data/benchmark_modulo_SIMD1.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
256 0.183045
2-
512 1.668042
3-
1024 14.721634
1+
256 0.308110
2+
512 2.604486
3+
1024 24.592209

src/data/benchmark_modulo_SIMD2.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
256 0.419965
2-
512 3.494216
3-
1024 43.159186
1+
256 0.297660
2+
512 2.419746
3+
1024 23.314639

src/data/benchmark_modulo_SIMD3.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
256 0.465952
2-
512 3.660137
3-
1024 46.676813
1+
256 0.362122
2+
512 2.807826
3+
1024 26.424386

src/data/benchmark_modulo_naive.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
256 0.143224
2-
512 1.673726
3-
1024 11.690024
1+
256 0.246962
2+
512 2.012571
3+
1024 20.047736

src/data/benchmark_order_ijk.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
256 0.120296
2+
512 1.062440
3+
1024 10.452757

src/data/benchmark_order_ikj.txt

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,3 @@
1-
256 0.067483
2-
512 0.544910
3-
1024 4.401343
4-
256 0.070410
5-
512 0.540539
6-
1024 4.440159
7-
256 0.067825
8-
512 0.613097
9-
1024 4.583750
10-
256 0.069220
11-
512 0.557678
12-
256 0.068348
13-
512 0.549756
14-
1024 4.589121
1+
256 0.111407
2+
512 0.900301
3+
1024 7.632323

src/data/benchmark_order_jik.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
256 0.080670
2-
512 0.631910
3-
1024 5.759593
1+
256 0.116215
2+
512 0.927812
3+
1024 10.074881

src/data/benchmark_order_jki.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
256 0.078748
2-
512 0.709504
3-
1024 18.785399
1+
256 0.129568
2+
512 1.156672
3+
1024 29.783366

src/data/benchmark_order_kij.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
256 0.073515
2-
512 0.571375
3-
1024 4.816130
1+
256 0.111931
2+
512 0.919294
3+
1024 7.818451

src/data/benchmark_order_kji.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
256 0.082192
2-
512 0.650841
3-
1024 18.953033
1+
256 0.129981
2+
512 1.088789
3+
1024 29.408653

src/main.c

Lines changed: 86 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -2,185 +2,176 @@
22
#include "matrix_mul.h"
33
#include <math.h>
44

5-
void benchmark_ijk(double** A, double** B, int n){
6-
FILE* f = fopen("data/benchmark_order_ijk.txt", "a");
5+
double benchmark_ijk(double** A, double** B, int n){
76
double** C = zero_matrix(n);
87

98
clock_t initial = clock();
109
mp_ijk(A, B, C, n);
1110
clock_t final = clock();
1211

13-
double time = ((double) (final - initial)) / CLOCKS_PER_SEC;
14-
printf("IJK: n = %d time = %f \n", n, time);
15-
fprintf(f, "%d %f \n", n, time);
16-
17-
fclose(f);
12+
return ((double) (final - initial)) / CLOCKS_PER_SEC;
1813
}
1914

20-
void benchmark_kij(double** A, double** B, int n){
21-
FILE* f = fopen("data/benchmark_order_kij.txt", "a");
15+
double benchmark_kij(double** A, double** B, int n){
2216
double** C = zero_matrix(n);
2317

2418
clock_t initial = clock();
2519
mp_kij(A, B, C, n);
2620
clock_t final = clock();
2721

28-
double time = ((double) (final - initial)) / CLOCKS_PER_SEC;
29-
printf("KIJ: n = %d time = %f \n", n, time);
30-
fprintf(f, "%d %f \n", n, time);
31-
32-
fclose(f);
22+
return ((double) (final - initial)) / CLOCKS_PER_SEC;
3323
}
3424

35-
void benchmark_jki(double** A, double** B, int n){
36-
FILE* f = fopen("data/benchmark_order_jki.txt", "a");
25+
double benchmark_jki(double** A, double** B, int n){
3726
double** C = zero_matrix(n);
3827

3928
clock_t initial = clock();
4029
mp_jki(A, B, C, n);
4130
clock_t final = clock();
4231

43-
double time = ((double) (final - initial)) / CLOCKS_PER_SEC;
44-
printf("JKI: n = %d time = %f \n", n, time);
45-
fprintf(f, "%d %f \n", n, time);
46-
47-
fclose(f);
32+
return ((double) (final - initial)) / CLOCKS_PER_SEC;
4833
}
4934

50-
void benchmark_ikj(double** A, double** B, int n){
51-
FILE* f = fopen("data/benchmark_order_ikj.txt", "a");
35+
double benchmark_ikj(double** A, double** B, int n){
5236
double** C = zero_matrix(n);
5337

5438
clock_t initial = clock();
5539
mp_ikj(A, B, C, n);
5640
clock_t final = clock();
5741

58-
double time = ((double) (final - initial)) / CLOCKS_PER_SEC;
59-
printf("IKJ: n = %d time = %f \n", n, time);
60-
fprintf(f, "%d %f \n", n, time);
61-
62-
fclose(f);
42+
return ((double) (final - initial)) / CLOCKS_PER_SEC;
6343
}
6444

65-
void benchmark_jik(double** A, double** B, int n){
66-
FILE* f = fopen("data/benchmark_order_jik.txt", "a");
45+
double benchmark_jik(double** A, double** B, int n){
6746
double** C = zero_matrix(n);
6847

6948
clock_t initial = clock();
7049
mp_jik(A, B, C, n);
7150
clock_t final = clock();
7251

73-
double time = ((double) (final - initial)) / CLOCKS_PER_SEC;
74-
printf("JIK: n = %d time = %f \n", n, time);
75-
fprintf(f, "%d %f \n", n, time);
76-
77-
fclose(f);
52+
return ((double) (final - initial)) / CLOCKS_PER_SEC;
7853
}
7954

80-
void benchmark_kji(double** A, double** B, int n){
81-
FILE* f = fopen("data/benchmark_order_kji.txt", "a");
55+
double benchmark_kji(double** A, double** B, int n){
8256
double** C = zero_matrix(n);
8357

8458
clock_t initial = clock();
8559
mp_kji(A, B, C, n);
8660
clock_t final = clock();
8761

88-
double time = ((double) (final - initial)) / CLOCKS_PER_SEC;
89-
printf("KJI: n = %d time = %f \n\n", n, time);
90-
fprintf(f, "%d %f \n", n, time);
91-
92-
fclose(f);
62+
return ((double) (final - initial)) / CLOCKS_PER_SEC;
9363
}
9464

95-
void benchmark_loops_order(double p){
96-
97-
for (int i=8; i<11; i++){
98-
int n = (int) pow(2, i);
99-
double**A = random_matrix(n, p);
100-
double**B = random_matrix(n, p);
101-
benchmark_ijk(A, B, n);
102-
benchmark_kij(A, B, n);
103-
benchmark_jki(A, B, n);
104-
benchmark_ikj(A, B, n);
105-
benchmark_jik(A, B, n);
106-
benchmark_kji(A, B, n);
107-
}
65+
void write_benchmark_time(char* filename, char* text, int n, double time){
66+
FILE* f = fopen(filename, "a");
67+
printf("%s: n = %d time = %f \n", text,n, time);
68+
fprintf(f, "%d %f \n", n, time);
69+
fclose(f);
10870
}
10971

110-
void benchmark_mod_naive(double** A, double** B, int n, double p){
111-
FILE* f = fopen("data/benchmark_modulo_naive.txt", "a");
72+
double benchmark_mod_naive(double** A, double** B, int n, double p){
11273
double** C = zero_matrix(n);
11374

11475
clock_t initial = clock();
11576
mp_naive(A, B, C, n, p);
11677
clock_t final = clock();
11778

118-
double time = ((double) (final - initial)) / CLOCKS_PER_SEC;
119-
printf("Modulo Naive: n = %d time = %f \n\n", n, time);
120-
fprintf(f, "%d %f \n", n, time);
121-
122-
fclose(f);
79+
return ((double) (final - initial)) / CLOCKS_PER_SEC;
12380
}
12481

125-
void benchmark_mod_SIMD1(double** A, double** B, int n, double p, double u){
126-
FILE* f = fopen("data/benchmark_modulo_SIMD1.txt", "a");
82+
double benchmark_mod_SIMD1(double** A, double** B, int n, double p, double u){
12783
double** C = zero_matrix(n);
12884

12985
clock_t initial = clock();
13086
mp_SIMD1(A, B, C, n, p, u);
13187
clock_t final = clock();
13288

133-
double time = ((double) (final - initial)) / CLOCKS_PER_SEC;
134-
printf("Modulo SIMD1: n = %d time = %f \n\n", n, time);
135-
fprintf(f, "%d %f \n", n, time);
136-
137-
fclose(f);
89+
return ((double) (final - initial)) / CLOCKS_PER_SEC;
13890
}
13991

140-
void benchmark_mod_SIMD2(double** A, double** B, int n, double p, double u){
141-
FILE* f = fopen("data/benchmark_modulo_SIMD2.txt", "a");
92+
double benchmark_mod_SIMD2(double** A, double** B, int n, double p, double u){
14293
double** C = zero_matrix(n);
14394

14495
clock_t initial = clock();
14596
mp_SIMD2(A, B, C, n, p, u);
14697
clock_t final = clock();
14798

148-
double time = ((double) (final - initial)) / CLOCKS_PER_SEC;
149-
printf("Modulo SIMD2: n = %d time = %f \n\n", n, time);
150-
fprintf(f, "%d %f \n", n, time);
151-
152-
fclose(f);
99+
return ((double) (final - initial)) / CLOCKS_PER_SEC;
153100
}
154101

155-
void benchmark_mod_SIMD3(double** A, double** B, int n, double p, double u){
156-
FILE* f = fopen("data/benchmark_modulo_SIMD3.txt", "a");
102+
double benchmark_mod_SIMD3(double** A, double** B, int n, double p, double u){
157103
double** C = zero_matrix(n);
158104

159105
clock_t initial = clock();
160106
mp_SIMD3(A, B, C, n, p, u);
161107
clock_t final = clock();
162108

163-
double time = ((double) (final - initial)) / CLOCKS_PER_SEC;
164-
printf("Modulo SIMD3: n = %d time = %f \n\n", n, time);
165-
fprintf(f, "%d %f \n", n, time);
109+
return ((double) (final - initial)) / CLOCKS_PER_SEC;
110+
}
166111

167-
fclose(f);
112+
void benchmark_loops_order(double p){
113+
/* Benchmarking the order of loops.
114+
The most efficient one is IKJ.
115+
*/
116+
int m = 5; // Executes m times each algo
117+
for (int i=8; i<11; i++){
118+
int n = (int) pow(2, i);
119+
double sum_ijk = 0;
120+
double sum_kij = 0;
121+
double sum_jki = 0;
122+
double sum_ikj = 0;
123+
double sum_jik = 0;
124+
double sum_kji = 0;
125+
126+
for (int j=0; j<m; j++){
127+
double**A = random_matrix(n, p);
128+
double**B = random_matrix(n, p);
129+
sum_ijk += benchmark_ijk(A, B, n);
130+
sum_kij += benchmark_kij(A, B, n);
131+
sum_jki += benchmark_jki(A, B, n);
132+
sum_ikj += benchmark_ikj(A, B, n);
133+
sum_jik += benchmark_jik(A, B, n);
134+
sum_kji += benchmark_kji(A, B, n);
135+
}
136+
printf("\n");
137+
write_benchmark_time("data/benchmark_order_ijk.txt", "IJK", n, sum_ijk/m);
138+
write_benchmark_time("data/benchmark_order_kij.txt", "KIJ", n, sum_kij/m);
139+
write_benchmark_time("data/benchmark_order_jki.txt", "JKI", n, sum_jki/m);
140+
write_benchmark_time("data/benchmark_order_ikj.txt", "IKJ", n, sum_ikj/m);
141+
write_benchmark_time("data/benchmark_order_jik.txt", "JIK", n, sum_jik/m);
142+
write_benchmark_time("data/benchmark_order_kji.txt", "KJI", n, sum_kji/m);
143+
144+
}
168145
}
169146

170147
void benchmark_modulos(double p, double u){
171-
148+
/* Benchmarking different modulos.
149+
The most efficient one is IKJ.
150+
*/
151+
int m = 5; // Executes m times each algo
172152
for (int i=8; i<11; i++){
173153
int n = (int) pow(2, i);
174-
double**A = random_matrix(n, p);
175-
double**B = random_matrix(n, p);
176-
benchmark_mod_naive(A, B, n, p); // Worst
177-
benchmark_ikj(A, B, n); // Best
178-
benchmark_mod_SIMD1(A, B, n, p, u);
179-
benchmark_mod_SIMD2(A, B, n, p, u);
180-
benchmark_mod_SIMD3(A, B, n, p, u);
154+
double sum_mod_naive = 0;
155+
double sum_mod_SIMD1 = 0;
156+
double sum_mod_SIMD2 = 0;
157+
double sum_mod_SIMD3 = 0;
158+
159+
for (int j=0; j<m; j++){
160+
double**A = random_matrix(n, p);
161+
double**B = random_matrix(n, p);
162+
sum_mod_naive += benchmark_mod_naive(A, B, n, p);
163+
sum_mod_SIMD1 += benchmark_mod_SIMD1(A, B, n, p, u);
164+
sum_mod_SIMD2 += benchmark_mod_SIMD2(A, B, n, p, u);
165+
sum_mod_SIMD3 += benchmark_mod_SIMD3(A, B, n, p, u);
166+
}
167+
168+
printf("\n");
169+
write_benchmark_time("data/benchmark_modulo_naive.txt", "Mod Naive", n, sum_mod_naive/m);
170+
write_benchmark_time("data/benchmark_modulo_SIMD1.txt", "Mod SIMD1", n, sum_mod_SIMD1/m);
171+
write_benchmark_time("data/benchmark_modulo_SIMD2.txt", "Mod SIMD2", n, sum_mod_SIMD2/m);
172+
write_benchmark_time("data/benchmark_modulo_SIMD3.txt", "Mod SIMD3", n, sum_mod_SIMD3/m);
181173

182174
}
183-
184175
}
185176

186177
void clean_file_loops(){
@@ -195,11 +186,10 @@ void clean_file_loops(){
195186
}
196187

197188
void clean_file_modulos(){
198-
char noms[5][64] = {"data/benchmark_modulo_naive.txt", "data/benchmark_order_ijk.txt",\
199-
"data/benchmark_modulo_SIMD1.txt", "data/benchmark_modulo_SIMD2.txt",\
200-
"data/benchmark_modulo_SIMD3.txt"};
189+
char noms[4][64] = {"data/benchmark_modulo_naive.txt", "data/benchmark_modulo_SIMD1.txt",\
190+
"data/benchmark_modulo_SIMD2.txt", "data/benchmark_modulo_SIMD3.txt"};
201191

202-
for (int i=0; i<5; i++){
192+
for (int i=0; i<4; i++){
203193
FILE* f = fopen(noms[i], "w");
204194
fclose(f);
205195
}
@@ -215,6 +205,7 @@ int main(){
215205

216206

217207
// // // Testing loops order
208+
// 07/07/23 13:27 I did a benchmark for 5
218209
// clean_file_loops();
219210
// benchmark_loops_order(p);
220211

0 commit comments

Comments
 (0)