Subversion Repositories programming

Rev

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

Rev Author Line No. Line
319 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
#include <sys/timeb.h>
34
 
35
#include <mpi.h>
36
 
37
#define ROOT_NODE 0
38
#define TAG_SIZE  0xaaaa
39
#define TAG_DATA  0xbbbb
40
#define TAG_SEED  0xcccc
41
#define TAG_TOTAL 0xdddd
42
 
43
#ifdef SOLARIS
44
 
45
    /* NOTE: Solaris is broken, and RAND_MAX is NOT the maximum size
46
     * number that random() outputs. Here is the correct value. */
47
    #define RAND_MAX LONG_MAX
48
 
49
#endif
50
 
51
int find_seed_occurrances (int *a, int asize, int seed)
52
{
53
    int i, count=0;
54
 
55
    for (i=0; i<asize; i++)
56
        if (a[i] == seed)
57
            count++;
58
 
59
    return count;
60
}
61
 
62
int main (int argc, char *argv[])
63
{
64
    int numprocs, myid, i;
65
    int n = -1;
66
    int c = -1;
67
 
68
    int size, seed, total, g_total;
69
    int *numbers;
70
 
71
    /* Declare timeb structs */
72
    struct timeb time1;
73
    struct timeb time2;
74
 
75
    MPI_Status status;
76
 
77
    MPI_Init (&argc, &argv);
78
    MPI_Comm_size (MPI_COMM_WORLD, &numprocs);
79
    MPI_Comm_rank (MPI_COMM_WORLD, &myid);
80
 
81
    if (myid == ROOT_NODE)
82
    {
83
        /* Parse the command line options */
84
        opterr = 0;
85
 
86
        while ((c = getopt (argc, argv, "n:")) != -1)
87
        {
88
            switch (c)
89
            {
90
                /* Try to get -n option */
91
                case 'n':
92
                    n = atoi (optarg);
93
                    break;
94
 
95
                /* Catch bad options */
96
                case '?':
97
                    if (isprint (optopt))
98
                        fprintf (stderr, "Unknown option '-%c'.\n", optopt);
99
                    else
100
                        fprintf (stderr, "Unknown option character '\\x%x'.\n", optopt);
101
 
102
                    return 1;
103
                    break;
104
                default:
105
                    abort ();
106
                    break;
107
            }
108
        }
109
 
110
        /* Check if the n value is valid */
111
        if (n<0)
112
        {
113
            fprintf (stderr, "Please specify the '-n NUMBER' option\n");
114
            return 1;
115
        }
116
 
117
        /* Make sure that we have the ability to calculate the answer */
118
        if (n % numprocs != 0)
119
        {
120
            fprintf (stderr, "n must be evenly divisble by numprocs\n");
121
            fprintf (stderr, "Please adjust -n and -np adequately\n");
122
            return 2;
123
        }
124
 
125
        /* Allocate memory for the random number array */
126
        if ((numbers = (int*) malloc (n * sizeof(int))) == NULL)
127
        {
128
            puts ("Out of memory");
129
            return 1;
130
        }
131
 
132
        /* Seed the random number generator */
133
        srandom (time(NULL));
134
 
135
        /* Generate random numbers, staying in the range [0,n) */
136
        for (i=0; i<n; i++)
137
            numbers[i] = (int) (n * (random() / (RAND_MAX + 1.0)));
138
 
139
        /* Generate the seed value */
140
        seed = (int) (n * (random() / (RAND_MAX + 1.0)));
141
 
142
        /* Calculate slice size */
143
        size = n/numprocs;
144
    }
145
 
146
    /* Start the clock */
147
    if (myid == ROOT_NODE)
148
        ftime (&time1);
149
 
150
    /* Broadcast size to all nodes */
151
    //MPI_Bcast (&size, 1, MPI_INT, ROOT_NODE, MPI_COMM_WORLD);
152
    if (myid == ROOT_NODE)
153
    {
154
        for (i=1; i<numprocs; i++)
155
            MPI_Send (&size, 1, MPI_INT, i, TAG_SIZE, MPI_COMM_WORLD);
156
    }
157
    else
158
    {
159
        MPI_Recv (&size, 1, MPI_INT, ROOT_NODE, TAG_SIZE, MPI_COMM_WORLD, &status);
160
    }
161
 
162
    if (myid != ROOT_NODE)
163
    {
164
        if ((numbers = (int*) malloc (size * sizeof(int))) == NULL)
165
        {
166
            puts ("Out of memory");
167
            return 1;
168
        }
169
    }
170
 
171
    /* Scatter the numbers array to all of the children */
172
    //MPI_Scatter (numbers, size, MPI_INT, numbers, size, MPI_INT, ROOT_NODE, MPI_COMM_WORLD);
173
    if (myid == ROOT_NODE)
174
    {
175
        for (i=1; i<numprocs; i++)
176
            MPI_Send (numbers+(i*size), size, MPI_INT, i, TAG_DATA, MPI_COMM_WORLD);
177
    }
178
    else
179
    {
180
        MPI_Recv (numbers, size, MPI_INT, ROOT_NODE, TAG_DATA, MPI_COMM_WORLD, &status);
181
    }
182
 
183
    /* Broadcast x to all of the children */
184
    //MPI_Bcast (&seed, 1, MPI_INT, ROOT_NODE, MPI_COMM_WORLD);
185
    if (myid == ROOT_NODE)
186
    {
187
        for (i=1; i<numprocs; i++)
188
            MPI_Send (&seed, 1, MPI_INT, i, TAG_SEED, MPI_COMM_WORLD);
189
    }
190
    else
191
    {
192
        MPI_Recv (&seed, 1, MPI_INT, ROOT_NODE, TAG_SEED, MPI_COMM_WORLD, &status);
193
    }
194
 
195
    total = find_seed_occurrances (numbers, size, seed);
196
 
197
    /* Sum everyone's results */
198
    //MPI_Reduce (&total, &g_total, 1, MPI_INT, MPI_SUM, ROOT_NODE, MPI_COMM_WORLD);
199
    if (myid == ROOT_NODE)
200
    {
201
        g_total = total;
202
 
203
        for (i=1; i<numprocs; i++)
204
        {
205
            MPI_Recv (&total, 1, MPI_INT, i, TAG_TOTAL, MPI_COMM_WORLD, &status);
206
            g_total += total;
207
        }
208
    }
209
    else
210
    {
211
        MPI_Send (&total, 1, MPI_INT, ROOT_NODE, TAG_TOTAL, MPI_COMM_WORLD);
212
    }
213
 
214
 
215
    /* Stop the clock */
216
    if (myid == ROOT_NODE)
217
        ftime (&time2);
218
 
219
    if (myid == ROOT_NODE)
220
    {
221
        /* Print time taken */
222
        long time_taken = ((time2.time - time1.time) * 1000) + (time2.millitm - time1.millitm);
223
        printf ("Time taken: %d (milliseconds)\n", time_taken);
224
 
225
        /* Print the final status */
226
        printf ("Total occurrance of seed (%d) out of %d numbers: %d\n", seed, n, g_total);
227
    }
228
 
229
    free (numbers);
230
    MPI_Finalize ();
231
 
232
    return 0;
233
}
234