fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <mpi.h>
  4.  
  5. void matrix_vector_multiply(double *A, double *b, double *c, int start_row, int end_row, int M) {
  6. for (int i = start_row; i < end_row; i++) {
  7. c[i] = 0;
  8. for (int j = 0; j < M; j++) {
  9. c[i] += A[i * M + j] * b[j];
  10. }
  11. }
  12. }
  13.  
  14. int parse_integer(const char *str) {
  15. int value = 0;
  16. while (*str) {
  17. if (*str < '0' || *str > '9') {
  18. return -1; // Invalid input
  19. }
  20. value = value * 10 + (*str - '0');
  21. str++;
  22. }
  23. return value;
  24. }
  25.  
  26. int main(int argc, char **argv) {
  27. int rank, size, N, M;
  28.  
  29. // Initialize MPI
  30. MPI_Init(&argc, &argv);
  31. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  32. MPI_Comm_size(MPI_COMM_WORLD, &size);
  33.  
  34. // Check command-line arguments for matrix dimensions
  35. if (argc != 3) {
  36. if (rank == 0) {
  37. printf("Usage: %s <num_rows> <num_columns>\n", argv[0]);
  38. }
  39. MPI_Abort(MPI_COMM_WORLD, EXIT_FAILURE);
  40. }
  41.  
  42.  
  43. // Parse matrix dimensions
  44. N = parse_integer(argv[1]);
  45.  
  46. M = parse_integer(argv[2]);
  47.  
  48. if (N <= 0 || M <= 0) {
  49. if (rank == 0) {
  50. printf("Invalid matrix dimensions: %s x %s\n", argv[1], argv[2]);
  51. }
  52. MPI_Abort(MPI_COMM_WORLD, EXIT_FAILURE);
  53. }
  54.  
  55. double *A = NULL;
  56. double *b = NULL;
  57. double *c = (double *)malloc(N * sizeof(double));
  58.  
  59. // Master process initializes the matrix and vector
  60. if (rank == 0) {
  61. A = (double *)malloc(N * M * sizeof(double));
  62. b = (double *)malloc(M * sizeof(double));
  63.  
  64. // Initialize matrix A and vector b with simple values
  65. for (int i = 0; i < N; i++)
  66. for (int j = 0; j < M; j++)
  67. A[i * M + j] = 1.0; // Example initialization
  68. for (int j = 0; j < M; j++)
  69. b[j] = 1.0; // Example initialization
  70. }
  71.  
  72. // Broadcast vector b to all processes
  73. MPI_Bcast(b, M, MPI_DOUBLE, 0, MPI_COMM_WORLD);
  74.  
  75. // Each process computes its assigned rows
  76. int rows_per_process = N / size;
  77. int start_row = rank * rows_per_process;
  78. int end_row = (rank == size - 1) ? N : start_row + rows_per_process;
  79.  
  80. double *local_c = (double *)malloc((end_row - start_row) * sizeof(double));
  81. matrix_vector_multiply(A, b, local_c, start_row, end_row, M);
  82.  
  83. // Gather results in the master process
  84. MPI_Gather(local_c, end_row - start_row, MPI_DOUBLE, c, end_row - start_row, MPI_DOUBLE, 0, MPI_COMM_WORLD);
  85.  
  86. // Master process prints the result
  87. if (rank == 0) {
  88. for (int i = 0; i < N; i++) {
  89. printf("c[%d] = %f\n", i, c[i]);
  90. }
  91. free(A);
  92. free(b);
  93. }
  94.  
  95. free(local_c);
  96. free(c);
  97. MPI_Finalize();
  98. return 0;
  99.  
  100.  
  101. }
  102.  
  103.  
Success #stdin #stdout #stderr 0.23s 40676KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Error: unexpected symbol in "void matrix_vector_multiply"
Execution halted