fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <mpi.h>
  5. #include <time.h>
  6.  
  7. #define ROWS 1000
  8. #define COLUMNS 1000
  9.  
  10. // Function to check if a string is a palindrome
  11. int is_palindrome(const char *str, int len) {
  12. for (int start = 0, end = len - 1; start < end; ++start, --end) {
  13. if (str[start] != str[end]) {
  14. return 0;
  15. }
  16. }
  17. return 1;
  18. }
  19.  
  20. int main(int argc, char **argv) {
  21. int rank, size;
  22. MPI_Init(&argc, &argv);
  23. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  24. MPI_Comm_size(MPI_COMM_WORLD, &size);
  25.  
  26. // Create and initialize the matrix with random characters
  27. char matrix[ROWS][COLUMNS];
  28. if (rank == 0) {
  29. for (int i = 0; i < ROWS; ++i) {
  30. for (int j = 0; j < COLUMNS; ++j) {
  31. matrix[i][j] = (rand() % 26) + 'A';
  32. }
  33. }
  34. }
  35.  
  36. // Broadcast the matrix to all processes
  37. MPI_Bcast(matrix, ROWS * COLUMNS, MPI_CHAR, 0, MPI_COMM_WORLD);
  38.  
  39. // Palindrome lengths to search for
  40. int palindrome_lengths[] = {3, 4, 5, 6};
  41. int total_lengths = sizeof(palindrome_lengths) / sizeof(palindrome_lengths[0]);
  42.  
  43. if (rank == 0) {
  44. printf("Palindrome search\n\n");
  45. }
  46.  
  47. for (int num_threads = 1; num_threads <= 8; ++num_threads) {
  48. if (rank == 0) {
  49. printf("***************************************************************************\n");
  50. }
  51.  
  52. for (int idx = 0; idx < total_lengths; ++idx) {
  53. int length = palindrome_lengths[idx];
  54. int local_count = 0, global_count = 0;
  55.  
  56. // Divide the rows among processes
  57. int rows_per_process = ROWS / size;
  58. int start_row = rank * rows_per_process;
  59. int end_row = (rank == size - 1) ? ROWS : start_row + rows_per_process;
  60.  
  61. // Start timer on rank 0
  62. double start_time;
  63. if (rank == 0) {
  64. start_time = MPI_Wtime();
  65. }
  66.  
  67. // Each process searches for palindromes in its assigned rows
  68. for (int row = start_row; row < end_row; ++row) {
  69. for (int col = 0; col < COLUMNS; ++col) {
  70. // Check horizontal palindromes
  71. if (col + length <= COLUMNS) {
  72. if (is_palindrome(&matrix[row][col], length)) {
  73. local_count++;
  74. }
  75. }
  76.  
  77. // Check vertical palindromes
  78. if (row + length <= ROWS) {
  79. char temp[length];
  80. for (int k = 0; k < length; ++k) {
  81. temp[k] = matrix[row + k][col];
  82. }
  83. if (is_palindrome(temp, length)) {
  84. local_count++;
  85. }
  86. }
  87.  
  88. // Check diagonal (down-right) palindromes
  89. if (row + length <= ROWS && col + length <= COLUMNS) {
  90. char temp[length];
  91. for (int k = 0; k < length; ++k) {
  92. temp[k] = matrix[row + k][col + k];
  93. }
  94. if (is_palindrome(temp, length)) {
  95. local_count++;
  96. }
  97. }
  98.  
  99. // Check diagonal (up-right) palindromes
  100. if (row - length + 1 >= 0 && col + length <= COLUMNS) {
  101. char temp[length];
  102. for (int k = 0; k < length; ++k) {
  103. temp[k] = matrix[row - k][col + k];
  104. }
  105. if (is_palindrome(temp, length)) {
  106. local_count++;
  107. }
  108. }
  109. }
  110. }
  111.  
  112. // Reduce the local counts to rank 0
  113. MPI_Reduce(&local_count, &global_count, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
  114.  
  115. // Rank 0 prints the results
  116. if (rank == 0) {
  117. double end_time = MPI_Wtime();
  118. printf("%d palindromes of size %d found in %.6f seconds using %d threads.\n",
  119. global_count, length, end_time - start_time, num_threads);
  120. }
  121.  
  122. MPI_Barrier(MPI_COMM_WORLD);
  123. }
  124. }
  125.  
  126. MPI_Finalize();
  127. return 0;
  128. }
  129.  
Success #stdin #stdout #stderr 0.28s 40820KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Error: unexpected '/' in "/"
Execution halted