source: icGREP/icgrep-devel/icgrep/icgrep.cpp @ 4151

Last change on this file since 4151 was 4151, checked in by linmengl, 5 years ago

experimental support for BLOCK_SIZE 256 ready to test

File size: 15.4 KB
Line 
1/*
2 *  Copyright (c) 2014 International Characters.
3 *  This software is licensed to the public under the Open Software License 3.0.
4 *  icgrep is a trademark of International Characters.
5 */
6
7#include "icgrep.h"
8
9#include "utf_encoding.h"
10#include "re_compiler.h"
11
12#include <fstream>
13#include <sstream>
14#include <iostream>
15#include <string>
16#include <stdint.h>
17
18#include <stdio.h>
19#include <stdlib.h>
20#include <unistd.h>
21#include <errno.h>
22#include <sys/types.h>
23#include <sys/stat.h>
24
25#include <simd-lib/bitblock.hpp>
26#include <simd-lib/carryQ.hpp>
27#include <simd-lib/pabloSupport.hpp>
28#include <simd-lib/s2p.hpp>
29#include <simd-lib/buffer.hpp>
30#include <simd-lib/bitblock_iterator.hpp>
31
32// mmap system
33#include <sys/mman.h>
34#include <fcntl.h>
35
36#if (BLOCK_SIZE == 128)
37#define SEGMENT_BLOCKS 7
38#endif
39
40#if (BLOCK_SIZE == 256)
41#define SEGMENT_BLOCKS 15
42#endif
43
44#define SEGMENT_SIZE (BLOCK_SIZE * SEGMENT_BLOCKS)
45
46#define BUFFER_SEGMENTS 15
47#define BUFFER_SIZE (BUFFER_SEGMENTS * SEGMENT_SIZE)
48
49#define BitBlock_declare(name)  BitBlock name
50
51#define ubitblock_declare(name, n) \
52  ubitblock name[n];\
53  do {int i;\
54      for (i = 0; i < n; i++) name[i]._128 = simd<1>::constant<0>();\
55     }\
56  while (0)
57
58BitBlock EOF_mask = simd<1>::constant<1>();
59
60struct Output {
61    BitBlock matches;
62    BitBlock LF;
63};
64
65#include <simd-lib/transpose.hpp>
66
67using namespace std;
68
69typedef void (*process_block_fcn)(const Basis_bits &basis_bits, BitBlock carry_q[], Output &output);
70
71
72#define USE_MMAP
73#ifndef USE_MMAP
74void do_process(FILE *infile, FILE *outfile, int count_only_option, int carry_count, process_block_fcn process_block);
75#endif
76#ifdef USE_MMAP
77void do_process(char * infile_buffer, size_t infile_size, FILE *outfile, int count_only_option, int carry_count, process_block_fcn process_block);
78#endif
79
80
81BitBlock get_category(Basis_bits &basis_bits, const char* category);
82
83int main(int argc, char *argv[])
84{
85    char * inregex, * fileregex, * infilename, * outfilename;
86    FILE *infile, *outfile, *regexfile;
87
88#ifdef USE_MMAP
89    int fdSrc;
90    struct stat infile_sb;
91    char * infile_buffer;
92#endif
93
94    int opt_code;
95    int count_only_option = 0;
96    int print_version_option = 0;
97    int regex_from_file_option = 0;
98    int ascii_only_option = 0;
99
100    int compile_time_option = 0;
101
102    unsigned long long cycles = 0;
103    double timer = 0;
104
105    long lSize = 0;
106
107    size_t result;
108
109    while ((opt_code = getopt(argc, argv, "cvfta")) != -1)
110    {
111        switch (opt_code)
112        {
113        case 'c':
114            count_only_option = 1;
115            break;
116        case 'v':
117            print_version_option = 1;
118            break;
119        case 'f':
120            regex_from_file_option = 1;
121            break;
122        case 't':
123            compile_time_option = 1;
124            break;
125        case 'a':
126            ascii_only_option = 1;
127            break;
128        case '?':
129            break;
130        default:
131            printf ("Invalid option: %c\n", opt_code);
132            printf("Usage: %s [-c] [-v] [-f] [-t] [-a] <regex|regexfile> <inputfile> [<outputfile>]\n", argv[0]);
133                    exit(-1);
134        }
135    }
136
137    if (optind >= argc)
138    {
139        printf ("Too few arguments\n");
140        printf("Usage: %s [-c] [-v] [-f] [-t] [-a] <regex|regexfile> <inputfile> [<outputfile>]\n", argv[0]);
141        exit(-1);
142    }
143
144    inregex = argv[optind++];
145    if (inregex == 0)
146    {
147        fprintf(stderr, "Error: cannot read the regular expression.\n");
148        exit(-1);
149    }
150
151    if (regex_from_file_option)
152    {
153        regexfile = fopen(inregex, "rb");
154        if (!regexfile){
155            fprintf(stderr, "Error: cannot open %s for processing.\n", inregex);
156            exit(-1);
157        }
158
159        fseek (regexfile , 0 , SEEK_END);
160        lSize = ftell (regexfile);
161        rewind (regexfile);
162
163        fileregex = (char*) malloc (sizeof(char)*lSize);
164        if (fileregex == NULL) {fputs ("Memory error",stderr); exit (2);}
165
166        result = fread (fileregex, 1, lSize, regexfile);
167        if (result != lSize) {fputs ("Reading error",stderr); exit (3);}
168        fclose(regexfile);
169
170        if (fileregex[lSize - 1] == '\n') fileregex[lSize - 1] = '\0';
171    }
172
173    infilename = argv[optind++];
174#ifndef USE_MMAP
175    infile = fopen(infilename, "rb");
176    if (!infile) {
177        fprintf(stderr, "Error: cannot open %s for processing.\n", infilename);
178        exit(-1);
179    }
180#endif
181
182    if (optind >= argc) {
183        outfile = stdout;
184    }
185    else {
186        outfilename = argv[optind++];
187        if (optind != argc)
188        {
189            printf ("Too many arguments\n");
190            printf("Usage: %s [-c] [-v] [-f] [-t] [-a] <regex|regexfile> <inputfile> [<outputfile>]\n", argv[0]);
191            exit(-1);
192        }
193        outfile = fopen(outfilename, "wb");
194        if (!outfile)
195        {
196            fprintf(stderr, "Error: cannot open %s for writing.\n", outfilename);
197            exit(-1);
198        }
199    }
200
201#ifdef USE_MMAP
202    fdSrc = open(infilename, O_RDONLY);
203    if (fdSrc == -1) {
204        fprintf(stderr, "Error: cannot open %s for processing.\n", infilename);
205        exit(-1);
206    }
207    if (fstat(fdSrc, &infile_sb) == -1) {
208        fprintf(stderr, "Error: cannot stat %s for processing.\n", infilename);
209        exit(-1);
210    }
211    if (infile_sb.st_size == 0) {
212        if (count_only_option) fprintf(outfile, "Matching Lines: %d\n", 0);
213        exit(0);
214    }
215    infile_buffer = (char *) mmap(NULL, infile_sb.st_size, PROT_READ, MAP_PRIVATE, fdSrc, 0);
216    if (infile_buffer == MAP_FAILED) {
217        fprintf(stderr, "Error: mmap of %s failure.\n", infilename);
218        exit(-1);
219    }
220#endif
221
222    if (print_version_option)
223    {
224        fprintf(outfile, "Parabix icgrep implementation: August 2014\n");
225    }
226
227    UTF_Encoding encoding;
228    encoding.setName("UTF-8");
229    encoding.setBits(8);
230    encoding.setMask(0xFF);
231
232    RE_Compiler* re_compiler = new RE_Compiler();
233    if (compile_time_option)
234    {
235        cycles = get_hrcycles();
236        timer = getElapsedTime();
237    }
238    LLVM_Gen_RetVal llvm_codegen = re_compiler->compile(compile_time_option,
239                                                        ascii_only_option,
240                                                        "basis_bits.bit_",
241                                                        "temp",
242                                                        encoding ,
243                                                        (regex_from_file_option ? fileregex : inregex));
244
245    if (compile_time_option)
246    {
247        cycles = get_hrcycles() - cycles;
248        timer = getElapsedTime() - timer;
249        std::cout << "Total compile time - cycles:       " << cycles << std::endl;
250        std::cout << "Total compile time - milliseconds: " << timer << std::endl;
251    }
252
253    if (llvm_codegen.process_block_fptr != 0)
254    {
255        void (*FP)(const Basis_bits &basis_bits, BitBlock carry_q[], Output &output) = (void (*)(const Basis_bits &basis_bits, BitBlock carry_q[], Output &output))(void*)llvm_codegen.process_block_fptr;
256#ifndef USE_MMAP
257        do_process(infile, outfile, count_only_option, llvm_codegen.carry_q_size, FP);
258#endif
259#ifdef USE_MMAP
260        do_process(infile_buffer, infile_sb.st_size, outfile, count_only_option, llvm_codegen.carry_q_size, FP);
261#endif
262    }
263
264    delete re_compiler;
265#ifndef USE_MMAP
266    fclose(infile);
267#endif
268#ifdef USE_MMAP
269    munmap((void *) infile_buffer, infile_sb.st_size);
270    close(fdSrc);
271#endif
272    fclose(outfile);
273    if (regex_from_file_option) free(fileregex);
274
275    return 0;
276}
277
278#if (BLOCK_SIZE == 256)
279typedef BitStreamScanner<BitBlock, uint64_t, uint64_t, SEGMENT_BLOCKS> ScannerT;
280#endif
281
282#if (BLOCK_SIZE == 128)
283typedef BitStreamScanner<BitBlock, uint32_t, uint32_t, SEGMENT_BLOCKS> ScannerT;
284#endif
285
286//
287// Write matched lines from a buffer to an output file, given segment
288// scanners for line ends and matches (where matches are a subset of line ends).
289// The buffer pointer must point to the first byte of the segment
290// corresponding to the scanner indexes.   The first_line_start is the
291// start position of the first line relative to the buffer start position.
292// It must be zero or negative;  if negative, the buffer must permit negative
293// indexing so that the lineup to the buffer start position can also be printed.
294// The start position of the final line in the processed segment is returned.
295//
296
297ssize_t write_matches(FILE * outfile, ScannerT line_scanner, ScannerT match_scanner, char * buffer, ssize_t first_line_start) {
298
299  ssize_t line_start = first_line_start;
300  size_t match_pos;
301  size_t line_end;
302  while (match_scanner.has_next()) {
303    match_pos = match_scanner.scan_to_next();
304    // If we found a match, it must be at a line end.
305    line_end = line_scanner.scan_to_next();
306    while (line_end < match_pos) {
307      line_start = line_end + 1;
308      line_end = line_scanner.scan_to_next();
309    }
310    fwrite(&buffer[line_start], 1, line_end - line_start + 1, outfile);
311    line_start = line_end + 1;
312
313  }
314  while(line_scanner.has_next()) {
315    line_end = line_scanner.scan_to_next();
316    line_start = line_end+1;
317  }
318  return line_start;
319}
320
321
322
323#ifndef USE_MMAP
324void do_process(FILE *infile, FILE *outfile, int count_only_option, int carry_count, process_block_fcn process_block) {
325#endif
326#ifdef USE_MMAP
327void do_process(char * infile_buffer, size_t infile_size, FILE *outfile, int count_only_option, int carry_count, process_block_fcn process_block) {
328#endif
329
330    struct Basis_bits basis_bits;
331    struct Output output;
332
333    BitBlock carry_q[carry_count];
334    memset (carry_q, 0, sizeof(BitBlock) * carry_count);
335
336    BitBlock match_vector = simd<1>::constant<0>();
337    int match_count=0;
338    int blk = 0;
339    int block_base  = 0;
340    int block_pos   = 0;
341    int buffer_pos  = 0;
342    int chars_avail = 0;
343    int chars_read  = 0;
344
345    int line_start = 0;
346    int line_end = 0;
347    int match_pos = 0;
348    int line_no = 0;
349
350    ScannerT LF_scanner;
351    ScannerT match_scanner;
352
353    char * buffer_ptr;
354#ifndef USE_MMAP
355    ATTRIBUTE_SIMD_ALIGN char src_buffer[SEGMENT_SIZE];
356    buffer_ptr = &src_buffer;
357    chars_read = fread((void *)&src_buffer[0], 1, SEGMENT_SIZE, infile);
358    chars_avail = chars_read;
359    if (chars_avail >= SEGMENT_SIZE) chars_avail = SEGMENT_SIZE;
360#endif
361#ifdef USE_MMAP
362    int segment = 0;
363    int segment_base = 0;
364    chars_avail = infile_size;
365
366#endif
367//////////////////////////////////////////////////////////////////////////////////////////
368// Full Segments
369//////////////////////////////////////////////////////////////////////////////////////////
370
371
372
373    while (chars_avail >= SEGMENT_SIZE) {
374
375#ifdef USE_MMAP
376        segment_base = segment * SEGMENT_SIZE;
377#endif
378        LF_scanner.init();
379        match_scanner.init();
380
381        for (blk = 0; blk < SEGMENT_BLOCKS; blk++) {
382#ifndef USE_MMAP
383            block_base = blk*BLOCK_SIZE;
384            s2p_do_block((BytePack *) &src_buffer[block_base], basis_bits);
385#endif
386#ifdef USE_MMAP
387            block_base = blk*BLOCK_SIZE + segment_base;
388            s2p_do_block((BytePack *) &infile_buffer[block_base], basis_bits);
389#endif
390            process_block(basis_bits, carry_q, output);
391
392            LF_scanner.load_block(output.LF, blk);
393            match_scanner.load_block(output.matches, blk);
394            if (count_only_option){
395                if (bitblock::any(output.matches))
396                {
397                    if (bitblock::any(simd_and(match_vector, output.matches))){
398                        match_count += bitblock::popcount(match_vector);
399                        match_vector = output.matches;
400                    }
401                    else
402                    {
403                        match_vector = simd_or(match_vector, output.matches);
404                    }
405                }
406            }
407        }
408
409#ifndef USE_MMAP
410        int copy_back_pos = 0;
411
412
413        if (LF_scanner.count() > 0) {
414            copy_back_pos = LF_scanner.get_final_pos() + 1;
415            memset (carry_q, 0, sizeof(BitBlock) * carry_count);
416        }
417        else {
418            copy_back_pos =  SEGMENT_SIZE;
419        }
420
421        int  copy_back_size = SEGMENT_SIZE - copy_back_pos;
422#endif
423#ifdef USE_MMAP
424    buffer_ptr = &infile_buffer[segment_base];
425#endif
426
427        if (!count_only_option) {
428          line_start = write_matches(outfile, LF_scanner, match_scanner, buffer_ptr, line_start);
429        }
430#ifndef USE_MMAP
431        memmove(&src_buffer[0], &src_buffer[copy_back_pos], copy_back_size);
432
433        //Do another read.
434        chars_read = fread(&src_buffer[copy_back_size], 1, copy_back_pos, infile);
435        chars_avail = chars_read + copy_back_size;
436        if (chars_avail >= SEGMENT_SIZE) chars_avail = SEGMENT_SIZE;
437        buffer_pos += chars_avail;
438#endif
439#ifdef USE_MMAP
440        segment++;
441        line_start -= SEGMENT_SIZE;  /* Will be negative offset for use within next segment. */
442        chars_avail -= SEGMENT_SIZE;
443
444#endif
445    }
446
447
448//////////////////////////////////////////////////////////////////////////////////////////
449// For the Final Partial Segment.
450//////////////////////////////////////////////////////////////////////////////////////////
451
452#ifdef USE_MMAP
453    segment_base = segment * SEGMENT_SIZE;
454#endif
455    int remaining = chars_avail;
456
457    LF_scanner.init();
458    match_scanner.init();
459
460    /* Full Blocks */
461    blk = 0;
462    while (remaining >= BLOCK_SIZE) {
463    //fprintf(outfile, "Remaining = %i\n", remaining);
464#ifndef USE_MMAP
465        block_base = block_pos;
466        s2p_do_block((BytePack *) &src_buffer[block_base], basis_bits);
467#endif
468#ifdef USE_MMAP
469        block_base = block_pos + segment_base;
470        s2p_do_block((BytePack *) &infile_buffer[block_base], basis_bits);
471#endif
472        process_block(basis_bits, carry_q, output);
473
474        LF_scanner.load_block(output.LF, blk);
475        match_scanner.load_block(output.matches, blk);
476        if (count_only_option)
477        {
478            if (bitblock::any(output.matches))
479            {
480                if (bitblock::any(simd_and(match_vector, output.matches)))
481                {
482                    match_count += bitblock::popcount(match_vector);
483                    match_vector = output.matches;
484                }
485                else
486                {
487                    match_vector = simd_or(match_vector, output.matches);
488                }
489            }
490        }
491
492        block_pos += BLOCK_SIZE;
493        remaining -= BLOCK_SIZE;
494        blk++;
495    }
496    block_base = block_pos;
497    //fprintf(stderr, "Remaining = %i\n", remaining);
498
499    //For the last partial block, or for any carry.
500    EOF_mask = bitblock::srl(simd<1>::constant<1>(), convert(BLOCK_SIZE-remaining));
501#ifndef USE_MMAP
502     block_base = block_pos;
503     s2p_do_final_block((BytePack *) &src_buffer[block_base], basis_bits, EOF_mask);
504#endif
505#ifdef USE_MMAP
506     block_base = block_pos + segment_base;
507     s2p_do_final_block((BytePack *) &infile_buffer[block_base], basis_bits, EOF_mask);
508#endif
509    process_block(basis_bits, carry_q, output);
510
511    if (count_only_option)
512    {
513        match_count += bitblock::popcount(match_vector);
514        if (bitblock::any(output.matches))
515        {
516            match_count += bitblock::popcount(output.matches);
517        }
518        fprintf(outfile, "Matching Lines:%d\n", match_count);
519    }
520    else
521    {
522        LF_scanner.load_block(output.LF, blk);
523        match_scanner.load_block(output.matches, blk);
524        blk++;
525        for (int i = blk; i < SEGMENT_BLOCKS; i++)
526        {
527            LF_scanner.load_block(simd<1>::constant<0>(), i);
528            match_scanner.load_block(simd<1>::constant<0>(), i);
529        }
530#ifndef USE_MMAP
531        line_start = 0;
532#endif
533#ifdef USE_MMAP
534        buffer_ptr = &infile_buffer[segment_base];
535#endif
536        line_start = write_matches(outfile, LF_scanner, match_scanner, buffer_ptr, line_start);
537    }
538
539    buffer_pos += chars_avail;
540}
541
542
543
Note: See TracBrowser for help on using the repository browser.