-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdotprod_serial.c
executable file
·73 lines (61 loc) · 1.77 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
/*****************************************************************************
* FILE: omp_dotprod_serial.c
* DESCRIPTION:
* This simple program is the serial version of a dot product and the
* first of four codes used to show the progression from a serial program to a
* hybrid MPI/OpenMP program. The relevant codes are:
* - omp_dotprod_serial.c - Serial version
* - omp_dotprod_openmp.c - OpenMP only version
* - omp_dotprod_mpi.c - MPI only version
* - omp_dotprod_hybrid.c - Hybrid MPI and OpenMP version
* SOURCE: Blaise Barney
* LAST REVISED: 06/02/17 Blaise Barney
******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include "timing.h"
/* Define length of dot product vectors */
#define VECLEN 1000000000
int main (int argc, char* argv[])
{
int i,len=VECLEN;
double *a, *b;
double sum;
timing_t tstart, tend;
printf("Starting omp_dotprod_serial\n");
/* Assign storage for dot product vectors */
a = (double*) malloc (len*sizeof(double));
b = (double*) malloc (len*sizeof(double));
/* Initialize dot product vectors */
for (i=0; i<len; i++) {
a[i]=1.0;
b[i]=a[i];
}
/* Perform the dot product */
sum = 0.0;
get_time(&tstart);
/*
for (i=0; i<len; i++)
{
sum += (a[i] * b[i]);
}*/
/* Perform the dot product with 2x loop unrolling
for (i=0; i<len; i+=2)
{
sum += (a[i] * b[i]);
sum += (a[i+1] * b[i+1]);
}*/
/* Perform the dot product with 4x loop unrolling*/
for (i=0; i<len; i+=4)
{
sum += (a[i] * b[i]);
sum += (a[i+1] * b[i+1]);
sum += (a[i+2] * b[i+2]);
sum += (a[i+3] * b[i+3]);
}
get_time(&tend);
printf ("Serial version: sum = %f \n"
"Elapsed time: %g s\n", sum, timespec_diff(tstart,tend));
free (a);
free (b);
}