fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include < mpi.h>
  4. int main(int argc, char **argv)
  5. {
  6. int rank, size, i, j, n,
  7.  
  8. // Initialize MPI
  9. MPI_Init(&argc, &argv);
  10. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  11. MPI_Comm_size(MPI_COMM_WORLD, &size);
  12.  
  13. // Take input for n
  14. if (rank == 0)
  15. {
  16. printf("Enter the value of n (must be divisible by p): ");
  17. scanf_s("%d", &n);
  18. }
  19.  
  20. // Broadcast n to all processes
  21. MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD);
  22.  
  23. // Take input for p
  24. if (rank == 0)
  25. {
  26. printf("Enter the value of p (must be smaller than n): ");
  27. scanf_s("%d", &p);
  28. }
  29.  
  30. // Broadcast p to all processes
  31. MPI_Bcast(&p, 1, MPI_INT, 0, MPI_COMM_WORLD);
  32.  
  33. // Error handling for p and n
  34. if (n % p != 0)
  35. {
  36. if (rank == 0)
  37. printf("Error: n must be divisible by p.\n");
  38. MPI_Finalize();
  39. return 0;
  40. }
  41.  
  42. if (p >= n)
  43. {
  44. if (rank == 0)
  45. printf("Error: p must be smaller than n.\n");
  46. MPI_Finalize();
  47. return 0;
  48. }
  49.  
  50. // Define matrices and vectors dynamically
  51. int *A = (int *)malloc(sizeof(int) * n * n);
  52. int *x = (int *)malloc(sizeof(int) * n);
  53. int *y = (int *)malloc(sizeof(int) * n);
  54. int *local_A = (int *)malloc(sizeof(int) * (n / p) * n);
  55. int *local_x = (int *)malloc(sizeof(int) * (n / p));
  56. int *local_y = (int *)malloc(sizeof(int) * (n / p));
  57.  
  58. // Initialize matrices and vectors with random values
  59. if (rank == 0)
  60. {
  61. printf("\nMatrix A:\n");
  62. for (i = 0; i < n * n; i++)
  63. {
  64. A[i] = rand() % 101; // random integer between 0 and 100
  65. printf("%d\t", A[i]);
  66. if ((i + 1) % n == 0)
  67. printf("\n");
  68. }
  69.  
  70. printf("\nVector x:\n");
  71. for (i = 0; i < n; i++)
  72. {
  73. x[i] = rand() % 101; // random integer between 0 and 100
  74. printf("%d\n", x[i]);
  75. }
  76. }
  77.  
  78. // Broadcast matrices and vectors to all processes
  79. MPI_Bcast(A, n * n, MPI_INT, 0, MPI_COMM_WORLD);
  80. MPI_Bcast(x, n, MPI_INT, 0, MPI_COMM_WORLD);
  81.  
  82. // Calculate the local size for each process
  83. int local_n = n / size;
  84.  
  85. // Scatter matrix A to all processes
  86. MPI_Scatter(A, local_n * n, MPI_INT, local_A, local_n * n, MPI_INT, 0, MPI_COMM_WORLD);
  87.  
  88. // Scatter vector x to all processes
  89. MPI_Scatter(x, local_n, MPI_INT, local_x, local_n, MPI_INT, 0, MPI_COMM_WORLD);
  90.  
  91. // Compute local vector elements
  92. for (i = 0; i < local_n; i++)
  93. {
  94. local_y[i] = 0;
  95. for (j = 0; j < n; j++)
  96. {
  97. local_y[i] += local_A[i * n + j] * local_x[j];
  98. }
  99. }
  100.  
  101. // Gather local results back to process 0
  102. MPI_Gather(local_y, local_n, MPI_INT, y, local_n, MPI_INT, 0, MPI_COMM_WORLD);
  103.  
  104. // Process 0 prints the result
  105. if (rank == 0)
  106. {
  107. printf("\nResultant Vector y:\n");
  108. for (i = 0; i < n; i++)
  109. {
  110. printf("y[%d] = %d\n", i, y[i]);
  111. }
  112. }
  113.  
  114. // Free dynamically allocated memory
  115. free(A);
  116. free(x);
  117. free(y);
  118. free(local_A);
  119. free(local_x);
  120. free(local_y);
  121.  
  122. // Finalize MPI
  123. MPI_Finalize();
  124.  
  125. return 0;
  126. }
Success #stdin #stdout #stderr 0.28s 40728KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Error: unexpected symbol in "int main"
Execution halted