Subversion Repositories programming

Rev

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

Rev 326 Rev 327
Line -... Line 1...
-
 
1
/* proj3_random.c - implements the APSP algorithm for a matrix of size nxn
-
 
2
 *                  as provided in the project description, randomly generating
-
 
3
 *                  the weight matrix.
-
 
4
 *
-
 
5
 * Copyright (c) 2006, Ira W. Snyder (devel@irasnyder.com)
-
 
6
 */
-
 
7
 
-
 
8
 
1
#include <stdio.h>
9
#include <stdio.h>
-
 
10
#include <stdlib.h>
2
#include <limits.h>
11
#include <limits.h>
3
#include <mpi.h>
12
#include <mpi.h>
-
 
13
#include <unistd.h>
4
 
14
 
5
const int ROOT_NODE = 0;
15
const int ROOT_NODE = 0;
6
int numprocs;
16
int numprocs;
7
int myid;
17
int myid;
8
 
18
 
-
 
19
#ifdef SOLARIS
-
 
20
 
-
 
21
    /* NOTE: Solaris is broken, and RAND_MAX is NOT the maximum size
-
 
22
     * number that random() outputs. Here is the correct value. */
-
 
23
    #define RAND_MAX LONG_MAX
-
 
24
 
-
 
25
#endif
-
 
26
 
9
/**
27
/**
10
 * NOTE: Accessing "matrices" in this program is a little strange.
28
 * NOTE: Accessing "matrices" in this program is a little strange.
11
 * I decided to implement matrices using an array. To get to a[row][col],
29
 * I decided to implement matrices using an array. To get to a[row][col],
12
 * you would use a[row*a_size+col]. Pretty simple actually.
30
 * you would use a[row*a_size+col]. Pretty simple actually.
13
 */
31
 */
Line 99... Line 117...
99
    }
117
    }
100
}
118
}
101
 
119
 
102
int main (int argc, char *argv[])
120
int main (int argc, char *argv[])
103
{
121
{
104
    const int n = 4;
122
    const int n = 10;
105
    int *b, *c;
123
    int *b, *c;
106
    int i, j, k, temp, t;
124
    int i, j, k, temp, t;
107
    MPI_Status status;
125
    MPI_Status status;
108
 
126
 
109
    /* Get all of the necessary info for each process */
127
    /* Get all of the necessary info for each process */
Line 130... Line 148...
130
    if (myid == ROOT_NODE)
148
    if (myid == ROOT_NODE)
131
    {
149
    {
132
        /* Allocate the full c, since we need to initialize it */
150
        /* Allocate the full c, since we need to initialize it */
133
        c = allocate (n, n);
151
        c = allocate (n, n);
134
 
152
 
135
        /* Initialize values */
153
        /* Seed the rng */
-
 
154
        srandom (time(NULL));
-
 
155
 
-
 
156
        /* Initialize values to random number in [1,100] */
136
        for (i=0; i<n; i++)
157
        for (i=0; i<n; i++)
137
            for (j=0; j<n; j++)
158
            for (j=0; j<n; j++)
138
                b[i*n+j] = INT_MAX;
159
                b[i*n+j] = (int) (1.0 + (100.0 * (random() / (RAND_MAX + 1.0))));
139
 
160
 
140
        b[0] = 0;
-
 
141
        b[1] = 2;
-
 
142
        b[2] = INT_MAX;
-
 
143
        b[3] = 10;
-
 
144
 
-
 
145
        b[4] = 2;
-
 
146
        b[5] = 0;
-
 
147
        b[6] = 2;
-
 
148
        b[7] = INT_MAX;
-
 
149
 
-
 
150
        b[8]  = INT_MAX;
-
 
151
        b[9]  = 2;
-
 
152
        b[10] = 0;
161
        /* Force b[i][i] = 0 */
153
        b[11] = 3;
-
 
154
 
-
 
155
        b[12] = 10;
-
 
156
        b[13] = INT_MAX;
162
        for (i=0; i<n; i++)
157
        b[14] = 3;
-
 
158
        b[15] = 0;
163
            b[i*n+i] = 0; 
159
 
164
 
160
        if (n<=100)
165
        if (n<=100)
161
            print_matrix (b, n, "Input Matrix:");
166
            print_matrix (b, n, "Input Matrix:");
162
        else
167
        else
163
        {
168
        {