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

Last change on this file since 4272 was 4242, checked in by nmedfort, 5 years ago

Minor changes in preperation for adding multiplexing.

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    const auto llvm_codegen = icgrep::compile(encoding, (regex_from_file_option ? fileregex : inregex), compile_time_option);
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    BitBlock match_vector;
322    BitBlock carry_q[carry_count];
323    int match_count=0;
324    int blk = 0;
325    int block_base  = 0;
326    int block_pos   = 0;
327    int buffer_pos  = 0;
328    int chars_avail = 0;
329    int chars_read  = 0;
330    int line_start = 0;
331    int line_end = 0;
332    int match_pos = 0;
333    int line_no = 0;
334
335    ScannerT LF_scanner;
336    ScannerT match_scanner;
337
338    match_vector = simd<1>::constant<0>();
339    memset (carry_q, 0, sizeof(BitBlock) * carry_count);
340
341    char * buffer_ptr;
342#ifndef USE_MMAP
343    ATTRIBUTE_SIMD_ALIGN char src_buffer[SEGMENT_SIZE];
344    buffer_ptr = &src_buffer;
345    chars_read = fread((void *)&src_buffer[0], 1, SEGMENT_SIZE, infile);
346    chars_avail = chars_read;
347    if (chars_avail >= SEGMENT_SIZE) chars_avail = SEGMENT_SIZE;
348#endif
349#ifdef USE_MMAP
350    int segment = 0;
351    int segment_base = 0;
352    chars_avail = infile_size;
353
354#endif
355//////////////////////////////////////////////////////////////////////////////////////////
356// Full Segments
357//////////////////////////////////////////////////////////////////////////////////////////
358
359
360
361    while (chars_avail >= SEGMENT_SIZE) {
362
363#ifdef USE_MMAP
364        segment_base = segment * SEGMENT_SIZE;
365#endif
366        LF_scanner.init();
367        match_scanner.init();
368
369        for (blk = 0; blk < SEGMENT_BLOCKS; blk++) {
370#ifndef USE_MMAP
371            block_base = blk*BLOCK_SIZE;
372            s2p_do_block((BytePack *) &src_buffer[block_base], basis_bits);
373#endif
374#ifdef USE_MMAP
375            block_base = blk*BLOCK_SIZE + segment_base;
376            s2p_do_block((BytePack *) &infile_buffer[block_base], basis_bits);
377#endif
378            process_block(basis_bits, carry_q, output);
379
380            LF_scanner.load_block(output.LF, blk);
381            match_scanner.load_block(output.matches, blk);
382            if (count_only_option){
383                if (bitblock::any(output.matches))
384                {
385                    if (bitblock::any(simd_and(match_vector, output.matches))){
386                        match_count += bitblock::popcount(match_vector);
387                        match_vector = output.matches;
388                    }
389                    else
390                    {
391                        match_vector = simd_or(match_vector, output.matches);
392                    }
393                }
394            }
395        }
396
397#ifndef USE_MMAP
398        int copy_back_pos = 0;
399
400
401        if (LF_scanner.count() > 0) {
402            copy_back_pos = LF_scanner.get_final_pos() + 1;
403            memset (carry_q, 0, sizeof(BitBlock) * carry_count);
404        }
405        else {
406            copy_back_pos =  SEGMENT_SIZE;
407        }
408
409        int  copy_back_size = SEGMENT_SIZE - copy_back_pos;
410#endif
411#ifdef USE_MMAP
412    buffer_ptr = &infile_buffer[segment_base];
413#endif
414
415        if (!count_only_option) {
416          line_start = write_matches(outfile, LF_scanner, match_scanner, buffer_ptr, line_start);
417        }
418#ifndef USE_MMAP
419        memmove(&src_buffer[0], &src_buffer[copy_back_pos], copy_back_size);
420
421        //Do another read.
422        chars_read = fread(&src_buffer[copy_back_size], 1, copy_back_pos, infile);
423        chars_avail = chars_read + copy_back_size;
424        if (chars_avail >= SEGMENT_SIZE) chars_avail = SEGMENT_SIZE;
425        buffer_pos += chars_avail;
426#endif
427#ifdef USE_MMAP
428        segment++;
429        line_start -= SEGMENT_SIZE;  /* Will be negative offset for use within next segment. */
430        chars_avail -= SEGMENT_SIZE;
431#endif
432    }
433
434
435//////////////////////////////////////////////////////////////////////////////////////////
436// For the Final Partial Segment.
437//////////////////////////////////////////////////////////////////////////////////////////
438
439#ifdef USE_MMAP
440    segment_base = segment * SEGMENT_SIZE;
441#endif
442    int remaining = chars_avail;
443
444    LF_scanner.init();
445    match_scanner.init();
446
447    /* Full Blocks */
448    blk = 0;
449    while (remaining >= BLOCK_SIZE) {
450    //fprintf(outfile, "Remaining = %i\n", remaining);
451#ifndef USE_MMAP
452        block_base = block_pos;
453        s2p_do_block((BytePack *) &src_buffer[block_base], basis_bits);
454#endif
455#ifdef USE_MMAP
456        block_base = block_pos + segment_base;
457        s2p_do_block((BytePack *) &infile_buffer[block_base], basis_bits);
458#endif
459        process_block(basis_bits, carry_q, output);
460
461        LF_scanner.load_block(output.LF, blk);
462        match_scanner.load_block(output.matches, blk);
463        if (count_only_option)
464        {
465            if (bitblock::any(output.matches))
466            {
467                if (bitblock::any(simd_and(match_vector, output.matches)))
468                {
469                    match_count += bitblock::popcount(match_vector);
470                    match_vector = output.matches;
471                }
472                else
473                {
474                    match_vector = simd_or(match_vector, output.matches);
475                }
476            }
477        }
478
479        block_pos += BLOCK_SIZE;
480        remaining -= BLOCK_SIZE;
481        blk++;
482    }
483    block_base = block_pos;
484    //fprintf(stderr, "Remaining = %i\n", remaining);
485
486    //For the last partial block, or for any carry.
487    EOF_mask = bitblock::srl(simd<1>::constant<1>(), convert(BLOCK_SIZE-remaining));
488#ifndef USE_MMAP
489     block_base = block_pos;
490     s2p_do_final_block((BytePack *) &src_buffer[block_base], basis_bits, EOF_mask);
491#endif
492#ifdef USE_MMAP
493     block_base = block_pos + segment_base;
494     s2p_do_final_block((BytePack *) &infile_buffer[block_base], basis_bits, EOF_mask);
495#endif
496    process_block(basis_bits, carry_q, output);
497
498    if (count_only_option)
499    {
500        match_count += bitblock::popcount(match_vector);
501        if (bitblock::any(output.matches))
502        {
503            match_count += bitblock::popcount(output.matches);
504        }
505        fprintf(outfile, "Matching Lines:%d\n", match_count);
506    }
507    else
508    {
509        LF_scanner.load_block(output.LF, blk);
510        match_scanner.load_block(output.matches, blk);
511        blk++;
512        for (int i = blk; i < SEGMENT_BLOCKS; i++)
513        {
514            LF_scanner.load_block(simd<1>::constant<0>(), i);
515            match_scanner.load_block(simd<1>::constant<0>(), i);
516        }
517#ifndef USE_MMAP
518        line_start = 0;
519#endif
520#ifdef USE_MMAP
521        buffer_ptr = &infile_buffer[segment_base];
522#endif
523        line_start = write_matches(outfile, LF_scanner, match_scanner, buffer_ptr, line_start);
524    }
525
526    buffer_pos += chars_avail;
527}
528
529
530
Note: See TracBrowser for help on using the repository browser.