316 |
ira |
1 |
/*******************************************************************************
|
|
|
2 |
* prob1-div.c - a divide-and-conquer method to calculate a McLauran series
|
|
|
3 |
******************************************************************************/
|
|
|
4 |
|
|
|
5 |
#include <mpi.h>
|
|
|
6 |
#include <stdio.h>
|
|
|
7 |
#include <stdlib.h>
|
|
|
8 |
|
|
|
9 |
int main (int argc, char *argv[])
|
|
|
10 |
{
|
|
|
11 |
int myid, numprocs, slice_size, *a;
|
|
|
12 |
float f, f_total;
|
|
|
13 |
|
|
|
14 |
MPI_Init (&argc, &argv);
|
|
|
15 |
MPI_Comm_rank (MPI_COMM_WORLD, &myid);
|
|
|
16 |
MPI_Comm_size (MPI_COMM_WORLD, &numprocs);
|
|
|
17 |
|
|
|
18 |
// allocate a here
|
|
|
19 |
|
|
|
20 |
if (myid == 0)
|
|
|
21 |
{
|
|
|
22 |
// read all a[i]'s here
|
|
|
23 |
// read x here
|
|
|
24 |
// read n here
|
|
|
25 |
}
|
|
|
26 |
|
|
|
27 |
/* Find the slice size */
|
|
|
28 |
slice_size = n/numprocs;
|
|
|
29 |
|
|
|
30 |
/* Send everything needed to everyone */
|
|
|
31 |
MPI_Scatter (a, slice_size, MPI_INT, a, slice_size, MPI_INT, 0, MPI_COMM_WORLD);
|
|
|
32 |
MPI_Bcast (&n, 1, MPI_INT, 0, MPI_COMM_WORLD);
|
|
|
33 |
MPI_Bcast (&x, 1, MPI_INT, 0, MPI_COMM_WORLD);
|
|
|
34 |
|
|
|
35 |
/* Each process calculates it's piece */
|
|
|
36 |
for (i=0; i<slice_size; i++)
|
|
|
37 |
f += a[i] * pow (x, (myid*slice_size)+i);
|
|
|
38 |
|
|
|
39 |
/* Reduce every process' individual sum onto the root */
|
|
|
40 |
MPI_Reduce (&f, &f_total, 1, MPI_FLOAT, MPI_SUM, 0, MPI_COMM_WORLD);
|
|
|
41 |
|
|
|
42 |
MPI_Finalize ();
|
|
|
43 |
|
|
|
44 |
return 0;
|
|
|
45 |
}
|
|
|
46 |
|