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

Last change on this file since 4212 was 4209, checked in by nmedfort, 5 years ago

Minor revisions

File size: 15.1 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#include "utf_encoding.h"
9#include "compiler.h"
10
11#include <fstream>
12#include <sstream>
13#include <iostream>
14#include <string>
15#include <stdint.h>
16
17#include <stdio.h>
18#include <stdlib.h>
19#include <unistd.h>
20#include <errno.h>
21#include <sys/types.h>
22#include <sys/stat.h>
23
24#include "include/simd-lib/bitblock.hpp"
25#include "include/simd-lib/carryQ.hpp"
26#include "include/simd-lib/pabloSupport.hpp"
27#include "include/simd-lib/s2p.hpp"
28#include "include/simd-lib/buffer.hpp"
29#include "include/simd-lib/bitblock_iterator.hpp"
30#include "include/simd-lib/transpose.hpp"
31
32#include "hrtime.h"
33
34// mmap system
35#include <sys/mman.h>
36#include <fcntl.h>
37
38#if (BLOCK_SIZE == 128)
39#define SEGMENT_BLOCKS 7
40#endif
41
42#if (BLOCK_SIZE == 256)
43#define SEGMENT_BLOCKS 15
44#endif
45
46#define SEGMENT_SIZE (BLOCK_SIZE * SEGMENT_BLOCKS)
47
48#define BUFFER_SEGMENTS 15
49#define BUFFER_SIZE (BUFFER_SEGMENTS * SEGMENT_SIZE)
50
51#define BitBlock_declare(name)  BitBlock name
52
53#define ubitblock_declare(name, n) \
54  ubitblock name[n];\
55  do {int i;\
56      for (i = 0; i < n; i++) name[i]._128 = simd<1>::constant<0>();\
57     }\
58  while (0)
59
60BitBlock EOF_mask = simd<1>::constant<1>();
61
62struct Output {
63    BitBlock matches;
64    BitBlock LF;
65};
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    Encoding encoding(ascii_only_option ? Encoding::Type::ASCII : Encoding::Type::UTF_8, 8);
228    if (compile_time_option)
229    {
230        cycles = get_hrcycles();
231        timer = getElapsedTime();
232    }
233    LLVM_Gen_RetVal llvm_codegen = icgrep::compile(compile_time_option, encoding, (regex_from_file_option ? fileregex : inregex));
234
235    if (compile_time_option)
236    {
237        cycles = get_hrcycles() - cycles;
238        timer = getElapsedTime() - timer;
239        std::cout << "Total compile time - cycles:       " << cycles << std::endl;
240        std::cout << "Total compile time - milliseconds: " << timer << std::endl;
241    }
242
243    if (llvm_codegen.process_block_fptr != 0)
244    {
245        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;
246#ifndef USE_MMAP
247        do_process(infile, outfile, count_only_option, llvm_codegen.carry_q_size, FP);
248#endif
249#ifdef USE_MMAP
250        do_process(infile_buffer, infile_sb.st_size, outfile, count_only_option, llvm_codegen.carry_q_size, FP);
251#endif
252    }
253
254#ifndef USE_MMAP
255    fclose(infile);
256#endif
257#ifdef USE_MMAP
258    munmap((void *) infile_buffer, infile_sb.st_size);
259    close(fdSrc);
260#endif
261    fclose(outfile);
262    if (regex_from_file_option) free(fileregex);
263
264    return 0;
265}
266
267#if (BLOCK_SIZE == 256)
268typedef BitStreamScanner<BitBlock, uint64_t, uint64_t, SEGMENT_BLOCKS> ScannerT;
269#endif
270
271#if (BLOCK_SIZE == 128)
272typedef BitStreamScanner<BitBlock, uint32_t, uint32_t, SEGMENT_BLOCKS> ScannerT;
273#endif
274
275//
276// Write matched lines from a buffer to an output file, given segment
277// scanners for line ends and matches (where matches are a subset of line ends).
278// The buffer pointer must point to the first byte of the segment
279// corresponding to the scanner indexes.   The first_line_start is the
280// start position of the first line relative to the buffer start position.
281// It must be zero or negative;  if negative, the buffer must permit negative
282// indexing so that the lineup to the buffer start position can also be printed.
283// The start position of the final line in the processed segment is returned.
284//
285
286ssize_t write_matches(FILE * outfile, ScannerT line_scanner, ScannerT match_scanner, char * buffer, ssize_t first_line_start) {
287
288  ssize_t line_start = first_line_start;
289  size_t match_pos;
290  size_t line_end;
291  while (match_scanner.has_next()) {
292    match_pos = match_scanner.scan_to_next();
293    // If we found a match, it must be at a line end.
294    line_end = line_scanner.scan_to_next();
295    while (line_end < match_pos) {
296      line_start = line_end + 1;
297      line_end = line_scanner.scan_to_next();
298    }
299    fwrite(&buffer[line_start], 1, line_end - line_start + 1, outfile);
300    line_start = line_end + 1;
301
302  }
303  while(line_scanner.has_next()) {
304    line_end = line_scanner.scan_to_next();
305    line_start = line_end+1;
306  }
307  return line_start;
308}
309
310
311
312#ifndef USE_MMAP
313void do_process(FILE *infile, FILE *outfile, int count_only_option, int carry_count, process_block_fcn process_block) {
314#endif
315#ifdef USE_MMAP
316void do_process(char * infile_buffer, size_t infile_size, FILE *outfile, int count_only_option, int carry_count, process_block_fcn process_block) {
317#endif
318
319    struct Basis_bits basis_bits;
320    struct Output output;
321
322    BitBlock carry_q[carry_count];
323    memset (carry_q, 0, sizeof(BitBlock) * carry_count);
324
325    BitBlock match_vector = simd<1>::constant<0>();
326    int match_count=0;
327    int blk = 0;
328    int block_base  = 0;
329    int block_pos   = 0;
330    int buffer_pos  = 0;
331    int chars_avail = 0;
332    int chars_read  = 0;
333
334    int line_start = 0;
335    int line_end = 0;
336    int match_pos = 0;
337    int line_no = 0;
338
339    ScannerT LF_scanner;
340    ScannerT match_scanner;
341
342    char * buffer_ptr;
343#ifndef USE_MMAP
344    ATTRIBUTE_SIMD_ALIGN char src_buffer[SEGMENT_SIZE];
345    buffer_ptr = &src_buffer;
346    chars_read = fread((void *)&src_buffer[0], 1, SEGMENT_SIZE, infile);
347    chars_avail = chars_read;
348    if (chars_avail >= SEGMENT_SIZE) chars_avail = SEGMENT_SIZE;
349#endif
350#ifdef USE_MMAP
351    int segment = 0;
352    int segment_base = 0;
353    chars_avail = infile_size;
354
355#endif
356//////////////////////////////////////////////////////////////////////////////////////////
357// Full Segments
358//////////////////////////////////////////////////////////////////////////////////////////
359
360
361
362    while (chars_avail >= SEGMENT_SIZE) {
363
364#ifdef USE_MMAP
365        segment_base = segment * SEGMENT_SIZE;
366#endif
367        LF_scanner.init();
368        match_scanner.init();
369
370        for (blk = 0; blk < SEGMENT_BLOCKS; blk++) {
371#ifndef USE_MMAP
372            block_base = blk*BLOCK_SIZE;
373            s2p_do_block((BytePack *) &src_buffer[block_base], basis_bits);
374#endif
375#ifdef USE_MMAP
376            block_base = blk*BLOCK_SIZE + segment_base;
377            s2p_do_block((BytePack *) &infile_buffer[block_base], basis_bits);
378#endif
379            process_block(basis_bits, carry_q, output);
380
381            LF_scanner.load_block(output.LF, blk);
382            match_scanner.load_block(output.matches, blk);
383            if (count_only_option){
384                if (bitblock::any(output.matches))
385                {
386                    if (bitblock::any(simd_and(match_vector, output.matches))){
387                        match_count += bitblock::popcount(match_vector);
388                        match_vector = output.matches;
389                    }
390                    else
391                    {
392                        match_vector = simd_or(match_vector, output.matches);
393                    }
394                }
395            }
396        }
397
398#ifndef USE_MMAP
399        int copy_back_pos = 0;
400
401
402        if (LF_scanner.count() > 0) {
403            copy_back_pos = LF_scanner.get_final_pos() + 1;
404            memset (carry_q, 0, sizeof(BitBlock) * carry_count);
405        }
406        else {
407            copy_back_pos =  SEGMENT_SIZE;
408        }
409
410        int  copy_back_size = SEGMENT_SIZE - copy_back_pos;
411#endif
412#ifdef USE_MMAP
413    buffer_ptr = &infile_buffer[segment_base];
414#endif
415
416        if (!count_only_option) {
417          line_start = write_matches(outfile, LF_scanner, match_scanner, buffer_ptr, line_start);
418        }
419#ifndef USE_MMAP
420        memmove(&src_buffer[0], &src_buffer[copy_back_pos], copy_back_size);
421
422        //Do another read.
423        chars_read = fread(&src_buffer[copy_back_size], 1, copy_back_pos, infile);
424        chars_avail = chars_read + copy_back_size;
425        if (chars_avail >= SEGMENT_SIZE) chars_avail = SEGMENT_SIZE;
426        buffer_pos += chars_avail;
427#endif
428#ifdef USE_MMAP
429        segment++;
430        line_start -= SEGMENT_SIZE;  /* Will be negative offset for use within next segment. */
431        chars_avail -= SEGMENT_SIZE;
432
433#endif
434    }
435
436
437//////////////////////////////////////////////////////////////////////////////////////////
438// For the Final Partial Segment.
439//////////////////////////////////////////////////////////////////////////////////////////
440
441#ifdef USE_MMAP
442    segment_base = segment * SEGMENT_SIZE;
443#endif
444    int remaining = chars_avail;
445
446    LF_scanner.init();
447    match_scanner.init();
448
449    /* Full Blocks */
450    blk = 0;
451    while (remaining >= BLOCK_SIZE) {
452    //fprintf(outfile, "Remaining = %i\n", remaining);
453#ifndef USE_MMAP
454        block_base = block_pos;
455        s2p_do_block((BytePack *) &src_buffer[block_base], basis_bits);
456#endif
457#ifdef USE_MMAP
458        block_base = block_pos + segment_base;
459        s2p_do_block((BytePack *) &infile_buffer[block_base], basis_bits);
460#endif
461        process_block(basis_bits, carry_q, output);
462
463        LF_scanner.load_block(output.LF, blk);
464        match_scanner.load_block(output.matches, blk);
465        if (count_only_option)
466        {
467            if (bitblock::any(output.matches))
468            {
469                if (bitblock::any(simd_and(match_vector, output.matches)))
470                {
471                    match_count += bitblock::popcount(match_vector);
472                    match_vector = output.matches;
473                }
474                else
475                {
476                    match_vector = simd_or(match_vector, output.matches);
477                }
478            }
479        }
480
481        block_pos += BLOCK_SIZE;
482        remaining -= BLOCK_SIZE;
483        blk++;
484    }
485    block_base = block_pos;
486    //fprintf(stderr, "Remaining = %i\n", remaining);
487
488    //For the last partial block, or for any carry.
489    EOF_mask = bitblock::srl(simd<1>::constant<1>(), convert(BLOCK_SIZE-remaining));
490#ifndef USE_MMAP
491     block_base = block_pos;
492     s2p_do_final_block((BytePack *) &src_buffer[block_base], basis_bits, EOF_mask);
493#endif
494#ifdef USE_MMAP
495     block_base = block_pos + segment_base;
496     s2p_do_final_block((BytePack *) &infile_buffer[block_base], basis_bits, EOF_mask);
497#endif
498    process_block(basis_bits, carry_q, output);
499
500    if (count_only_option)
501    {
502        match_count += bitblock::popcount(match_vector);
503        if (bitblock::any(output.matches))
504        {
505            match_count += bitblock::popcount(output.matches);
506        }
507        fprintf(outfile, "Matching Lines:%d\n", match_count);
508    }
509    else
510    {
511        LF_scanner.load_block(output.LF, blk);
512        match_scanner.load_block(output.matches, blk);
513        blk++;
514        for (int i = blk; i < SEGMENT_BLOCKS; i++)
515        {
516            LF_scanner.load_block(simd<1>::constant<0>(), i);
517            match_scanner.load_block(simd<1>::constant<0>(), i);
518        }
519#ifndef USE_MMAP
520        line_start = 0;
521#endif
522#ifdef USE_MMAP
523        buffer_ptr = &infile_buffer[segment_base];
524#endif
525        line_start = write_matches(outfile, LF_scanner, match_scanner, buffer_ptr, line_start);
526    }
527
528    buffer_pos += chars_avail;
529}
530
531
532
Note: See TracBrowser for help on using the repository browser.