fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <time.h>
  5. #include <mpi.h>
  6.  
  7. enum {
  8. ROWS = 1000,
  9. COLUMNS = 1000
  10. };
  11.  
  12. int isPalindrome(const char *str, int len) {
  13. for (int i = 0; i < len / 2; i++) {
  14. if (str[i] != str[len - i - 1]) {
  15. return 0;
  16. }
  17. }
  18. return 1;
  19. }
  20.  
  21. int countPaL(const char **matrix, int row, int col, int len) {
  22. int count = 0;
  23. // Horizontal WISE
  24. if (col + len <= COLUMNS) {
  25. if (isPalindrome(matrix[row] + col, len)) {
  26. count++;
  27. }
  28. }
  29. // Vertical WISE
  30. if (row + len <= ROWS) {
  31. char *tmp = (char *)malloc(len * sizeof(char));
  32. for (int i = 0; i < len; i++) {
  33. tmp[i] = matrix[row + i][col];
  34. }
  35. if (isPalindrome(tmp, len)) {
  36. count++;
  37. }
  38. free(tmp);
  39. }
  40. // Diagonal WISE
  41. if (row + len <= ROWS && col + len <= COLUMNS) {
  42. char *tmp = (char *)malloc(len * sizeof(char));
  43. for (int i = 0; i < len; i++) {
  44. tmp[i] = matrix[row + i][col + i];
  45. }
  46. if (isPalindrome(tmp, len)) {
  47. count++;
  48. }
  49. free(tmp);
  50. }
  51. return count;
  52. }
  53.  
  54. void broadCast(char** matrix) {
  55. int i3 = 0;
  56. while (i3 < ROWS) {
  57. MPI_Bcast(matrix[i3], COLUMNS, MPI_CHAR, 0, MPI_COMM_WORLD);
  58. i3++;
  59. }
  60. }
  61.  
  62. void Free(char** matrix){
  63. int i5 = 0;
  64. while (i5 < ROWS) {
  65. free(matrix[i5]);
  66. i5++;
  67. }
  68. free(matrix);
  69. }
  70.  
  71. char** alloNgenMatrix(int rank){
  72. char **matrix = (char **)malloc(ROWS * sizeof(char *));
  73. int i1 = 0;
  74. while (i1 < ROWS) {
  75. matrix[i1] = (char*) malloc(COLUMNS * sizeof(char));
  76. i1++;
  77. }
  78. if (rank == 0) {
  79. srand(time(NULL));
  80. int i2 = 0;
  81. while (i2 < ROWS) {
  82. int j1 = 0;
  83. while (j1 < COLUMNS) {
  84. matrix[i2][j1] = (rand() % 26) + 'A';
  85. j1++;
  86. }
  87. i2++;
  88. }
  89.  
  90. }
  91. return matrix;
  92. }
  93.  
  94. void findPalindrome(int rank, char** matrix, int length, int size){
  95. double start_time = MPI_Wtime();
  96. int local_count = 0, global_count = 0, i4 = rank;
  97. while (i4 < ROWS) {
  98. for (int j = 0; j < COLUMNS; j++) {
  99. local_count += countPaL((const char **)matrix, i4, j, length);
  100. }
  101. i4 += size;
  102. }
  103. MPI_Reduce(&local_count, &global_count, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
  104. double end_time = MPI_Wtime();
  105. if (rank == 0) {
  106. printf("%d palindromes of size %d found in %lf s. using %d processes.\n",
  107. global_count, length, end_time - start_time, size);
  108. }
  109. }
  110.  
  111. int main(int argc, char *argv[]) {
  112. int rank, size;
  113. MPI_Init(&argc, &argv);
  114. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  115. MPI_Comm_size(MPI_COMM_WORLD, &size);
  116.  
  117. // Allocate and generate matrix
  118. char** matrix = alloNgenMatrix(rank);
  119. // Broadcast matrix to all processes
  120. broadCast(matrix);
  121. MPI_Barrier(MPI_COMM_WORLD);
  122.  
  123. for (int length = 3; length < 7; ++ length) {
  124. findPalindrome(rank, matrix, length, size);
  125. }
  126. if (rank == 0) {
  127. printf("=============================***********=============================\n");
  128. }
  129.  
  130.  
  131. Free(matrix);
  132. MPI_Finalize();
  133.  
  134. return 0;
  135. }
Success #stdin #stdout #stderr 0.26s 40820KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Error: unexpected '{' in "enum {"
Execution halted