source: proto/SymbolTable/wcd_pbgs_identity_template.cpp @ 1721

Last change on this file since 1721 was 1721, checked in by vla24, 8 years ago

SymbolTable?: completed dictionary implementation and refactored templates

File size: 11.1 KB
Line 
1#include "../common_definitions.h"
2#include <pbgs_identity_symbol_table.h>
3
4#include "../wcd_common_functions.h"
5#include "../symtab_common_functions.h"
6#include "parser_common_functions_generated.h"
7
8#ifdef BUFFER_PROFILING
9        BOM_Table * parser_timer;
10
11#elif CODE_CLOCKER
12        #define NUM_EVENTS 1
13        int Events[NUM_EVENTS] = {PAPI_TOT_CYC};
14        //int Events[NUM_EVENTS] = {PAPI_L2_DCM};
15        //int Events[NUM_EVENTS] = {PAPI_TOT_CYC, PAPI_BR_MSP};
16        int cal_size = 20;
17        CC * parser_timer = new CC(Events,NUM_EVENTS,cal_size);
18#else
19        void * parser_timer;
20#endif
21
22int block_base=0;
23int buffer_base=0;
24int buffer_last;
25char * source;
26
27BitBlock elem_starts;
28int previous_block_last_elem_start;
29BytePack hashvalues[2];
30
31vector <int> gids;
32PBGSIdentitySymbolTable pbgs_symbol_table;
33
34static inline void postprocess_do_block(Dictionary& dictionary, Hash_data hash_data);
35template<bool allow_performance_check> void do_process(FILE *infile, FILE *outfile);
36
37template <int L> static inline void validate_block_length_grouping(BitBlockForwardIterator & start, int block_base);
38template <int L> static inline int ElemEnd_grouping(int pos);
39
40int main(int argc, char * argv[]) {
41    char * dictionaryfilename, * infilename, * outfilename;
42    FILE * dictionaryfile, *infile, *outfile;
43
44    getFilenames(argc, argv, dictionaryfilename, infilename, outfilename);
45    openInputOutputFiles(dictionaryfilename, infilename, outfilename,
46                         dictionaryfile, infile, outfile);
47
48    int greatest_GID_in_dictionary;
49    populateDictionary(dictionaryfile, outfile, gids, greatest_GID_in_dictionary);
50
51//      PERF_SEC_BIND(1);
52
53    PERF_SEC_INIT(parser_timer);
54
55    // store symbols form text to Symbol Table
56    do_process<true>(infile, outfile);
57
58    PERF_SEC_DUMP(parser_timer);
59
60    PERF_SEC_DESTROY(parser_timer);
61
62    // gather dictionary statistics
63    int totalKnownWordsInDictionary, totalUnknownWordsInDictionary;
64    wordCountInDictionary(greatest_GID_in_dictionary, gids, totalUnknownWordsInDictionary, totalKnownWordsInDictionary);
65    printWordCountInDictionary(totalUnknownWordsInDictionary, totalKnownWordsInDictionary);
66
67    fclose(dictionaryfile);
68    fclose(infile);
69    fclose(outfile);
70
71#if PRINT_SYMBOL_DISTRIBUTION
72//    print_GIDS();
73    pbgs_symbol_table.Print_Symbol_Table_Distribution();
74#endif
75
76    return(0);
77}
78
79template <int L>
80static inline int ElemEnd_grouping(int end) {
81    int start = end - L;
82    int hashvalue = compute_hash_value(L, start - block_base, hashvalues);
83    int gid = pbgs_symbol_table.Lookup_or_Insert_Name<L>(source + start, hashvalue);
84    gids.push_back(gid);
85#if DEBUG
86    char* symbol = new char[L+1];
87    strncpy ( symbol, source + start, L );
88    symbol[L] ='\0';
89    printf ("%s | start: %i[%i] | end: %i[%i] | gid: %i | hashvalue: %i | symbol: %s\n", __FUNCTION__, start, start-buffer_base, end, end-buffer_base, gid, hashvalue, symbol );
90    delete symbol; symbol = 0;
91#endif
92    return 0;
93}
94
95template<>
96inline int ElemEnd_grouping<17>(int end) {
97    int start = ScanBackwardPos (&elem_starts, end - block_base, previous_block_last_elem_start) + block_base;
98    int lgth = end - start;
99    int hashvalue = compute_hash_value(lgth, start - block_base, hashvalues);
100    int gid = 0;
101
102//    if (lgth < 32)
103//    {
104//      gid = pbgs_symbol_table.Lookup_or_Insert_Name_32(source + start, hashvalue, lgth);
105//    }
106//    else
107    {
108        gid = pbgs_symbol_table.Lookup_or_Insert_Name(source + start, hashvalue, lgth);
109    }
110    gids.push_back(gid);
111#if DEBUG
112    char* symbol = new char[lgth+1];
113    strncpy ( symbol, source + start, lgth );
114    symbol[lgth] ='\0';
115    printf ("%s | start: %i[%i] | end: %i[%i] | lgth: %i | hashvalue: %i | gid: %i | symbol: %s\n", __FUNCTION__, start, start - block_base, end, end - block_base, lgth, hashvalue, gid, symbol);
116#endif
117    return 0;
118}
119
120template <int L>
121static inline void validate_block_length_grouping(BitBlockForwardIterator & start, int block_base) {
122
123    BitBlockForwardIterator end;
124    int block_pos;
125
126    while(start != end) {
127        block_pos = block_base + *start;
128        ElemEnd_grouping<L>(block_pos);
129        start++;
130    }
131}
132
133static inline void postprocess_do_block(Dictionary& dictionary, Hash_data hash_data){
134
135    elem_starts = dictionary.Word_starts;
136    hashvalues[1] = hash_data.Hash_value;
137
138    if ( bitblock_has_bit(dictionary.Word_ends_1) )
139    {
140        BitBlockForwardIterator iter_length_grouping_1(&dictionary.Word_ends_1);
141        validate_block_length_grouping<1>(iter_length_grouping_1, block_base);
142    }
143
144    if ( bitblock_has_bit(dictionary.Word_ends_2) )
145    {
146        BitBlockForwardIterator iter_length_grouping_2(&dictionary.Word_ends_2);
147        validate_block_length_grouping<2>(iter_length_grouping_2, block_base);
148    }
149
150    if ( bitblock_has_bit(dictionary.Word_ends_3) )
151    {
152        BitBlockForwardIterator iter_length_grouping_3(&dictionary.Word_ends_3);
153        validate_block_length_grouping<3>(iter_length_grouping_3, block_base);
154    }
155
156    if ( bitblock_has_bit(dictionary.Word_ends_4) )
157    {
158        BitBlockForwardIterator iter_length_grouping_4(&dictionary.Word_ends_4);
159        validate_block_length_grouping<4>(iter_length_grouping_4, block_base);
160    }
161
162    if ( bitblock_has_bit(dictionary.Word_ends_5) )
163    {
164        BitBlockForwardIterator iter_length_grouping_5(&dictionary.Word_ends_5);
165        validate_block_length_grouping<5>(iter_length_grouping_5, block_base);
166    }
167
168    if ( bitblock_has_bit(dictionary.Word_ends_6) )
169    {
170        BitBlockForwardIterator iter_length_grouping_6(&dictionary.Word_ends_6);
171        validate_block_length_grouping<6>(iter_length_grouping_6, block_base);
172    }
173
174    if ( bitblock_has_bit(dictionary.Word_ends_7) )
175    {
176        BitBlockForwardIterator iter_length_grouping_7(&dictionary.Word_ends_7);
177        validate_block_length_grouping<7>(iter_length_grouping_7, block_base);
178    }
179
180    if ( bitblock_has_bit(dictionary.Word_ends_8) )
181    {
182        BitBlockForwardIterator iter_length_grouping_8(&dictionary.Word_ends_8);
183        validate_block_length_grouping<8>(iter_length_grouping_8, block_base);
184    }
185
186    if ( bitblock_has_bit(dictionary.Word_ends_9) )
187    {
188        BitBlockForwardIterator iter_length_grouping_9(&dictionary.Word_ends_9);
189        validate_block_length_grouping<9>(iter_length_grouping_9, block_base);
190    }
191
192    if ( bitblock_has_bit(dictionary.Word_ends_10) )
193    {
194        BitBlockForwardIterator iter_length_grouping_10(&dictionary.Word_ends_10);
195        validate_block_length_grouping<10>(iter_length_grouping_10, block_base);
196    }
197
198    if ( bitblock_has_bit(dictionary.Word_ends_11) )
199    {
200        BitBlockForwardIterator iter_length_grouping_11(&dictionary.Word_ends_11);
201        validate_block_length_grouping<11>(iter_length_grouping_11, block_base);
202    }
203
204    if ( bitblock_has_bit(dictionary.Word_ends_12) )
205    {
206        BitBlockForwardIterator iter_length_grouping_12(&dictionary.Word_ends_12);
207        validate_block_length_grouping<12>(iter_length_grouping_12, block_base);
208    }
209
210    if ( bitblock_has_bit(dictionary.Word_ends_13) )
211    {
212        BitBlockForwardIterator iter_length_grouping_13(&dictionary.Word_ends_13);
213        validate_block_length_grouping<13>(iter_length_grouping_13, block_base);
214    }
215
216    if ( bitblock_has_bit(dictionary.Word_ends_14) )
217    {
218        BitBlockForwardIterator iter_length_grouping_14(&dictionary.Word_ends_14);
219        validate_block_length_grouping<14>(iter_length_grouping_14, block_base);
220    }
221
222    if ( bitblock_has_bit(dictionary.Word_ends_15) )
223    {
224        BitBlockForwardIterator iter_length_grouping_15(&dictionary.Word_ends_15);
225        validate_block_length_grouping<15>(iter_length_grouping_15, block_base);
226    }
227
228    if ( bitblock_has_bit(dictionary.Word_ends_16) )
229    {
230        BitBlockForwardIterator iter_length_grouping_16(&dictionary.Word_ends_16);
231        validate_block_length_grouping<16>(iter_length_grouping_16, block_base);
232    }
233
234    if ( bitblock_has_bit(dictionary.Word_ends_17_and_longer) )
235    {
236        BitBlockForwardIterator iter_length_grouping_remaining(&dictionary.Word_ends_17_and_longer);
237        validate_block_length_grouping<17>(iter_length_grouping_remaining, block_base);
238    }
239
240    // Store the last starting position in case we hit boundary case
241    previous_block_last_elem_start = - count_reverse_zeroes (elem_starts);
242
243    //copy current hash value data as previous one.
244    memmove (&hashvalues[0], &hashvalues[1], 16);
245}
246
247template<bool allow_performance_check> void do_process(FILE *infile, FILE *outfile) {
248
249@decl
250
251  int buf_pos = 0;
252  int block_pos = 0;
253  int errpos = 0;
254  int chars_avail = 0;
255  int check_pos = 0;
256  int chars_read = 0;
257  BytePack buf[(BUFFER_SIZE+BLOCK_SIZE+OVERLAP_BUFSIZE*2)/sizeof(SIMD_type)];
258
259  char * srcbuf = ((char *) buf) + OVERLAP_BUFSIZE;
260  buffer_base = buf_pos;
261  source = srcbuf;
262
263  chars_read = fread((void *)srcbuf, 1, BUFFER_SIZE + OVERLAP_BUFSIZE, infile);
264  chars_avail = chars_read;
265  if (chars_avail > BUFFER_SIZE) chars_avail = BUFFER_SIZE;
266
267  Entity_Info * e = new Entity_Info;
268  e->AnalyzeSignature((unsigned char *)srcbuf);
269
270  if (e->content_start != 0) {
271        memmove(&srcbuf[0], &srcbuf[e->content_start], chars_read - e->content_start);
272        buf_pos = e->content_start;
273        buffer_base = buf_pos;
274        if (chars_avail == BUFFER_SIZE) {
275                chars_read = chars_read - e->content_start +
276                             fread(&srcbuf[chars_read-e->content_start], 1, e->content_start, infile);
277                chars_avail = chars_read;
278                if (chars_avail > BUFFER_SIZE) chars_avail = BUFFER_SIZE;
279        }
280        else {
281          chars_read -=e->content_start;
282          chars_avail -=e->content_start;
283        }
284  }
285
286@stream_stmts
287
288/* Full Buffers */
289
290    while (chars_avail == BUFFER_SIZE) {
291      if (allow_performance_check)
292      {
293        PERF_SEC_START(parser_timer);
294      }
295
296      for (int blk = 0; blk < SEGMENT_BLOCKS; blk++) {
297          block_base = blk*BLOCK_SIZE;
298          s2p_do_block((BytePack *) &srcbuf[block_base], basis_bits);
299          @block_stmts
300          postprocess_do_block(dictionary, hash_data);
301      }
302
303      if (allow_performance_check)
304      {
305        PERF_SEC_END(parser_timer, chars_avail);
306      }
307      int bytes_left = chars_read - chars_avail;
308      memmove(buf, &srcbuf[BUFFER_SIZE - OVERLAP_BUFSIZE], bytes_left + OVERLAP_BUFSIZE);
309      chars_read = fread(&srcbuf[bytes_left],1, BUFFER_SIZE + OVERLAP_BUFSIZE - bytes_left, infile) + bytes_left;
310      chars_avail = chars_read;
311      if (chars_avail > BUFFER_SIZE) chars_avail = BUFFER_SIZE;
312      buf_pos += chars_avail;
313      buffer_base = buf_pos;
314    }
315/* Final Partial Buffer */
316    if (allow_performance_check)
317    {
318        PERF_SEC_START(parser_timer);
319    }
320
321    block_pos = 0;
322    int remaining = chars_avail;
323/* Full Blocks */
324    while (remaining >= BLOCK_SIZE) {
325          block_base = block_pos;
326          s2p_do_block((BytePack *) &srcbuf[block_pos], basis_bits);
327          @block_stmts
328          postprocess_do_block(dictionary, hash_data);
329          block_pos += BLOCK_SIZE;
330          remaining -= BLOCK_SIZE;
331    }
332    block_base = block_pos;
333    if (remaining > 0 || @any_carry) {
334          EOF_mask = bitblock::srl(simd<1>::constant<1>(), convert(BLOCK_SIZE-remaining));
335          s2p_do_final_block((BytePack *) &srcbuf[block_pos], basis_bits, EOF_mask);
336          @final_block_stmts
337          postprocess_do_block(dictionary, hash_data);
338    }
339    buf_pos += chars_avail;
340    buffer_base = buf_pos;
341    if (allow_performance_check)
342    {
343        PERF_SEC_END(parser_timer, chars_avail);
344    }
345}
Note: See TracBrowser for help on using the repository browser.