-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1_normal.c
140 lines (111 loc) · 3.37 KB
/
1_normal.c
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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
int N = 0;
void start_counter();
double get_counter();
double mhz();
int *ind;
/* Initialize the cycle counter */
static unsigned cyc_hi = 0;
static unsigned cyc_lo = 0;
/* Set *hi and *lo to the high and low order bits of the cycle counter.
Implementation requires assembly code to use the rdtsc instruction. */
void access_counter(unsigned *hi, unsigned *lo) {
asm("rdtsc; movl %%edx,%0; movl %%eax,%1" /* Read cycle counter */
: "=r"(*hi), "=r"(*lo) /* and move results to */
: /* No input */ /* the two outputs */
: "%edx", "%eax");
}
/* Record the current value of the cycle counter. */
void start_counter() {
access_counter(&cyc_hi, &cyc_lo);
}
/* Return the number of cycles since the last call to start_counter. */
double get_counter() {
unsigned ncyc_hi, ncyc_lo;
unsigned hi, lo, borrow;
double result;
/* Get cycle counter */
access_counter(&ncyc_hi, &ncyc_lo);
/* Do double precision subtraction */
lo = ncyc_lo - cyc_lo;
borrow = lo > ncyc_lo;
hi = ncyc_hi - cyc_hi - borrow;
result = (double)hi * (1 << 30) * 4 + lo;
if (result < 0) {
fprintf(stderr, "Error: counter returns neg value: %.0f\n", result);
}
return result;
}
double mhz(int verbose, int sleeptime) {
double rate;
start_counter();
sleep(sleeptime);
rate = get_counter() / (1e6 * sleeptime);
if (verbose)
printf("\n Processor clock rate = %.1f MHz\n", rate);
return rate;
}
double funcion(double a[N][8], double b[8][N], double c[8]) {
double e[N], f = 0.f;
double **d = (double **) malloc(sizeof(double) * N);
for (int i = 0; i < N; i++) {
d[i] = (double *) malloc(sizeof(double) * N);
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
for (int k = 0; k < 8; k++) {
// printf("%d %d %d\n", i, j, k);
d[i][j] += 2 * a[i][k] * (b[k][j] - c[k]);
}
}
}
for (int i = 0; i < N; i++) {
// printf("%d\n", i);
e[i] = d[ind[i]][ind[i]] / 2;
f += e[i];
}
return f;
}
void generarAleatorios() {
int *used = (int *)malloc(sizeof(int) * N);
for (int i = 0; i < N; i++) used[i] = 0;
if (ind != NULL) free(NULL);
ind = (int *)malloc(sizeof(int) * N);
for (int i = 0; i < N; i++) {
int start = rand() % N;
while (used[start]) {
start = (start + 1) % N;
}
ind[i] = start;
used[start] = 1;
}
free(used);
}
int main(int argc, char **argv) {
if (argc != 2) {
printf("Uso: %s [N]\n", argv[0]);
return 1;
} else {
N = atoi(argv[1]);
}
double ck;
double a[N][8], b[8][N], c[8];
srand(time(0));
for (int i = 0; i < N; i++) for (int j = 0; j < 8; j++) a[i][j] = 123;
for (int i = 0; i < 8; i++) for (int j = 0; j < N; j++) b[i][j] = 456;
for (int i = 0; i < 8; i++) c[i] = 789;
generarAleatorios();
start_counter();
printf("R=%lf\n", funcion(a, b, c));
ck = get_counter();
printf("\n Clocks=%1.10lf \n", ck);
FILE *f = fopen("out.txt", "w+");
fprintf(f, "%lf", ck);
fclose(f);
/* Esta rutina imprime a frecuencia de reloxo estimada coas rutinas start_counter/get_counter */
// mhz(1, 1);
return 0;
}