-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph.c
More file actions
373 lines (315 loc) · 9.41 KB
/
graph.c
File metadata and controls
373 lines (315 loc) · 9.41 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#include "graph.h"
/* Initialize graph. */
int initialize(graph *g, char *filename, char badji)
{
// Set number of threads
omp_set_num_threads(NTHREADS);
// Check graph file name length
if (strlen(filename) > FILENAMELEN)
{
fprintf(stderr, "Max file name length exceeded.\n");
return 1;
}
// Open graph file
strcpy(g->filename, filename);
g->stream = fopen(g->filename, "r");
// Check file stream
if (g->stream == NULL)
{
fprintf(stderr, "Could not open BADJ file.\n");
return 1;
}
// Get numbers of nodes and edges
fread(&g->n, sizeof(unsigned long long), 1, g->stream);
fread(&g->m, sizeof(unsigned long long), 1, g->stream);
// Check number of nodes
if (g->n > MAXNODES)
{
fprintf(stderr, "Too many nodes to handle: %llu\n", g->n);
return 1;
}
// Check for badji file
g->badji = badji;
// If graph has badji file
if (g->badji)
{
// Open badji file
char badjiname[FILENAMELEN];
strcpy(badjiname, g->filename);
strcat(badjiname, "i");
FILE *badjistream = (FILE *) fopen(badjiname, "r");
// Check file stream
if (badjistream == NULL)
{
fprintf(stderr, "Could not open badji file.\n");
return 1;
}
// Get number of blocks, block indices, and first nodes
fread(&g->nblks, sizeof(unsigned long long), 1, badjistream);
g->indices = malloc(g->nblks * sizeof(unsigned long long));
fread(g->indices, sizeof(unsigned long long), g->nblks, badjistream);
g->firstnodes = malloc(g->nblks * sizeof(unsigned int));
fread(g->firstnodes, sizeof(unsigned int), g->nblks, badjistream);
// Close badji file
fclose(badjistream);
if (g->nblks < NTHREADS)
{
fprintf(stderr, "Too many threads for too small a graph.\n");
return 1;
}
// Initialize blocks
unsigned int i;
for (i = 0; i < NTHREADS; i++)
{
// Open file for block
g->currblock[i] = fopen(g->filename, "r");
g->currblockno[i] = i - NTHREADS + 1;
// Check file stream
if (g->currblock[i] == NULL)
{
fprintf(stderr, "Could not open BADJ file.\n");
return 1;
}
}
}
return 0;
}
/* Destroy graph. */
int destroy(graph *g)
{
// Close graph file
fclose(g->stream);
// If graph has badji file
if (g->badji)
{
// Destroy blocks
unsigned int i;
for (i = 0; i < NTHREADS; i++)
{
// Close file for block
fclose(g->currblock[i]);
}
}
return 0;
}
/* Transpose a BADJ graph. */
int transpose(graph *g, char *filename)
{
// Allocate space for degrees in transpose
unsigned int *degt = malloc(sizeof(unsigned int) * g->n);
unsigned int *currdegt = malloc(sizeof(unsigned int) * g->n);
unsigned int i;
for (i = 0; i < g->n; i++)
{
degt[i] = 0;
currdegt[i] = 0;
}
// For each node
unsigned int deg;
unsigned int *adj;
for (i = 0; i < g->n; i++)
{
// Read adjacency list
fread(°, sizeof(unsigned int), 1, g->stream);
adj = malloc(sizeof(unsigned int) * deg);
fread(adj, sizeof(unsigned int), deg, g->stream);
// Increment degrees in transpose
unsigned int j;
for (j = 0; j < deg; j++)
{
degt[adj[j]]++;
}
free(adj);
}
// Allocate space for adjacency list in transpose
unsigned int **adjt = malloc(sizeof(unsigned int *) * g->n);
for (i = 0; i < g->n; i++)
{
adjt[i] = malloc(sizeof(unsigned int) * degt[i]);
}
// Rewind graph
fseek(g->stream, 2*sizeof(unsigned long long), SEEK_SET);
// For each node
for (i = 0; i < g->n; i++)
{
// Read adjacency list
fread(°, sizeof(unsigned int), 1, g->stream);
adj = malloc(sizeof(unsigned int) * deg);
fread(adj, sizeof(unsigned int), deg, g->stream);
// Update adjacency list in transpose
unsigned int j;
for (j = 0; j < deg; j++)
{
adjt[adj[j]][currdegt[adj[j]]] = i;
currdegt[adj[j]]++;
}
free(adj);
}
// Create BADJ file
FILE *out = fopen(filename, "w");
fwrite(&g->n, sizeof(unsigned long long), 1, out);
fwrite(&g->m, sizeof(unsigned long long), 1, out);
// For each node
for (i = 0; i < g->n; i++)
{
// Write degree and adjacent nodes in transpose
fwrite(°t[i], sizeof(unsigned int), 1, out);
fwrite(adjt[i], sizeof(unsigned int), degt[i], out);
}
// Clean up
fclose(out);
free(degt);
free(currdegt);
free(adjt);
return 0;
}
/* Compute the locality of a BADJ graph. */
int locality(graph *g, unsigned int window, double *loc)
{
// Initialize number of local references
unsigned int refs = 0;
// Initialize previous node
unsigned int prev = ((unsigned int) - 1) / 2;
// For each node
unsigned int deg;
unsigned int *adj;
unsigned int i;
for (i = 0; i < g->n; i++)
{
// Read adjacency list
fread(°, sizeof(unsigned int), 1, g->stream);
adj = malloc(sizeof(unsigned int) * deg);
fread(adj, sizeof(unsigned int), deg, g->stream);
// For each neighbor
unsigned int j;
for (j = 0; j < deg; j++)
{
// Increment number of local reference if node is within window
if (abs(adj[j] - prev) < window)
{
refs++;
}
// Update previous node
prev = adj[j];
}
free(adj);
}
// Find percentage of local references
*loc = (double) refs / (double) (g->m - 1);
return 0;
}
/* Create a badji file for a BADJ graph. */
int badjindex(graph *g)
{
// Declare variables
unsigned long long nblks = 0;
unsigned int currnode = 0;
unsigned long long indices[MAXBLKS];
unsigned int firstnodes[MAXBLKS];
// Initialize block
unsigned int *block = malloc(BLOCKLEN);
// For each block
while (1)
{
// Set block index and first node
indices[nblks] = ftello(g->stream);
firstnodes[nblks] = currnode;
// Read maximal block
unsigned int intsread = fread(block, sizeof(unsigned int), BLOCKLEN / sizeof(unsigned int), g->stream);
unsigned int blocklen = 0;
// Check for end of file
if (intsread == 0)
{
break;
}
// Increment the number of blocks
nblks++;
if (nblks > MAXBLKS)
{
fprintf(stderr, "Too many blocks to handle.\n");
return 1;
}
// For each node
while (1)
{
// Get degree
unsigned int deg = block[blocklen];
// Test whether node fits in block
if (blocklen + 1 + deg <= intsread)
{
// If so, increment block length
blocklen = blocklen + 1 + deg;
currnode++;
}
else
{
// Otherwise, unread node
int extra = intsread - blocklen;
fseek(g->stream, -extra*sizeof(unsigned int), SEEK_CUR);
break;
}
}
}
// Create badji file
char filename[FILENAMELEN];
strcpy(filename, g->filename);
strcat(filename, "i");
FILE *out = (FILE *) fopen(filename, "w");
// Check file stream
if (out == NULL)
{
fprintf(stderr, "Could not open file.\n");
return 1;
}
// Write number of blocks, block indices, and first nodes
fwrite(&nblks, sizeof(unsigned long long), 1, out);
fwrite(indices, sizeof(unsigned long long), nblks, out);
fwrite(firstnodes, sizeof(unsigned int), nblks, out);
// Close badji file
fclose(out);
// Free block
free(block);
return 0;
}
/* Get the next block of the graph. */
int nextblock(graph *g, unsigned int threadno)
{
// Test for badji file
if (!g->badji)
{
fprintf(stderr, "Graph must have a badji file.\n");
return 1;
}
// Increment block number
g->currblockno[threadno] += NTHREADS;
if (g->currblockno[threadno] > g->nblks)
{
g->currblockno[threadno] = threadno + 1;
}
// Set block file pointer
fseeko(g->currblock[threadno], g->indices[g->currblockno[threadno]-1], SEEK_SET);
// Set current node
g->currnode[threadno] = g->firstnodes[g->currblockno[threadno]-1];
return 0;
}
/* Get the next node of the block. */
unsigned int nextnode(graph *g, node *v, unsigned int threadno)
{
// Test for badji file
if (!g->badji)
{
fprintf(stderr, "Graph must have a badji file.\n");
return 1;
}
// If there is no next node
if ((g->currblockno[threadno] < g->nblks && g->currnode[threadno] == g->firstnodes[g->currblockno[threadno]]) || feof(g->currblock[threadno]))
{
return -1;
}
// Otherwise, read next node
fread(&v->deg, sizeof(unsigned int), 1, g->currblock[threadno]);
v->adj = malloc(v->deg * sizeof(unsigned int));
fread(v->adj, sizeof(unsigned int), v->deg, g->currblock[threadno]);
g->currnode[threadno]++;
return (g->currnode[threadno] - 1);
}