Subversion Repositories programming

Rev

Rev 319 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
319 ira 1
/*******************************************************************************
320 ira 2
 * proj2-normal.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 regular synchronous
5
 *                  communication routines of MPI to transfer the data.
319 ira 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
 
320 ira 145
        /* Start the clock */
319 ira 146
        ftime (&time1);
147
 
320 ira 148
        /* Broadcast size to all nodes */
319 ira 149
        for (i=1; i<numprocs; i++)
150
            MPI_Send (&size, 1, MPI_INT, i, TAG_SIZE, MPI_COMM_WORLD);
151
 
320 ira 152
        /* Scatter the numbers array to all of the children */
319 ira 153
        for (i=1; i<numprocs; i++)
154
            MPI_Send (numbers+(i*size), size, MPI_INT, i, TAG_DATA, MPI_COMM_WORLD);
155
 
320 ira 156
        /* Broadcast x to all of the children */
319 ira 157
        for (i=1; i<numprocs; i++)
158
            MPI_Send (&seed, 1, MPI_INT, i, TAG_SEED, MPI_COMM_WORLD);
159
 
320 ira 160
        /* Calculate my part of the total */
161
        total = find_seed_occurrances (numbers, size, seed);
319 ira 162
 
320 ira 163
        /* Sum everyone's results */
319 ira 164
        g_total = total;
165
 
166
        for (i=1; i<numprocs; i++)
167
        {
168
            MPI_Recv (&total, 1, MPI_INT, i, TAG_TOTAL, MPI_COMM_WORLD, &status);
169
            g_total += total;
170
        }
171
 
320 ira 172
        /* Stop the clock */
319 ira 173
        ftime (&time2);
174
 
175
        /* Print time taken */
176
        long time_taken = ((time2.time - time1.time) * 1000) + (time2.millitm - time1.millitm);
177
        printf ("Time taken: %d (milliseconds)\n", time_taken);
178
 
179
        /* Print the final status */
180
        printf ("Total occurrance of seed (%d) out of %d numbers: %d\n", seed, n, g_total);
181
    }
320 ira 182
    else
183
    {
184
        /* Recieve the size broadcast */
185
        MPI_Recv (&size, 1, MPI_INT, ROOT_NODE, TAG_SIZE, MPI_COMM_WORLD, &status);
319 ira 186
 
320 ira 187
        /* Allocate memory for numbers, based on size */
188
        if ((numbers = (int*) malloc (size * sizeof(int))) == NULL)
189
        {
190
            puts ("Out of memory");
191
            return 1;
192
        }
193
 
194
        /* Recieve the numbers array */
195
        MPI_Recv (numbers, size, MPI_INT, ROOT_NODE, TAG_DATA, MPI_COMM_WORLD, &status);
196
 
197
        /* Recieve the seed */
198
        MPI_Recv (&seed, 1, MPI_INT, ROOT_NODE, TAG_SEED, MPI_COMM_WORLD, &status);
199
 
200
        /* Calculate my part of the total */
201
        total = find_seed_occurrances (numbers, size, seed);
202
 
203
        /* Send my part of the results back to the root */
204
        MPI_Send (&total, 1, MPI_INT, ROOT_NODE, TAG_TOTAL, MPI_COMM_WORLD);
205
    }
206
 
319 ira 207
    free (numbers);
208
    MPI_Finalize ();
209
 
210
    return 0;
211
}
212