fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h> // For srand() and rand()
  4. #include <mpi.h>
  5.  
  6. #define N 100 // Maximum size of the matrix
  7. #define MAX_RANDOM 100 // Maximum random number
  8.  
  9. void fill_with_random(double A[N][N], double b[N], int n, int my_rank, int p, MPI_Comm comm) {
  10. int i, j;
  11.  
  12. // Seed the random number generator with the current time
  13. srand(time(NULL));
  14.  
  15. // Fill A with random numbers
  16. for (i = 0; i < n; i++) {
  17. for (j = 0; j < n; j++) {
  18. A[i][j] = rand() % MAX_RANDOM + 1; // Random number between 1 and MAX_RANDOM
  19. }
  20. }
  21.  
  22. // Fill b with random numbers
  23. for (i = 0; i < n; i++) {
  24. b[i] = rand() % MAX_RANDOM + 1; // Random number between 1 and MAX_RANDOM
  25. }
  26. }
  27.  
  28. void forward_elimination(double A[N][N], double b[N], int n, int k, int my_rank, int p, MPI_Comm comm) {
  29. int i, j, l, pivot_row;
  30. double pivot, tmp;
  31.  
  32. for (int col = 0; col < n; col++) {
  33. // Find the pivot row for column col among the rows assigned to this processor
  34. pivot = 0.0;
  35. for (i = col % k; i < n; i += k) {
  36. if (A[i][col] > pivot) {
  37. pivot = A[i][col];
  38. pivot_row = i;
  39. }
  40. }
  41. // Broadcast the pivot row to all processors
  42. MPI_Bcast(&pivot_row, 1, MPI_INT, col % k, comm);
  43. // Swap rows
  44. if (my_rank == col % k) {
  45. if (pivot_row != col) {
  46. for (j = 0; j < n; j++) {
  47. tmp = A[col][j];
  48. A[col][j] = A[pivot_row][j];
  49. A[pivot_row][j] = tmp;
  50. }
  51. tmp = b[col];
  52. b[col] = b[pivot_row];
  53. b[pivot_row] = tmp;
  54. }
  55. }
  56. // Eliminate elements below the pivot
  57. for (i = (col % k == my_rank) ? (col + 1) : ((col + 1) % k); i < n; i += k) {
  58. tmp = A[i][col] / A[col][col];
  59. for (j = col; j < n; j++) {
  60. A[i][j] -= tmp * A[col][j];
  61. }
  62. b[i] -= tmp * b[col];
  63. }
  64. }
  65. }
  66.  
  67. void backward_substitution(double A[N][N], double b[N], double x[N], int n, int k, int my_rank, int p, MPI_Comm comm) {
  68. int i, j;
  69. double sum;
  70.  
  71. for (int col = n - 1; col >= 0; col--) {
  72. sum = 0.0;
  73. for (i = (col % k == my_rank) ? col : ((col + 1) % k); i < n; i += k) {
  74. sum += A[col][i] * x[i];
  75. }
  76. MPI_Bcast(&sum, 1, MPI_DOUBLE, col % k, comm);
  77. if (col % k == my_rank) {
  78. x[col] = (b[col] - sum) / A[col][col];
  79. }
  80. }
  81. }
  82.  
  83. int main(int argc, char **argv) {
  84. int my_rank, p, n, k;
  85. double A[N][N], b[N], x[N];
  86. MPI_Comm comm;
  87.  
  88. MPI_Init(&argc, &argv);
  89. MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
  90. MPI_Comm_size(MPI_COMM_WORLD, &p);
  91.  
  92. if (my_rank == 0) {
  93. printf("Enter the size of the matrix (n): ");
  94. scanf("%d", &n);
  95. printf("Enter the value of k: ");
  96. scanf("%d", &k);
  97.  
  98. // Fill A and b with random numbers
  99. fill_with_random(A, b, n, my_rank, p, MPI_COMM_WORLD);
  100. }
  101.  
  102. // Broadcast n and k to all processors
  103. MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD);
  104. MPI_Bcast(&k, 1, MPI_INT, 0, MPI_COMM_WORLD);
  105.  
  106. // Scatter A and b among processors
  107. MPI_Bcast(A, N * N, MPI_DOUBLE, 0, MPI_COMM_WORLD);
  108. MPI_Bcast(b, N, MPI_DOUBLE, 0, MPI_COMM_WORLD);
  109.  
  110. // Perform forward elimination
  111. forward_elimination(A, b, n, k, my_rank, p, MPI_COMM_WORLD);
  112.  
  113. // Perform backward substitution
  114. backward_substitution(A, b, x, n, k, my_rank, p, MPI_COMM_WORLD);
  115.  
  116. // Gather the solution x to the root processor
  117. MPI_Gather(x, n, MPI_DOUBLE, x, n, MPI_DOUBLE, 0, MPI_COMM_WORLD);
  118.  
  119. // Print the solution
  120. if (my_rank == 0) {
  121. printf("Solution:\n");
  122. for (int i = 0; i < n; i++) {
  123. printf("%lf ", x[i]);
  124. }
  125. printf("\n");
  126. }
  127.  
  128. MPI_Finalize();
  129. return 0;
  130. }
  131.  
Success #stdin #stdout #stderr 0.3s 40476KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Error: unexpected symbol in "void fill_with_random"
Execution halted