-
Notifications
You must be signed in to change notification settings - Fork 1
/
vecdot.c
54 lines (44 loc) · 1.24 KB
/
vecdot.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
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
/* Define length of dot product vectors */
#define VECLEN 6000000000
int main (int argc, char* argv[])
{
/* MPI Initialization */
int myid, numprocs;
MPI_Init (&argc, &argv);
MPI_Comm_size (MPI_COMM_WORLD, &numprocs);
MPI_Comm_rank (MPI_COMM_WORLD, &myid);
long int i, len = VECLEN / numprocs;
double *a, *b;
double mysum, allsum;
/*
Each MPI task performs the dot product, obtains its partial sum, and then calls
MPI_Reduce to obtain the global sum.
*/
if (myid == 0)
printf("Starting omp_dotprod_mpi. Using %d tasks...\n",numprocs);
/* 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 */
mysum = 0.0;
for (i=0; i<len; i++)
{
mysum += a[i] * b[i];
}
printf("Task %d partial sum = %f\n",myid, mysum);
/* After the dot product, perform a summation of results on each node */
MPI_Reduce (&mysum, &allsum, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
if (myid == 0)
printf ("Done. MPI version: global sum = %f \n", allsum);
free (a);
free (b);
MPI_Finalize();
}