-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread-graph-el.c
153 lines (131 loc) · 2.7 KB
/
read-graph-el.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
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <stdint.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#include <sys/mman.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
//#include <getopt.h>
#include "defs.h"
#include "xmt-luc.h"
#include "read-graph-el.h"
#if defined(__MTA__)
#define MTA(x) _Pragma(x)
#else
#define MTA(x)
#endif
#if defined(_OPENMP)
#include <omp.h>
#define OMP(x) _Pragma(x)
#else
#define OMP(x)
#endif
static int io_ready = 0;
static void
io_init (void)
{
#if defined(__MTA__)
if (!io_ready) {
extern void xmt_luc_io_init (void);
xmt_luc_io_init ();
}
#endif
io_ready = 1;
}
MTA("mta inline")
MTA("mta expect parallel context")
static int64_t bs64 (int64_t)
#if defined(__GNUC__)
__attribute__((const))
#endif
;
int64_t
bs64 (int64_t xin)
{
uint64_t x = (uint64_t)xin; /* avoid sign-extension issues */
x = (x >> 32) | (x << 32);
x = ((x & ((uint64_t)0xFFFF0000FFFF0000ull)) >> 16)
| ((x & ((uint64_t)0x0000FFFF0000FFFFull)) << 16);
x = ((x & ((uint64_t)0xFF00FF00FF00FF00ull)) >> 8)
| ((x & ((uint64_t)0x00FF00FF00FF00FFull)) << 8);
return x;
}
static void
bs64_n (size_t n, int64_t * restrict d)
{
OMP("omp parallel for")
MTA("mta assert nodep")
for (size_t k = 0; k < n; ++k)
d[k] = bs64 (d[k]);
}
static ssize_t
xread(int fd, void *buf, size_t len)
{
ssize_t lenread;
while (1) {
lenread = read(fd, buf, len);
if ((lenread < 0) && (errno == EAGAIN || errno == EINTR))
continue;
return lenread;
}
}
static void
free_el (struct el * g)
{
if (!g || !g->mem) return;
xmunmap (g->mem, g->memsz);
memset (g, 0, sizeof (*g));
}
void
read_graph_el (const char * fname,
struct el * g)
{
const uint64_t endian_check = 0x1234ABCDul;
size_t sz;
int64_t *mem;
io_init ();
xmt_luc_stat (fname, &sz);
if (sz % sizeof (*mem)) {
fprintf (stderr, "graph file size is not a multiple of sizeof (int64_t)\n");
abort ();
}
mem = xmmap_alloc (sz);
#if !defined(__MTA__)
{
int fd;
if ( (fd = open (fname, O_RDONLY)) < 0) {
perror ("Error opening initial graph");
abort ();
}
if (sz != xread (fd, mem, sz)) {
perror ("Error reading initial graph");
abort ();
}
close (fd);
}
#else /* __MTA__ */
{
extern void xmt_luc_snapin (const char*, void*, size_t);
xmt_luc_snapin (fname, mem, sz);
}
#endif /* __MTA__ */
if (endian_check != *mem)
bs64_n (sz / sizeof (*mem), mem);
g->nv = mem[1];
g->ne = mem[2];
g->mem = mem;
g->memsz = sz;
g->d = &mem[3];
g->el = &mem[3+g->nv];
g->free = free_el;
}
void
free_graph_el (struct el * g)
{
if (g && g->mem && g->free) g->free (g);
}