Skip to content

Commit 2f06345

Browse files
Modular function fix and installing OpenBLAS
1 parent 67326f4 commit 2f06345

19 files changed

+7160
-1320
lines changed

src/data/Matrix_A.txt

Lines changed: 1025 additions & 201 deletions
Large diffs are not rendered by default.

src/data/Matrix_B.txt

Lines changed: 1025 additions & 201 deletions
Large diffs are not rendered by default.

src/data/Matrix_C.txt

Lines changed: 1025 additions & 201 deletions
Large diffs are not rendered by default.

src/data/Matrix_D.txt

Lines changed: 1025 additions & 201 deletions
Large diffs are not rendered by default.

src/data/Matrix_E.txt

Lines changed: 1025 additions & 201 deletions
Large diffs are not rendered by default.

src/data/Matrix_F.txt

Lines changed: 1025 additions & 201 deletions
Large diffs are not rendered by default.

src/data/Matrix_G.txt

Lines changed: 918 additions & 36 deletions
Large diffs are not rendered by default.

src/data/benchmark_modulo_Barrett.txt

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
255 0.356913
2-
511 3.051232
3-
256 0.353928
4-
512 3.022207
5-
256 0.366828
6-
512 3.066367
7-
1024 27.525820
1+
256 0.406682
2+
512 3.161513
3+
1024 28.813085
4+
256 0.360843
5+
512 3.050652
6+
1024 27.503507

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.447807
2-
512 2.914416
3-
1024 25.676863
1+
256 0.320855
2+
512 2.779970
3+
1024 25.977462

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.393323
2-
512 2.573454
3-
1024 24.374175
1+
256 0.353629
2+
512 2.799502
3+
1024 24.423494

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.308907
2-
512 2.618251
3-
1024 24.035276
1+
256 0.312455
2+
512 2.680338
3+
1024 24.309112

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.276151
2-
512 2.137076
3-
1024 20.249978
1+
256 0.236347
2+
512 2.194848
3+
1024 20.595512

src/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ void benchmark_modulos(double p, double u, double u_overline, double u_b){
159159
/* Benchmarking different modulos.
160160
The most efficient one is IKJ.
161161
*/
162-
int m = 1; // Executes m times each algo
162+
int m = 2; // Executes m times each algo
163163
for (int i=8; i<11; i++){
164164
int n = (int) pow(2, i);
165165
double sum_mod_naive = 0;

src/main_blas.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <cblas.h>
4+
5+
6+
int main(){
7+
int i=0;
8+
double A[6] = {1.0,2.0,1.0,-3.0,4.0,-1.0};
9+
double B[6] = {1.0,2.0,1.0,-3.0,4.0,-1.0};
10+
double C[9] = {.5,.5,.5,.5,.5,.5,.5,.5,.5};
11+
cblas_dgemm(CblasColMajor, CblasNoTrans, CblasTrans,3,3,2,1,A, 3, B, 3,2,C,3);
12+
13+
for(i=0; i<9; i++){
14+
printf("%lf ", C[i]);
15+
}
16+
printf("\n");
17+
return 0;
18+
}

src/main_test.c

Lines changed: 31 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ int main(int argc, char** argv){
2424
const int TEST1 = 0;
2525
const int TEST2 = 0;
2626
const int TEST3 = 0;
27-
const int TEST4 = 0;
27+
const int TEST4 = 1;
2828
const int TEST5 = 0;
29-
const int TEST6 = 1;
29+
// const int TEST6 = 0;
3030
// const int TEST7 = 1;
3131

3232

@@ -35,11 +35,11 @@ int main(int argc, char** argv){
3535
double p = pow(2, 26) - 5;
3636

3737
// Precomputed constants for Modular functions
38-
fesetround(FE_DOWNWARD);
38+
fesetround(FE_TONEAREST);
3939
double u = 1.0 / p; // Constant for SIMD
4040
fesetround(FE_UPWARD);
4141
double u_overline = 1.0 / p; // Constant for SIMD2 and SIMD3
42-
fesetround(FE_DOWNWARD);
42+
fesetround(FE_TONEAREST);
4343
u_int32_t u_b = (int) (pow(2, 56) / p); // Constant for Barrett
4444

4545
double a = pow(2, 26);
@@ -64,11 +64,11 @@ int main(int argc, char** argv){
6464
double p = pow(2, 26) - 5;
6565

6666
// Precomputed constants for Modular functions
67-
fesetround(FE_DOWNWARD);
67+
fesetround(FE_TONEAREST);
6868
double u = 1.0 / p; // Constant for SIMD
6969
fesetround(FE_UPWARD);
7070
double u_overline = 1.0 / p; // Constant for SIMD2 and SIMD3
71-
fesetround(FE_DOWNWARD);
71+
fesetround(FE_TONEAREST);
7272
u_int32_t u_b = (int) (pow(2, 56) / p); // Constant for Barrett
7373

7474
double a = (p-1) * (p-1) * 32;
@@ -93,11 +93,11 @@ int main(int argc, char** argv){
9393
double p = pow(2, 26) - 5;
9494

9595
// Precomputed constants for Modular functions
96-
fesetround(FE_DOWNWARD);
96+
fesetround(FE_TONEAREST);
9797
double u = 1.0 / p; // Constant for SIMD
9898
fesetround(FE_UPWARD);
9999
double u_overline = 1.0 / p; // Constant for SIMD2 and SIMD3
100-
fesetround(FE_DOWNWARD);
100+
fesetround(FE_TONEAREST);
101101
u_int32_t u_b = (int) (pow(2, 56) / p); // Constant for Barrett
102102

103103
double a = (p-1) * (p-1) * 16; // a > 2^(25+25+4) = 2^54
@@ -123,16 +123,16 @@ int main(int argc, char** argv){
123123
double p = pow(2, 26) - 5;
124124

125125
// Precomputed constants for Modular functions
126-
fesetround(FE_DOWNWARD);
126+
fesetround(FE_TONEAREST);
127127
double u = 1.0 / p; // Constant for SIMD
128128
fesetround(FE_UPWARD);
129129
double u_overline = 1.0 / p; // Constant for SIMD2 and SIMD3
130-
fesetround(FE_DOWNWARD);
131130
u_int32_t u_b = (int) (pow(2, 56) / p); // Constant for Barrett
131+
fesetround(FE_TONEAREST);
132132

133-
int n = 200;
133+
int n = 1024;
134134

135-
for (int i=0; i<10; i++){
135+
for (int i=0; i<1; i++){
136136

137137
double**A = random_matrix(n, p);
138138
double**B = random_matrix(n, p);
@@ -163,6 +163,16 @@ int main(int argc, char** argv){
163163
int nb3 = equals_matrix(C, F, n);
164164
int nb4 = equals_matrix(C, G, n);
165165

166+
delete_matrix(&A, n);
167+
delete_matrix(&B, n);
168+
delete_matrix(&C, n);
169+
delete_matrix(&D, n);
170+
delete_matrix(&E, n);
171+
delete_matrix(&F, n);
172+
delete_matrix(&G, n);
173+
174+
175+
printf("i=%d \n", i);
166176
assert(nb1==1);
167177
assert(nb2==1);
168178
assert(nb3==1);
@@ -177,49 +187,24 @@ int main(int argc, char** argv){
177187
/* Testing the equivalence between
178188
if (d < 0) return d+p; else return d;
179189
And
180-
return d + p()
190+
return d + (-(d<0) & (int)p)
191+
And
192+
return d + p * (d<0)
181193
*/
182194

183195
double d1 = -2;
184196
double d2 = 2;
185197
double p = 100.0;
186198

187-
double a = 1024.5;
188-
double b = 0b1111111111111111111111111111111111111111111111111111111111111111;
189-
double temp = b && a;
190-
191-
// 0b 1111 1111 ... 1111 for double = ...
192-
193-
double res1 = d1 + ((-(d1<0)) && p);
194-
double res2 = d2 + ((-(d2<0)) && p);
199+
double res1 = d1 + ((-(d1<0)) & (u_int64_t)p);
200+
double res2 = d2 + ((-(d2<0)) & (u_int64_t)p);
201+
double res3 = d1 + p * (d1<0);
202+
double res4 = d2 + p * (d2<0);
195203

196204
printf("res1 = %f \n", res1);
197205
printf("res2 = %f \n", res2);
198-
printf("b = %f \n", b);
199-
printf("temp = %f \n", temp);
200-
}
201-
202-
if (TEST6){
203-
/* Testing FE_DOWNWARD and FE_UPWARD */
204-
double p = pow(2, 26) - 5;
205-
// double u1 = (p-1) * (p-1) * (p-1);
206-
double u1 = -(p-1) * (p-1) * (p-1) * 64;
207-
fesetround(FE_DOWNWARD);
208-
double u2 = -(p-1) * (p-1) * (p-1) * 64;
209-
fesetround(FE_UPWARD);
210-
double u3 = -(p-1) * (p-1) * (p-1) * 64;
211-
fesetround(FE_TOWARDZERO);
212-
double u4 = -(p-1) * (p-1) * (p-1) * 64;
213-
fesetround(FE_TONEAREST);
214-
double u5 = -(p-1) * (p-1) * (p-1) * 64;
215-
216-
printf("u1=%f \n", u1);
217-
printf("u2=%f \n", u2);
218-
printf("u3=%f \n", u3);
219-
printf("u4=%f \n", u3);
220-
printf("u5=%f \n", u3);
221-
222-
206+
printf("res3 = %f \n", res3);
207+
printf("res4 = %f \n", res4);
223208
}
224209

225210
return 0;

src/makefile

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Targets
2-
all: main main_test
2+
all: main main_test main_blas
33

44
# Headers
55
matrix.o: matrix.c
@@ -12,7 +12,9 @@ main: main.c matrix.o matrix_mul.o
1212
gcc -o main main.c matrix.o matrix_mul.o -lm
1313
main_test: main_test.c matrix.o matrix_mul.o
1414
gcc -o main_test main_test.c matrix.o matrix_mul.o -lm
15+
main_blas: main_blas.c
16+
gcc -o main_blas main_blas.c -lopenblas
1517

1618
clean:
1719
rm *.o
18-
rm -rf main main_test
20+
rm -rf main main_test main_blas

src/matrix.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,6 @@ double** random_matrix(int size, double p){
4040
// int rand_int = rand();
4141
// mat[i][j] = modulo_SIMD1(rand_int, p);
4242
mat[i][j] = (double) (rand() % ((int) p));
43-
44-
// mat[i][j] = (double)rand()/(double)(RAND_MAX/p);
45-
46-
47-
// mat[i][j] = rand() % mod;
4843
}
4944
}
5045

src/matrix_mul.c

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,18 @@ double modulo_SIMD1(double a, double p, double u){
1212
double b = a * u;
1313
double c = (double)(int)b;
1414
double d = a - c * p;
15-
if (d >= p) return d-p;
16-
if (d < 0) return d+p;
15+
// if (d >= p) return d-p;
16+
if (d >= p) {
17+
printf("SIMD1: if1 \n");
18+
return d-p;
19+
}
20+
// printf("SIMD1: if1 \n");
21+
if (d < 0){
22+
printf("SIMD2: if2 \n");
23+
return d+p;
24+
}
25+
26+
// if (d < 0) return d+p;
1727
return d;
1828
}
1929

@@ -24,7 +34,11 @@ double modulo_SIMD2(double a, double p, double u){
2434
double b = a * u;
2535
double c = (double)(int)b;
2636
double d = a - c * p;
27-
if (d < 0) return d+p;
37+
// if (d < 0) return d+p;
38+
if (d < 0){
39+
printf("d < 0 \n");
40+
return d+p;
41+
}
2842
return d;
2943
}
3044

@@ -35,7 +49,8 @@ double modulo_SIMD3(double a, double p, double u){
3549
double b = a * u;
3650
double c = (double)(int)b;
3751
double d = a - c * p;
38-
return d + (-(d<0) && p);
52+
// return d + (-(d<0) & (int64_t) p);
53+
return d + p * (d<0);
3954
}
4055

4156
u_int32_t modulo_Barrett(u_int64_t a, u_int32_t p, u_int32_t u){

src/tools/gnuplot_modulos.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ replot "../data/benchmark_order_ikj.txt" using 1:2 title "No Mod" with linespoin
1919
replot "../data/benchmark_modulo_SIMD1.txt" using 1:2 title "SIMD1" with linespoints ls 3
2020
replot "../data/benchmark_modulo_SIMD2.txt" using 1:2 title "SIMD2" with linespoints ls 4
2121
replot "../data/benchmark_modulo_SIMD3.txt" using 1:2 title "SIMD3" with linespoints ls 5
22-
replot "../data/benchmark_modulo_Barrett.txt" using 1:2 title "Bararett" with linespoints ls 6
22+
# replot "../data/benchmark_modulo_Barrett.txt" using 1:2 title "Barrett" with linespoints ls 6
2323

2424
# Graphe Order of loops

0 commit comments

Comments
 (0)