source: CIVL/examples/compare/dot/mpithreads_mpi.c@ 7d77e64

main test-branch
Last change on this file since 7d77e64 was ea777aa, checked in by Alex Wilton <awilton@…>, 3 years ago

Moved examples, include, build_default.properties, common.xml, and README out from dev.civl.com into the root of the repo.

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@5704 fb995dde-84ed-4084-dfe6-e5aef3e2452c

  • Property mode set to 100644
File size: 3.5 KB
Line 
1/*****************************************************************************
2* FILE: mpithreads_mpi.c
3* DESCRIPTION:
4* This simple program illustrates the use of MPI in a program obtained
5* by modifying a serial code that performs a dot product. It is the third
6* of four codes used to show the progression from a serial program to a
7* hybrid MPI/Pthreads program. The other relevant codes are:
8* - mpithreads_serial.c - The serial version
9* - mpithreads_threads.c - A shared memory programming model using
10* Pthreads
11* - mpithreads_both.c - A hybrid model that utilizes both MPI and
12* Pthreads to execute on systems that are comprised of clusters
13* of SMP's.
14* Use of the SPMD model was chosen and for convenience, with replication
15* of the main data on all nodes. A more memory efficient implementation
16* would be advisable for larger data sets.
17* SOURCE: Vijay Sonnad, IBM
18* LAST REVISED: 01/29/09 Blaise Barney
19******************************************************************************/
20#include <mpi.h>
21#include <stdio.h>
22#include <stdlib.h>
23
24/*
25The following structure contains the necessary information to allow the
26function "dotprod" to access its input data and place its output into
27the structure. Note that this structure is unchanged from the sequential
28version.
29*/
30
31typedef struct
32 {
33 double *a;
34 double *b;
35 double sum;
36 int veclen;
37 } DOTDATA;
38
39/* Define globally accessible variables */
40#ifdef _CIVL
41$input int VECLEN;
42$output double _sum;
43#else
44#define VECLEN 100
45#endif
46DOTDATA dotstr;
47
48/*
49The function dotprod is very similar to the sequential version except that
50we now have each node working on a different part of the data. As before,
51all access to the input is through a structure of type DOTDATA and all
52output from this function is written into this same structure.
53*/
54
55void *dotprod()
56{
57
58 /* Define and use local variables for convenience */
59
60 int i, start, end, myid, len;
61 double mysum, *x, *y;
62
63 /* Obtain rank of this node */
64
65 MPI_Comm_rank (MPI_COMM_WORLD, &myid);
66
67 len = dotstr.veclen;
68 start = myid*len;
69 end = start + len;
70 x = dotstr.a;
71 y = dotstr.b;
72
73 /*
74 Perform the dot product and assign result to the appropriate variable in
75 the structure.
76 */
77
78 mysum = 0;
79 for (i=start; i<end ; i++)
80 {
81 mysum += (x[i] * y[i]);
82 }
83
84 dotstr.sum += mysum;
85
86}
87
88/*
89As before,the main program does very little computation. It does however make
90all the calls to the MPI routines. This is not a master-worker arrangement
91and all nodes participate equally in the work.
92*/
93
94int main (int argc, char* argv[])
95{
96int i,len=VECLEN;
97int myid, numprocs;
98double *a, *b;
99double mysum, allsum;
100
101/* MPI Initialization */
102MPI_Init (&argc, &argv);
103MPI_Comm_size (MPI_COMM_WORLD, &numprocs);
104MPI_Comm_rank (MPI_COMM_WORLD, &myid);
105
106/* Assign storage and initialize values */
107a = (double*) malloc (numprocs*len*sizeof(double));
108b = (double*) malloc (numprocs*len*sizeof(double));
109
110for (i=0; i<len*numprocs; i++) {
111 a[i]=1;
112 b[i]=a[i];
113 }
114
115dotstr.veclen = len;
116dotstr.a = a;
117dotstr.b = b;
118dotstr.sum=0;
119
120/* Call the dot product routine */
121dotprod();
122mysum = dotstr.sum;
123printf("Task %d partial sum is %f\n",myid, mysum);
124
125/* After the dot product, perform a summation of results on each node */
126MPI_Reduce (&mysum, &allsum, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
127
128if (myid == 0)
129 printf ("Done. MPI version: sum = %f \n", allsum);
130#ifdef _CIVL
131 if(myid == 0)
132 _sum=allsum;
133#endif
134free (a);
135free (b);
136MPI_Finalize();
137}
Note: See TracBrowser for help on using the repository browser.