Subversion Repositories programming

Rev

Rev 304 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 304 Rev 305
Line 52... Line 52...
52
        if (numbers[i] < *min)
52
        if (numbers[i] < *min)
53
            *min = numbers[i];
53
            *min = numbers[i];
54
    }
54
    }
55
}
55
}
56
 
56
 
-
 
57
void print_local_max_and_min (const int proc_id, const int max, const int min)
-
 
58
{
-
 
59
    printf ("Local max for process %d: %d\n", proc_id, max);
-
 
60
    printf ("Local min for process %d: %d\n", proc_id, min);
-
 
61
}
-
 
62
 
57
int main (int argc, char *argv[])
63
int main (int argc, char *argv[])
58
{
64
{
59
    int numprocs, myid, i;
65
    int numprocs, myid, i;
60
    int n = -1;
66
    int n = -1;
61
    int c = -1;
67
    int c = -1;
Line 64... Line 70...
64
    int *numbers;
70
    int *numbers;
65
 
71
 
66
    int max = INT_MIN;
72
    int max = INT_MIN;
67
    int min = INT_MAX;
73
    int min = INT_MAX;
68
 
74
 
69
    int temp;
75
    int temp_max, temp_min;
70
 
76
 
71
    MPI_Status status;
77
    MPI_Status status;
72
 
78
 
73
    MPI_Init (&argc, &argv);
79
    MPI_Init (&argc, &argv);
74
    MPI_Comm_size (MPI_COMM_WORLD, &numprocs);
80
    MPI_Comm_size (MPI_COMM_WORLD, &numprocs);
Line 100... Line 106...
100
                default:
106
                default:
101
                    abort ();
107
                    abort ();
102
            }
108
            }
103
        }
109
        }
104
 
110
 
105
        /* Seed the random number generator */
-
 
106
        srand (time(NULL));
-
 
107
 
-
 
108
        /* Check if the n value is valid */
111
        /* Check if the n value is valid */
109
        if (n<=0)
112
        if (n<=0)
110
        {
113
        {
111
            fprintf (stderr, "Bad value for n, must be greater than 0\n");
114
            fprintf (stderr, "Bad value for n, must be greater than 0\n");
112
            fprintf (stderr, "Please specify the '-n NUMBER' option\n");
115
            fprintf (stderr, "Please specify the '-n NUMBER' option\n");
113
            return 1;
116
            return 1;
114
        }
117
        }
115
 
118
 
-
 
119
        /* Make sure that we have the ability to calculate the answer */
-
 
120
        if (n % numprocs != 0)
-
 
121
        {
-
 
122
            fprintf (stderr, "n must be evenly divisble by numprocs\n");
-
 
123
            fprintf (stderr, "Please adjust -n and -np adequately\n");
-
 
124
            return 2;
-
 
125
        }
-
 
126
 
116
        /* Allocate memory for the random number array */
127
        /* Allocate memory for the random number array */
117
        if ((numbers = (int*) malloc (n * sizeof(int))) == NULL)
128
        if ((numbers = (int*) malloc (n * sizeof(int))) == NULL)
118
        {
129
        {
119
            puts ("Out of memory");
130
            puts ("Out of memory");
120
            return 1;
131
            return 1;
121
        }
132
        }
122
 
133
 
-
 
134
        /* Seed the random number generator */
-
 
135
        srand (time(NULL));
-
 
136
 
123
        /* Generate random numbers, staying in the range [0,10n) */
137
        /* Generate random numbers, staying in the range [0,10n) */
124
        for (i=0; i<n; i++)
138
        for (i=0; i<n; i++)
125
            numbers[i] = (int) ((n*10.0) * rand() / (RAND_MAX + 1.0));
139
            numbers[i] = (int) ((n*10.0) * rand() / (RAND_MAX + 1.0));
126
 
140
 
127
        /* Calculate slice size */
141
        /* Calculate slice size */
Line 133... Line 147...
133
 
147
 
134
        /* Send out data to everyone */
148
        /* Send out data to everyone */
135
        for (i=1; i<numprocs; i++)
149
        for (i=1; i<numprocs; i++)
136
            MPI_Send (numbers+(size*i), size, MPI_INT, i, TAG_DATA, MPI_COMM_WORLD);
150
            MPI_Send (numbers+(size*i), size, MPI_INT, i, TAG_DATA, MPI_COMM_WORLD);
137
 
151
 
-
 
152
        /* Print some information */
-
 
153
        printf ("Size of the List: %d\n", n);
-
 
154
        printf ("No. of processes: %d\n\n", numprocs);
-
 
155
 
138
        /* Calculate my part of the data */
156
        /* Calculate my part of the data */
139
        find_max_and_min (numbers, size, &max, &min);
157
        find_max_and_min (numbers, size, &max, &min);
140
 
158
 
-
 
159
        print_local_max_and_min (myid, max, min);
-
 
160
 
141
        /* Recieve data back */
161
        /* Recieve data back */
142
        for (i=1; i<numprocs; i++)
162
        for (i=1; i<numprocs; i++)
143
        {
163
        {
144
            /* Find actual max */
164
            /* Find actual max */
145
            MPI_Recv (&temp, 1, MPI_INT, i, TAG_MAX, MPI_COMM_WORLD, &status);
165
            MPI_Recv (&temp_max, 1, MPI_INT, i, TAG_MAX, MPI_COMM_WORLD, &status);
146
 
166
            
147
            if (temp > max)
167
            if (temp_max > max)
148
                max = temp;
168
                max = temp_max;
149
 
169
 
150
            /* Find actual min */
170
            /* Find actual min */
151
            MPI_Recv (&temp, 1, MPI_INT, i, TAG_MIN, MPI_COMM_WORLD, &status);
171
            MPI_Recv (&temp_min, 1, MPI_INT, i, TAG_MIN, MPI_COMM_WORLD, &status);
152
 
172
 
153
            if (temp < min)
173
            if (temp_min < min)
154
                min = temp;
174
                min = temp_min;
-
 
175
 
-
 
176
            print_local_max_and_min (i, temp_max, temp_min);
155
        }
177
        }
156
 
178
 
157
        printf ("final max: %d\n", max);
179
        printf ("\nOverall max: %d\n", max);
158
        printf ("final min: %d\n", min);
180
        printf ("Overall min: %d\n", min);
159
    }
181
    }
160
    else // not ROOT_NODE
182
    else // not ROOT_NODE
161
    {
183
    {
162
        /* Recieve size of the data */
184
        /* Recieve size of the data */
163
        MPI_Recv (&size, 1, MPI_INT, ROOT_NODE, TAG_SIZE, MPI_COMM_WORLD, &status);
185
        MPI_Recv (&size, 1, MPI_INT, ROOT_NODE, TAG_SIZE, MPI_COMM_WORLD, &status);
Line 176... Line 198...
176
        find_max_and_min (numbers, size, &max, &min);
198
        find_max_and_min (numbers, size, &max, &min);
177
 
199
 
178
        /* Send the max and min back to the root node */
200
        /* Send the max and min back to the root node */
179
        MPI_Send (&max, 1, MPI_INT, ROOT_NODE, TAG_MAX, MPI_COMM_WORLD);
201
        MPI_Send (&max, 1, MPI_INT, ROOT_NODE, TAG_MAX, MPI_COMM_WORLD);
180
        MPI_Send (&min, 1, MPI_INT, ROOT_NODE, TAG_MIN, MPI_COMM_WORLD);
202
        MPI_Send (&min, 1, MPI_INT, ROOT_NODE, TAG_MIN, MPI_COMM_WORLD);
181
 
-
 
182
        free (numbers);
-
 
183
    }
203
    }
184
 
204
 
-
 
205
    free (numbers);
185
    MPI_Finalize ();
206
    MPI_Finalize ();
186
 
207
 
187
    return 0;
208
    return 0;
188
}
209
}
189
 
210