1 #ifndef FILTER_IO_H_
2 #define FILTER_IO_H_
3
4 #include <gmp.h> // for mpz_t
5 #include <stdint.h> // for uint64_t, int64_t
6 #include <time.h> // for NULL
7 #include "bit_vector.h" // for bit_vector_srcptr
8 #include "timing.h" // for timingstats_dict_ptr
9 #include "typedefs.h" // for prime_t, weight_t
10
11 #define MAX_FILES 1000000
12
13 #define RELATION_MAX_BYTES 4096
14
15 /* Size of relations buffer between parsing & processing.
16 * CAREFUL! SIZE_BUF_REL must be greater (at least double) than (1<<(NNFR+1)).
17 * Stores the sentences precomputed but not inserted.
18 * About 64K sentences for the optimal.
19 */
20 // #define SIZE_BUF_REL MAX((1<<(NFR+NNFR+1+(NFR==0))),(1<<6))
21 #define SIZE_BUF_REL (1<<15)
22
23 /* we want the programs to specify in a completely explicit way whether
24 * they want stuff in base 10 or 16 */
25 #define EARLYPARSE_NEED_AB_DECIMAL 1
26 #define EARLYPARSE_NEED_AB_HEXA 2
27 #define EARLYPARSE_NEED_LINE 4
28 /* for reading ideals (e.g. output of las) */
29 #define EARLYPARSE_NEED_PRIMES 8
30 /* for reading index (i.e. renumber ideal) */
31 #define EARLYPARSE_NEED_INDEX 16
32 #define EARLYPARSE_NEED_SM 32
33 #define EARLYPARSE_NEED_SORTED 64
34 #define EARLYPARSE_NEED_INDEX_SORTED (EARLYPARSE_NEED_INDEX | EARLYPARSE_NEED_SORTED)
35
36
37 /* Initial size of primes_data array in earlyparsed_relation_s,
38 If more than NB_PRIMES_OPT is needed (should be rare), *primes is
39 allocated
40 */
41 #define NB_PRIMES_OPT 31
42
43
44 /* This field does not contain a proper relation, but only something
45 * which has undergone quite limited parsing within a thread whose job is
46 * to read data fast, and not to bother about the fine points of the
47 * relation semantics. Because of this, there is no unique definition of
48 * which are the valid fields below. This depends on how this field is
49 * meant to be used downstream. Depending on the earlyparse_needed_data
50 * bitmask argument fed to filter_relations, we may fill only some fields.
51 * Which fields are filled is controlled by which of the
52 * EARLYPARSE_NEED_* appear in the earlyparse_needed_data bitmask. The
53 * callback thread function given to process_rels is then called for each
54 * such "relation"
55 */
56 struct earlyparsed_relation_s {
57 int64_t a;
58 uint64_t b;
59 prime_t *primes; /*if nb_alloc <= NB_PRIME_OPT, primes == primes_data*/
60 prime_t primes_data[NB_PRIMES_OPT];
61 weight_t nb; /* number of primes */
62 weight_t nb_alloc; /* allocated space for primes
63 * (if > NB_PRIMES_OPT: indirect addressing,
64 * otherwise primes == primes_data) */
65 /* nb_above_min_index is counted only when ->primes is needed anyway,
66 * so we defer it to the callback function instead.
67 */
68 // weight_t nb_above_min_index; /* nb of primes above min_index, must be <=nb */
69 uint64_t num; /* (absolute) relation number */
70 char *line; /* If not NULL, contains the relation with a '\n' at the end */
71 mpz_t * sm;
72 int sm_size;
73 int sm_alloc;
74 };
75 typedef struct earlyparsed_relation_s earlyparsed_relation[1];
76 typedef struct earlyparsed_relation_s * earlyparsed_relation_ptr;
77 typedef const struct earlyparsed_relation_s * earlyparsed_relation_srcptr;
78
79 static const unsigned char ugly[256] = {
80 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
81 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
82 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
83 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 255, 255, 255, 255, 255, 255,
84 255, 10, 11, 12, 13, 14, 15, 255, 255, 255, 255, 255, 255, 255, 255, 255,
85 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
86 255, 10, 11, 12, 13, 14, 15, 255, 255, 255, 255, 255, 255, 255, 255, 255,
87 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
88 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
89 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
90 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
91 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
92 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
93 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
94 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
95 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255
96 };
97
98 #ifdef __cplusplus
99 extern "C" {
100 #endif
101
102 extern void realloc_buffer_primes (earlyparsed_relation_ptr buf);
103
realloc_buffer_primes_c(earlyparsed_relation_ptr buf)104 static inline void realloc_buffer_primes_c (earlyparsed_relation_ptr buf)
105 {
106 realloc_buffer_primes (buf);
107 }
108
109 extern int filter_rels_force_posix_threads;
110 /*
111 * A pointer to such a structure must be provided to filter_rels, and
112 * termination is indicated by f==NULL. The function specified in the
113 * k-th member (starting from 1) in this array describes the operation
114 * performed at level k in the process, level 0 being the implicit
115 * production of relation from the input files. Alongside with the
116 * relation on which the thread is allowed to work, all threads at level
117 * k receive the void* argument specified in the array member. n
118 * specifies the number of worker threads to be used for each step.
119 */
120
121 struct filter_rels_description {
122 void * (*f)(void*, earlyparsed_relation_ptr);
123 void * arg;
124 int n;
125 };
126
127 typedef void *(*filter_rels_callback_t) (void *, earlyparsed_relation_ptr);
128
129 extern uint64_t filter_rels2(char ** input_files,
130 struct filter_rels_description * desc,
131 int earlyparse_needed_data,
132 bit_vector_srcptr active,
133 timingstats_dict_ptr);
134
filter_rels(char ** input_files,filter_rels_callback_t f,void * arg,int earlyparse_needed_data,bit_vector_srcptr active,timingstats_dict_ptr stats)135 static inline uint64_t filter_rels(char ** input_files,
136 filter_rels_callback_t f,
137 void * arg,
138 int earlyparse_needed_data,
139 bit_vector_srcptr active,
140 timingstats_dict_ptr stats)
141 {
142 /* Of course I would prefer designated intializers here.
143 * Unfortunately this header is included by C++ code as well, which
144 * makes them illegal (at least with gcc-4.6.3 ; gcc-4.8.1 groks
145 * them). So I stick to dumb code.
146 */
147 struct filter_rels_description desc[2] = {
148 { f, arg, 1, }, { NULL, NULL, 0, },
149 };
150 return filter_rels2(input_files, desc, earlyparse_needed_data, active, stats);
151 }
152
153
154 #ifdef __cplusplus
155 }
156 #endif
157
158 #endif /* FILTER_IO_H_ */
159
160