Subversion Repositories programming

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
322 ira 1
#include <stdio.h>
2
#include <limits.h>
3
#include <mpi.h>
4
 
5
const int ROOT_NODE = 0;
6
 
7
/**
8
 * NOTE: Accessing "matrices" in this program is a little strange.
9
 * I decided to implement matrices using an array. To get to a[row][col],
10
 * you would use a[row*a_size+col]. Pretty simple actually.
11
 */
12
 
13
int* allocate (int rows, int cols)
14
{
15
    int *temp = NULL;
16
 
17
    if ((temp = (int*) malloc (rows * cols * sizeof(int))) == NULL)
18
        printf ("Could not allocate memory for array of size: %d x %d\n", rows, cols);
19
 
20
    return temp;
21
}
22
 
23
int main (int argc, char *argv[])
24
{
323 ira 25
    const int n = 3;
322 ira 26
    int myid, numprocs;
27
    int *a, *b, *c;
28
    int i, j, k;
29
    int temp;
30
 
31
    /* Get all of the necessary info for each process */
32
    MPI_Init (&argc, &argv);
33
    MPI_Comm_rank (MPI_COMM_WORLD, &myid);
34
    MPI_Comm_size (MPI_COMM_WORLD, &numprocs);
35
 
36
    /* Allocate all of b for each process, since we'll need it */
37
    b = allocate (n, n);
38
 
39
    if (myid == ROOT_NODE)
40
    {
41
        /* Allocate the full a and c, since we need to initialize it */
42
        a = allocate (n, n);
43
        c = allocate (n, n);
44
 
45
        /* Initialize values */
323 ira 46
#if 0
322 ira 47
        for (i=0; i<n; i++)
48
            for (j=0; j<n; j++)
49
            {
50
                a[i*n+j] = i*n+j+1;
51
                b[i*n+j] = i*n+j+1;
52
            }
323 ira 53
#endif
54
 
55
        b[0] = 0;
56
        b[1] = 5;
57
        b[2] = INT_MAX;
58
        b[3] = 5;
59
        b[4] = 0;
60
        b[5] = 10;
61
        b[6] = INT_MAX;
62
        b[7] = 10;
63
        b[8] = 0;
64
 
65
        memcpy (a, b, n*n*sizeof(int));
322 ira 66
    }
67
    else
68
    {
69
        /* Allocate our slice of a and c, since that's all we need */
70
        a = allocate (n/numprocs, n);
71
        c = allocate (n/numprocs, n);
72
    }
73
 
74
    /* Send a slice of a to each process.
75
     * Send all of b to each process, since they need all of it */
76
    MPI_Scatter (a, n*n/numprocs, MPI_INT, a, n*n/numprocs, MPI_INT, ROOT_NODE, MPI_COMM_WORLD);
77
    MPI_Bcast (b, n*n, MPI_INT, ROOT_NODE, MPI_COMM_WORLD);
78
 
323 ira 79
    for (i=0; i<n*n/numprocs; i++)
80
        c[i] = INT_MAX;
81
 
322 ira 82
    /* Multiply our slice of the matrix, store it in c */
83
    for (i=0; i<n/numprocs; i++)
84
    {
85
        for (j=0; j<n; j++)
86
        {
87
            // MATRIX_MULT
323 ira 88
            // c[i*n+j]=0;
322 ira 89
 
90
            // SHORTEST PATH
323 ira 91
            //c[i*n+j] = INT_MAX;
322 ira 92
 
93
            for (k=0; k<n; k++)
94
            {
95
                // MATRIX MULT
323 ira 96
                // c[i*n+j] += a[i*n+k] * b[k*n+j];
322 ira 97
 
98
                // SHORTEST PATH
323 ira 99
                if (a[i*n+k] == INT_MAX || b[k*n+j] == INT_MAX)
100
                    temp = INT_MAX;
101
                else
102
                    temp = a[i*n+k] + b[k*n+j];
322 ira 103
 
323 ira 104
                if (i*n+j == 3 || i*n+j == 12)
105
                    printf ("i=%d, j=%d, k=%d, a[%d]=%d, b[%d]=%d, temp=%d\n", i,j,k, i*n+k,a[i*n+k], k*n+j,b[k*n+j], temp);
106
 
107
                if (temp < c[i*n+j])
108
                    c[i*n+j] = temp; //a[i*n+k] + b[k*n+j];
322 ira 109
            }
110
        }
111
    }
112
 
113
    /* Recieve each slice from all processes, store the result in c */
114
    MPI_Gather (c, n*n/numprocs, MPI_INT, c, n*n/numprocs, MPI_INT, ROOT_NODE, MPI_COMM_WORLD);
115
 
116
    if (myid == ROOT_NODE)
117
    {
118
        for (i=0; i<n; i++)
323 ira 119
        {
322 ira 120
            for (j=0; j<n; j++)
323 ira 121
                printf ("%d\t", c[i*n+j]);
122
 
123
            printf ("\n");
124
        }
322 ira 125
    }
126
 
127
    /* Free all of the memory we allocated earlier */
128
    free (a);
129
    free (b);
130
    free (c);
131
 
132
    MPI_Finalize ();
133
    return 0;
134
}
135