-
Notifications
You must be signed in to change notification settings - Fork 162
Expand file tree
/
Copy pathddot.c
More file actions
79 lines (63 loc) · 1.97 KB
/
ddot.c
File metadata and controls
79 lines (63 loc) · 1.97 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
#include <stdio.h>
#include <stdlib.h>
// DDOT: Compute dot product of two vectors
// result = sum(x[i] * y[i])
// x: vector of length N with stride incx
// y: vector of length N with stride incy
double ddot(int N, const double* x, int incx, const double* y, int incy) {
double result = 0.0;
for (int i = 0; i < N; i++) {
result += x[i * incx] * y[i * incy];
}
return result;
}
// Simple version (stride = 1)
double simple_ddot(int N, const double* x, const double* y) {
double result = 0.0;
for (int i = 0; i < N; i++) {
result += x[i] * y[i];
}
return result;
}
// Single precision version
float sdot(int N, const float* x, int incx, const float* y, int incy) {
float result = 0.0f;
for (int i = 0; i < N; i++) {
result += x[i * incx] * y[i * incy];
}
return result;
}
int main() {
const int N = 5;
double x[] = {1.0, 2.0, 3.0, 4.0, 5.0};
double y[] = {2.0, 3.0, 4.0, 5.0, 6.0};
printf("DOT Product Test\n");
printf("x: [");
for (int i = 0; i < N; i++) {
printf("%.1f ", x[i]);
}
printf("]\n");
printf("y: [");
for (int i = 0; i < N; i++) {
printf("%.1f ", y[i]);
}
printf("]\n\n");
// Test simple version
double result = simple_ddot(N, x, y);
printf("dot(x, y) = %.1f\n", result);
// Manual verification
double manual = 0.0;
for (int i = 0; i < N; i++) {
manual += x[i] * y[i];
printf(" %.1f * %.1f = %.1f\n", x[i], y[i], x[i] * y[i]);
}
printf("Expected: %.1f, Actual: %.1f\n\n", manual, result);
// Test with stride
printf("Testing with stride=2 (every other element):\n");
double result_stride = ddot(3, x, 2, y, 2);
printf("dot(x[::2], y[::2]) = %.1f\n", result_stride);
printf("Manual: %.1f*%.1f + %.1f*%.1f + %.1f*%.1f = %.1f\n",
x[0], y[0], x[2], y[2], x[4], y[4],
x[0]*y[0] + x[2]*y[2] + x[4]*y[4]);
return 0;
}