-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathomp.c
45 lines (35 loc) · 978 Bytes
/
omp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <omp.h>
#include "include/graph.h"
#include "include/queue.h"
bool bfs_OMP(Graph *g, int startVertex, int targetVertex, int numThreads) {
bool visited[g->vertexCount];
int i, j;
for (i = 0; i < g->vertexCount; i++) {
visited[i] = false;
}
Queue q;
initQueue(&q);
visited[startVertex] = true;
enqueue(&q, startVertex);
bool found = false;
while (!isQueueEmpty(&q)) {
int currVertex = dequeue(&q);
#pragma omp parallel for num_threads(numThreads) shared(found)
for (j = 0; j < g->vertexCount; j++) {
if (g->adjMatrix[currVertex][j] == 1 && !visited[j]) {
visited[j] = true;
enqueue(&q, j);
if (j == targetVertex) {
found = true;
}
}
}
if (found) {
break;
}
}
return found;
}