Subversion Repositories programming

Rev

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

Rev 323 Rev 324
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 = 3;
25
    const int n = 4;
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 50... Line 50...
50
                a[i*n+j] = i*n+j+1;
50
                a[i*n+j] = i*n+j+1;
51
                b[i*n+j] = i*n+j+1;
51
                b[i*n+j] = i*n+j+1;
52
            }
52
            }
53
#endif
53
#endif
54
 
54
 
-
 
55
        for (i=0; i<n; i++)
-
 
56
            for (j=0; j<n; j++)
-
 
57
            {
-
 
58
                a[i*n+j] = INT_MAX;
-
 
59
                b[i*n+j] = INT_MAX;
-
 
60
            }
-
 
61
 
55
        b[0] = 0;
62
        b[0] = 0;
56
        b[1] = 5;
63
        b[1] = 2;
57
        b[2] = INT_MAX;
64
        b[2] = INT_MAX;
58
        b[3] = 5;
65
        b[3] = 10;
-
 
66
 
59
        b[4] = 0;
67
        b[4] = 2;
60
        b[5] = 10;
68
        b[5] = 0;
-
 
69
        b[6] = 2;
61
        b[6] = INT_MAX;
70
        b[7] = INT_MAX;
-
 
71
 
-
 
72
        b[8]  = INT_MAX;
-
 
73
        b[9]  = 2;
-
 
74
        b[10] = 0;
-
 
75
        b[11] = 3;
-
 
76
 
62
        b[7] = 10;
77
        b[12] = 10;
-
 
78
        b[13] = INT_MAX;
-
 
79
        b[14] = 3;
63
        b[8] = 0;
80
        b[15] = 0;
64
 
81
 
65
        memcpy (a, b, n*n*sizeof(int));
82
        memcpy (a, b, n*n*sizeof(int));
66
    }
83
    }
67
    else
84
    else
68
    {
85
    {
Line 74... Line 91...
74
    /* Send a slice of a to each process.
91
    /* Send a slice of a to each process.
75
     * Send all of b to each process, since they need all of it */
92
     * 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);
93
    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);
94
    MPI_Bcast (b, n*n, MPI_INT, ROOT_NODE, MPI_COMM_WORLD);
78
 
95
 
79
    for (i=0; i<n*n/numprocs; i++)
96
    for (i=0; i<n/numprocs; i++)
-
 
97
        for (j=0; j<n; j++)
80
        c[i] = INT_MAX;
98
            c[i*n+j] = INT_MAX;
81
 
99
 
82
    /* Multiply our slice of the matrix, store it in c */
100
    /* Multiply our slice of the matrix, store it in c */
83
    for (i=0; i<n/numprocs; i++)
101
    for (i=0; i<n/numprocs; i++)
84
    {
102
    {
85
        for (j=0; j<n; j++)
103
        for (j=0; j<n; j++)
Line 99... Line 117...
99
                if (a[i*n+k] == INT_MAX || b[k*n+j] == INT_MAX)
117
                if (a[i*n+k] == INT_MAX || b[k*n+j] == INT_MAX)
100
                    temp = INT_MAX;
118
                    temp = INT_MAX;
101
                else
119
                else
102
                    temp = a[i*n+k] + b[k*n+j];
120
                    temp = a[i*n+k] + b[k*n+j];
103
 
121
 
104
                if (i*n+j == 3 || i*n+j == 12)
122
                //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);
123
                //    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
 
124
 
107
                if (temp < c[i*n+j])
125
                if (temp < c[i*n+j])
108
                    c[i*n+j] = temp; //a[i*n+k] + b[k*n+j];
126
                    c[i*n+j] = temp; //a[i*n+k] + b[k*n+j];
109
            }
127
            }
110
        }
128
        }