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
{
25
    const int n = 5;
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 */
46
        for (i=0; i<n; i++)
47
            for (j=0; j<n; j++)
48
            {
49
                a[i*n+j] = i*n+j+1;
50
                b[i*n+j] = i*n+j+1;
51
            }
52
    }
53
    else
54
    {
55
        /* Allocate our slice of a and c, since that's all we need */
56
        a = allocate (n/numprocs, n);
57
        c = allocate (n/numprocs, n);
58
    }
59
 
60
    /* Send a slice of a to each process.
61
     * Send all of b to each process, since they need all of it */
62
    MPI_Scatter (a, n*n/numprocs, MPI_INT, a, n*n/numprocs, MPI_INT, ROOT_NODE, MPI_COMM_WORLD);
63
    MPI_Bcast (b, n*n, MPI_INT, ROOT_NODE, MPI_COMM_WORLD);
64
 
65
    /* Multiply our slice of the matrix, store it in c */
66
    for (i=0; i<n/numprocs; i++)
67
    {
68
        for (j=0; j<n; j++)
69
        {
70
            // MATRIX_MULT
71
            c[i*n+j]=0;
72
 
73
            // SHORTEST PATH
74
            // c[i*n+j]=INT_MAX;
75
 
76
            for (k=0; k<n; k++)
77
            {
78
                // MATRIX MULT
79
                c[i*n+j] += a[i*n+k] * b[k*n+j];
80
 
81
                // SHORTEST PATH
82
                // temp = a[i*n+k] + b[k*n+j];
83
 
84
                // if (temp < c[i*n+j])
85
                //    c[i*n+j] = a[i*n+k] + b[k*n+j];
86
            }
87
        }
88
    }
89
 
90
    /* Recieve each slice from all processes, store the result in c */
91
    MPI_Gather (c, n*n/numprocs, MPI_INT, c, n*n/numprocs, MPI_INT, ROOT_NODE, MPI_COMM_WORLD);
92
 
93
    if (myid == ROOT_NODE)
94
    {
95
        for (i=0; i<n; i++)
96
            for (j=0; j<n; j++)
97
                printf ("c[%d][%d] = %d\n", i,j,c[i*n+j]);
98
    }
99
 
100
    /* Free all of the memory we allocated earlier */
101
    free (a);
102
    free (b);
103
    free (c);
104
 
105
    MPI_Finalize ();
106
    return 0;
107
}
108