Subversion Repositories programming

Rev

Rev 322 | Rev 324 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 322 Rev 323
Line 20... Line 20...
20
    return temp;
20
    return temp;
21
}
21
}
22
 
22
 
23
int main (int argc, char *argv[])
23
int main (int argc, char *argv[])
24
{
24
{
25
    const int n = 5;
25
    const int n = 3;
26
    int myid, numprocs;
26
    int myid, numprocs;
27
    int *a, *b, *c;
27
    int *a, *b, *c;
28
    int i, j, k;
28
    int i, j, k;
29
    int temp;
29
    int temp;
30
 
30
 
Line 41... Line 41...
41
        /* Allocate the full a and c, since we need to initialize it */
41
        /* Allocate the full a and c, since we need to initialize it */
42
        a = allocate (n, n);
42
        a = allocate (n, n);
43
        c = allocate (n, n);
43
        c = allocate (n, n);
44
 
44
 
45
        /* Initialize values */
45
        /* Initialize values */
-
 
46
#if 0
46
        for (i=0; i<n; i++)
47
        for (i=0; i<n; i++)
47
            for (j=0; j<n; j++)
48
            for (j=0; j<n; j++)
48
            {
49
            {
49
                a[i*n+j] = i*n+j+1;
50
                a[i*n+j] = i*n+j+1;
50
                b[i*n+j] = i*n+j+1;
51
                b[i*n+j] = i*n+j+1;
51
            }
52
            }
-
 
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));
52
    }
66
    }
53
    else
67
    else
54
    {
68
    {
55
        /* Allocate our slice of a and c, since that's all we need */
69
        /* Allocate our slice of a and c, since that's all we need */
56
        a = allocate (n/numprocs, n);
70
        a = allocate (n/numprocs, n);
Line 60... Line 74...
60
    /* Send a slice of a to each process.
74
    /* Send a slice of a to each process.
61
     * Send all of b to each process, since they need all of it */
75
     * 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);
76
    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);
77
    MPI_Bcast (b, n*n, MPI_INT, ROOT_NODE, MPI_COMM_WORLD);
64
 
78
 
-
 
79
    for (i=0; i<n*n/numprocs; i++)
-
 
80
        c[i] = INT_MAX;
-
 
81
 
65
    /* Multiply our slice of the matrix, store it in c */
82
    /* Multiply our slice of the matrix, store it in c */
66
    for (i=0; i<n/numprocs; i++)
83
    for (i=0; i<n/numprocs; i++)
67
    {
84
    {
68
        for (j=0; j<n; j++)
85
        for (j=0; j<n; j++)
69
        {
86
        {
70
            // MATRIX_MULT
87
            // MATRIX_MULT
71
            c[i*n+j]=0;
88
            // c[i*n+j]=0;
72
 
89
 
73
            // SHORTEST PATH
90
            // SHORTEST PATH
74
            // c[i*n+j]=INT_MAX;
91
            //c[i*n+j] = INT_MAX;
75
 
92
 
76
            for (k=0; k<n; k++)
93
            for (k=0; k<n; k++)
77
            {
94
            {
78
                // MATRIX MULT
95
                // MATRIX MULT
79
                c[i*n+j] += a[i*n+k] * b[k*n+j];
96
                // c[i*n+j] += a[i*n+k] * b[k*n+j];
80
 
97
 
81
                // SHORTEST PATH
98
                // SHORTEST PATH
-
 
99
                if (a[i*n+k] == INT_MAX || b[k*n+j] == INT_MAX)
-
 
100
                    temp = INT_MAX;
-
 
101
                else
82
                // temp = a[i*n+k] + b[k*n+j];
102
                    temp = a[i*n+k] + b[k*n+j];
-
 
103
 
-
 
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);
83
 
106
 
84
                // if (temp < c[i*n+j])
107
                if (temp < c[i*n+j])
85
                //    c[i*n+j] = a[i*n+k] + b[k*n+j];
108
                    c[i*n+j] = temp; //a[i*n+k] + b[k*n+j];
86
            }
109
            }
87
        }
110
        }
88
    }
111
    }
89
 
112
 
90
    /* Recieve each slice from all processes, store the result in c */
113
    /* 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);
114
    MPI_Gather (c, n*n/numprocs, MPI_INT, c, n*n/numprocs, MPI_INT, ROOT_NODE, MPI_COMM_WORLD);
92
 
115
 
93
    if (myid == ROOT_NODE)
116
    if (myid == ROOT_NODE)
94
    {
117
    {
95
        for (i=0; i<n; i++)
118
        for (i=0; i<n; i++)
-
 
119
        {
96
            for (j=0; j<n; j++)
120
            for (j=0; j<n; j++)
97
                printf ("c[%d][%d] = %d\n", i,j,c[i*n+j]);
121
                printf ("%d\t", c[i*n+j]);
-
 
122
 
-
 
123
            printf ("\n");
-
 
124
        }
98
    }
125
    }
99
 
126
 
100
    /* Free all of the memory we allocated earlier */
127
    /* Free all of the memory we allocated earlier */
101
    free (a);
128
    free (a);
102
    free (b);
129
    free (b);