source: proto/SymbolTable/symtab_pbgs_div_template.cpp @ 1786

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

Added some code to support performance tests using PAPI

File size: 13.6 KB
Line 
1#include "../common_definitions.h"
2#include <pbgs_div_symbol_table.h>
3
4#include "../symtab_common_functions.h"
5#include "../xmlwf_common_functions.h"
6
7#ifdef BUFFER_PROFILING
8        BOM_Table * parser_timer;
9
10#elif CODE_CLOCKER
11        #define NUM_EVENTS 1
12        //      int Events[NUM_EVENTS] = {PAPI_TOT_CYC};
13//              int Events[NUM_EVENTS] = {PAPI_L2_DCM};
14                int Events[NUM_EVENTS] = {PAPI_L1_DCM};
15//              int Events[NUM_EVENTS] = {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
22
23int block_base=0;
24int buffer_base=0;
25int buffer_last;
26
27TagMatcher matcher;
28
29BitBlock elem_ends;
30int last_elem_start;
31bool block_boundary_case = false;
32BytePack hashvalues[2];
33
34vector <int> gids;
35PBGSDivSymbolTable pbgs_symbol_table;
36
37static inline void postprocess_do_block(Lex & lex, CtCDPI_Callouts & ctCDPI_Callouts, Ref_Callouts & ref_Callouts, Check_streams & check_streams, Tag_Callouts & tag_Callouts, Hash_data & hash_data, int chars_avail);
38
39void do_process(FILE *infile, FILE *outfile);
40
41template <int L> static inline void validate_block_length_grouping(BitBlockForwardIterator & start, int block_base);
42
43static inline int ElemStart_grouping(int start_pos, int lgth); // lgth > 16
44template <int L> static inline int ElemEnd_grouping(int pos, int length);
45
46int main(int argc, char * argv[]) {
47    char * infilename, * outfilename;
48    FILE *infile, *outfile;
49
50    getFilenames(argc, argv, infilename, outfilename);
51    openInputOutputFiles(infilename, outfilename,
52                         infile, outfile);
53
54//      PERF_SEC_BIND(1);
55
56    PERF_SEC_INIT(parser_timer);
57
58    // store symbols form text to Symbol Table
59    do_process(infile, outfile);
60
61    PERF_SEC_DUMP(parser_timer);
62
63    PERF_SEC_DESTROY(parser_timer);
64
65    fclose(infile);
66    fclose(outfile);
67
68#if PRINT_SYMBOL_DISTRIBUTION
69//    print_GIDS();
70    pbgs_symbol_table.Print_Symbol_Table_Distribution();
71#endif
72
73        return(0);
74}
75
76// length in [1,16]
77template <int L>
78static inline int ElemEnd_grouping(int end) {
79    int start = end - L;
80    int hashvalue = compute_hash_value(L, start - block_base, hashvalues);
81    int gid = pbgs_symbol_table.Lookup_or_Insert_Name<L>(source + start, hashvalue);
82    gids.push_back(gid);
83#if DEBUG
84    char* symbol = new char[L+1];
85    strncpy ( symbol, source + start, L );
86    symbol[L] ='\0';
87    printf ("%s | start: %i[%i] | end: %i[%i] | lgth: %i | gid: %i | hashvalue: %i | symbol: %s\n", __FUNCTION__, start, start-buffer_base, end, end-buffer_base, L, gid, hashvalue, symbol );
88    delete symbol; symbol = 0;
89#endif
90    return 0;
91}
92
93// length > 16
94static inline int ElemStart_grouping(int start, int lgth) {
95    int hashvalue = compute_hash_value(lgth, start - block_base, hashvalues);
96    int gid = pbgs_symbol_table.Lookup_or_Insert_Name(source + start, hashvalue, lgth);
97    gids.push_back(gid);
98#if DEBUG
99    char* symbol = new char[lgth+1];
100    strncpy ( symbol, source + start, lgth );
101    symbol[lgth] ='\0';
102    printf ("%s | start: %i[%i] | lgth: %i | hashvalue: %i | gid: %i | symbol: %s\n", __FUNCTION__, start, start - block_base, lgth, hashvalue, gid, symbol);
103#endif
104    return 0;
105}
106
107// L = 2, pass in bitstream for symbols length [1,2]
108// L = 4, pass in bitstream for symbols length [3,4]
109// L = 6, pass in bitstream for symbols length [5,6]
110// L = 8, pass in bitstream for symbols length [7,8]
111// L = 10, pass in bitstream for symbols length [9,10]
112// L = 12, pass in bitstream for symbols length [11,12]
113// L = 14, pass in bitstream for symbols length [13,14]
114// L = 16, pass in bitstream for symbols length [15,16]
115// L = 17, pass in bitstream for symbols length longer than 16
116template <int L>
117static inline void validate_block_length_grouping(BitBlockForwardIterator & start, int block_base) {
118
119    BitBlockForwardIterator end;
120    int end_pos;
121
122    while(start != end) {
123        end_pos = *start;
124        ElemEnd_grouping<L>(end_pos + block_base);
125        start++;
126    }
127}
128
129template <>
130inline void validate_block_length_grouping<17>(BitBlockForwardIterator & start, int block_base) {
131
132    BitBlockForwardIterator end;
133    int start_pos, end_pos;
134
135    while(start != end) {
136        start_pos = *start;
137        end_pos = ScanForwardPos (&elem_ends, start_pos);
138        if (end_pos)
139        {
140            ElemStart_grouping(start_pos - 16 + block_base, end_pos - start_pos + 16);
141        }
142        else
143        {
144#if DEBUG
145            printf ("There is no more 1 bit in the block. pos: %i | sym: %c%c[%c]\n", start_pos,
146                    source[start_pos + block_base-2], source[start_pos + block_base-1], source[start_pos + block_base]);
147#endif
148            //handle boundary case
149            block_boundary_case = true;
150            last_elem_start = start_pos - 16 - BLOCK_SIZE;
151#if DEBUG
152            printf ("last_elem_start: %i\n", last_elem_start);
153#endif
154        }
155        start++;
156    }
157}
158
159static inline void postprocess_do_block(Lex & lex, CtCDPI_Callouts & ctCDPI_Callouts, Ref_Callouts & ref_Callouts, Check_streams & check_streams, Tag_Callouts & tag_Callouts, Hash_data & hash_data, int chars_avail){
160
161    tracker.StoreNewlines(lex.LF);
162
163    elem_ends = tag_Callouts.ElemName_ends;
164    hashvalues[1] = hash_data.Hash_value;
165
166    // Check for block boundary case for length 16 and above
167    if (block_boundary_case)
168    {
169#if DEBUG
170        printf ("block boundary case! Special handle!\n");
171#endif
172        int lgth = count_forward_zeroes(elem_ends)-last_elem_start;
173        int start = block_base + last_elem_start;
174        int hashvalue = compute_hash_value(lgth, last_elem_start, hashvalues);
175        int gid = pbgs_symbol_table.Lookup_or_Insert_Name(source + start, hashvalue, lgth);
176        gids.push_back(gid);
177#if DEBUG
178        printf ("%s | start: %i[%i] | lgth: %i | hashvalue: %i | gid: %i \n", __FUNCTION__, start, start - block_base, lgth, hashvalue, gid);
179#endif
180        block_boundary_case = false;
181    }
182
183    if ( bitblock_has_bit(tag_Callouts.ElemName_ends_1_to_2) )
184    {
185        BitBlockForwardIterator iter_length_grouping_2(&tag_Callouts.ElemName_ends_1_to_2);
186        validate_block_length_grouping<2>(iter_length_grouping_2, block_base);
187    }
188
189    if ( bitblock_has_bit(tag_Callouts.ElemName_ends_3_to_4) )
190    {
191        BitBlockForwardIterator iter_length_grouping_4(&tag_Callouts.ElemName_ends_3_to_4);
192        validate_block_length_grouping<4>(iter_length_grouping_4, block_base);
193    }
194
195    if ( bitblock_has_bit(tag_Callouts.ElemName_ends_5_to_6) )
196    {
197        BitBlockForwardIterator iter_length_grouping_6(&tag_Callouts.ElemName_ends_5_to_6);
198        validate_block_length_grouping<6>(iter_length_grouping_6, block_base);
199    }
200
201    if ( bitblock_has_bit(tag_Callouts.ElemName_ends_7_to_8) )
202    {
203        BitBlockForwardIterator iter_length_grouping_8(&tag_Callouts.ElemName_ends_7_to_8);
204        validate_block_length_grouping<8>(iter_length_grouping_8, block_base);
205    }
206
207    if ( bitblock_has_bit(tag_Callouts.ElemName_ends_9_to_10) )
208    {
209        BitBlockForwardIterator iter_length_grouping_10(&tag_Callouts.ElemName_ends_9_to_10);
210        validate_block_length_grouping<10>(iter_length_grouping_10, block_base);
211    }
212
213    if ( bitblock_has_bit(tag_Callouts.ElemName_ends_11_to_12) )
214    {
215        BitBlockForwardIterator iter_length_grouping_12(&tag_Callouts.ElemName_ends_11_to_12);
216        validate_block_length_grouping<12>(iter_length_grouping_12, block_base);
217    }
218
219    if ( bitblock_has_bit(tag_Callouts.ElemName_ends_13_to_14) )
220    {
221        BitBlockForwardIterator iter_length_grouping_14(&tag_Callouts.ElemName_ends_13_to_14);
222        validate_block_length_grouping<14>(iter_length_grouping_14, block_base);
223    }
224
225    if ( bitblock_has_bit(tag_Callouts.ElemName_ends_15_to_16) )
226    {
227        BitBlockForwardIterator iter_length_grouping_16(&tag_Callouts.ElemName_ends_15_to_16);
228        validate_block_length_grouping<16>(iter_length_grouping_16, block_base);
229    }
230
231    if ( bitblock_has_bit(tag_Callouts.ElemName_remaining_ends) )
232    {
233        BitBlockForwardIterator iter_length_grouping_remaining(&tag_Callouts.ElemName_remaining_ends);
234        validate_block_length_grouping<17>(iter_length_grouping_remaining, block_base);
235    }
236
237    //copy current hash value data as previous one.
238    memmove (&hashvalues[0], &hashvalues[1], 16);
239
240    if (bitblock_has_bit(simd_or(check_streams.non_ascii_name_starts, check_streams.non_ascii_names))) {
241        BitBlockForwardIterator iter_NameStrt_check(&check_streams.non_ascii_name_starts);
242        validate_block(iter_NameStrt_check, block_base, NameStrt_check);
243        BitBlockForwardIterator iter_Name_check(&check_streams.non_ascii_names);
244        validate_block(iter_Name_check, block_base, Name_check);
245    }
246    if (bitblock_has_bit(ctCDPI_Callouts.PI_name_starts)){
247        BitBlockForwardIterator iter_PI_name_starts(&(ctCDPI_Callouts.PI_name_starts));
248        validate_block(iter_PI_name_starts, block_base, buffer_base, PIName_check);
249    }
250    if (bitblock_has_bit(ctCDPI_Callouts.CD_starts)){
251        BitBlockForwardIterator iter_CD_check(&ctCDPI_Callouts.CD_starts);
252        validate_block(iter_CD_check, block_base, CD_check);
253    }
254    if(bitblock_has_bit(ref_Callouts.GenRef_starts)){
255        BitBlockForwardIterator iter_GenRef_check(&ref_Callouts.GenRef_starts);
256        validate_block(iter_GenRef_check, block_base, GenRef_check);
257    }
258    if(bitblock_has_bit(ref_Callouts.DecRef_starts)){
259        BitBlockForwardIterator iter_DecRef_check(&ref_Callouts.DecRef_starts);
260        validate_block(iter_DecRef_check, block_base, DecRef_check);
261    }
262    if(bitblock_has_bit(ref_Callouts.HexRef_starts)){
263        BitBlockForwardIterator iter_HexRef_check(&ref_Callouts.HexRef_starts);
264        validate_block(iter_HexRef_check, block_base, HexRef_check);
265    }
266    if(bitblock_has_bit(check_streams.att_refs)){
267        BitBlockForwardIterator iter_AttRef_check(&check_streams.att_refs);
268        validate_block(iter_AttRef_check, block_base, AttRef_check);
269    }
270
271    if(error_tracker.Has_Noted_Error()){
272        int error_line, error_column;
273        tracker.get_Line_and_Column(error_tracker.Noted_Pos_In_Block(), error_line, error_column);
274        ReportError(error_tracker.Noted_Error_Msg(), error_line, error_column);
275        exit(-1);
276    }
277
278    matcher.store_streams(check_streams.tag_marks, check_streams.name_follows, check_streams.misc_mask, chars_avail);
279    tracker.AdvanceBlock();
280}
281
282void do_process(FILE *infile, FILE *outfile) {
283
284@decl
285  int buf_pos = 0;
286  int block_pos = 0;
287  int errpos = 0;
288  int chars_avail = 0;
289  int check_pos = 0;
290  int chars_read = 0;
291  BytePack buf[(BUFFER_SIZE+BLOCK_SIZE+OVERLAP_BUFSIZE*2)/sizeof(SIMD_type)];
292
293  char * srcbuf = ((char *) buf) + OVERLAP_BUFSIZE;
294  buffer_base = buf_pos;
295  source = srcbuf;
296  chars_read = fread((void *)srcbuf, 1, BUFFER_SIZE + OVERLAP_BUFSIZE, infile);
297  chars_avail = chars_read;
298  if (chars_avail > BUFFER_SIZE) chars_avail = BUFFER_SIZE;
299
300  matcher.setSrc(srcbuf);
301
302  if(chars_read<4){
303    fprintf(stderr,"File is too short. Not well formed.\n");
304    exit(-1);
305  }
306
307  Entity_Info * e = new Entity_Info;
308  e->AnalyzeSignature((unsigned char *)srcbuf);
309
310  if (e->code_unit_base == ASCII) {
311
312    XML_Decl_Parser<ASCII> decl_parser((unsigned char *)srcbuf);
313
314    decl_parser.ReadXMLInfo(*e);
315
316    if (e->code_unit_size != SingleByte || (e->has_encoding_decl && (!at_UTF_8(e->encoding)))){
317        fprintf(stderr,"Sorry, this xmlwf demo only works for UTF-8.\n");
318        exit(-1);
319    }
320  }
321  else {
322    fprintf(stderr,"Sorry, this xmlwf demo does not process EBCDIC.\n");
323        exit(-1);
324  }
325
326  if (e->content_start != 0) {
327        memmove(&srcbuf[0], &srcbuf[e->content_start], chars_read - e->content_start);
328        buf_pos = e->content_start;
329        buffer_base = buf_pos;
330        if (chars_avail == BUFFER_SIZE) {
331                chars_read = chars_read - e->content_start +
332                             fread(&srcbuf[chars_read-e->content_start], 1, e->content_start, infile);
333                chars_avail = chars_read;
334                if (chars_avail > BUFFER_SIZE) chars_avail = BUFFER_SIZE;
335        }
336        else {
337          chars_read -=e->content_start;
338          chars_avail -=e->content_start;
339        }
340  }
341
342@stream_stmts
343
344/* Full Buffers */
345    while (chars_avail == BUFFER_SIZE) {
346      PERF_SEC_START(parser_timer);
347      for (int blk = 0; blk < SEGMENT_BLOCKS; blk++) {
348          block_base = blk*BLOCK_SIZE;
349          s2p_do_block((BytePack *) &srcbuf[block_base], basis_bits);
350          @block_stmts
351          postprocess_do_block(lex, ctCDPI_Callouts, ref_Callouts, check_streams, tag_Callouts, hash_data, chars_avail);
352      }
353      matcher.StreamScan(chars_avail);
354      matcher.Advance_buffer();
355      tracker.Advance_buffer();
356#ifndef CACHE_PERF
357    PERF_SEC_END(parser_timer, chars_avail);
358#else
359    PERF_SEC_END(parser_timer, 1);
360#endif
361
362      int bytes_left = chars_read - chars_avail;
363      //memmove(srcbuf, &srcbuf[BUFFER_SIZE], bytes_left);
364      memmove(buf, &srcbuf[BUFFER_SIZE - OVERLAP_BUFSIZE], bytes_left + OVERLAP_BUFSIZE);
365      chars_read = fread(&srcbuf[bytes_left],1, BUFFER_SIZE + OVERLAP_BUFSIZE - bytes_left, infile) + bytes_left;
366
367      chars_avail = chars_read;
368      if (chars_avail > BUFFER_SIZE) chars_avail = BUFFER_SIZE;
369      buf_pos += chars_avail;
370      buffer_base = buf_pos;
371
372  }
373/* Final Partial Buffer */
374    PERF_SEC_START(parser_timer);
375
376    block_pos = 0;
377    int remaining = chars_avail;
378/* Full Blocks */
379    while (remaining >= BLOCK_SIZE) {
380          block_base = block_pos;
381          s2p_do_block((BytePack *) &srcbuf[block_pos], basis_bits);
382          @block_stmts
383          postprocess_do_block(lex, ctCDPI_Callouts, ref_Callouts, check_streams, tag_Callouts, hash_data, chars_avail);
384          block_pos += BLOCK_SIZE;
385          remaining -= BLOCK_SIZE;
386    }
387    block_base = block_pos;
388    if (remaining > 0 || @any_carry) {
389          EOF_mask = bitblock::srl(simd<1>::constant<1>(),convert(BLOCK_SIZE-remaining));
390          s2p_do_final_block((BytePack *) &srcbuf[block_pos], basis_bits, EOF_mask);
391          @final_block_stmts
392          postprocess_do_block(lex, ctCDPI_Callouts, ref_Callouts, check_streams, tag_Callouts, hash_data, chars_avail);
393    }
394    buf_pos += chars_avail;
395    buffer_base = buf_pos;
396
397    matcher.StreamScan(chars_avail);
398    matcher.Advance_buffer();
399    tracker.Advance_buffer();
400#ifndef CACHE_PERF
401    PERF_SEC_END(parser_timer, chars_avail);
402#else
403    PERF_SEC_END(parser_timer, 1);
404#endif
405    if (matcher.depth != 0) {
406      fprintf(stderr, "tag matching error (depth %i) at position %i\n", matcher.depth, buffer_base);
407      exit(-1);
408    }
409}
Note: See TracBrowser for help on using the repository browser.