-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathio.c
405 lines (326 loc) · 10.9 KB
/
io.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
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#define _POSIX_SOURCE
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdint.h>
#include <inttypes.h>
#include <stdbool.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <assert.h>
#include <GraphBLAS.h>
#if !defined(USE_SUITESPARSE)
#include <LucataGraphBLAS.h>
#endif
#include "cmdline.h"
#include "compat.h"
#include "globals.h"
extern struct gengetopt_args_info args;
static const char filetag[] = "mxmtimer";
static const char reverse_filetag[] = "remitmxm";
int
open_filename (const char* filename) {
int f;
if (args.dump_flag && args.run_powers_flag)
DIE("Dumping not compatible with running the powers kernel\n");
errno = 0;
if (args.dump_flag) {
if (!strcmp(filename, "-"))
DIE("Cannot write to stdout");
f = open (filename, O_CREAT|O_WRONLY|O_TRUNC, 0666); //fopen (filename, "w+");
} else {
if (!strcmp(filename, "-"))
f = dup(0); // stdin
else
f = open (filename, O_RDONLY); //fopen (filename, "r");
}
if (errno)
DIE_PERROR("Error opening \"%s\": ", filename);
return f;
}
GrB_Info
make_mtx_from_file (GrB_Matrix *A_out, GrB_Index * NV_out, GrB_Index * NE_out, int fd)
{
GrB_Info info = GrB_SUCCESS;
char name[1024]; // Yeah, bad idea;
GrB_Index nrows, ncols, nvals;
GrB_Index *off = NULL;
GrB_Index *colind = NULL;
uint64_t *val = NULL;
// For text, "reopen" for the f* family.
errno = 0;
int newfd = dup (fd);
if (newfd < 0)
DIE_PERROR("Cannot duplicate file descriptor");
FILE *f = fdopen (newfd, "r");
if (!f)
DIE_PERROR("Cannot move fd %d to FILE*", fd);
fscanf (f, "%s", name);
// Next input: nr, nc, nvals
{
long nrl, ncl, nvl;
fscanf (f, "%ld %ld %ld", &nrl, &ncl, &nvl);
nrows = nrl; ncols = ncl; nvals = nvl;
DEBUG_PRINT("Read %s dims %ld %ld %ld\n", name, nrl, ncl, nvl);
}
if (NV_out) *NV_out = nrows;
if (NE_out) *NE_out = nvals;
off = malloc((nrows+1) * sizeof(*off));
colind = malloc(nvals * sizeof(*colind));
val = malloc(nvals * sizeof(*val));
if (!off || !colind || !val)
DIE_PERROR("Memory allocation failed reading matrix %s: ", name);
// The nrows+1 offsets
for (size_t k = 0; k <= nrows; ++k) {
long tmp;
fscanf (f, "%ld", &tmp);
off[k] = tmp;
}
if (off[nrows] != nvals)
DIE("off[nrows] != nvals reading %s\n", name);
// Now the nvals colinds
for (size_t k = 0; k < nvals; ++k) {
long tmp;
fscanf (f, "%ld", &tmp);
if (tmp >= ncols)
DIE("Out of range column reading %s\n", name);
colind[k] = tmp;
}
// Finally the nvals values
for (size_t k = 0; k < nvals; ++k)
fscanf (f, "%" SCNu64, &val[k]);
GrB_Matrix A;
#if !defined(USE_SUITESPARSE)
info = LGB_Matrix_import_CSR_UINT64 (&A, GrB_UINT64, nrows, ncols, off, colind, val, 0);
#else
info = GxB_Matrix_import_CSR (&A, GrB_UINT64, nrows, ncols, &off, &colind, (void**)&val, (nrows+1)*sizeof(GrB_Index), nvals*sizeof(GrB_Index), nvals*sizeof(uint64_t), 0, 0, GrB_NULL);
#endif
if (info != GrB_SUCCESS)
DIE("Importing matrix %s failed: %ld\n", name, (long)info);
*A_out = A;
#if !defined(USE_SUITESPARSE)
free (val); free (colind); free (off);
#endif
fclose (f);
return GrB_SUCCESS;
}
static inline uint64_t
ensure_byteorder64 (uint64_t x, bool needs_bs)
{
if (!needs_bs) return x;
#ifdef USE_BUILTIN_BSWAP64
return __builtin_bswap64(x);
#else
x = (x >> 32) | (x << 32);
x = ((x >> 16) & UINT64_C(0x0000FFFF0000FFFF)) |
((x & UINT64_C(0x0000FFFF0000FFFF)) << 16);
x = ((x >> 8) & UINT64_C(0x00FF00FF00FF00FF)) |
((x & UINT64_C(0x00FF00FF00FF00FF)) << 8);
return x;
#endif
}
GrB_Info
make_mtx_from_binfile (GrB_Matrix *A_out, GrB_Index * NV_out, GrB_Index * NE_out, int fd)
{
GrB_Info info = GrB_SUCCESS;
char tag[9];
memset (tag, 0, sizeof(tag));
bool needs_bs = false;
uint64_t namelen;
char name[1025]; // Yeah, bad idea;
memset (name, 0, sizeof(name));
GrB_Index nrows, ncols, nvals;
GrB_Index *off = NULL;
GrB_Index *colind = NULL;
uint64_t *val = NULL;
read (fd, tag, 8);
if (!strcmp(tag, reverse_filetag))
needs_bs = true;
else if (strcmp(tag, filetag))
DIE("Unrecognized file tag %s\n", tag);
read (fd, &namelen, 8);
if (namelen > sizeof(name))
DIE("Name too long.\n");
read (fd, name, namelen);
DEBUG_PRINT("Name: %s\n", name);
{
uint64_t dims[3];
read (fd, dims, 8*3);
nrows = ensure_byteorder64(dims[0], needs_bs);
ncols = ensure_byteorder64(dims[1], needs_bs);
nvals = ensure_byteorder64(dims[2], needs_bs);
}
if (NV_out) *NV_out = nrows;
if (NE_out) *NE_out = nvals;
off = malloc((nrows+1) * sizeof(*off));
colind = malloc(nvals * sizeof(*colind));
val = malloc(nvals * sizeof(*val));
if (!off || !colind || !val)
DIE_PERROR("Memory allocation failed reading matrix %s: ", name);
// The nrows+1 offsets
read (fd, off, 8 * (nrows+1));
if (needs_bs) {
parfor (size_t k = 0; k <= nrows; ++k)
off[k] = ensure_byteorder64(off[k], true);
}
if (off[nrows] != nvals)
DIE("off[nrows] != nvals reading %s\n", name);
// Now the nvals colinds
read (fd, colind, 8 * nvals);
if (needs_bs) {
parfor (size_t k = 0; k < nvals; ++k) {
colind[k] = ensure_byteorder64(colind[k], true);
if (colind[k] >= ncols)
DIE("Out of range column reading %s\n", name);
}
}
// Finally the nvals values
read (fd, val, 8 * nvals);
if (needs_bs)
parfor (size_t k = 0; k < nvals; ++k)
val[k] = ensure_byteorder64(val[k], true);
GrB_Matrix A;
#if !defined(USE_SUITESPARSE)
info = LGB_Matrix_import_CSR_UINT64 (&A, GrB_UINT64, nrows, ncols, off, colind, val, 0);
#else
info = GxB_Matrix_import_CSR (&A, GrB_UINT64, nrows, ncols, &off, &colind, (void**)&val, (nrows+1)*sizeof(GrB_Index), nvals*sizeof(GrB_Index), nvals*sizeof(uint64_t), 0, 0, GrB_NULL);
#endif
if (info != GrB_SUCCESS)
DIE ("Importing matrix %s failed: %ld\n", name, (long)info);
*A_out = A;
#if !defined(USE_SUITESPARSE)
free (val); free (colind); free (off);
#endif
return GrB_SUCCESS;
}
void
make_file_from_mtx (GrB_Matrix A, const char *name, int fd)
{
GrB_Info info = GrB_SUCCESS;
GrB_Index nrows, ncols;
GrB_Index *off = NULL;
GrB_Index *colind = NULL;
uint64_t *val = NULL;
errno = 0;
int newfd = dup (fd);
if (newfd < 0)
DIE_PERROR("Cannot duplicate file descriptor");
FILE *f = fdopen (newfd, "w");
#if !defined(USE_SUITESPARSE)
info = LGB_Matrix_export_CSR_UINT64 (A, NULL, &nrows, &ncols, &off, &colind, &val, NULL);
if (info != GrB_SUCCESS)
DIE("Export of %s failed: %ld\n", name, (long)info);
#else
{
GrB_Matrix dupA;
info = GrB_Matrix_dup (&dupA, A);
if (info != GrB_SUCCESS)
DIE("Duplicating matrix %s: %ld\n", name, (long)info);
GrB_Type type;
GrB_Index off_size, colind_size, val_size;
bool is_uniformed;
bool jumbled;
info = GxB_Matrix_export_CSR (&dupA, &type, &nrows, &ncols, &off, &colind, (void**)&val, &off_size, &colind_size, &val_size, &is_uniformed, &jumbled, GrB_NULL);
if (info != GrB_SUCCESS)
DIE("Export of %s failed: %ld\n", name, (long)info);
/* Not only is this not implemented, it's wrong. */
/* if (is_uniformed) */
/* DIE("%s is wearing a funny hat.\n", name); */
if (jumbled)
DIE("Assumed %s is sorted and it ain't.", name);
GrB_free (&dupA);
}
#endif
GrB_Index nnz = off[nrows];
DEBUG_PRINT("Writing name %s dims %ld %ld %ld\n", name, (long)nrows, (long)ncols, (long)nnz);
fprintf (f, "%s\n", name);
fprintf (f, "%ld %ld %ld\n", (long)nrows, (long)ncols, (long)nnz);
if (nrows > 0) {
fprintf (f, "%ld", (long)off[0]);
for (size_t k = 1; k <= nrows; ++k)
fprintf (f, " %ld", (long)off[k]);
fprintf (f, "\n");
}
if (nnz > 0) {
for (size_t i = 0; i < nrows; ++i) {
const size_t begin = off[i];
const size_t end = off[i+1];
if (begin != end) {
fprintf (f, "%ld", (long)colind[begin]);
for (size_t k = begin+1; k < end; ++k)
fprintf (f, " %ld", (long)colind[k]);
fprintf (f, "\n\n");
}
}
fprintf (f, "\n\n\n\n");
for (size_t i = 0; i < nrows; ++i) {
const size_t begin = off[i];
const size_t end = off[i+1];
if (begin != end) {
fprintf (f, "%" PRIu64, val[begin]);
for (size_t k = begin+1; k < end; ++k)
fprintf (f, " %" PRIu64, val[k]);
fprintf (f, "\n\n");
}
}
fprintf (f, "\n");
}
free (val);
free (colind);
free (off);
fclose (f);
}
void
make_binfile_from_mtx (GrB_Matrix A, const char *name, int fd)
{
GrB_Info info = GrB_SUCCESS;
GrB_Index nrows, ncols;
GrB_Index *off = NULL;
GrB_Index *colind = NULL;
uint64_t *val = NULL;
#if !defined(USE_SUITESPARSE)
info = LGB_Matrix_export_CSR_UINT64 (A, NULL, &nrows, &ncols, &off, &colind, &val, NULL);
if (info != GrB_SUCCESS)
DIE("Export of %s failed: %ld\n", name, (long)info);
#else
{
GrB_Matrix dupA;
info = GrB_Matrix_dup (&dupA, A);
if (info != GrB_SUCCESS)
DIE("Duplicating matrix %s: %ld\n", name, (long)info);
GrB_Type type;
GrB_Index off_size, colind_size, val_size;
bool is_uniformed;
bool jumbled;
info = GxB_Matrix_export_CSR (&dupA, &type, &nrows, &ncols, &off, &colind, (void**)&val, &off_size, &colind_size, &val_size, &is_uniformed, &jumbled, GrB_NULL);
if (info != GrB_SUCCESS)
DIE("Export of %s failed: %ld\n", name, (long)info);
/* Not only is this not implemented, it's wrong. */
/* if (is_uniformed) */
/* DIE("%s is wearing a funny hat.\n", name); */
if (jumbled)
DIE("Assumed %s is sorted and it ain't.", name);
GrB_free (&dupA);
}
#endif
GrB_Index nnz = off[nrows];
DEBUG_PRINT("Writing name %s dims %ld %ld %ld\n", name, (long)nrows, (long)ncols, (long)nnz);
write (fd, filetag, 8);
uint64_t namelen = strlen(name)+1;
write (fd, &namelen, 8);
write (fd, name, namelen);
write (fd, &nrows, 8);
write (fd, &ncols, 8);
write (fd, &nnz, 8);
write (fd, off, 8 * (nrows+1));
if (nnz > 0) {
write (fd, colind, 8 * nnz);
write (fd, val, 8 * nnz);
}
free (val);
free (colind);
free (off);
}