fork download
  1. #include <mpi.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. #define MATRIX_SIZE 1024 // Change this to 64, 128, 256, 512, 1024 as needed
  6.  
  7. void matrix_multiply(int *A, int *B, int *C, int size) {
  8. int i, j, k;
  9. for (i = 0; i < size; i++) {
  10. for (j = 0; j < size; j++) {
  11. C[i * size + j] = 0;
  12. for (k = 0; k < size; k++) {
  13. C[i * size + j] += A[i * size + k] * B[k * size + j];
  14. }
  15. }
  16. }
  17. }
  18.  
  19. int main(int argc, char *argv[]) {
  20. int rank, size;
  21. int nrows_per_proc;
  22. int *A, *B, *C, *local_A, *local_C;
  23. int i, j;
  24.  
  25. MPI_Init(&argc, &argv);
  26. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  27. MPI_Comm_size(MPI_COMM_WORLD, &size);
  28.  
  29. // Determine the number of rows per processor
  30. nrows_per_proc = MATRIX_SIZE / size;
  31.  
  32. // Allocate memory for matrices
  33. if (rank == 0) {
  34. A = (int *)malloc(MATRIX_SIZE * MATRIX_SIZE * sizeof(int));
  35. B = (int *)malloc(MATRIX_SIZE * MATRIX_SIZE * sizeof(int));
  36. C = (int *)malloc(MATRIX_SIZE * MATRIX_SIZE * sizeof(int));
  37.  
  38. // Initialize matrices A and B with random values
  39. for (i = 0; i < MATRIX_SIZE; i++) {
  40. for (j = 0; j < MATRIX_SIZE; j++) {
  41. A[i * MATRIX_SIZE + j] = rand() % 10;
  42. B[i * MATRIX_SIZE + j] = rand() % 10;
  43. }
  44. }
  45. }
  46.  
  47. // Allocate memory for local matrices
  48. local_A = (int *)malloc(nrows_per_proc * MATRIX_SIZE * sizeof(int));
  49. local_C = (int *)malloc(nrows_per_proc * MATRIX_SIZE * sizeof(int));
  50. B = (int *)malloc(MATRIX_SIZE * MATRIX_SIZE * sizeof(int)); // Each process needs the entire matrix B
  51.  
  52. // Scatter rows of A to different processes
  53. MPI_Scatter(A, nrows_per_proc * MATRIX_SIZE, MPI_INT, local_A, nrows_per_proc * MATRIX_SIZE, MPI_INT, 0, MPI_COMM_WORLD);
  54.  
  55. // Broadcast matrix B to all processes
  56. MPI_Bcast(B, MATRIX_SIZE * MATRIX_SIZE, MPI_INT, 0, MPI_COMM_WORLD);
  57.  
  58. // Perform local matrix multiplication
  59. for (i = 0; i < nrows_per_proc; i++) {
  60. for (j = 0; j < MATRIX_SIZE; j++) {
  61. local_C[i * MATRIX_SIZE + j] = 0;
  62. for (int k = 0; k < MATRIX_SIZE; k++) {
  63. local_C[i * MATRIX_SIZE + j] += local_A[i * MATRIX_SIZE + k] * B[k * MATRIX_SIZE + j];
  64. }
  65. }
  66. }
  67.  
  68. // Gather the result matrix C from all processes
  69. MPI_Gather(local_C, nrows_per_proc * MATRIX_SIZE, MPI_INT, C, nrows_per_proc * MATRIX_SIZE, MPI_INT, 0, MPI_COMM_WORLD);
  70.  
  71. // Print result if necessary
  72. if (rank == 0) {
  73. printf("Result Matrix C (Showing first 5x5):\n");
  74. for (i = 0; i < 5; i++) {
  75. for (j = 0; j < 5; j++) {
  76. printf("%d ", C[i * MATRIX_SIZE + j]);
  77. }
  78. printf("\n");
  79. }
  80. }
  81.  
  82. // Clean up memory
  83. if (rank == 0) {
  84. free(A);
  85. free(B);
  86. free(C);
  87. }
  88. free(local_A);
  89. free(local_C);
  90.  
  91. MPI_Finalize();
  92. return 0;
  93. }
  94.  
  95.  
  96.  
  97.  
  98.  
Success #stdin #stdout #stderr 0.23s 40676KB
stdin
1233 234
stdout
Standard output is empty
stderr
Error: unexpected symbol in "void matrix_multiply"
Execution halted