-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnectedComponents.c
85 lines (76 loc) · 1.63 KB
/
connectedComponents.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <stdio.h>
#include <stdlib.h>
#ifdef __MTA__
#include <sys/mta_task.h>
#include <machine/mtaops.h>
#include <machine/runtime.h>
#else
#include "compat/xmt-ops.h"
#endif
#include "defs.h"
#include "globals.h"
int64_t connectedComponents(graph *G)
{
int64_t count = 0;
int64_t max_iter = 10;
int64_t nchanged;
const int64_t NV = G->numVertices;
const int64_t NE = G->numEdges;
const int64_t * restrict sV = G->startVertex;
const int64_t * restrict eV = G->endVertex;
int64_t * restrict D = G->marks;
OMP("omp parallel for")
for (int64_t i = 0; i < NV; ++i)
D[i] = i;
while (max_iter > 0)
{
max_iter--;
nchanged = 0;
MTA("mta assert nodep")
OMP("omp parallel for reduction(+:nchanged)")
for (int64_t k = 0; k < NE; ++k)
{
const int64_t i = sV[k];
const int64_t j = eV[k];
if (D[i] < D[j])
{
D[j] = D[i];
++nchanged;
}
}
if (!nchanged) break;
}
if (nchanged) {
while (1)
{
nchanged = 0;
MTA("mta assert nodep")
OMP("omp parallel for reduction(+:nchanged)")
for (int64_t k = 0; k < NE; ++k)
{
const int64_t i = sV[k];
const int64_t j = eV[k];
if (D[i] < D[j] && D[j] == D[D[j]])
{
D[D[j]] = D[i];
++nchanged;
}
}
if (!nchanged) break;
MTA("mta assert nodep")
OMP("omp parallel for")
for (int64_t i = 0; i < NV; ++i)
while (D[i] != D[D[i]])
D[i] = D[D[i]];
}
}
MTA("mta assert nodep")
OMP("omp parallel for reduction(+:count)")
for (int64_t i = 0; i < NV; ++i)
{
while (D[i] != D[D[i]])
D[i] = D[D[i]];
if (D[i] == i) ++count;
}
return count;
}