Subversion Repositories programming

Rev

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

Rev Author Line No. Line
327 ira 1
/* proj3_5.c - implements the APSP algorithm for a matrix of size 5x5
2
 *             as provided in the project description.
3
 *
4
 * Copyright (c) 2006, Ira W. Snyder (devel@irasnyder.com)
5
 */
6
 
7
 
322 ira 8
#include <stdio.h>
9
#include <limits.h>
10
#include <mpi.h>
11
 
12
const int ROOT_NODE = 0;
325 ira 13
int numprocs;
14
int myid;
322 ira 15
 
16
/**
17
 * NOTE: Accessing "matrices" in this program is a little strange.
18
 * I decided to implement matrices using an array. To get to a[row][col],
19
 * you would use a[row*a_size+col]. Pretty simple actually.
20
 */
21
 
22
int* allocate (int rows, int cols)
23
{
24
    int *temp = NULL;
25
 
26
    if ((temp = (int*) malloc (rows * cols * sizeof(int))) == NULL)
27
        printf ("Could not allocate memory for array of size: %d x %d\n", rows, cols);
28
 
29
    return temp;
30
}
31
 
325 ira 32
void print_matrix (int *b, int n, const char *s)
33
{
34
    int i,j;
35
 
36
    if (n<=10) // n<=10
37
    {
38
        printf ("%s\n", s);
39
 
40
        for (i=0; i<n; i++)
41
        {
42
            for (j=0; j<n; j++)
43
                printf ("%d\t", b[i*n+j]);
44
 
45
            printf ("\n");
46
        }
47
        printf ("\n");
48
    }
49
    else if (n<=100) // 10<n<=100
50
    {
51
        printf ("%s\n", s);
52
 
53
        // First 2 rows
54
        for (i=0; i<2; i++)
55
        {
56
            for (j=0; j<n; j++)
57
                printf ("%d\t", b[i*n+j]);
58
 
59
            printf ("\n");
60
        }
61
        printf ("\n");
62
 
63
        // First 2 cols
64
        for (i=0; i<n; i++)
65
        {
66
            for (j=0; j<2; j++)
67
                printf ("%d\t", b[i*n+j]);
68
 
69
            printf ("\n");
70
        }
71
    }
72
}
73
 
74
void matrix_mult (int *b, int *c, int n)
75
{
76
    int i,j,k,ii,temp;
77
 
78
    /* Multiply our slice of the matrix, store it in c */
79
    for (ii=0; ii<n/numprocs; ii++)
80
    {
81
        i = ii + myid * n/numprocs; // i is the offset into b
82
 
83
        for (j=0; j<n; j++)
84
        {
85
            // MATRIX_MULT
86
            // c[i*n+j]=0;
87
 
88
            // SHORTEST PATH
89
            c[ii*n+j] = INT_MAX;
90
 
91
            for (k=0; k<n; k++)
92
            {
93
                // MATRIX MULT
94
                // c[i*n+j] += a[i*n+k] * b[k*n+j];
95
 
96
                // SHORTEST PATH
97
                if (b[i*n+k] == INT_MAX || b[k*n+j] == INT_MAX)
98
                    temp = INT_MAX;
99
                else
100
                    temp = b[i*n+k] + b[k*n+j];
101
 
102
                if (temp < c[ii*n+j])
103
                    c[ii*n+j] = temp;
104
            }
105
        }
106
    }
107
}
108
 
322 ira 109
int main (int argc, char *argv[])
110
{
327 ira 111
    const int n = 5;
325 ira 112
    int *b, *c;
113
    int i, j, k, temp, t;
114
    MPI_Status status;
322 ira 115
 
116
    /* Get all of the necessary info for each process */
117
    MPI_Init (&argc, &argv);
118
    MPI_Comm_rank (MPI_COMM_WORLD, &myid);
119
    MPI_Comm_size (MPI_COMM_WORLD, &numprocs);
120
 
326 ira 121
    /* Do a quick sanity check for the correct number of processors */
122
    if (n % numprocs != 0)
123
    {
124
        if (myid == 0)
125
        {
126
            printf ("%d mod %d must be zero!\n", n, numprocs);
127
            printf ("You should probably change the -np parameter\n");
128
        }
129
 
130
        MPI_Finalize ();
131
        return 1;
132
    }
133
 
322 ira 134
    /* Allocate all of b for each process, since we'll need it */
135
    b = allocate (n, n);
136
 
137
    if (myid == ROOT_NODE)
138
    {
325 ira 139
        /* Allocate the full c, since we need to initialize it */
322 ira 140
        c = allocate (n, n);
141
 
142
        /* Initialize values */
143
        for (i=0; i<n; i++)
144
            for (j=0; j<n; j++)
324 ira 145
                b[i*n+j] = INT_MAX;
146
 
327 ira 147
        b[1] = 5;
148
        b[2] = 2;
323 ira 149
 
327 ira 150
        b[8] = 8;
324 ira 151
 
327 ira 152
        b[11] = 1;
324 ira 153
 
327 ira 154
        b[19] = 9;
324 ira 155
 
327 ira 156
        b[20] = 6;
157
        b[22] = 2;
158
 
159
        /* Force b[i][i] = 0 */
160
        for (i=0; i<n; i++)
161
            b[i*n+i] = 0; 
162
 
325 ira 163
        if (n<=100)
164
            print_matrix (b, n, "Input Matrix:");
165
        else
166
        {
167
            printf ("Beginning calculation for all shortest paths\n");
168
            printf ("for a graph with %d nodes\n", n);
169
        }
170
 
322 ira 171
    }
172
    else
173
    {
325 ira 174
        /* Allocate our slice of c, since that's all we need */
322 ira 175
        c = allocate (n/numprocs, n);
176
    }
177
 
325 ira 178
    /* Send all of b to each process, since they need all of it */
322 ira 179
    MPI_Bcast (b, n*n, MPI_INT, ROOT_NODE, MPI_COMM_WORLD);
180
 
325 ira 181
    /* Run our piece of the calculation.
182
     * Synchronize data with all other processes at the end of each step. */
183
    for (t=0; t<ceil(log(n-1)); t++)
322 ira 184
    {
325 ira 185
        matrix_mult (b, c, n);
186
        MPI_Allgather (c, n*n/numprocs, MPI_INT, b, n*n/numprocs, MPI_INT, MPI_COMM_WORLD);
322 ira 187
    }
188
 
325 ira 189
    /* Print out the result matrix. */
322 ira 190
    if (myid == ROOT_NODE)
191
    {
325 ira 192
        if (n<=100)
193
            print_matrix (b, n, "Result Matrix:");
194
        else
323 ira 195
        {
325 ira 196
            printf ("Finished calculation for all shortest paths\n");
197
            printf ("for a graph with %d nodes\n", n);
323 ira 198
        }
322 ira 199
    }
200
 
201
    /* Free all of the memory we allocated earlier */
202
    free (b);
203
    free (c);
204
 
205
    MPI_Finalize ();
206
    return 0;
207
}
208