1 /* Copyright (c) 2019-2023. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
15 void multiply(int istart, int iend)
17 for (int i = istart; i <= iend; ++i){
18 for (int j = 0; j < size; ++j) {
19 for (int k = 0; k < size; ++k) {
20 c[i][j] += a[i][k] * b[k][j];
26 int main(int argc, char* argv[])
32 MPI_Init(&argc, &argv);
33 MPI_Comm_size(MPI_COMM_WORLD, &nproc);
34 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
36 // MPI_Barrier(MPI_COMM_WORLD);
37 // start = MPI_Wtime();
40 // Initialize buffers.
41 for (int i = 0; i < size; ++i) {
42 for (int j = 0; j < size; ++j) {
43 a[i][j] = (float)i + j;
44 b[i][j] = (float)i - j;
50 // Broadcast matrices to all workers.
51 MPI_Bcast(a, size*size, MPI_FLOAT, 0,MPI_COMM_WORLD);
52 MPI_Bcast(b, size*size, MPI_FLOAT, 0,MPI_COMM_WORLD);
53 MPI_Bcast(c, size*size, MPI_FLOAT, 0,MPI_COMM_WORLD);
55 // Partition work by i-for-loop.
56 istart = (size / nproc) * rank;
57 iend = (size / nproc) * (rank + 1) - 1;
59 // Compute matrix multiplication in [istart,iend]
62 multiply(istart, iend);
64 // Gather computed results.
65 MPI_Gather(c + (size/nproc*rank),
68 c + (size/nproc*rank),
75 // Compute remaining multiplications
76 // when size % nproc > 0.
77 if (size % nproc > 0) {
78 multiply((size/nproc)*nproc, size-1);
82 // MPI_Barrier(MPI_COMM_WORLD);
87 // if (rank == 0) { /* use time on master node */
88 // float msec_total = 0.0f;
90 // // Compute and print the performance
91 // float msec_per_matrix_mul = end-start;
92 // double flops_per_matrix_mul = 2.0 * (double)size * (double)size * (double)size;
93 // double giga_flops = (flops_per_matrix_mul * 1.0e-9f) / (msec_per_matrix_mul / 1000.0f);
95 // "Performance= %.2f GFlop/s, Time= %.3f msec, Size= %.0f Ops\n",
97 // msec_per_matrix_mul,
98 // flops_per_matrix_mul);