source: trunk/symbol_table/main_template.cpp @ 1993

Last change on this file since 1993 was 1993, checked in by ksherdy, 7 years ago

Updated.

File size: 9.1 KB
Line 
1/*
2 * Created on: 18-December-2011
3 * Author: Ken Herdy
4 *
5 * Length sorted symbol table main.
6 *
7 * Lookahead versus Lookback
8 *
9 * The current implementation applies bit stream length grouping based on 'end' markers.
10 * In a sense, 'end' markers are precomputed 'lookahead'.
11 * True 'lookahead' would compute the current block and number of 'lookahead' position and
12 * support 'shift back' and to mark the 'start' rather than the 'end' positions of lexical items.
13 *
14 * In any case, the current implementation 'expects' that the previous block will be located in a contiguous
15 * memory location that may be indexed as some negative offset of the base address of the current
16 * block.
17 *
18 * Further, to reduce complexity in processing, although structs of BitBlock types are not stored
19 * contiguously in memory, BitBlock struct members are copied into contiguous memory positions.
20 *
21 * Design Issues
22 *
23 * (1) Max hash table size.
24 * (2) Negative shift values.
25 * (3) Template classes on Length L.
26 *
27 */
28
29#include "transpose.hpp"
30#include "buffer.hpp"
31
32#include "../lib/bitblock.hpp"
33#include "../lib/allocator.hpp"
34#include "../lib/s2p.hpp"
35#include "../lib/perflib/perfsec.h"
36#include "../lib/bitblock_scan.hpp"
37#include "marker_strms.hpp"     // GENERATED HEADER
38#include "hash_strms.hpp"       // GENERATED HEADER
39#include "id_group_strms.hpp"   // GENERATED HEADER
40#include "id_symbol_table.hpp"
41#include <string>
42#include <iostream>
43#include <fstream>
44#include <sstream>
45using namespace std;
46
47#ifdef BUFFER_PROFILING
48    BOM_Table * parser_timer;
49#elif CODE_CLOCKER
50    #define NUM_EVENTS 1
51    int Events[NUM_EVENTS] = {PAPI_TOT_CYC};
52    //int Events[NUM_EVENTS] = {PAPI_L2_DCM};
53    //int Events[NUM_EVENTS] = {PAPI_TOT_CYC, PAPI_BR_MSP};
54    int cal_size = 20;
55    CC * parser_timer = new CC(Events,NUM_EVENTS,cal_size);
56#else
57    void * parser_timer;
58#endif
59
60
61// Target symbol type must inherit from AoS_symbol
62class Symbol: public AoS_symbol
63{
64public:
65    bool parameter_1;
66};
67
68int main(int argc, char * argv[]) {
69
70    if (argc < 2) {
71            cout << "Usage: " << argv[0] << " <filename>" << endl;
72            exit(-1);
73    }
74
75    stringstream filename;
76    filename << argv[1];
77
78    ifstream is;
79    is.open (filename.str().c_str(), ios::binary);
80
81    if (!is) {
82        cerr << "Error: " << filename << endl;
83        abort();
84    }
85
86    // PERF_SEC_BIND(1);
87    PERF_SEC_INIT(parser_timer);
88
89    ///////////////////////////////////////////////////////////////////////////
90    // Stream Definitions
91    ///////////////////////////////////////////////////////////////////////////
92
93    // Byte Segments - Raw byte streams - With lookback.
94    BitBlock aligned_buffer[SEGMENT_ALLOC_SIZE];
95    uint8_t * lookback = (uint8_t *)aligned_buffer;
96    memset(lookback,0,LOOKBACK_SIZE);
97    uint8_t * raw_buffer = &lookback[LOOKBACK_SIZE];
98
99    // Bit Segments - Hash bit streams - With lookback.
100
101    // hash 0
102    BitBlock aligned_h0[SEGMENT_ALLOC_SIZE/8];
103    BitBlock * lookback_h0 = (BitBlock *) aligned_h0;
104    memset(lookback_h0,0,LOOKBACK_SIZE/BLOCK_SIZE);
105    BitBlock * h0 = &lookback_h0[LOOKBACK_SIZE/BLOCK_SIZE];
106
107    // hash 1
108    BitBlock aligned_h1[SEGMENT_ALLOC_SIZE/8];
109    BitBlock * lookback_h1 = (BitBlock *) aligned_h1;
110    memset(lookback_h1,0,LOOKBACK_SIZE/BLOCK_SIZE);
111    BitBlock * h1 = &lookback_h1[LOOKBACK_SIZE/BLOCK_SIZE];
112
113    // starts
114    BitBlock aligned_starts[SEGMENT_ALLOC_SIZE/8];
115    BitBlock * lookback_starts = (BitBlock *) aligned_starts;
116    memset(lookback_starts,0,LOOKBACK_SIZE/BLOCK_SIZE);
117    BitBlock * starts = &lookback_starts[LOOKBACK_SIZE/BLOCK_SIZE];
118
119    // ends_gte_17
120    BitBlock aligned_ends_gte_17[SEGMENT_ALLOC_SIZE/8];
121    BitBlock * lookback_ends_gte_17 = (BitBlock *) aligned_ends_gte_17;
122    memset(lookback_ends_gte_17,0,LOOKBACK_SIZE/BLOCK_SIZE);
123    BitBlock * ends_gte_17 = &lookback_h1[LOOKBACK_SIZE/BLOCK_SIZE];
124
125
126    // BitSteams - Without lookback
127    Basis_bits basis_bits[SEGMENT_BLOCKS];
128    Markers markers[SEGMENT_BLOCKS];
129    Hash hash[SEGMENT_BLOCKS];
130    Groups groups[SEGMENT_BLOCKS];
131
132    // Symbol Table
133    const uint32_t SYMBOL_COUNT = LOOKBACK_SIZE + SEGMENT_SIZE;
134    Symbol * symbol_ary[SYMBOL_COUNT];
135
136    // TODO - DESTROY
137    for(int i =0;i<SYMBOL_COUNT;i++) {
138        symbol_ary[i] = new Symbol();
139    }
140
141
142    id_symbol_table<fast_pool_allocator<1024> > symbol_table;
143
144    is.read ((char *)raw_buffer, SEGMENT_SIZE);
145    uint32_t chars_avail = is.gcount();
146
147    ///////////////////////////////////////////////////////////////////////////
148    // Full Segments
149    ///////////////////////////////////////////////////////////////////////////
150    while (chars_avail >= SEGMENT_SIZE) {
151
152      uint32_t blk;
153      for(blk=0;blk<SEGMENT_BLOCKS;blk++) {
154        s2p_do_block((BytePack *) &raw_buffer[blk*BLOCK_SIZE], basis_bits[blk]);    // transpose
155        markers_do_block(basis_bits[blk], markers[blk]);                            // gen symbol spans, mark starts & follows
156        hash_strms_do_block(basis_bits[blk], hash[blk]);                            // gen hash bit streams
157        identity_group_do_block(markers[blk], groups[blk]);                         // sort marker bit stream (identity)
158      }
159
160//    for(int k=0;k<SEGMENT_BLOCKS;k++) {
161//        cout << "RAW " << string((((char*)&raw_buffer[0])+k*BLOCK_SIZE),BLOCK_SIZE) << endl;
162//    }
163
164      for(int blk=0;blk<SEGMENT_BLOCKS;blk++) { // write contiguous bit streams
165        h0[blk] = hash[blk].h0;
166        h1[blk] = hash[blk].h1;
167        starts[blk] = groups[blk].starts;
168        ends_gte_17[blk] = groups[blk].ends_gte_17;
169      }
170
171      PERF_SEC_START(parser_timer);
172      symbol_table.resolve(raw_buffer, groups, starts, ends_gte_17, h0, h1, SEGMENT_BLOCKS, (AoS_symbol **) symbol_ary/*, SYMBOL_COUNT*/);
173      PERF_SEC_END(parser_timer, SEGMENT_SIZE);
174
175      // copy loopback bytes
176      memmove(lookback,&raw_buffer[SEGMENT_SIZE-LOOKBACK_SIZE],LOOKBACK_SIZE);
177      // copy loopback bits
178      memmove(lookback_h0,&((uint8_t *)h0)[(SEGMENT_SIZE-LOOKBACK_SIZE)/8],LOOKBACK_SIZE/8);
179      memmove(lookback_h1,&((uint8_t *)h1)[(SEGMENT_SIZE-LOOKBACK_SIZE)/8],LOOKBACK_SIZE/8);
180
181      memmove(lookback_starts,&((uint8_t *)starts)[(SEGMENT_SIZE-LOOKBACK_SIZE)/8],LOOKBACK_SIZE/8);
182      memmove(lookback_ends_gte_17,&((uint8_t *)ends_gte_17)[(SEGMENT_SIZE-LOOKBACK_SIZE)/8],LOOKBACK_SIZE/8);
183
184      //lookback_h0[0] = h0[SEGMENT_BLOCKS-1];
185      //lookback_h1[0] = h1[SEGMENT_BLOCKS-1];
186      is.read ((char *)(raw_buffer), SEGMENT_SIZE);
187      chars_avail = is.gcount();
188
189      // test
190      uint32_t blk_base = 0;
191      uint32_t idx = 0;
192      for(blk=0;blk<SEGMENT_BLOCKS + LOOKBACK_BLOCKS;blk++) {
193          blk_base = blk * BLOCK_SIZE;
194          ForwardScanner<BitBlock, scanword_t> fscanner(&starts[blk]);
195          fscanner.scan_to_next();
196          while(!fscanner.is_done()) {
197              idx = LOOKBACK_SIZE + blk_base + fscanner.get_pos();
198              cout <<"[" << idx << "]" << "=" << "(" << symbol_ary[idx]->gid << "," << symbol_ary[idx]->lgth << ")" << endl;
199              fscanner.scan_to_next();
200          }
201      }
202    }
203
204    /* Resolve Partial Segments */
205    uint32_t remaining = chars_avail;
206
207    ///////////////////////////////////////////////////////////////////////////
208    // Full blocks
209    ///////////////////////////////////////////////////////////////////////////
210    uint32_t blk = 0;
211    while (remaining >= BLOCK_SIZE) {
212        s2p_do_block((BytePack *) &raw_buffer[blk*BLOCK_SIZE], basis_bits[blk]);
213        markers_do_block(basis_bits[blk], markers[blk]);
214        hash_strms_do_block(basis_bits[blk], hash[blk]);
215        identity_group_do_block(markers[blk], groups[blk]);
216        blk++;
217        remaining -= BLOCK_SIZE;
218    }
219
220    ///////////////////////////////////////////////////////////////////////////
221    // Final partial block or any carry
222    ///////////////////////////////////////////////////////////////////////////
223    if (remaining > 0 || @marker_strms_any_carry /*|| hash_strms_any_carry*/) {
224          BitBlock EOF_mask = bitblock::srl(simd<1>::constant<1>(), convert(BLOCK_SIZE-remaining));
225          s2p_do_final_block((BytePack *) &raw_buffer[blk*BLOCK_SIZE], basis_bits[blk], EOF_mask);
226          markers_do_final_block(basis_bits[blk], markers[blk], EOF_mask);
227          hash_strms_do_final_block(basis_bits[blk], hash[blk], EOF_mask);
228          identity_group_do_final_block(markers[blk], groups[blk], EOF_mask);
229          blk++;
230    }
231
232//    for(int k=0;k<blk;k++) {
233//      cout << "RAW " << string((((char*)&raw_buffer[0])+k*BLOCK_SIZE),BLOCK_SIZE) << endl;
234//    }
235
236    uint32_t segment_size = blk;
237    for(int blk=0;blk<segment_size;blk++) { // write contiguous hash bit streams
238        h0[blk] = hash[blk].h0;
239        h1[blk] = hash[blk].h1;
240        starts[blk] = groups[blk].starts;
241        ends_gte_17[blk] = groups[blk].ends_gte_17;
242    }
243
244    //PERF_SEC_START(parser_timer);
245    symbol_table.resolve(raw_buffer, groups, starts, ends_gte_17, h0, h1, blk, (AoS_symbol **) symbol_ary/*, SYMBOL_COUNT*/);
246    //PERF_SEC_END(parser_timer, chars_avail+1);
247
248    // test
249    uint32_t blk_base = 0;
250    uint32_t idx = 0;
251    for(blk=0;blk<segment_size + LOOKBACK_BLOCKS;blk++) {
252        blk_base = blk * BLOCK_SIZE;
253        ForwardScanner<BitBlock, scanword_t> fscanner(&starts[blk]);
254        fscanner.scan_to_next();
255        while(!fscanner.is_done()) {
256            idx = LOOKBACK_SIZE + blk_base + fscanner.get_pos();
257            cout <<"[" << idx << "]" << "=" << "(" << symbol_ary[idx]->gid << "," << symbol_ary[idx]->lgth << ")" << endl;
258            fscanner.scan_to_next();
259        }
260    }
261
262    PERF_SEC_DUMP(parser_timer);
263    PERF_SEC_DESTROY(parser_timer);
264
265    is.close();
266
267    return 1;
268}
269
270
Note: See TracBrowser for help on using the repository browser.