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