-
Notifications
You must be signed in to change notification settings - Fork 0
/
dotprod_serial.c
97 lines (79 loc) · 1.91 KB
/
dotprod_serial.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
/*
* FILE: dotprod_serial.c
* DESCRIPTION:
* This is a simple serial program which computes the dot product of two
* vectors. The threaded version can is dotprod_mutex.c.
* SOURCE: Vijay Sonnad, IBM
* LAST REVISED: 01/29/09 Blaise Barney
*/
#include <stdio.h>
#include <stdlib.h>
/*
The following structure contains the necessary information
to allow the function "dotprod" to access its input data and
place its output so that it can be accessed later.
*/
typedef struct
{
double *a;
double *b;
double sum;
int veclen;
} DOTDATA;
#define VECLEN 100
DOTDATA dotstr;
/*
We will use a function (dotprod) to perform the scalar product.
All input to this routine is obtained through a structure of
type DOTDATA and all output from this function is written into
this same structure. While this is unnecessarily restrictive
for a sequential program, it will turn out to be useful when
we modify the program to compute in parallel.
*/
void dotprod()
{
/* Define and use local variables for convenience */
int start, end, i;
double mysum, *x, *y;
start=0;
end = dotstr.veclen;
x = dotstr.a;
y = dotstr.b;
/*
Perform the dot product and assign result
to the appropriate variable in the structure.
*/
mysum = 0;
for (i=start; i<end ; i++)
{
mysum += (x[i] * y[i]);
}
dotstr.sum = mysum;
}
/*
The main program initializes data and calls the dotprd() function.
Finally, it prints the result.
*/
int main (int argc, char *argv[])
{
int i,len;
double *a, *b;
/* Assign storage and initialize values */
len = VECLEN;
a = (double*) malloc (len*sizeof(double));
b = (double*) malloc (len*sizeof(double));
for (i=0; i<len; i++) {
a[i]=1;
b[i]=a[i];
}
dotstr.veclen = len;
dotstr.a = a;
dotstr.b = b;
dotstr.sum=0;
/* Perform the dotproduct */
dotprod ();
/* Print result and release storage */
printf ("Sum = %f \n", dotstr.sum);
free (a);
free (b);
}