Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- long* calculate_omp(int** matrix, int length, int start) {
- bool* visited = new bool[length];
- long* distances = new long[length];
- long* mins = new long[omp_get_num_threads()];
- int* minNumbers = new int[omp_get_num_threads()];
- long min;
- for (int i = 0; i < length; i++) {
- distances[i] = MAXLONG;
- visited[i] = false;
- }
- for (int i = 0; i < omp_get_max_threads(); i++) {
- mins[i] = MAXLONG;
- minNumbers[i] = -1;
- }
- distances[start] = 0;
- visited[start] = true;
- int cur = start;
- int j;
- for (int i = 0; i < length - 1; i++) {
- #pragma omp parallel for shared(matrix, distances, visited, mins, minNumbers) firstprivate(length) private(cur, j) schedule(dynamic)
- for (j = 0; j < length; j++) {
- cur = minNumbers[omp_get_thread_num()];
- if(!visited[j] && (cur == -1 || distances[j] < distances[cur])) {
- mins[omp_get_thread_num()] = distances[j];
- minNumbers[omp_get_thread_num()] = j;
- }
- }
- min = MAXLONG;
- for (j = 0; j < omp_get_max_threads(); j++) {
- if (mins[j] < min) {
- min = mins[j];
- cur = j;
- }
- }
- if (distances[cur] == MAXLONG)
- break;
- visited[cur] = true;
- #pragma omp parallel for firstprivate(cur, length) private(j) shared(matrix, distances) schedule(dynamic)
- for(j = 0; j < length; j++) {
- if (distances[cur] + matrix[cur][j] < distances[j]) {
- distances[j] = distances[cur] + matrix[cur][j];
- }
- }
- }
- delete[] visited;
- return distances;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement