bokunda

Radi stvarno PP

Apr 19th, 2021 (edited)
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.97 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <mpi.h>
  5.  
  6. #define ROW_LENGTH 3
  7.  
  8. void printMatrix(int **matrix, int m, int n)
  9. {
  10. int i, j;
  11. for (i = 0; i < m; i++)
  12. {
  13. for (j = 0; j < n; j++)
  14. {
  15. printf("%d ", matrix[i][j]);
  16. }
  17. printf("\n");
  18. }
  19. }
  20.  
  21. int getDistance(int **matrix, int *visited, int c, int n)
  22. {
  23. int i, j, tempDistance = 0, distance = 0;
  24. visited[c] = 1;
  25.  
  26. for (i = 0; i < n; i++)
  27. {
  28. // If our city is in the route
  29. if ((matrix[i][0] == c || matrix[i][1] == c))
  30. {
  31. // Check if city is not visited
  32. int cityId = matrix[i][0] != c ? matrix[i][0] : matrix[i][1];
  33. if (visited[cityId] == -1)
  34. {
  35. visited[cityId] = 1;
  36. distance += matrix[i][2];
  37. }
  38. if (distance == 0) return distance;
  39. else
  40. {
  41. distance += getDistance(matrix, visited, cityId, n);
  42. }
  43. }
  44. }
  45.  
  46. return distance;
  47. }
  48.  
  49. int findCost(int **matrix, int X, int Y,int n, int i) {
  50. for(i = 0; i < n; i++) if ((matrix[i][0] == X && matrix[i][1] == Y) || (matrix[i][0] == Y && matrix[i][1] == X)) return matrix[i][2];
  51. return 99999;
  52. }
  53. int calculateMaxLocalDistance(int **matrix, int startI, int stopI, int n, int rank)
  54. {
  55. int maxLocalDistance = -1, distance = 0;
  56.  
  57. int w, ww, flag = 0;
  58.  
  59. for(w = startI; w <= stopI; w++)
  60. {
  61. for(ww = 1; ww <= n; ww++)
  62. {
  63. int dist[n],prev[n],selected[n],i,m,min,start,d,j;
  64. char path[n];
  65. int target = ww;
  66. for(i=1;i<n;i++)
  67. {
  68. dist[i] = 99999;
  69. selected[i] = 0;
  70. }
  71. start = w;
  72. selected[start]=1;
  73. dist[start] = 0;
  74. while(selected[target] ==0)
  75. {
  76. min = 99999;
  77. m = 0;
  78. for(i=1;i< n;i++)
  79. {
  80. int findedCost = findCost(matrix, start, i, n, 0);
  81. d = dist[start] + findedCost;
  82. if(d < dist[i]&&selected[i]==0)
  83. {
  84. dist[i] = d;
  85. }
  86. if(min>dist[i] && selected[i]==0)
  87. {
  88. min = dist[i];
  89. m = i;
  90. }
  91. }
  92. start = m;
  93. selected[start] = 1;
  94. }
  95.  
  96. distance = dist[target];
  97.  
  98. if (distance > maxLocalDistance && distance < 100)
  99. {
  100. maxLocalDistance = distance;
  101. }
  102. }
  103. }
  104.  
  105.  
  106.  
  107. printf("Max local distance: %d\n", maxLocalDistance);
  108. return maxLocalDistance;
  109. }
  110.  
  111. void doWork(int **matrix, int n, int rank, int size)
  112. {
  113. // Set indexes
  114. int maxDistance;
  115. int batchCount = n / size;
  116. int startI = rank * batchCount + 1;
  117. int stopI = startI + batchCount;
  118. if (rank != 0) startI++;
  119. if (rank == size - 1 && n % 2 != 0) stopI++;
  120.  
  121. // Calculate max route where start city is in batch range startI-stopI
  122. int maxLocalDistance = calculateMaxLocalDistance(matrix, startI, stopI, n, rank);
  123.  
  124. // Do reduction to get longest calculated distances between cities
  125. MPI_Reduce(&maxLocalDistance, &maxDistance, 1, MPI_INT, MPI_MAX, 0, MPI_COMM_WORLD);
  126.  
  127. // Print the result
  128. if (rank == 0) printf("%d\n", maxDistance);
  129. }
  130.  
  131. int main(int argc, char **argv)
  132. {
  133. int rank, size;
  134. int i, j, n, N;
  135.  
  136. MPI_Init(&argc, &argv);
  137.  
  138. MPI_Comm_size(MPI_COMM_WORLD, &size);
  139. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  140.  
  141. int **matrix = (int**)malloc(n * sizeof(int*));
  142. for (i = 0; i < n; i++) matrix[i] = (int*)malloc(ROW_LENGTH * sizeof(int));
  143.  
  144. int **matrixG = (int**)malloc(n * sizeof(int*));
  145. for (i = 0; i < n; i++) matrixG[i] = (int*)malloc(ROW_LENGTH * sizeof(int));
  146.  
  147. // Read input data
  148. if (rank == 0)
  149. {
  150. scanf("%d", &N);
  151. n = N;
  152.  
  153. for (i = 0; i < n - 1; i++) scanf("%d %d %d", &matrix[i][0], &matrix[i][1], &matrix[i][2]);
  154. }
  155.  
  156. // Broadcast input data
  157. MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD);
  158. for (i = 0; i < n - 1; i++) MPI_Bcast(matrix[i], ROW_LENGTH, MPI_INT, 0, MPI_COMM_WORLD);
  159.  
  160. MPI_Barrier(MPI_COMM_WORLD);
  161.  
  162. //printMatrix(matrix, n - 1, ROW_LENGTH);
  163.  
  164.  
  165. // doWork
  166. // Idea is to check all possible combinations in next way
  167. // Because we have N cities we will create batches N / size of cities to check
  168. // and each city process will calculate longest posible route for its batch
  169. // In the end, we will get the best one by reduction.
  170.  
  171. // Do work returns maxLocalDistance -> after this step, we must calculate global maximum for all processes.
  172. doWork(matrix, n - 1, rank, size);
  173.  
  174. MPI_Finalize();
  175.  
  176. return 0;
  177. }
Add Comment
Please, Sign In to add comment