-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathundirected.c
173 lines (143 loc) · 3.77 KB
/
undirected.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <assert.h>
#include "defs.h"
#include "globals.h"
#if !defined(__MTA__)
#include "compat/xmt-ops.h"
#endif
#include <sys/mman.h>
#include "stinger-atomics.h"
static int
intcmp (const void *pa, const void *pb)
{
const int64_t a = *(const int64_t *) pa;
const int64_t b = *(const int64_t *) pb;
return a - b;
}
/*
Form transpose.
Sort transpose.
Count output degrees.
Allocate output.
Merge.
*/
graph *
makeUndirected(graph *G, struct graph_shared ** shared, char ** name)
{
graph * out = NULL;
int64_t NV, NE;
int64_t * restrict xoff, * restrict xadj, * restrict newoff;
int64_t sum;
if (!G) return NULL;
out = xmalloc (sizeof (*out));
NV = G->numVertices;
NE = G->numEdges;
xoff = (int64_t *)xmmap (NULL, (2*(NV+1) + 2*NE) * sizeof (int64_t), PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANON, 0,0);
xadj = &xoff[NV+1];
newoff = &xadj[2*NE];
memset (xoff, 0, (2*(NV+1) + 2*NE) * sizeof (int64_t));
OMP("omp parallel for")
MTA("mta assert nodep")
for (int64_t k = 0; k < NE; ++k)
stinger_int_fetch_add (&xoff[1+G->endVertex[k]], 1);
sum = 0;
for (int64_t k = 1; k <= NV; ++k)
{
int64_t tmp = xoff[k];
xoff[k] = sum;
sum += tmp;
}
OMP("omp parallel for")
MTA("mta assert nodep")
for (int64_t k = 0; k < NE; ++k)
{
const int64_t i = G->endVertex[k];
const int64_t loc = stinger_int_fetch_add (&xoff[i+1], 1);
xadj[2*loc] = G->startVertex[k];
xadj[1+2*loc] = G->intWeight[k];
}
assert (xoff[NV] == NE);
OMP("omp parallel for")
MTA("mta assert parallel")
for (int64_t k = 0; k < NV; ++k)
if (xoff[k] < xoff[k+1])
qsort (&xadj[2*xoff[k]], xoff[k+1] - xoff[k], 2*sizeof (int64_t), intcmp);
OMP("omp parallel for")
MTA("mta assert nodep")
for (int64_t i = 0; i < NV; ++i)
{
int64_t deg = xoff[i+1] - xoff[i];
const int64_t adjend = G->edgeStart[i+1];
for (int64_t k = G->edgeStart[i]; k != adjend; ++k)
{
const int64_t j = G->endVertex[k];
void * foo = NULL;
foo = bsearch (&j, &xadj[2*xoff[i]], xoff[i+1] - xoff[i], 2*sizeof (int64_t), intcmp);
if (!foo) ++deg;
}
newoff[i] = deg;
}
sum = 0;
for (int64_t i = 0; i < NV; ++i)
{
int64_t tmp = newoff[i];
newoff[i] = sum;
sum += tmp;
}
newoff[NV] = sum;
assert (sum <= 2*NE);
if (shared == NULL)
alloc_graph (out, NV, sum);
else
alloc_shared_graph (out, NV, sum, shared, name);
MTA("mta assert nodep")
for (int64_t i = 0; i < NV; ++i)
{
int64_t kout = newoff[i];
int64_t deg = xoff[i+1] - xoff[i];
const int64_t adjend = G->edgeStart[i+1];
for (int64_t k = 0; k < deg; ++k)
{
out->startVertex[kout] = i;
out->endVertex[kout] = xadj[2*(k + xoff[i])];
out->intWeight[kout] = xadj[1+2*(k + xoff[i])];
++kout;
}
for (int64_t k = G->edgeStart[i]; k != adjend; ++k)
{
const int64_t j = G->endVertex[k];
int64_t * ploc = NULL;
MTA("mta assert nodep")
for (int64_t kk = newoff[i]; kk < newoff[i]+deg; ++kk)
{
if (j == out->endVertex[kk]) ploc = &out->endVertex[kk];
}
if (!ploc)
{
out->startVertex[kout] = i;
out->endVertex[kout] = j;
out->intWeight[kout] = G->intWeight[k];
++kout;
}
else
{
int64_t loc = (ploc - out->endVertex) / sizeof (int64_t);
out->intWeight[loc] += G->intWeight[k];
}
}
assert (kout == newoff[i+1]);
}
assert (sum == newoff[NV]);
OMP("omp parallel for")
MTA("mta assert nodep")
for (int64_t i = 0; i <= NV; ++i) out->edgeStart[i] = newoff[i];
OMP("omp parallel for")
MTA("mta assert nodep")
for (int64_t i = 0; i < NV; ++i) out->marks[i] = 0;
munmap (xoff, (NV+1 + 2*NE) * sizeof (int64_t));
return out;
}