fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <mpi.h>
  4.  
  5. #define N 4 // Size of the matrix (Assuming square matrix for simplicity)
  6.  
  7. // Function to print matrix (for debugging)
  8. void printMatrix(double *matrix, int rows, int cols) {
  9. for (int i = 0; i < rows; ++i) {
  10. for (int j = 0; j < cols; ++j) {
  11. printf("%.2f\t", matrix[i * cols + j]);
  12. }
  13. printf("\n");
  14. }
  15. printf("\n");
  16. }
  17.  
  18. // Perform Gaussian elimination using MPI
  19. void gaussianEliminationMPI(double *matrix, double *result, int rows_per_process, int my_rank, int num_procs) {
  20. MPI_Status status;
  21.  
  22. for (int k = 0; k < N - 1; ++k) {
  23. // Broadcast pivot row to all processes
  24. if (my_rank == k / rows_per_process) {
  25. for (int proc = 0; proc < num_procs; ++proc) {
  26. MPI_Bcast(&matrix[k * N], N, MPI_DOUBLE, proc, MPI_COMM_WORLD);
  27. }
  28. }
  29.  
  30. // Elimination
  31. for (int i = k + 1; i < N; ++i) {
  32. if (i / rows_per_process == my_rank) {
  33. double factor = matrix[i * N + k] / matrix[k * N + k];
  34. for (int j = k; j < N; ++j) {
  35. matrix[i * N + j] -= factor * matrix[k * N + j];
  36. }
  37. }
  38. }
  39.  
  40. // Synchronize after each elimination step
  41. MPI_Barrier(MPI_COMM_WORLD);
  42. }
  43.  
  44. // Back substitution (not parallelized)
  45. if (my_rank == 0) {
  46. for (int i = N - 1; i >= 0; --i) {
  47. result[i] = matrix[i * N + N] / matrix[i * N + i];
  48. for (int j = i - 1; j >= 0; --j) {
  49. matrix[j * N + N] -= matrix[j * N + i] * result[i];
  50. }
  51. }
  52. }
  53. }
  54.  
  55. int main(int argc, char *argv[]) {
  56. MPI_Init(&argc, &argv);
  57.  
  58. int my_rank, num_procs;
  59. MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
  60. MPI_Comm_size(MPI_COMM_WORLD, &num_procs);
  61.  
  62. int rows_per_process = N / num_procs;
  63. double *matrix = (double *)malloc(N * (N + 1) * sizeof(double));
  64. double *result = (double *)malloc(N * sizeof(double));
  65.  
  66. // Initialize matrix (for simplicity, assume a specific matrix here)
  67. if (my_rank == 0) {
  68. // Initialize the matrix [A | b] (assuming a specific matrix for demonstration)
  69. matrix[0] = 2.0; matrix[1] = 1.0; matrix[2] = -1.0; matrix[3] = 8.0;
  70. matrix[4] = -3.0; matrix[5] = -1.0; matrix[6] = 2.0; matrix[7] = -11.0;
  71. matrix[8] = -2.0; matrix[9] = 1.0; matrix[10] = 2.0; matrix[11] = -3.0;
  72. matrix[12] = 5.0; matrix[13] = 2.0; matrix[14] = -1.0; matrix[15] = 9.0;
  73.  
  74. printf("Original Matrix:\n");
  75. printMatrix(matrix, N, N + 1);
  76. }
  77.  
  78. // Scatter matrix to all processes
  79. MPI_Scatter(matrix, rows_per_process * (N + 1), MPI_DOUBLE, matrix, rows_per_process * (N + 1), MPI_DOUBLE, 0, MPI_COMM_WORLD);
  80.  
  81. // Perform Gaussian elimination
  82. gaussianEliminationMPI(matrix, result, rows_per_process, my_rank, num_procs);
  83.  
  84. // Gather results back to root process
  85. MPI_Gather(result, rows_per_process, MPI_DOUBLE, result, rows_per_process, MPI_DOUBLE, 0, MPI_COMM_WORLD);
  86.  
  87. // Print the final result (only root process)
  88. if (my_rank == 0) {
  89. printf("Result:\n");
  90. for (int i = 0; i < N; ++i) {
  91. printf("x%d = %.2f\n", i, result[i]);
  92. }
  93. }
  94.  
  95. free(matrix);
  96. free(result);
  97.  
  98. MPI_Finalize();
  99. return 0;
  100. }
  101.  
Success #stdin #stdout #stderr 0.28s 40716KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Error: unexpected '/' in "/"
Execution halted