fork download
  1. #include <mpi.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5.  
  6. int main(int argc, char* argv[]) {
  7. int N = atoi(argv[1]); // Matrix size from command line
  8. int rank, size;
  9. MPI_Init(&argc, &argv);
  10. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  11. MPI_Comm_size(MPI_COMM_WORLD, &size);
  12.  
  13. double *A, *B, *C_seq, *C_par;
  14. if (rank == 0) {
  15. A = (double*)malloc(N * N * sizeof(double));
  16. B = (double*)malloc(N * N * sizeof(double));
  17. C_seq = (double*)malloc(N * N * sizeof(double));
  18. C_par = (double*)malloc(N * N * sizeof(double));
  19.  
  20. // Initialize matrices A and B with values for testing
  21. for (int i = 0; i < N * N; i++) {
  22. A[i] = 1.0;
  23. B[i] = 1.0;
  24. }
  25.  
  26. // Sequential matrix multiplication
  27. clock_t start_seq = clock();
  28. for (int i = 0; i < N; i++) {
  29. for (int j = 0; j < N; j++) {
  30. C_seq[i * N + j] = 0;
  31. for (int k = 0; k < N; k++) {
  32. C_seq[i * N + j] += A[i * N + k] * B[k * N + j];
  33. }
  34. }
  35. }
  36. clock_t end_seq = clock();
  37. double time_seq = (double)(end_seq - start_seq) / CLOCKS_PER_SEC;
  38. printf("Sequential time for %d x %d matrix: %f seconds\n", N, N, time_seq);
  39. }
  40.  
  41. // Parallel matrix multiplication
  42. int rowsPerProcess = N / size;
  43. double *localA = (double*)malloc(rowsPerProcess * N * sizeof(double));
  44. double *localC = (double*)malloc(rowsPerProcess * N * sizeof(double));
  45.  
  46. MPI_Scatter(A, rowsPerProcess * N, MPI_DOUBLE, localA, rowsPerProcess * N, MPI_DOUBLE, 0, MPI_COMM_WORLD);
  47. MPI_Bcast(B, N * N, MPI_DOUBLE, 0, MPI_COMM_WORLD);
  48.  
  49. double start_par = MPI_Wtime();
  50. for (int i = 0; i < rowsPerProcess; i++) {
  51. for (int j = 0; j < N; j++) {
  52. localC[i * N + j] = 0;
  53. for (int k = 0; k < N; k++) {
  54. localC[i * N + j] += localA[i * N + k] * B[k * N + j];
  55. }
  56. }
  57. }
  58. double end_par = MPI_Wtime();
  59. double localTime = end_par - start_par;
  60.  
  61. double maxTime;
  62. MPI_Reduce(&localTime, &maxTime, 1, MPI_DOUBLE, MPI_MAX, 0, MPI_COMM_WORLD);
  63.  
  64. MPI_Gather(localC, rowsPerProcess * N, MPI_DOUBLE, C_par, rowsPerProcess * N, MPI_DOUBLE, 0, MPI_COMM_WORLD);
  65.  
  66. if (rank == 0) {
  67. printf("Parallel time for %d x %d matrix with %d processors: %f seconds\n", N, N, size, maxTime);
  68.  
  69. // Calculate speedup and efficiency
  70. double speedup = time_seq / maxTime;
  71. double efficiency = speedup / size;
  72. printf("Speedup: %f\n", speedup);
  73. printf("Efficiency: %f\n", efficiency);
  74.  
  75. // Cleanup
  76. free(A);
  77. free(B);
  78. free(C_seq);
  79. free(C_par);
  80. }
  81.  
  82. free(localA);
  83. free(localC);
  84. MPI_Finalize();
  85. return 0;
  86. }
  87.  
Success #stdin #stdout #stderr 0.27s 40556KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Error: unexpected symbol in "int main"
Execution halted