Subversion Repositories programming

Rev

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

Rev 319 Rev 320
Line 1... Line 1...
1
/*******************************************************************************
1
/*******************************************************************************
2
 * proj2-group.c - implement a simple program that will find the number of
2
 * proj2-normal.c - implement a simple program that will find the number of
3
 *                 occurrances of a random "seed" in a randomly generated
3
 *                  occurrances of a random "seed" in a randomly generated
4
 *                 array of n numbers. This uses the group communication
4
 *                  array of n numbers. This uses the regular synchronous
5
 *                 routines of MPI to transfer the data.
5
 *                  communication routines of MPI to transfer the data.
6
 *
6
 *
7
 * Copyright (c) 2006, Ira W. Snyder (devel@irasnyder.com)
7
 * Copyright (c) 2006, Ira W. Snyder (devel@irasnyder.com)
8
 * All rights reserved.
8
 * All rights reserved.
9
 *
9
 *
10
 * Permission is hereby granted, free of charge, to any person obtaining a copy
10
 * Permission is hereby granted, free of charge, to any person obtaining a copy
Line 139... Line 139...
139
        /* Generate the seed value */
139
        /* Generate the seed value */
140
        seed = (int) (n * (random() / (RAND_MAX + 1.0)));
140
        seed = (int) (n * (random() / (RAND_MAX + 1.0)));
141
 
141
 
142
        /* Calculate slice size */
142
        /* Calculate slice size */
143
        size = n/numprocs;
143
        size = n/numprocs;
144
    }
-
 
145
 
144
 
146
    /* Start the clock */
145
        /* Start the clock */
147
    if (myid == ROOT_NODE)
-
 
148
        ftime (&time1);
146
        ftime (&time1);
149
 
147
 
150
    /* Broadcast size to all nodes */
148
        /* 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++)
149
        for (i=1; i<numprocs; i++)
155
            MPI_Send (&size, 1, MPI_INT, i, TAG_SIZE, MPI_COMM_WORLD);
150
            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
 
151
 
171
    /* Scatter the numbers array to all of the children */
152
        /* 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++)
153
        for (i=1; i<numprocs; i++)
176
            MPI_Send (numbers+(i*size), size, MPI_INT, i, TAG_DATA, MPI_COMM_WORLD);
154
            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
 
155
 
183
    /* Broadcast x to all of the children */
156
        /* 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++)
157
        for (i=1; i<numprocs; i++)
188
            MPI_Send (&seed, 1, MPI_INT, i, TAG_SEED, MPI_COMM_WORLD);
158
            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
 
159
 
-
 
160
        /* Calculate my part of the total */
195
    total = find_seed_occurrances (numbers, size, seed);
161
        total = find_seed_occurrances (numbers, size, seed);
196
 
162
 
197
    /* Sum everyone's results */
163
        /* 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;
164
        g_total = total;
202
 
165
 
203
        for (i=1; i<numprocs; i++)
166
        for (i=1; i<numprocs; i++)
204
        {
167
        {
205
            MPI_Recv (&total, 1, MPI_INT, i, TAG_TOTAL, MPI_COMM_WORLD, &status);
168
            MPI_Recv (&total, 1, MPI_INT, i, TAG_TOTAL, MPI_COMM_WORLD, &status);
206
            g_total += total;
169
            g_total += total;
207
        }
170
        }
208
    }
-
 
209
    else
-
 
210
    {
-
 
211
        MPI_Send (&total, 1, MPI_INT, ROOT_NODE, TAG_TOTAL, MPI_COMM_WORLD);
-
 
212
    }
-
 
213
 
171
 
214
 
-
 
215
    /* Stop the clock */
172
        /* Stop the clock */
216
    if (myid == ROOT_NODE)
-
 
217
        ftime (&time2);
173
        ftime (&time2);
218
 
174
 
219
    if (myid == ROOT_NODE)
-
 
220
    {
-
 
221
        /* Print time taken */
175
        /* Print time taken */
222
        long time_taken = ((time2.time - time1.time) * 1000) + (time2.millitm - time1.millitm);
176
        long time_taken = ((time2.time - time1.time) * 1000) + (time2.millitm - time1.millitm);
223
        printf ("Time taken: %d (milliseconds)\n", time_taken);
177
        printf ("Time taken: %d (milliseconds)\n", time_taken);
224
 
178
 
225
        /* Print the final status */
179
        /* Print the final status */
226
        printf ("Total occurrance of seed (%d) out of %d numbers: %d\n", seed, n, g_total);
180
        printf ("Total occurrance of seed (%d) out of %d numbers: %d\n", seed, n, g_total);
227
    }
181
    }
-
 
182
    else
-
 
183
    {
-
 
184
        /* Recieve the size broadcast */
-
 
185
        MPI_Recv (&size, 1, MPI_INT, ROOT_NODE, TAG_SIZE, MPI_COMM_WORLD, &status);
-
 
186
 
-
 
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
    }
228
 
206
 
229
    free (numbers);
207
    free (numbers);
230
    MPI_Finalize ();
208
    MPI_Finalize ();
231
 
209
 
232
    return 0;
210
    return 0;