-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
199 lines (164 loc) · 5.6 KB
/
main.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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// --------------------------------------------------------------
// LBYARCH S12,
// Group of Mia Bernice Cruz & Jose Mari Victorio Genuino
//
// --------------------------------------------------------------
// Project Specs: Dot Product Calculator [x86-64 to C interface]
// --------------------------------------------------------------
// Libraries
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <math.h>
#include <time.h>
// Pre-defined Constants
#define NUM 30
#define MAX 100.0f
// Dot Product Function 1 [x86_64 Assembly]
extern float dotProduct(int n, float* A, float* B);
// Dot Product Function 2 [C lang]
float cDotProduct(int n, float* A, float* B) {
// initialize sdot to 0 float
float sdot = 0.0f;
for (int i = 0; i < n; i++) {
sdot += A[i] * B[i];
}
return sdot;
}
// Generate Random Values into Vectors A and B
void generateRandomVector(int n, float* vector) {
int count = 0;
int pow = 1;
for (int i = 0; i < n; i++) {
vector[i] = ((float)rand() / (float)(RAND_MAX)) * MAX;
vector[i] = (int)(vector[i] * MAX) / MAX;
if (rand() % 2 == 0)
vector[i] /= -1;
if (i == pow - 1) {
printf("%i\n", pow);
count++;
pow *= 2;
}
}
}
double computeAverageTime(double* timesTaken) {
double average = 0.0;
for (int i = 0; i < NUM; i++)
average += timesTaken[i];
return average / NUM;
}
// Main Function C
int main()
{
// Initialize Variables
int n = 0;
float* A = (float*)malloc((int)pow(2, 28) * sizeof(float));
float* B = (float*)malloc((int)pow(2, 28) * sizeof(float));
float sdot = 0;
float sdotC = 0;
float sdotAsm = 0;
// Times
clock_t startTime;
double cTimes[NUM];
double cAverage;
double asmTimes[NUM];
double asmAverage;
// Generate Vector Values
srand((unsigned int)time(NULL));
printf("A =\n");
generateRandomVector((int)pow(2, 28), A);
printf("\nDone\n\n");
printf("B =\n");
generateRandomVector((int)pow(2, 28), B);
printf("\nDone\n\n");
printf("\n======================================================\n");
printf("\n==========Start of Program ...========================\n");
printf("\n======================================================\n");
n = (int)pow(2, 20);
printf("\nN = 2^20 = %i\n\n", n);
// Time C Function Call
printf("C Dot Product Function:\n");
for (int i = 0; i < NUM; i++) {
startTime = clock();
sdot = cDotProduct(n, A, B);
startTime = clock() - startTime;
cTimes[i] = ((double)startTime) / CLOCKS_PER_SEC;
}
sdotC = sdot;
cAverage = computeAverageTime(cTimes);
printf("\tsdot Base10: %f\n", sdotC);
printf("\tsdot Base16: %x\n", *(unsigned int*)&sdotC);
printf("\tAverage Time: %lf\n\n", cAverage);
// Time x86_64 Function Call
printf("x86_64 Dot Product Function:\n");
for (int i = 0; i < NUM; i++) {
startTime = clock();
sdot = dotProduct(n, A, B);
startTime = clock() - startTime;
asmTimes[i] = ((double)startTime) / CLOCKS_PER_SEC;
}
sdotAsm = sdot;
asmAverage = computeAverageTime(asmTimes);
printf("\tsdot Base10: %f\n", sdotAsm);
printf("\tsdot Base16: %x\n", *(unsigned int*)&sdotAsm);
printf("\tAverage Time: %lf\n\n", asmAverage);
// Results
printf("Results:\n");
printf("\tSimilarity: %.2f %%\n", sdotC * 100 / sdotAsm);
if (asmAverage <= 0)
printf("\tTime Difference: 0 (0 %% faster)\n");
else {
printf("\tTime Difference: %lf ", cAverage - asmAverage);
if (cAverage > asmAverage)
printf("(x86_64 is faster by %.2lf times)\n", cAverage / asmAverage);
else
printf("(C is faster by %.2lf times)\n", asmAverage / cAverage);
}
printf("\n======================================================\n");
printf("\n==========Start of Program 2...=======================\n");
printf("\n======================================================\n");
n = (int)pow(2, 24);
printf("\nN = 2^24 = %i\n\n", n);
// Time C Function Call
printf("C Dot Product Function:\n");
for (int i = 0; i < NUM; i++) {
startTime = clock();
sdot = cDotProduct(n, A, B);
startTime = clock() - startTime;
cTimes[i] = ((double)startTime) / CLOCKS_PER_SEC;
}
sdotC = sdot;
cAverage = computeAverageTime(cTimes);
printf("\tsdot Base10: %f\n", sdotC);
printf("\tsdot Base16: %x\n", *(unsigned int*)&sdotC);
printf("\tAverage Time: %lf\n\n", cAverage);
// Time x86_64 Function Call
printf("x86_64 Dot Product Function:\n");
for (int i = 0; i < NUM; i++) {
startTime = clock();
sdot = dotProduct(n, A, B);
startTime = clock() - startTime;
asmTimes[i] = ((double)startTime) / CLOCKS_PER_SEC;
}
sdotAsm = sdot;
asmAverage = computeAverageTime(asmTimes);
printf("\tsdot Base10: %f\n", sdotAsm);
printf("\tsdot Base16: %x\n", *(unsigned int*)&sdotAsm);
printf("\tAverage Time: %lf\n\n", asmAverage);
// Results
printf("Results:\n");
printf("\tSimilarity: %.2f %%\n", sdotC * 100 / sdotAsm);
if (asmAverage <= 0)
printf("\tTime Difference: 0 (0 %% faster)\n");
else {
printf("\tTime Difference: %lf ", cAverage - asmAverage);
if (cAverage > asmAverage)
printf("(x86_64 is faster by %.2lf times)\n", cAverage / asmAverage);
else
printf("(C is faster by %.2lf times)\n", asmAverage / cAverage);
}
// Free Memory Allocation
free(A);
free(B);
return 0;
}