-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBase Code.c
More file actions
176 lines (147 loc) · 5.3 KB
/
Copy pathBase Code.c
File metadata and controls
176 lines (147 loc) · 5.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#define CLEAR_STDIN() do { int ch; while ((ch = getchar()) != '\n' && ch != EOF); } while(0)
void fill(double* x, int n) {
int i;
for (i=0, n=n*n; i<n; i++, x++)
*x = ((double) (1 + rand() % 12345)) / ((double) (1 + rand() % 6789));
}
void matrix_mult_index (int n, double* a, double* b, double* c) {
int i, j, k;
for (i=0; i<n; i++)
for (j=0; j<n; j++) {
c[i*n+j] =0;
for(k = 0; k < n; k++)
c[i*n+j] += a[i*n+k] * b[k*n+j];
}
}
void matrix_mult_ptr_reg (int n, double* a, double* b, double* c) {
register double cij;
register double *at, *bt;
register int i, j, k;
for (i=0; i<n; i++, a+=n)
for (j = 0; j < n; j++, c++) {
cij = 0;
for(k = 0, at = a, bt = &b[j]; k < n; k++, at++, bt+=n)
cij += *at * *bt;
*c = cij;
}
}
void matrix_mult_ptr_no_reg (int n, double* a, double* b, double* c) {
double cij;
double *at, *bt;
int i, j, k;
for (i=0; i<n; i++, a+=n)
for (j = 0; j < n; j++, c++) {
cij = 0;
for(k = 0, at = a, bt = &b[j]; k < n; k++, at++, bt+=n)
cij += *at * *bt;
*c = cij;
}
}
int main()
{
clock_t t0, t1;
int n, ref;
do{
printf("Input size of matrix, n = ");
scanf("%d", &n);
ref = 0;
double *A = (double*)_aligned_malloc(n * n * sizeof(double), 64 /*sizeof(double)*/); // 64 is cache line size
double *B = (double*)_aligned_malloc(n * n * sizeof(double), 64 /*sizeof(double)*/);
double *C1 = (double*)_aligned_malloc(n * n * sizeof(double), 64 /*sizeof(double)*/);
double *C2 = (double*)_aligned_malloc(n * n * sizeof(double), 64 /*sizeof(double)*/);
if(A == NULL || B == NULL || C1 == NULL || C2 == NULL){
printf("Memory Allocation Error\n\n");
return(-1);
}
unsigned int seed = time(NULL);
printf("\nseed = %u\n", seed);
srand(seed);
fill(A, n);
fill(B, n);
//fflush(stdin);
CLEAR_STDIN();
printf("\n\nDo you want to run matrix_mult_index (y/n)? ");
if(getchar() == 'y'){
ref = 1;
t0 = clock();
matrix_mult_index(n, A, B, C1);
t1 = clock();
printf("\n\t\t\tExecution time of matrix_mult_index = %0.2f s", (float)(t1-t0)/CLOCKS_PER_SEC);
}
//fflush(stdin);
CLEAR_STDIN();
printf("\n\nDo you want to run matrix_mult_ptr_reg (y/n)? ");
if(getchar() == 'y'){
ref++;
t0 = clock();
matrix_mult_ptr_reg(n, A, B, ref == 1 ? C1 : C2);
t1 = clock();
printf("\n\t\t\tExecution time of matrix_mult_ptr_reg = %0.2f s\n", (float)(t1-t0)/CLOCKS_PER_SEC);
}
//fflush(stdin);
CLEAR_STDIN();
printf("\n\nDo you want to run matrix_mult_ptr_no_reg (y/n)? ");
if(getchar() == 'y'){
if(++ref > 2) ref = 2;
t0 = clock();
matrix_mult_ptr_no_reg(n, A, B, ref == 1 ? C1 : C2);
t1 = clock();
printf("\n\t\t\tExecution time of matrix_mult_ptr_no_reg = %0.2f s", (float)(t1-t0)/CLOCKS_PER_SEC);
}
/*
//fflush(stdin);
CLEAR_STDIN();
printf("\n\nDo you want to run matrix_mult_transpose (y/n)? ");
if(getchar() == 'y'){
if(++ref > 2) ref = 2;
t0 = clock();
matrix_mult_transpose(n, A, B, ref == 1 ? C1 : C2);
t1 = clock();
printf("\n\t\t\tExecution time of matrix_mult_transpose = %0.2f s", (float)(t1-t0)/CLOCKS_PER_SEC);
}
//fflush(stdin);
CLEAR_STDIN();
printf("\n\nDo you want to run matrix_mult_block (y/n)? ");
if(getchar() == 'y'){
if(++ref > 2) ref = 2;
int block_size;
printf("\n\tInput size of block = ");
scanf("%d", &block_size);
t0 = clock();
matrix_mult_block(n, block_size, A, B, ref == 1 ? C1 : C2);
t1 = clock();
printf("\n\t\t\tExecution time of matrix_mult_block = %0.2f s", (float)(t1-t0)/CLOCKS_PER_SEC);
}
*/
printf("\n\n\nEnd Of Execution\n\n");
if(ref == 2){
int i;
double *c1, *c2;
printf("\n\nStart of Compare: ");
for(i=0, c1=C1, c2=C2, n=n*n; i<n; i++, c1++, c2++){
if(fabs((*c1 - *c2) / *c1) > 1E-10)
break;
if(i % (n/20) == 0)
printf(".");
}
if(i != n)
printf(" Ooops, Error Found @ %d: %0.3f vs %0.3f\n\n",i, *c1, *c2);
else
printf(" OK, OK, Matrixes are equivalent.\n\n");
}
else
printf("\n\nNo Compare due to No Reference or No Data.\n\n");
_aligned_free(A);
_aligned_free(B);
_aligned_free(C1);
_aligned_free(C2);
//fflush(stdin);
CLEAR_STDIN();
printf("\n\nDo you want to continue (y/n)? ");
} while(getchar() == 'y');
return 0;
}