source: trunk/symbol_table/templates/main.template.cpp @ 2298

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

Added const modifier.

File size: 8.6 KB
Line 
1/*
2 * Created on: 18-December-2011
3 * Author: Ken Herdy
4 *
5 * A simple test driver.
6 *
7 * Lookahead versus COPYBACK
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 */
22
23#include <simd-lib/allocator.hpp>
24#include <simd-lib/bitblock.hpp>
25#include <simd-lib/bitblock_scan.hpp>
26#include <simd-lib/buffer.hpp>
27#include <simd-lib/carryQ.hpp>
28#include <simd-lib/s2p.hpp>
29#include <simd-lib/perflib/perfsec.h>
30#include "../src/transpose.hpp"
31
32#include "marker_strms.hpp"     // GENERATED HEADER
33#include "hash_strms.hpp"       // GENERATED HEADER
34#include "group_strms.hpp"      // GENERATED HEADER
35#include <string>
36#include <iostream>
37#include <fstream>
38#include <sstream>
39using namespace std;
40
41#define TEST_DELIMETER // ','
42#include "../src/symbol_table.hpp"
43
44#ifdef BUFFER_PROFILING
45    BOM_Table * parser_timer;
46#elif CODE_CLOCKER
47    #define NUM_EVENTS 1
48    int Events[NUM_EVENTS] = {PAPI_TOT_CYC};
49    //int Events[NUM_EVENTS] = {PAPI_L2_DCM};
50    //int Events[NUM_EVENTS] = {PAPI_TOT_CYC, PAPI_BR_MSP};
51    int cal_size = 20;
52    CC * parser_timer = new CC(Events,NUM_EVENTS,cal_size);
53#else
54    void * parser_timer;
55#endif
56
57int main(int argc, char * argv[]) {
58
59    if (argc < 2) {
60            cout << "Usage: " << argv[0] << " <filename>" << endl;
61            exit(-1);
62    }
63
64    stringstream filename;
65    filename << argv[1];
66
67    ifstream is;
68    is.open (filename.str().c_str(), ios::binary);
69
70    if (!is) {
71        cerr << "Error: " << filename << endl;
72        abort();
73    }
74
75    // PERF_SEC_BIND(1);
76    PERF_SEC_INIT(parser_timer);
77
78        ///////////////////////////////////////////////////////////////////////////
79        // Buffer Constants
80        ///////////////////////////////////////////////////////////////////////////
81        //
82        // NOTE:    Buffer segments are defined as multiples of BLOCK_SIZE.
83        //              BLOCK_SIZE defined as a number of bytes.
84        #define PADDING_BLOCKS 1
85        #define PADDING_SIZE (BLOCK_SIZE * PADDING_BLOCKS)
86        #define COPYBACK_BLOCKS 3
87        #define COPYBACK_SIZE (BLOCK_SIZE * COPYBACK_BLOCKS)
88        #define LOOKAHEAD_BLOCKS 1
89        #define LOOKAHEAD_SIZE (BLOCK_SIZE * LOOKAHEAD_BLOCKS)
90        #define SEGMENT_BLOCKS  12
91        #define SEGMENT_SIZE (BLOCK_SIZE * SEGMENT_BLOCKS)
92
93        #define BUFFER_SIZE (COPYBACK_SIZE + SEGMENT_SIZE + LOOKAHEAD_SIZE + PADDING_SIZE)
94
95    ///////////////////////////////////////////////////////////////////////////
96    // Parallel Data Streams with optional CopyBack
97    ///////////////////////////////////////////////////////////////////////////
98    uint8_t * COPYBACK;
99    uint8_t * src_buffer;
100    ALLOC_STATIC_ALIGNED_BYTE_BUFFER_WITH_COPYBACK(COPYBACK, src_buffer);
101
102    // hash 0
103    BitBlock * COPYBACK_h0;
104    BitBlock * h0;
105    ALLOC_STATIC_ALIGNED_BITBLOCK_BUFFER_WITH_COPYBACK(COPYBACK_h0, h0);
106
107    // hash 1
108    BitBlock * COPYBACK_h1;
109    BitBlock * h1;
110    ALLOC_STATIC_ALIGNED_BITBLOCK_BUFFER_WITH_COPYBACK(COPYBACK_h1, h1);
111
112    // starts
113    BitBlock * COPYBACK_starts;
114    BitBlock * starts;
115    ALLOC_STATIC_ALIGNED_BITBLOCK_BUFFER_WITH_COPYBACK(COPYBACK_starts, starts);
116
117    ///////////////////////////////////////////////////////////////////////////
118    // Parallel Data Streams - No CopyBack
119    ///////////////////////////////////////////////////////////////////////////
120
121    Basis_bits basis_bits[SEGMENT_BLOCKS];
122    Marker marker[SEGMENT_BLOCKS];
123    Hash hash[SEGMENT_BLOCKS];
124    Groups groups[SEGMENT_BLOCKS];
125
126        ///////////////////////////////////////////////////////////////////////////
127    // Symbol Table
128        ///////////////////////////////////////////////////////////////////////////
129        gid<SEGMENT_SIZE> gids;
130    symbol_table<gid<SEGMENT_SIZE>, fast_pool_allocator<1024> > st;
131
132    is.read ((char *)src_buffer, SEGMENT_SIZE);
133    uint32_t chars_avail = is.gcount();
134
135    #if IDENTITY_TEST
136        cout << ","; // prepend delimeter
137    #endif
138
139    ///////////////////////////////////////////////////////////////////////////
140    // Full Segments
141    ///////////////////////////////////////////////////////////////////////////
142    while (chars_avail >= SEGMENT_SIZE) {
143                uint32_t blk;
144                for(blk=0;blk<SEGMENT_BLOCKS;blk++) {
145                        s2p_do_block((BytePack *) &src_buffer[blk*BLOCK_SIZE], basis_bits[blk]);    // transpose
146                        marker_do_block(basis_bits[blk], marker[blk]);                              // gen symbol spans, mark starts & follows
147                        hash_strms_do_block(basis_bits[blk], hash[blk]);                            // gen hash bit streams
148                        group_strms_do_block(marker[blk].starts, marker[blk].follows, groups[blk]);                         // sort marker bit stream (identity)
149    }
150
151    for(int blk=0;blk<SEGMENT_BLOCKS;blk++) {   // write contiguous bit streams
152                        h0[blk] = hash[blk].h0;
153                        h1[blk] = hash[blk].h1;
154                        starts[blk] = groups[blk].starts;
155    }
156
157    PERF_SEC_START(parser_timer);
158    st.resolve(src_buffer, groups, starts, h0, h1, SEGMENT_BLOCKS, gids);
159    PERF_SEC_END(parser_timer, SEGMENT_SIZE);
160
161    COPY_BACK_BYTE_BUFFER(COPYBACK, src_buffer);
162    COPY_BACK_BITBLOCK_BUFFER(COPYBACK_h0,h0);
163    COPY_BACK_BITBLOCK_BUFFER(COPYBACK_h1,h1);
164    COPY_BACK_BITBLOCK_BUFFER(COPYBACK_starts,starts);
165
166    is.read ((char *)(src_buffer), SEGMENT_SIZE);
167    chars_avail = is.gcount();
168
169    #ifdef IDENTITY_TEST
170    uint32_t blk_offset;
171    for(int blk=0;blk<SEGMENT_BLOCKS;blk++) {
172                        blk_offset = blk * BLOCK_SIZE;
173                        gid_type gid;
174
175                        #ifdef INDEX_AT_STARTS
176                                        ForwardScanner<BitBlock, scanword_t> scanner(&(marker[blk].starts));
177                        #else
178                                        ForwardScanner<BitBlock, scanword_t> scanner(&(marker[blk].follows));
179                        #endif
180
181                        scanner.scan_to_next();
182                        while(!scanner.is_done()) {
183                                gid = gids.at[scanner.get_pos() + blk_offset];
184                                cout << string((char *)st.get_raw_data(gid), st.get_lgth(gid)) << ",";
185                                scanner.scan_to_next();
186                        }
187    }
188    #endif
189    }
190
191    /* Resolve Partial Segments */
192    uint32_t remaining = chars_avail;
193
194    ///////////////////////////////////////////////////////////////////////////
195    // Full blocks
196    ///////////////////////////////////////////////////////////////////////////
197    uint32_t blk = 0;
198    while (remaining >= BLOCK_SIZE) {
199                        s2p_do_block((BytePack *) &src_buffer[blk*BLOCK_SIZE], basis_bits[blk]);
200                        marker_do_block(basis_bits[blk], marker[blk]);
201                        hash_strms_do_block(basis_bits[blk], hash[blk]);
202                        group_strms_do_block(marker[blk].starts, marker[blk].follows, groups[blk]);
203                        blk++;
204                        remaining -= BLOCK_SIZE;
205    }
206
207    ///////////////////////////////////////////////////////////////////////////
208    // Final partial block or any carry
209    ///////////////////////////////////////////////////////////////////////////
210    if (remaining > 0 || @marker_strms_any_carry || @group_strms_any_carry /*|| hash_strms_any_carry*/) {
211                        const BitBlock EOF_mask = bitblock::srl(simd<1>::constant<1>(), convert(BLOCK_SIZE-remaining));
212                        s2p_do_final_block((BytePack *) &src_buffer[blk*BLOCK_SIZE], basis_bits[blk], EOF_mask);
213                        marker_do_final_block(basis_bits[blk], marker[blk], EOF_mask);
214                        hash_strms_do_final_block(basis_bits[blk], hash[blk], EOF_mask);
215                        group_strms_do_final_block(marker[blk].starts, marker[blk].follows, groups[blk], EOF_mask);
216                        blk++;
217    }
218
219    uint32_t segment_blocks = blk;
220    for(int blk=0;blk<segment_blocks;blk++) { // write contiguous hash bit streams
221                        h0[blk] = hash[blk].h0;
222                        h1[blk] = hash[blk].h1;
223                        starts[blk] = groups[blk].starts;
224    }
225
226    // PERF_SEC_BIND(0);
227    PERF_SEC_START(parser_timer);
228    st.resolve(src_buffer, groups, starts, h0, h1, segment_blocks, gids);
229    PERF_SEC_END(parser_timer, chars_avail);
230
231    #ifdef IDENTITY_TEST
232    uint32_t blk_offset;
233    for(int blk=0;blk<segment_blocks;blk++) {
234                blk_offset = blk * BLOCK_SIZE;
235                gid_type gid;
236
237                #ifdef INDEX_AT_STARTS
238                          ForwardScanner<BitBlock, scanword_t> scanner(&(marker[blk].starts));
239                #else
240                          ForwardScanner<BitBlock, scanword_t> scanner(&(marker[blk].follows));
241                #endif
242
243                scanner.scan_to_next();
244                while(!scanner.is_done()) {
245                        gid = gids.at[scanner.get_pos() + blk_offset];
246                        cout << string((char *)st.get_raw_data(gid), st.get_lgth(gid)) << ",";
247                        scanner.scan_to_next();
248                }
249    }
250    #endif
251
252    #ifdef GID_TEST
253                        cout << st.get_max_gid() << endl;
254    #endif
255
256    PERF_SEC_DUMP(parser_timer);
257    PERF_SEC_DESTROY(parser_timer);
258
259    is.close();
260
261    return 1;
262}
263
264
Note: See TracBrowser for help on using the repository browser.