Subversion Repositories programming

Rev

Blame | Last modification | View Log | RSS feed

/*******************************************************************************
 * prob1-pipe.c - a pipeline method to calculate a McLauran series
 ******************************************************************************/

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

#define TAG_DATA 0xdddd

int main (int argc, char *argv[])
{
    int myid, numprocs;
    int *a, mya, x, n;
    float temp;

    MPI_Status status;

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

    if (myid == 0) /* root */
    {
        // allocate a
        // read a[i]'s
        // read n
        // read x
    }

    /* Do a sanity check.
     * We MUST have exactly enough processors available
     * to construct the pipeline. */
    if (numprocs != n)
        exit (1); // not enough processors

    /* Send everyone a single a value, and the x value */
    MPI_Scatter (a, 1, MPI_INT, &mya, 1, MPI_INT, 0, MPI_COMM_WORLD);
    MPI_Bcast (&x, 1, MPI_INT, 0, MPI_COMM_WORLD);

    if (myid == 0)
    {
        /* We are the root node, so do a single calculation, and then
         * forward the result on down the pipeline */
        temp = mya * pow (x, myid);
        MPI_Send (&temp, 1, MPI_FLOAT, myid+1, TAG_DATA, MPI_COMM_WORLD);
    }
    else if (myid < (numprocs - 1))
    {
        /* We are in the middle of the pipeline, so recieve a value from
         * our neighbor to the left, do a calculation, and pass the result
         * to our neighbor to the right */
        MPI_Recv (&temp, 1, MPI_FLOAT, myid-1, TAG_DATA, MPI_COMM_WORLD, &status);
        temp += mya * pow (x, myid);
        MPI_Send (&temp, 1, MPI_FLOAT, myid+1, TAG_DATA, MPI_COMM_WORLD);
    }
    else
    {
        /* We are at the end of the pipeline, so recieve a value from
         * our neightbor to the left, do a calculation, and print
         * the final resulting value */
        MPI_Recv (&temp, 1, MPI_FLOAT, myid-1, TAG_DATA, MPI_COMM_WORLD, &status);
        temp += mya * pow (x, myid);

        printf ("f = %e\n", temp);
    }

    MPI_Finalize ();

    return 0;
}