10 void multiply(int istart, int iend)
12 for (int i = istart; i <= iend; ++i){
13 for (int j = 0; j < size; ++j) {
14 for (int k = 0; k < size; ++k) {
15 c[i][j] += a[i][k] * b[k][j];
21 int main(int argc, char* argv[])
27 MPI_Init(&argc, &argv);
28 MPI_Comm_size(MPI_COMM_WORLD, &nproc);
29 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
31 // MPI_Barrier(MPI_COMM_WORLD);
32 // start = MPI_Wtime();
35 // Initialize buffers.
36 for (int i = 0; i < size; ++i) {
37 for (int j = 0; j < size; ++j) {
38 a[i][j] = (float)i + j;
39 b[i][j] = (float)i - j;
45 // Broadcast matrices to all workers.
46 MPI_Bcast(a, size*size, MPI_FLOAT, 0,MPI_COMM_WORLD);
47 MPI_Bcast(b, size*size, MPI_FLOAT, 0,MPI_COMM_WORLD);
48 MPI_Bcast(c, size*size, MPI_FLOAT, 0,MPI_COMM_WORLD);
50 // Partition work by i-for-loop.
51 istart = (size / nproc) * rank;
52 iend = (size / nproc) * (rank + 1) - 1;
54 // Compute matrix multiplication in [istart,iend]
57 multiply(istart, iend);
59 // Gather computed results.
60 MPI_Gather(c + (size/nproc*rank),
63 c + (size/nproc*rank),
70 // Compute remaining multiplications
71 // when size % nproc > 0.
72 if (size % nproc > 0) {
73 multiply((size/nproc)*nproc, size-1);
77 // MPI_Barrier(MPI_COMM_WORLD);
82 // if (rank == 0) { /* use time on master node */
83 // float msec_total = 0.0f;
85 // // Compute and print the performance
86 // float msec_per_matrix_mul = end-start;
87 // double flops_per_matrix_mul = 2.0 * (double)size * (double)size * (double)size;
88 // double giga_flops = (flops_per_matrix_mul * 1.0e-9f) / (msec_per_matrix_mul / 1000.0f);
90 // "Performance= %.2f GFlop/s, Time= %.3f msec, Size= %.0f Ops\n",
92 // msec_per_matrix_mul,
93 // flops_per_matrix_mul);