Advertisement
Guest User

Untitled

a guest
May 15th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.71 KB | None | 0 0
  1. long* calculate_omp(int** matrix, int length, int start) {
  2.     bool* visited = new bool[length];
  3.     long* distances = new long[length];
  4.     long* mins = new long[omp_get_num_threads()];
  5.     int* minNumbers = new int[omp_get_num_threads()];
  6.     long min;
  7.  
  8.     for (int i = 0; i < length; i++) {
  9.         distances[i] = MAXLONG;
  10.         visited[i] = false;
  11.     }
  12.  
  13.     for (int i = 0; i < omp_get_max_threads(); i++) {
  14.         mins[i] = MAXLONG;
  15.         minNumbers[i] = -1;
  16.     }
  17.  
  18.     distances[start] = 0;
  19.     visited[start] = true;
  20.     int cur = start;
  21.     int j;
  22.  
  23.     for (int i = 0; i < length - 1; i++) {
  24.         #pragma omp parallel for shared(matrix, distances, visited, mins, minNumbers) firstprivate(length) private(cur, j) schedule(dynamic)
  25.         for (j = 0; j < length; j++) {
  26.             cur = minNumbers[omp_get_thread_num()];
  27.             if(!visited[j] && (cur == -1 || distances[j] < distances[cur])) {
  28.                 mins[omp_get_thread_num()] = distances[j];
  29.                 minNumbers[omp_get_thread_num()] = j;
  30.             }
  31.         }
  32.  
  33.         min = MAXLONG;
  34.         for (j = 0; j < omp_get_max_threads(); j++) {
  35.             if (mins[j] < min) {
  36.                 min = mins[j];
  37.                 cur = j;
  38.             }
  39.         }
  40.  
  41.         if (distances[cur] == MAXLONG)
  42.             break;
  43.         visited[cur] = true;
  44.  
  45.         #pragma omp parallel for firstprivate(cur, length) private(j) shared(matrix, distances) schedule(dynamic)
  46.         for(j = 0; j < length; j++) {
  47.             if (distances[cur] + matrix[cur][j] < distances[j]) {
  48.                 distances[j] = distances[cur] + matrix[cur][j];
  49.             }
  50.         }
  51.     }
  52.  
  53.     delete[] visited;
  54.     return distances;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement