Subversion Repositories programming

Rev

Blame | Last modification | View Log | RSS feed

/*******************************************************************************
 * prob1-div.c - a divide-and-conquer method to calculate a McLauran series
 ******************************************************************************/

#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>

int main (int argc, char *argv[])
{
    int myid, numprocs, slice_size, *a;
    float f, f_total;

    MPI_Init (&argc, &argv);
    MPI_Comm_rank (MPI_COMM_WORLD, &myid);
    MPI_Comm_size (MPI_COMM_WORLD, &numprocs);

    // allocate a here

    if (myid == 0)
    {
        // read all a[i]'s here
        // read x here
        // read n here
    }

    /* Find the slice size */
    slice_size = n/numprocs;

    /* Send everything needed to everyone */
    MPI_Scatter (a, slice_size, MPI_INT, a, slice_size, MPI_INT, 0, MPI_COMM_WORLD);
    MPI_Bcast (&n, 1, MPI_INT, 0, MPI_COMM_WORLD);
    MPI_Bcast (&x, 1, MPI_INT, 0, MPI_COMM_WORLD);

    /* Each process calculates it's piece */
    for (i=0; i<slice_size; i++)
        f += a[i] * pow (x, (myid*slice_size)+i);

    /* Reduce every process' individual sum onto the root */
    MPI_Reduce (&f, &f_total, 1, MPI_FLOAT, MPI_SUM, 0, MPI_COMM_WORLD);

    MPI_Finalize ();

    return 0;
}