source: CIVL/examples/mpi-pthread/mpithreads_mpi.c

main
Last change on this file 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.4 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
41#define VECLEN 100
42DOTDATA dotstr;
43
44/*
45The function dotprod is very similar to the sequential version except that
46we now have each node working on a different part of the data. As before,
47all access to the input is through a structure of type DOTDATA and all
48output from this function is written into this same structure.
49*/
50
51void dotprod()
52{
53
54 /* Define and use local variables for convenience */
55
56 int i, start, end, myid, len;
57 double mysum, *x, *y;
58
59 /* Obtain rank of this node */
60
61 MPI_Comm_rank (MPI_COMM_WORLD, &myid);
62
63 len = dotstr.veclen;
64 start = myid*len;
65 end = start + len;
66 x = dotstr.a;
67 y = dotstr.b;
68
69 /*
70 Perform the dot product and assign result to the appropriate variable in
71 the structure.
72 */
73
74 mysum = 0;
75 for (i=start; i<end ; i++)
76 {
77 mysum += (x[i] * y[i]);
78 }
79
80 dotstr.sum += mysum;
81
82}
83
84/*
85As before,the main program does very little computation. It does however make
86all the calls to the MPI routines. This is not a master-worker arrangement
87and all nodes participate equally in the work.
88*/
89
90int main (int argc, char* argv[])
91{
92int i,len=VECLEN;
93int myid, numprocs;
94double *a, *b;
95double mysum, allsum;
96
97/* MPI Initialization */
98MPI_Init (&argc, &argv);
99MPI_Comm_size (MPI_COMM_WORLD, &numprocs);
100MPI_Comm_rank (MPI_COMM_WORLD, &myid);
101
102/* Assign storage and initialize values */
103a = (double*) malloc (numprocs*len*sizeof(double));
104b = (double*) malloc (numprocs*len*sizeof(double));
105
106for (i=0; i<len*numprocs; i++) {
107 a[i]=1;
108 b[i]=a[i];
109 }
110
111dotstr.veclen = len;
112dotstr.a = a;
113dotstr.b = b;
114dotstr.sum=0;
115
116/* Call the dot product routine */
117dotprod();
118mysum = dotstr.sum;
119printf("Task %d partial sum is %f\n",myid, mysum);
120
121/* After the dot product, perform a summation of results on each node */
122MPI_Reduce (&mysum, &allsum, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
123
124if (myid == 0)
125 printf ("Done. MPI version: sum = %f \n", allsum);
126free (a);
127free (b);
128MPI_Finalize();
129return 0;
130}
Note: See TracBrowser for help on using the repository browser.