Subversion Repositories programming

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
318 ira 1
/*******************************************************************************
2
 * proj2-group.c - implement a simple program that will find the number of
3
 *                 occurrances of a random "seed" in a randomly generated
4
 *                 array of n numbers. This uses the group communication
5
 *                 routines of MPI to transfer the data.
6
 *
7
 * Copyright (c) 2006, Ira W. Snyder (devel@irasnyder.com)
8
 * All rights reserved.
9
 *
10
 * Permission is hereby granted, free of charge, to any person obtaining a copy
11
 * of this software and associated documentation files (the "Software"), to
12
 * deal in the Software without restriction, including without limitation the
13
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
14
 * sell copies of the Software, and to permit persons to whom the Software is
15
 * furnished to do so, subject to the following conditions:
16
 *
17
 * The above copyright notice and this permission notice shall be included in
18
 * all copies or substantial portions of the Software.
19
 *
20
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
26
 * IN THE SOFTWARE.
27
 ******************************************************************************/
28
 
29
#include <stdio.h>
30
#include <stdlib.h>
31
#include <unistd.h>
32
#include <limits.h>
33
 
34
#include <mpi.h>
35
 
36
#define ROOT_NODE 0
37
 
38
#ifdef SOLARIS
39
 
40
    /* NOTE: Solaris is broken, and RAND_MAX is NOT the maximum size
41
     * number that random() outputs. Here is the correct value. */
42
    #define RAND_MAX LONG_MAX
43
 
44
#endif
45
 
46
int find_seed_occurrances (int *a, int asize, int seed)
47
{
48
    int i, count=0;
49
 
50
    for (i=0; i<asize; i++)
51
        if (a[i] == seed)
52
            count++;
53
 
54
    return count;
55
}
56
 
57
int main (int argc, char *argv[])
58
{
59
    int numprocs, myid, i;
60
    int n = -1;
61
    int c = -1;
62
 
63
    int size, seed, total, g_total;
64
    int *numbers;
65
 
66
    MPI_Status status;
67
 
68
    MPI_Init (&argc, &argv);
69
    MPI_Comm_size (MPI_COMM_WORLD, &numprocs);
70
    MPI_Comm_rank (MPI_COMM_WORLD, &myid);
71
 
72
    if (myid == ROOT_NODE)
73
    {
74
        /* Parse the command line options */
75
        opterr = 0;
76
 
77
        while ((c = getopt (argc, argv, "n:")) != -1)
78
        {
79
            switch (c)
80
            {
81
                /* Try to get -n option */
82
                case 'n':
83
                    n = atoi (optarg);
84
                    break;
85
 
86
                /* Catch bad options */
87
                case '?':
88
                    if (isprint (optopt))
89
                        fprintf (stderr, "Unknown option '-%c'.\n", optopt);
90
                    else
91
                        fprintf (stderr, "Unknown option character '\\x%x'.\n", optopt);
92
 
93
                    return 1;
94
                    break;
95
                default:
96
                    abort ();
97
                    break;
98
            }
99
        }
100
 
101
        /* Check if the n value is valid */
102
        if (n<0)
103
        {
104
            fprintf (stderr, "Please specify the '-n NUMBER' option\n");
105
            return 1;
106
        }
107
 
108
        /* Make sure that we have the ability to calculate the answer */
109
        if (n % numprocs != 0)
110
        {
111
            fprintf (stderr, "n must be evenly divisble by numprocs\n");
112
            fprintf (stderr, "Please adjust -n and -np adequately\n");
113
            return 2;
114
        }
115
 
116
        /* Allocate memory for the random number array */
117
        if ((numbers = (int*) malloc (n * sizeof(int))) == NULL)
118
        {
119
            puts ("Out of memory");
120
            return 1;
121
        }
122
 
123
        /* Seed the random number generator */
124
        srandom (time(NULL));
125
 
126
        /* Generate random numbers, staying in the range [0,n) */
127
        for (i=0; i<n; i++)
128
            numbers[i] = (int) (n * (random() / (RAND_MAX + 1.0)));
129
 
130
        /* Generate the seed value */
131
        seed = (int) (n * (random() / (RAND_MAX + 1.0)));
132
 
133
        /* Calculate slice size */
134
        size = n/numprocs;
135
    }
136
 
137
    /* Broadcast size to all nodes */
138
    MPI_Bcast (&size, 1, MPI_INT, ROOT_NODE, MPI_COMM_WORLD);
139
 
140
    if (myid != ROOT_NODE)
141
    {
142
        if ((numbers = (int*) malloc (size * sizeof(int))) == NULL)
143
        {
144
            puts ("Out of memory");
145
            return 1;
146
        }
147
    }
148
 
149
    /* Scatter the numbers array to all of the children */
150
    MPI_Scatter (numbers, size, MPI_INT, numbers, size, MPI_INT, ROOT_NODE, MPI_COMM_WORLD);
151
 
152
    /* Broadcast x to all of the children */
153
    MPI_Bcast (&seed, 1, MPI_INT, ROOT_NODE, MPI_COMM_WORLD);
154
 
155
    total = find_seed_occurrances (numbers, size, seed);
156
 
157
    /* Sum everyone's results */
158
    MPI_Reduce (&total, &g_total, 1, MPI_INT, MPI_SUM, ROOT_NODE, MPI_COMM_WORLD);
159
 
160
    if (myid == ROOT_NODE)
161
    {
162
        /* Print the final status */
163
        printf ("Total occurrance of seed (%d) out of %d numbers: %d\n", seed, n, g_total);
164
    }
165
 
166
    free (numbers);
167
    MPI_Finalize ();
168
 
169
    return 0;
170
}
171