Subversion Repositories programming

Rev

Rev 300 | Rev 304 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
294 ira 1
/*******************************************************************************
303 ira 2
 * proj1-parallel.c - implement a simple program that will find the minimum
3
 *                    and maximum numbers out of a randomly generated list of
4
 *                    integers, using MPI to parallelize the operation.
294 ira 5
 *
6
 * Copyright (c) 2006, Ira W. Snyder (devel@irasnyder.com)
7
 * All rights reserved.
8
 *
9
 * Permission is hereby granted, free of charge, to any person obtaining a copy
10
 * of this software and associated documentation files (the "Software"), to
11
 * deal in the Software without restriction, including without limitation the
12
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
13
 * sell copies of the Software, and to permit persons to whom the Software is
14
 * furnished to do so, subject to the following conditions:
15
 *
16
 * The above copyright notice and this permission notice shall be included in
17
 * all copies or substantial portions of the Software.
18
 *
19
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
25
 * IN THE SOFTWARE.
26
 ******************************************************************************/
27
 
303 ira 28
#include <limits.h>
294 ira 29
#include <stdio.h>
30
#include <stdlib.h>
31
#include <unistd.h>
32
 
303 ira 33
#include <mpi.h>
34
 
35
#define ROOT_NODE 0
36
#define TAG_MAX  0x1010
37
#define TAG_MIN  0x0101
38
#define TAG_SIZE 0x0808
39
#define TAG_DATA 0x0c0c
40
 
41
void find_max_and_min (const int *numbers, const int size, int *max, int *min)
42
{
43
    int i;
44
    *max = INT_MIN;
45
    *min = INT_MAX;
46
 
47
    for (i=0; i<size; i++)
48
    {
49
        if (numbers[i] > *max)
50
            *max = numbers[i];
51
 
52
        if (numbers[i] < *min)
53
            *min = numbers[i];
54
    }
55
}
56
 
294 ira 57
int main (int argc, char *argv[])
58
{
303 ira 59
    int numprocs, myid;
60
    int n = 6400;
294 ira 61
    int i;
62
 
303 ira 63
    int size;
294 ira 64
    int *numbers;
65
 
303 ira 66
    int max = INT_MIN;
67
    int min = INT_MAX;
294 ira 68
 
303 ira 69
    int temp;
70
 
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)
294 ira 78
    {
303 ira 79
        if ((numbers = (int*) malloc (n * sizeof(int))) == NULL)
294 ira 80
        {
303 ira 81
            puts ("Out of memory");
82
            return 1;
83
        }
294 ira 84
 
303 ira 85
        for (i=0; i<n; i++)
86
            numbers[i] = i;
294 ira 87
 
303 ira 88
        /* Calculate slice size */
89
        size = n/numprocs;
294 ira 90
 
303 ira 91
        /* Send out n to everyone */
92
        for (i=1; i<numprocs; i++)
93
            MPI_Send (&size, 1, MPI_INT, i, TAG_SIZE, MPI_COMM_WORLD);
94
 
95
        /* Send out data to everyone */
96
        for (i=1; i<numprocs; i++)
97
            MPI_Send (numbers+(size*i), size, MPI_INT, i, TAG_DATA, MPI_COMM_WORLD);
98
 
99
        /* Calculate my part of the data */
100
        find_max_and_min (numbers, size, &max, &min);
101
 
102
        /* Recieve data back */
103
        for (i=1; i<numprocs; i++)
104
        {
105
            /* Find actual max */
106
            MPI_Recv (&temp, 1, MPI_INT, i, TAG_MAX, MPI_COMM_WORLD, &status);
107
 
108
            if (temp > max)
109
                max = temp;
110
 
111
            /* Find actual min */
112
            MPI_Recv (&temp, 1, MPI_INT, i, TAG_MIN, MPI_COMM_WORLD, &status);
113
 
114
            if (temp < min)
115
                min = temp;
294 ira 116
        }
303 ira 117
 
118
        printf ("final max: %d\n", max);
119
        printf ("final min: %d\n", min);
294 ira 120
    }
303 ira 121
    else
122
    {
123
        /* Recieve size of the data */
124
        MPI_Recv (&size, 1, MPI_INT, ROOT_NODE, TAG_SIZE, MPI_COMM_WORLD, &status);
294 ira 125
 
303 ira 126
        /* Allocate memory to store the data */
127
        if ((numbers = (int*) malloc (size * sizeof(int))) == NULL)
128
        {
129
            puts ("Out of memory");
130
            return 1;
131
        }
294 ira 132
 
303 ira 133
        /* Recieve the data */
134
        MPI_Recv (numbers, size, MPI_INT, ROOT_NODE, TAG_DATA, MPI_COMM_WORLD, &status);
294 ira 135
 
303 ira 136
        /* Calculate the max and min of my part of the data */
137
        find_max_and_min (numbers, size, &max, &min);
294 ira 138
 
303 ira 139
        /* Send the max and min back to the root node */
140
        MPI_Send (&max, 1, MPI_INT, ROOT_NODE, TAG_MAX, MPI_COMM_WORLD);
141
        MPI_Send (&min, 1, MPI_INT, ROOT_NODE, TAG_MIN, MPI_COMM_WORLD);
294 ira 142
 
303 ira 143
        free (numbers);
294 ira 144
    }
145
 
303 ira 146
    MPI_Finalize ();
294 ira 147
 
148
    return 0;
149
}
150