Subversion Repositories programming

Rev

Rev 318 | Blame | Compare with Previous | Last modification | View Log | RSS feed

/*******************************************************************************
 * proj2-group.c - implement a simple program that will find the number of
 *                 occurrances of a random "seed" in a randomly generated
 *                 array of n numbers. This uses the group communication
 *                 routines of MPI to transfer the data.
 *
 * Copyright (c) 2006, Ira W. Snyder (devel@irasnyder.com)
 * All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 ******************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <limits.h>
#include <sys/timeb.h>

#include <mpi.h>

#define ROOT_NODE 0

#ifdef SOLARIS

    /* NOTE: Solaris is broken, and RAND_MAX is NOT the maximum size
     * number that random() outputs. Here is the correct value. */
    #define RAND_MAX LONG_MAX

#endif

int find_seed_occurrances (int *a, int asize, int seed)
{
    int i, count=0;

    for (i=0; i<asize; i++)
        if (a[i] == seed)
            count++;

    return count;
}

int main (int argc, char *argv[])
{
    int numprocs, myid, i;
    int n = -1;
    int c = -1;

    int size, seed, total, g_total;
    int *numbers;

    /* Declare timeb structs */
    struct timeb time1;
    struct timeb time2;

    MPI_Status status;

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

    if (myid == ROOT_NODE)
    {
        /* Parse the command line options */
        opterr = 0;

        while ((c = getopt (argc, argv, "n:")) != -1)
        {
            switch (c)
            {
                /* Try to get -n option */
                case 'n':
                    n = atoi (optarg);
                    break;

                /* Catch bad options */
                case '?':
                    if (isprint (optopt))
                        fprintf (stderr, "Unknown option '-%c'.\n", optopt);
                    else
                        fprintf (stderr, "Unknown option character '\\x%x'.\n", optopt);

                    return 1;
                    break;
                default:
                    abort ();
                    break;
            }
        }

        /* Check if the n value is valid */
        if (n<0)
        {
            fprintf (stderr, "Please specify the '-n NUMBER' option\n");
            return 1;
        }

        /* Make sure that we have the ability to calculate the answer */
        if (n % numprocs != 0)
        {
            fprintf (stderr, "n must be evenly divisble by numprocs\n");
            fprintf (stderr, "Please adjust -n and -np adequately\n");
            return 2;
        }

        /* Allocate memory for the random number array */
        if ((numbers = (int*) malloc (n * sizeof(int))) == NULL)
        {
            puts ("Out of memory");
            return 1;
        }

        /* Seed the random number generator */
        srandom (time(NULL));

        /* Generate random numbers, staying in the range [0,n) */
        for (i=0; i<n; i++)
            numbers[i] = (int) (n * (random() / (RAND_MAX + 1.0)));

        /* Generate the seed value */
        seed = (int) (n * (random() / (RAND_MAX + 1.0)));

        /* Calculate slice size */
        size = n/numprocs;
    }

    /* Start the clock */
    if (myid == ROOT_NODE)
        ftime (&time1);

    /* Broadcast size to all nodes */
    MPI_Bcast (&size, 1, MPI_INT, ROOT_NODE, MPI_COMM_WORLD);

    if (myid != ROOT_NODE)
    {
        if ((numbers = (int*) malloc (size * sizeof(int))) == NULL)
        {
            puts ("Out of memory");
            return 1;
        }
    }

    /* Scatter the numbers array to all of the children */
    MPI_Scatter (numbers, size, MPI_INT, numbers, size, MPI_INT, ROOT_NODE, MPI_COMM_WORLD);

    /* Broadcast x to all of the children */
    MPI_Bcast (&seed, 1, MPI_INT, ROOT_NODE, MPI_COMM_WORLD);

    total = find_seed_occurrances (numbers, size, seed);

    /* Sum everyone's results */
    MPI_Reduce (&total, &g_total, 1, MPI_INT, MPI_SUM, ROOT_NODE, MPI_COMM_WORLD);

    /* Stop the clock */
    if (myid == ROOT_NODE)
        ftime (&time2);

    if (myid == ROOT_NODE)
    {
        /* Print time taken */
        long time_taken = ((time2.time - time1.time) * 1000) + (time2.millitm - time1.millitm);
        printf ("Time taken: %d (milliseconds)\n", time_taken);

        /* Print the final status */
        printf ("Total occurrance of seed (%d) out of %d numbers: %d\n", seed, n, g_total);
    }

    free (numbers);
    MPI_Finalize ();

    return 0;
}