source: trunk/symbol_table/main_template.cpp @ 1989

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

Update main template.

File size: 8.0 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 "marker_strms.hpp"     // GENERATED HEADER
37#include "hash_strms.hpp"       // GENERATED HEADER
38#include "id_group_strms.hpp"   // GENERATED HEADER
39#include "id_symbol_table.hpp"
40#include <string>
41#include <iostream>
42#include <fstream>
43#include <sstream>
44using namespace std;
45
46#ifdef BUFFER_PROFILING
47    BOM_Table * parser_timer;
48#elif CODE_CLOCKER
49    #define NUM_EVENTS 1
50    int Events[NUM_EVENTS] = {PAPI_TOT_CYC};
51    //int Events[NUM_EVENTS] = {PAPI_L2_DCM};
52    //int Events[NUM_EVENTS] = {PAPI_TOT_CYC, PAPI_BR_MSP};
53    int cal_size = 20;
54    CC * parser_timer = new CC(Events,NUM_EVENTS,cal_size);
55#else
56    void * parser_timer;
57#endif
58
59
60// Target symbol type must inherit from AoS_symbol
61typedef struct symbol: public AoS_symbol
62{
63    string parameter_1;
64    string parameter_2;
65    bool parameter_3;
66} symbol;
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 = SEGMENT_SIZE;
134    symbol symbol_ary[SYMBOL_COUNT];
135    id_symbol_table<fast_pool_allocator<1024> > symbol_table;
136
137    is.read ((char *)raw_buffer, SEGMENT_SIZE);
138    uint32_t chars_avail = is.gcount();
139
140    ///////////////////////////////////////////////////////////////////////////
141    // Full Segments
142    ///////////////////////////////////////////////////////////////////////////
143    while (chars_avail >= SEGMENT_SIZE) {
144
145      uint32_t blk;
146      for(blk=0;blk<SEGMENT_BLOCKS;blk++) {
147        s2p_do_block((BytePack *) &raw_buffer[blk*BLOCK_SIZE], basis_bits[blk]);    // transpose
148        markers_do_block(basis_bits[blk], markers[blk]);                            // gen symbol spans, mark starts & follows
149        hash_strms_do_block(basis_bits[blk], hash[blk]);                            // gen hash bit streams
150        identity_group_do_block(markers[blk], groups[blk]);                         // sort marker bit stream (identity)
151      }
152
153//    for(int k=0;k<SEGMENT_BLOCKS;k++) {
154//        cout << "RAW " << string((((char*)&raw_buffer[0])+k*BLOCK_SIZE),BLOCK_SIZE) << endl;
155//    }
156
157      for(int blk=0;blk<SEGMENT_BLOCKS;blk++) { // write contiguous bit streams
158        h0[blk] = hash[blk].h0;
159        h1[blk] = hash[blk].h1;
160        starts[blk] = groups[blk].starts;
161        ends_gte_17[blk] = groups[blk].ends_gte_17;
162      }
163
164      PERF_SEC_START(parser_timer);
165      symbol_table.resolve(raw_buffer, groups, starts, ends_gte_17, h0, h1, SEGMENT_BLOCKS, symbol_ary, SYMBOL_COUNT);
166      PERF_SEC_END(parser_timer, SEGMENT_SIZE);
167
168      // copy loopback bytes
169      memmove(lookback,&raw_buffer[SEGMENT_SIZE-LOOKBACK_SIZE],LOOKBACK_SIZE);
170      // copy loopback bits
171      memmove(lookback_h0,&((uint8_t *)h0)[(SEGMENT_SIZE-LOOKBACK_SIZE)/8],LOOKBACK_SIZE/8);
172      memmove(lookback_h1,&((uint8_t *)h1)[(SEGMENT_SIZE-LOOKBACK_SIZE)/8],LOOKBACK_SIZE/8);
173
174      memmove(lookback_starts,&((uint8_t *)starts)[(SEGMENT_SIZE-LOOKBACK_SIZE)/8],LOOKBACK_SIZE/8);
175      memmove(lookback_ends_gte_17,&((uint8_t *)ends_gte_17)[(SEGMENT_SIZE-LOOKBACK_SIZE)/8],LOOKBACK_SIZE/8);
176
177      //lookback_h0[0] = h0[SEGMENT_BLOCKS-1];
178      //lookback_h1[0] = h1[SEGMENT_BLOCKS-1];
179      is.read ((char *)(raw_buffer), SEGMENT_SIZE);
180      chars_avail = is.gcount();
181    }
182
183    /* Resolve Partial Segments */
184    uint32_t remaining = chars_avail;
185
186    ///////////////////////////////////////////////////////////////////////////
187    // Full blocks
188    ///////////////////////////////////////////////////////////////////////////
189    uint32_t blk = 0;
190    while (remaining >= BLOCK_SIZE) {
191        s2p_do_block((BytePack *) &raw_buffer[blk*BLOCK_SIZE], basis_bits[blk]);
192        markers_do_block(basis_bits[blk], markers[blk]);
193        hash_strms_do_block(basis_bits[blk], hash[blk]);
194        identity_group_do_block(markers[blk], groups[blk]);
195        blk++;
196        remaining -= BLOCK_SIZE;
197    }
198
199    ///////////////////////////////////////////////////////////////////////////
200    // Final partial block or any carry
201    ///////////////////////////////////////////////////////////////////////////
202    if (remaining > 0 || @marker_strms_any_carry /*|| hash_strms_any_carry*/) {
203          BitBlock EOF_mask = bitblock::srl(simd<1>::constant<1>(), convert(BLOCK_SIZE-remaining));
204          s2p_do_final_block((BytePack *) &raw_buffer[blk*BLOCK_SIZE], basis_bits[blk], EOF_mask);
205          markers_do_final_block(basis_bits[blk], markers[blk], EOF_mask);
206          hash_strms_do_final_block(basis_bits[blk], hash[blk], EOF_mask);
207          identity_group_do_final_block(markers[blk], groups[blk], EOF_mask);
208          blk++;
209    }
210
211//    for(int k=0;k<blk;k++) {
212//      cout << "RAW " << string((((char*)&raw_buffer[0])+k*BLOCK_SIZE),BLOCK_SIZE) << endl;
213//    }
214
215    uint32_t segment_size = blk;
216    for(int blk=0;blk<segment_size;blk++) { // write contiguous hash bit streams
217        h0[blk] = hash[blk].h0;
218        h1[blk] = hash[blk].h1;
219        starts[blk] = groups[blk].starts;
220        ends_gte_17[blk] = groups[blk].ends_gte_17;
221    }
222
223    //PERF_SEC_START(parser_timer);
224    symbol_table.resolve(raw_buffer, groups, starts, ends_gte_17, h0, h1, blk, symbol_ary, SYMBOL_COUNT);
225    //PERF_SEC_END(parser_timer, chars_avail+1);
226
227    PERF_SEC_DUMP(parser_timer);
228    PERF_SEC_DESTROY(parser_timer);
229
230    is.close();
231
232    return 1;
233}
234
235
Note: See TracBrowser for help on using the repository browser.