-
Notifications
You must be signed in to change notification settings - Fork 8
/
listz_handle.c
557 lines (496 loc) · 15.2 KB
/
listz_handle.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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
#define _GNU_SOURCE
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#ifdef _OPENMP
#include <omp.h>
#endif
#if defined(HAVE_AIO_READ) && defined(WANT_AIO)
/* For EAGAIN etc. */
#include <errno.h>
#endif
#include "listz_handle.h"
/* #define TRACE_ITER yes */
/* Init a listz_handle_t to store up to len residues (modulo m).
If filename != NULL, uses disk storage, otherwise memory.
Returns NULL if something goes wrong (i.e., if a memory allocation
or opening a file fails) */
listz_handle_t
listz_handle_init (const char *filename, const uint64_t len, const mpz_t m)
{
listz_handle_t F;
void *buf;
F = malloc (sizeof (_listz_handle_t));
if (F == NULL)
return NULL;
/* Find out how many file_word_t's m has */
buf = (file_word_t *) mpz_export (NULL, &F->words, -1, sizeof(file_word_t),
-1, 0, m);
if (buf == NULL)
{
free (F);
return NULL;
}
free(buf);
F->len = len;
if (filename == NULL)
{
F->storage = 0; /* Memory storage */
F->data.mem = init_list2 (len, mpz_sizeinbase(m, 2));
if (F->data.mem == NULL)
{
free (F);
F = NULL;
}
} else {
F->storage = 1; /* Disk storage */
F->filename = (char *) malloc ((strlen (filename) + 1) * sizeof(char));
if (F->filename == NULL)
{
free (F);
return NULL;
}
strcpy (F->filename, filename);
F->data.file = fopen (F->filename, "rb+");
if (F->data.file == NULL)
F->data.file = fopen (F->filename, "wb+");
if (F->data.file == NULL)
{
free (F->filename);
free (F);
return NULL;
}
#ifdef HAVE_FALLOCATE
fallocate (fileno(F->data.file), 0, (off_t) 0,
F->words * sizeof(file_word_t) * len);
#endif
#if defined(HAVE_SETVBUF) && defined(HAVE_AIO_READ)
/* Set to unbuffered mode as we use aio_*() functions for reading
in the background */
setvbuf (F->data.file, NULL, _IONBF, 0);
#endif
}
return F;
}
listz_handle_t
listz_handle_from_listz (const listz_t l, const uint64_t len, const mpz_t m)
{
listz_handle_t F;
void *buf;
F = malloc (sizeof (_listz_handle_t));
if (F == NULL)
return NULL;
/* Find out how many file_word_t's m has */
buf = (file_word_t *) mpz_export (NULL, &F->words, -1, sizeof(file_word_t),
-1, 0, m);
if (buf == NULL)
{
free (F);
return NULL;
}
free(buf);
F->len = len;
F->storage = 0; /* Memory storage */
F->data.mem = l;
return F;
}
void
listz_handle_clear (listz_handle_t F)
{
if (F->storage == 0)
{
clear_list (F->data.mem, F->len);
F->data.mem = NULL;
}
else
{
fclose (F->data.file);
F->data.file = NULL;
remove (F->filename);
free (F->filename);
}
free (F);
}
static inline void
export_residue (file_word_t *buf, const size_t bufsize, const mpz_t r)
{
size_t nr;
/* Export r to buf */
mpz_export (buf, &nr, -1, sizeof(file_word_t), 0, 0, r);
ASSERT_ALWAYS (nr <= bufsize);
/* Pad buf with zeroes */
for ( ; nr < bufsize; nr++)
buf[nr] = 0;
}
static inline int
listz_handle_seek_entry (listz_handle_t F, const uint64_t index)
{
int64_t foffset;
ASSERT (F->storage == 1);
ASSERT (index <= INT64_MAX);
ASSERT (index <= INT64_MAX / sizeof(file_word_t) / F->words);
foffset = (int64_t) index * F->words * sizeof(file_word_t);
return aux_fseek64 (F->data.file, foffset, SEEK_SET);
}
/* Output a polynomial of degree len-1, or a monic polynomial of degree len.
In either case, len is the number of coefficients read from "l".
If symmetric == 1, then the polynomial is printed as a reciprocal Laurent
polynomial where the coefficients stored in l (and perhaps the leading
monomial) are in standard basis. */
void
listz_handle_output_poly (const listz_handle_t l, const uint64_t len,
const int monic, const int symmetric,
const char *prefix, const char *suffix,
const int verbosity)
{
uint64_t i;
mpz_t m;
listz_iterator_t *iter;
if (!test_verbose(verbosity))
return;
if (prefix != NULL)
outputf (verbosity, prefix);
if (len == 0)
{
if (monic)
outputf (verbosity, "1\n");
else
outputf (verbosity, "0\n");
return;
}
mpz_init (m);
iter = listz_iterator_init (l, 0);
for (i = 0; i < len; i++)
{
const uint64_t deg = len - ((monic != 0) ? 0U : 1U);
const char *plus = (i < deg) ? " + " : "";
listz_iterator_read (iter, m);
if (symmetric)
outputf (verbosity, "Mod(%Zd,N) * (x^%" PRIu64 " + x^-%" PRIu64 ")%s",
m, i, i, plus);
else
outputf (verbosity, "Mod(%Zd,N) * x^%" PRIu64 "%s", m, i, plus);
}
if (monic)
{
if (symmetric)
outputf (verbosity, "(x^%" PRIu64 " + x^-%" PRIu64 ")", len, len);
else
outputf (verbosity, "x^%" PRIu64, len);
}
listz_iterator_clear (iter);
if (suffix != NULL)
outputf (verbosity, suffix);
mpz_clear (m);
}
/* Iterator functions for sequential access to elements of a
list_handle_t. */
static void
listz_iterator_fetch (listz_iterator_t *iter, const uint64_t offset)
{
ASSERT (iter->handle->storage == 1);
iter->offset = offset;
#if defined(HAVE_AIO_READ) && defined(WANT_AIO)
iter->cb.aio_offset = (off_t) offset * iter->handle->words * sizeof(file_word_t);
iter->cb.aio_buf = iter->buf[iter->active_buffer];
iter->cb.aio_nbytes = iter->bufsize * iter->handle->words * sizeof(file_word_t);
{
int r = aio_read (&iter->cb);
if (r != 0)
{
fprintf (stderr, "%s(): aio_read() returned %d\n", __func__, r);
abort ();
}
}
#else /* ifdef HAVE_AIO_READ */
#ifdef _OPENMP
#pragma omp critical
#endif
{
listz_handle_seek_entry (iter->handle, iter->offset);
iter->valid = fread (iter->buf, iter->handle->words * sizeof(file_word_t),
iter->bufsize, iter->handle->data.file);
}
#endif /* ifdef HAVE_AIO_READ else */
}
#if defined(HAVE_AIO_READ) && defined(WANT_AIO)
static size_t
listz_iterator_suspend (struct aiocb * const cb)
{
int r;
ssize_t s;
do {
const struct aiocb * aiocb_list[1] = {cb};
r = aio_suspend (aiocb_list, 1, NULL);
} while (r == EAGAIN || r == EINTR);
if (r != 0)
{
fprintf (stderr, "%s(): aio_suspend() returned error, errno = %d\n",
__func__, errno);
abort ();
}
s = aio_return (cb);
if (s < 0)
{
fprintf (stderr, "%s(): aio_return() returned error code %ld\n",
__func__, (long int) s);
abort();
}
return (size_t) s;
}
#endif
static void
listz_iterator_flush (listz_iterator_t *iter)
{
ASSERT (iter->handle->storage == 1);
if (iter->writeptr == 0)
return;
#if defined(HAVE_AIO_READ) && defined(WANT_AIO)
{
size_t nbytes, written;
int r;
iter->cb.aio_offset =
(off_t) iter->offset * iter->handle->words * sizeof(file_word_t);
iter->cb.aio_buf = iter->buf[iter->active_buffer];
nbytes = iter->writeptr * iter->handle->words * sizeof(file_word_t);
iter->cb.aio_nbytes = nbytes;
r = aio_write (&iter->cb);
if (r != 0)
{
fprintf (stderr, "%s(): aio_write() returned error, errno = %d\n",
__func__, errno);
abort ();
}
written = listz_iterator_suspend (&iter->cb);
ASSERT_ALWAYS (written == nbytes);
}
#else
#ifdef _OPENMP
#pragma omp critical
#endif
{
size_t written;
listz_handle_seek_entry (iter->handle, iter->offset);
written = fwrite (iter->buf, sizeof(file_word_t) * iter->handle->words,
iter->writeptr, iter->handle->data.file);
ASSERT_ALWAYS (written == iter->writeptr);
}
#endif
iter->writeptr = 0;
}
listz_iterator_t *
listz_iterator_init2 (listz_handle_t h, const uint64_t firstres,
const size_t nr_buffered)
{
listz_iterator_t *iter;
iter = (listz_iterator_t *) malloc (sizeof(listz_iterator_t));
if (iter == NULL)
return NULL;
iter->handle = h;
if (iter->handle->storage == 0)
iter->readptr = iter->writeptr = (size_t) firstres;
else
{
iter->offset = firstres;
iter->readptr = iter->writeptr = iter->valid = 0;
iter->bufsize = nr_buffered;
#if defined(HAVE_AIO_READ) && defined(WANT_AIO)
iter->buf[0] = malloc (iter->bufsize * iter->handle->words *
sizeof(file_word_t));
iter->buf[1] = malloc (iter->bufsize * iter->handle->words *
sizeof(file_word_t));
if (iter->buf[0] == NULL || iter->buf[1] == NULL)
{
free (iter->buf[0]);
free (iter->buf[1]);
free (iter);
return NULL;
}
#ifdef _OPENMP
#pragma omp critical
#endif
{
/* Prevent other access to the file which would lead to data
corruption */
iter->hidden_file = h->data.file;
h->data.file = NULL;
}
ASSERT_ALWAYS (iter->hidden_file != NULL);
iter->active_buffer = 0;
memset (&iter->cb, 0, sizeof(struct aiocb));
iter->cb.aio_fildes = fileno (iter->hidden_file);
iter->cb.aio_reqprio = 0;
iter->cb.aio_sigevent.sigev_notify = SIGEV_NONE;
#else
iter->buf = malloc (iter->bufsize * iter->handle->words *
sizeof(file_word_t));
if (iter->buf == NULL)
{
free (iter);
return NULL;
}
#endif
}
return iter;
}
listz_iterator_t *
listz_iterator_init (listz_handle_t h, const uint64_t firstres)
{
const size_t listz_iterator_nr_buffered = 1<<16;
return listz_iterator_init2 (h, firstres, listz_iterator_nr_buffered);
}
void
listz_iterator_clear (listz_iterator_t *iter)
{
if (iter->handle->storage == 1)
{
#if defined(HAVE_AIO_READ) && defined(WANT_AIO)
if (iter->valid > 0)
{
listz_iterator_suspend (&iter->cb);
}
#endif
listz_iterator_flush (iter);
#if defined(HAVE_AIO_READ) && defined(WANT_AIO)
iter->handle->data.file = iter->hidden_file;
free (iter->buf[0]);
free (iter->buf[1]);
#else
free (iter->buf);
#endif
}
free (iter);
}
/* Outside of listz_iterator_newbuf() we have:
If iter->valid == 0, then there is no outstanding read request
If iter->valid != 0, then there is an outstanding read request for the
non-active buffer
There is no outstanding write request
If there is an outstanding read request, then iter->offset is the start
position in the file (in units of residues) of that read request
*/
static inline void
listz_iterator_switchbuf (listz_iterator_t *iter)
{
#if defined(HAVE_AIO_READ) && defined(WANT_AIO)
if (iter->valid == 0)
{
/* We never read data before. We have to fetch data for the
current buffer and wait for it to finish, then issue a fetch
for the next buffer */
listz_iterator_fetch (iter, iter->offset);
iter->active_buffer ^= 1;
}
iter->valid = listz_iterator_suspend (&iter->cb)
/ sizeof(file_word_t) / iter->handle->words;
#endif
listz_iterator_flush (iter);
listz_iterator_fetch (iter, iter->offset + iter->valid);
#if defined(HAVE_AIO_READ) && defined(WANT_AIO)
iter->active_buffer ^= 1;
#endif
iter->readptr = 0;
ASSERT_ALWAYS (iter->valid > 0);
}
void
listz_iterator_read (listz_iterator_t *iter, mpz_t r)
{
if (iter->handle->storage == 0)
{
mpz_set (r, iter->handle->data.mem[iter->readptr]);
#if defined(TRACE_ITER)
gmp_printf ("%s(): readptr = %" PRIu64 ", r = %Zd (in memory)\n",
__func__, (uint64_t) iter->readptr, r);
#endif
}
else
{
/* Try to detect incorrect use of iterator. We allow either read-only,
in which case we must have writeptr == 0 at all times, or sequential
update (read-then-write) of each residue, in which case we must have
writeptr == readptr here */
ASSERT (iter->writeptr == 0 || iter->readptr == iter->writeptr);
if (iter->readptr == iter->valid)
listz_iterator_switchbuf (iter);
#if defined(HAVE_AIO_READ) && defined(WANT_AIO)
mpz_import (r, iter->handle->words, -1, sizeof(file_word_t), 0, 0,
&iter->buf[iter->active_buffer][iter->readptr * iter->handle->words]);
#else
mpz_import (r, iter->handle->words, -1, sizeof(file_word_t), 0, 0,
&iter->buf[iter->readptr * iter->handle->words]);
#endif
#if defined(TRACE_ITER)
gmp_printf ("%s(): offset = %" PRIu64 ", readptr = %" PRIu64
", r = %Zd (on disk)\n",
__func__, iter->offset, (uint64_t) iter->readptr, r);
#endif
}
iter->readptr++;
}
void
listz_iterator_write (listz_iterator_t *iter, const mpz_t r)
{
if (iter->handle->storage == 0)
{
#if defined(TRACE_ITER)
gmp_printf ("%s(): writeptr = %"PRIu64", r = %Zd\n",
__func__, (uint64_t) iter->writeptr, r);
#endif
mpz_set (iter->handle->data.mem[iter->writeptr], r);
}
else
{
/* Try to detect incorrect use of iterator. We allow either write-only,
in which case we must have readptr == 0 at all times, or sequential
update (read-then-write) of each residue, in which case we must have
writeptr + 1 == readptr */
ASSERT (iter->readptr == 0 || iter->writeptr + 1 == iter->readptr);
ASSERT (iter->writeptr <= iter->bufsize);
#if defined(TRACE_ITER)
gmp_printf ("%s(): offset = %"PRIu64", writeptr = %"PRIu64
", r = %Zd (on disk)\n",
__func__, iter->offset, (uint64_t) iter->writeptr, r);
#endif
if (iter->writeptr == iter->bufsize)
{
#if defined(TRACE_ITER)
printf ("%s(): flushing %"PRIu64" entries from buffer %d\n",
iter->writeptr, iter->active_buffer);
#endif
listz_iterator_flush (iter);
iter->offset += iter->bufsize;
}
ASSERT_ALWAYS (mpz_sgn (r) >= 0);
/* TODO: we may want to allow residues that are not fully reduced
(mod modulus), but only as far as the ECRT reduces them. */
#if defined(HAVE_AIO_READ) && defined(WANT_AIO)
export_residue (&iter->buf[iter->active_buffer][iter->writeptr * iter->handle->words],
iter->handle->words, r);
#else
export_residue (&iter->buf[iter->writeptr * iter->handle->words],
iter->handle->words, r);
#endif
}
iter->writeptr++;
}
/* Functions that can be used as callbacks to listz_iterator_read() and
listz_iterator_write() in mpzspv_fromto_mpzv(). Note that calling, e.g.,
listz_iterator_read() by de-referencing a pointer of type mpz_producerfunc_t
leads to undefined program behavior according to the C standard, even though
it happens to work fine on x86[_64] architectures at least. On other
architectures, a function pointer may carry contextual information which
could be incorrect when de-referencing a function pointer of the wrong type.
*/
void
listz_iterator_read_callback (void *iter, mpz_t r)
{
listz_iterator_read ((listz_iterator_t *) iter, r);
}
void
listz_iterator_write_callback (void *iter, const mpz_t r)
{
listz_iterator_write ((listz_iterator_t *) iter, r);
}