source: CIVL/examples/compare/dot/mpithreads_serial.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: 2.7 KB
RevLine 
[2ceb09a]1/*****************************************************************************
2* FILE: mpithreads_serial.c
3* DESCRIPTION:
4* This is a simple serial program that computes the dot product of two
5* vectors. It is the first of four codes used to show the progression
6* from a serial program to a hybrid MPI/Pthreads program. The other
7* relevant codes are:
8* - mpithreads_threads.c - A shared memory programming model using
9* Pthreads
10* - mpithreads_mpi.c - A distributed memory programming model with MPI
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* SOURCE: Vijay Sonnad, IBM
15* LAST REVISED: 01/29/09 Blaise Barney
16******************************************************************************/
17#include <stdio.h>
18#include <stdlib.h>
19
20/*
21The following structure contains the necessary information to allow the
22function "dotprod" to access its input data and place its output so that
23it can be accessed later.
24*/
25
26typedef struct
27 {
28 double *a;
29 double *b;
30 double sum;
31 int veclen;
32 } DOTDATA;
33
34#ifdef _CIVL
35$input int VECLEN;
36$output double _sum;
37#else
38#define VECLEN 100
39#endif
40DOTDATA dotstr;
41
42/*
43We will use a function (dotprod) to perform the scalar product. All input to
44this routine is obtained through a structure of type DOTDATA and all output
45from this function is written into this same structure. While this is
46unnecessarily restrictive for a sequential program, it will turn out to be
47useful when we modify the program to compute in parallel.
48*/
49
50void* dotprod(void)
51{
52
53 /* Define and use local variables for convenience */
54
55 int start, end, i;
56 double mysum, *x, *y;
57
58 start=0;
59 end = dotstr.veclen;
60 x = dotstr.a;
61 y = dotstr.b;
62
63 /*
64 Perform the dot product and assign result to the appropriate variable in
65 the structure.
66 */
67
68 mysum = 0;
69 for (i=start; i<end ; i++)
70 {
71 mysum += (x[i] * y[i]);
72 }
73 dotstr.sum = mysum;
74
75}
76
77/*
78The main program initializes data and calls the dotprd() function. Finally,
79it prints the result.
80*/
81
82int main (int argc, char* argv[])
83{
84int i,len;
85double *a, *b;
86
87/* Assign storage and initialize values */
88len = VECLEN;
89a = (double*) malloc (len*sizeof(double));
90b = (double*) malloc (len*sizeof(double));
91
92for (i=0; i<len; i++) {
93 a[i]=1;
94 b[i]=a[i];
95 }
96
97dotstr.veclen = len;
98dotstr.a = a;
99dotstr.b = b;
100dotstr.sum=0;
101
102/* Perform the dotproduct */
103dotprod ();
104#ifdef _CIVL
105 _sum=dotstr.sum;
106#endif
107/* Print result and release storage */
108printf ("Done. Serial version: sum = %f \n", dotstr.sum);
109free (a);
110free (b);
111}
Note: See TracBrowser for help on using the repository browser.