316 |
ira |
1 |
/*******************************************************************************
|
|
|
2 |
* prob1-pipe.c - a pipeline method to calculate a McLauran series
|
|
|
3 |
******************************************************************************/
|
|
|
4 |
|
|
|
5 |
#include <mpi.h>
|
|
|
6 |
#include <stdio.h>
|
|
|
7 |
#include <stdlib.h>
|
|
|
8 |
|
|
|
9 |
#define TAG_DATA 0xdddd
|
|
|
10 |
|
|
|
11 |
int main (int argc, char *argv[])
|
|
|
12 |
{
|
|
|
13 |
int myid, numprocs;
|
|
|
14 |
int *a, mya, x, n;
|
|
|
15 |
float temp;
|
|
|
16 |
|
|
|
17 |
MPI_Status status;
|
|
|
18 |
|
|
|
19 |
MPI_Init (&argc, &argv);
|
|
|
20 |
MPI_Comm_rank (MPI_COMM_WORLD, &myid);
|
|
|
21 |
MPI_Comm_size (MPI_COMM_WORLD, &numprocs);
|
|
|
22 |
|
|
|
23 |
if (myid == 0) /* root */
|
|
|
24 |
{
|
|
|
25 |
// allocate a
|
|
|
26 |
// read a[i]'s
|
|
|
27 |
// read n
|
|
|
28 |
// read x
|
|
|
29 |
}
|
|
|
30 |
|
|
|
31 |
/* Do a sanity check.
|
|
|
32 |
* We MUST have exactly enough processors available
|
|
|
33 |
* to construct the pipeline. */
|
|
|
34 |
if (numprocs != n)
|
|
|
35 |
exit (1); // not enough processors
|
|
|
36 |
|
|
|
37 |
/* Send everyone a single a value, and the x value */
|
|
|
38 |
MPI_Scatter (a, 1, MPI_INT, &mya, 1, MPI_INT, 0, MPI_COMM_WORLD);
|
|
|
39 |
MPI_Bcast (&x, 1, MPI_INT, 0, MPI_COMM_WORLD);
|
|
|
40 |
|
|
|
41 |
if (myid == 0)
|
|
|
42 |
{
|
|
|
43 |
/* We are the root node, so do a single calculation, and then
|
|
|
44 |
* forward the result on down the pipeline */
|
|
|
45 |
temp = mya * pow (x, myid);
|
|
|
46 |
MPI_Send (&temp, 1, MPI_FLOAT, myid+1, TAG_DATA, MPI_COMM_WORLD);
|
|
|
47 |
}
|
|
|
48 |
else if (myid < (numprocs - 1))
|
|
|
49 |
{
|
|
|
50 |
/* We are in the middle of the pipeline, so recieve a value from
|
|
|
51 |
* our neighbor to the left, do a calculation, and pass the result
|
|
|
52 |
* to our neighbor to the right */
|
|
|
53 |
MPI_Recv (&temp, 1, MPI_FLOAT, myid-1, TAG_DATA, MPI_COMM_WORLD, &status);
|
|
|
54 |
temp += mya * pow (x, myid);
|
|
|
55 |
MPI_Send (&temp, 1, MPI_FLOAT, myid+1, TAG_DATA, MPI_COMM_WORLD);
|
|
|
56 |
}
|
|
|
57 |
else
|
|
|
58 |
{
|
|
|
59 |
/* We are at the end of the pipeline, so recieve a value from
|
|
|
60 |
* our neightbor to the left, do a calculation, and print
|
|
|
61 |
* the final resulting value */
|
|
|
62 |
MPI_Recv (&temp, 1, MPI_FLOAT, myid-1, TAG_DATA, MPI_COMM_WORLD, &status);
|
|
|
63 |
temp += mya * pow (x, myid);
|
|
|
64 |
|
|
|
65 |
printf ("f = %e\n", temp);
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
MPI_Finalize ();
|
|
|
69 |
|
|
|
70 |
return 0;
|
|
|
71 |
}
|
|
|
72 |
|