source: proto/SymbolTable/wcd_ls_template.cpp @ 3427

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

updated wcd_ls template for papi performance test

File size: 7.1 KB
Line 
1// WARNING: This implementation does not run, it will give the wrong result and crash.
2//#error "This implementation does not run, it will give the wrong result and/or crash."
3
4#define USE_LS_SYMBOL_TABLE
5
6#include "../common_definitions.h"
7#include <ls_symbol_table.h>
8
9#include "../wcd_common_functions.h"
10#include "../symtab_common_functions.h"
11#include "parser_common_functions_generated.h"
12
13#ifdef BUFFER_PROFILING
14        BOM_Table * parser_timer;
15
16#elif CODE_CLOCKER
17        #define NUM_EVENTS 1
18#ifdef L2_PERF
19        int Events[NUM_EVENTS] = {PAPI_L2_DCM};
20#elif L1_PERF
21        int Events[NUM_EVENTS] = {PAPI_L1_DCM};
22#else
23        int Events[NUM_EVENTS] = {PAPI_BR_MSP};
24#endif
25        int cal_size = 20;
26        CC * parser_timer = new CC(Events,NUM_EVENTS,cal_size);
27#else
28        void * parser_timer;
29#endif
30
31int block_base=0;
32int buffer_base=0;
33char * source;
34
35queue <size_t> elem_starts_buf;
36queue <size_t> elem_ends_buf;
37vector <int> gids;
38LSSymbolTable ls_symbol_table;
39
40static inline void postprocess_do_block(Dictionary& dictionary);
41
42template<bool allow_performance_check, bool finalize_gids> void do_process(FILE *infile, FILE *outfile);
43
44static inline void do_symbol_table_lookup();
45
46int main(int argc, char * argv[]) {
47    char * dictionaryfilename, * infilename, * outfilename;
48    FILE * dictionaryfile, *infile, *outfile;
49
50    getFilenames(argc, argv, dictionaryfilename, infilename, outfilename);
51    openInputOutputFiles(dictionaryfilename, infilename, outfilename,
52                         dictionaryfile, infile, outfile);
53    int greatest_GID_in_dictionary;
54//    populateDictionary(dictionaryfile, outfile, gids, greatest_GID_in_dictionary);
55
56//    ls_symbol_table.clear();
57//    rewind (dictionaryfile);
58//    do_process<true, false>(dictionaryfile, outfile);
59
60//      PERF_SEC_BIND(1);
61
62    PERF_SEC_INIT(parser_timer);
63
64    // store symbols form text to Symbol Table
65    do_process<true, true>(infile, outfile);
66
67    PERF_SEC_DUMP(parser_timer);
68
69    PERF_SEC_DESTROY(parser_timer);
70
71    //print_GIDS(ls_symbol_table);
72
73    // gather dictionary statistics
74//    int totalKnownWordsInDictionary, totalUnknownWordsInDictionary;
75//    wordCountInDictionary(greatest_GID_in_dictionary, gids, totalUnknownWordsInDictionary, totalKnownWordsInDictionary);
76//    printWordCountInDictionary(totalUnknownWordsInDictionary, totalKnownWordsInDictionary);
77
78    fclose(dictionaryfile);
79    fclose(infile);
80    fclose(outfile);
81
82#if PRINT_SYMBOL_DISTRIBUTION
83        print_GIDS(ls_symbol_table);
84#endif
85
86        return(0);
87}
88
89static inline int ElemStrt_check(int pos) {
90        elem_starts_buf.push(buffer_base + pos);
91        return 0;
92}
93
94static inline int ElemEnd_check(int pos) {
95        elem_ends_buf.push(buffer_base + pos);
96        return 0;
97}
98
99static inline void do_symbol_table_lookup()
100{
101    while( !elem_starts_buf.empty() && !elem_ends_buf.empty() )
102    {
103        int start = elem_starts_buf.front();
104        int end = elem_ends_buf.front();
105        elem_starts_buf.pop();
106        elem_ends_buf.pop();
107        int length = end - start;
108
109        //lookup or insert to symbol table
110#if DEBUG
111        char* symbol = new char[length+1];
112        strncpy ( symbol, source + start - buffer_base, length );
113        symbol[length] ='\0';
114        printf ("start: %i[%i] | end: %i[%i] | length: %i | symbol: %s\n", start, start-buffer_base, end, end-buffer_base, length, symbol );
115
116        delete symbol; symbol = 0;
117#endif
118
119        ls_symbol_table.put((unsigned char*)source + start - buffer_base, length);
120    }
121}
122
123static inline void postprocess_do_block(Dictionary& dictionary){
124
125    if ( bitblock_has_bit(dictionary.Word_starts))
126    {
127        BitBlockForwardIterator iter_length_grouping_starts(&dictionary.Word_starts);
128        validate_block(iter_length_grouping_starts, block_base, ElemStrt_check);
129    }
130
131    if ( bitblock_has_bit(dictionary.Word_ends) )
132    {
133        BitBlockForwardIterator iter_length_grouping_ends(&dictionary.Word_ends);
134        validate_block(iter_length_grouping_ends, block_base, ElemEnd_check);
135    }
136
137    do_symbol_table_lookup();
138}
139
140template<bool allow_performance_check, bool finalize_gids> void do_process(FILE *infile, FILE *outfile) {
141
142@decl
143
144  int buf_pos = 0;
145  int block_pos = 0;
146  int errpos = 0;
147  int chars_avail = 0;
148  int check_pos = 0;
149  int chars_read = 0;
150  BytePack buf[(BUFFER_SIZE+BLOCK_SIZE+OVERLAP_BUFSIZE*2)/sizeof(SIMD_type)];
151
152  char * srcbuf = ((char *) buf) + OVERLAP_BUFSIZE;
153  buffer_base = buf_pos;
154  source = srcbuf;
155
156  chars_read = fread((void *)srcbuf, 1, BUFFER_SIZE + OVERLAP_BUFSIZE, infile);
157  chars_avail = chars_read;
158  if (chars_avail > BUFFER_SIZE) chars_avail = BUFFER_SIZE;
159
160  Entity_Info * e = new Entity_Info;
161  e->AnalyzeSignature((unsigned char *)srcbuf);
162
163  if (e->content_start != 0) {
164        memmove(&srcbuf[0], &srcbuf[e->content_start], chars_read - e->content_start);
165        buf_pos = e->content_start;
166        buffer_base = buf_pos;
167        if (chars_avail == BUFFER_SIZE) {
168                chars_read = chars_read - e->content_start +
169                             fread(&srcbuf[chars_read-e->content_start], 1, e->content_start, infile);
170                chars_avail = chars_read;
171                if (chars_avail > BUFFER_SIZE) chars_avail = BUFFER_SIZE;
172        }
173        else {
174          chars_read -=e->content_start;
175          chars_avail -=e->content_start;
176        }
177  }
178
179@stream_stmts
180
181/* Full Buffers */
182
183    while (chars_avail == BUFFER_SIZE) {
184      if (allow_performance_check)
185      {
186        PERF_SEC_START(parser_timer);
187      }
188      for (int blk = 0; blk < SEGMENT_BLOCKS; blk++) {
189          block_base = blk*BLOCK_SIZE;
190          s2p_do_block((BytePack *) &srcbuf[block_base], basis_bits);
191          @block_stmts
192          postprocess_do_block(dictionary);
193      }
194      if (allow_performance_check)
195      {
196        PERF_SEC_END(parser_timer, chars_avail);
197      }
198
199      int bytes_left = chars_read - chars_avail;
200      memmove(buf, &srcbuf[BUFFER_SIZE - OVERLAP_BUFSIZE], bytes_left + OVERLAP_BUFSIZE);
201      chars_read = fread(&srcbuf[bytes_left],1, BUFFER_SIZE + OVERLAP_BUFSIZE - bytes_left, infile) + bytes_left;
202      chars_avail = chars_read;
203      if (chars_avail > BUFFER_SIZE) chars_avail = BUFFER_SIZE;
204      buf_pos += chars_avail;
205      buffer_base = buf_pos;
206    }
207/* Final Partial Buffer */
208    if (allow_performance_check)
209    {
210      PERF_SEC_START(parser_timer);
211    }
212
213    block_pos = 0;
214    int remaining = chars_avail;
215/* Full Blocks */
216    while (remaining >= BLOCK_SIZE) {
217          block_base = block_pos;
218          s2p_do_block((BytePack *) &srcbuf[block_pos], basis_bits);
219          @block_stmts
220          postprocess_do_block(dictionary);
221          block_pos += BLOCK_SIZE;
222          remaining -= BLOCK_SIZE;
223    }
224    block_base = block_pos;
225    if (remaining > 0 || @any_carry) {
226          EOF_mask = bitblock::srl(simd<1>::constant<1>(), convert(BLOCK_SIZE-remaining));
227          s2p_do_final_block((BytePack *) &srcbuf[block_pos], basis_bits, EOF_mask);
228          @final_block_stmts
229          postprocess_do_block(dictionary);
230    }
231    buf_pos += chars_avail;
232    buffer_base = buf_pos;
233
234    if (finalize_gids)
235    {
236        ls_symbol_table.bind();
237        ls_symbol_table.finalize();
238    }
239
240    if (allow_performance_check)
241    {
242      PERF_SEC_END(parser_timer, chars_avail);
243    }
244
245    // get gids
246    if (finalize_gids)
247    {
248        gids = ls_symbol_table.get_flattened_gids();
249    }
250}
Note: See TracBrowser for help on using the repository browser.