Changeset 4048 for proto/s2k


Ignore:
Timestamp:
Aug 19, 2014, 5:21:59 PM (5 years ago)
Author:
ksherdy
Message:

Cleaned up template.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • proto/s2k/trunk/framework/input/templates/cpplang/push_grep.template

    r4043 r4048  
    7474// Runtime libraries
    7575#include <simd-lib/bitblock.hpp>
     76#include <simd-lib/bitblock_iterator.hpp>
    7677#include <simd-lib/runtime.hpp>
    7778#include <simd-lib/pabloSupport.hpp>
     
    8687#include <sys/stat.h>
    8788#include <sys/mman.h>
    88 
    89 // mmap
    9089#include <fcntl.h>
    9190using namespace std;
    9291
    93 // -------------------
    94 
    95 template <class bitblock_t, class scanblock_t>
    96 class BitBlockStreamScanner {
    97 
    98 public:
    99 
    100 BitBlockStreamScanner():  bitblock_ary(NULL),
    101                           bitblock_cnt(-1),
    102                           bitblock_idx(-1),
    103                           scanblock_idx(-1)
    104 {
    105   remaining._bitblock = simd<1>::constant<0>();
    106 }
    107 
    108 void init(bitblock_t *s, uint32_t size) {
    109   bitblock_ary           = s;
    110   bitblock_cnt           = size;
    111   bitblock_idx           = 0;
    112   scanblock_idx          = 0;
    113   remaining._bitblock    = bitblock_ary[0];
    114 }
    115 
    116 int scan_to_next() {
    117 
    118   while(1) {
    119    
    120     for(scanblock_idx; scanblock_idx < (sizeof(bitblock_t)/sizeof(scanblock_t)); scanblock_idx++) {
    121       scanblock_t scan_item = remaining._scanblock[scanblock_idx];
    122       if(scan_item != 0) {
    123         int bit_pos = scan_forward_zeroes(scan_item);
    124         scan_item = scan_item & (scan_item - 1);
    125         remaining._scanblock[scanblock_idx] = scan_item;
    126         return (bitblock_idx * (BLOCK_SIZE)) + (scanblock_idx * sizeof(scanblock_t)*8) + bit_pos;
    127       }
    128     }
    129    
    130     scanblock_idx = 0;
    131     bitblock_idx++;
    132 
    133     if(bitblock_idx >= bitblock_cnt) {
    134       return -1;
    135     }
    136     remaining._bitblock = bitblock_ary[bitblock_idx];
    137   }
    138 
    139 }
    140 private:
    141 
    142   union {  bitblock_t   _bitblock;
    143            scanblock_t  _scanblock[sizeof(bitblock_t)/sizeof(scanblock_t)];} remaining;
    144 
    145   bitblock_t * bitblock_ary;
    146   uint32_t bitblock_cnt;
    147   uint32_t bitblock_idx;
    148   uint32_t scanblock_idx; 
    149  
    150 };
    151 
    152 // -------------------
    153 
    15492// S2K Generated
    15593###@global ###
    15694
    157 // Segment-at-a-time buffered stream processing parameters.
    158 const int SCANBLOCK_SIZE     = sizeof(ScanWord) * 8;
    159 const int SCANFIELD_SIZE     = sizeof(ScanWord) * 8;
    160 const int SEGMENT_BLOCKS     = SCANBLOCK_SIZE * SCANFIELD_SIZE / BLOCK_SIZE;
    161 const int SEGMENT_SIZE       = SEGMENT_BLOCKS * BLOCK_SIZE;
    162 
    163 // // \s2k{} bind
    164 // ATTRIBUTE_SIMD_ALIGN char buffer[SEGMENT_SIZE];
    165 // ATTRIBUTE_SIMD_ALIGN BitBlock line_starts[SEGMENT_SIZE/8];
    166 // ATTRIBUTE_SIMD_ALIGN BitBlock line_ends[SEGMENT_SIZE/8];
    167 // ATTRIBUTE_SIMD_ALIGN BitBlock match_follows[SEGMENT_SIZE/8];
    168 
     95// S2K Bind
    16996char * buffer;
    17097BitBlock * line_starts;
     
    17299BitBlock * match_follows;
    173100
    174 
    175 
    176101struct pipeline {
    177102 
    178103  public:
    179104 
    180   pipeline()
     105  pipeline() { }
     106  ~pipeline() { }
     107
     108  void process()
    181109  {
    182     // Initialize struct members
    183     basis_bits.bit_7 = simd<1>::constant<0>();
    184     basis_bits.bit_6 = simd<1>::constant<0>();
    185     basis_bits.bit_5 = simd<1>::constant<0>();
    186     basis_bits.bit_4 = simd<1>::constant<0>();
    187     basis_bits.bit_3 = simd<1>::constant<0>();
    188     basis_bits.bit_2 = simd<1>::constant<0>();
    189     basis_bits.bit_1 = simd<1>::constant<0>();
    190     basis_bits.bit_0 = simd<1>::constant<0>();
    191    
    192     lex.a   = simd<1>::constant<0>();
    193     lex.p   = simd<1>::constant<0>();
    194     lex.l   = simd<1>::constant<0>();
    195     lex.e   = simd<1>::constant<0>();
    196     lex.LF  = simd<1>::constant<0>();
    197    
    198     output.match_follows  = simd<1>::constant<0>();
    199     output.lines          = simd<1>::constant<0>();
    200     output.line_starts    = simd<1>::constant<0>();
    201     output.line_ends      = simd<1>::constant<0>();
    202   }
    203  
    204   ~pipeline() { }
    205 
    206   void process(int buffer_size)
    207   {
    208       int bytes_remaining = buffer_size;
    209       int block_index     = 0;
     110    int bytes_remaining = s2k::BUFFER_SIZE;
     111    int block_index     = 0;
    210112     
     113      // \s2k{} bind.
     114      char * byte_data;
     115
     116      // Process blocks.
    211117      while (bytes_remaining >= BLOCK_SIZE) {
     118       
     119        // \s2k{} bind.
    212120        byte_data = &buffer[block_index * BLOCK_SIZE];
    213121       
     
    215123        ###@filter_do_block ###
    216124 
    217          // \s2k{} writes
     125        // \s2k{} write.
    218126        match_follows[block_index]  = output.match_follows;
    219127        line_starts[block_index]    = output.line_starts;
     
    227135      BitBlock EOF_mask = bitblock::srl(simd<1>::constant<1>(),
    228136                                        convert(BLOCK_SIZE - bytes_remaining));
     137
     138      // \s2k{} bind.
    229139      byte_data = &buffer[block_index * BLOCK_SIZE];
    230140         
     
    232142      ###@filter_do_final_block ###
    233143     
    234       // \s2k{} writes
     144      // \s2k{} write.
    235145      match_follows[block_index]  = output.match_follows & EOF_mask;
    236146      line_starts[block_index]    = output.line_starts & EOF_mask;
    237147      line_ends[block_index]      = output.line_ends & EOF_mask;
    238 
    239148  }
    240149 
    241150  private:
    242  
    243   char * byte_data; /* fix, bind buffer on byte_data */
    244    
     151
    245152// S2K Generated
    246153###@struct_decls ###
     
    254161
    255162  public:
    256  
    257   grep():infile(stdin), outfile(stdout),
     163
     164  grep():infile(NULL), outfile(stdout),
    258165         pattern_size(sizeof("apple")-1),
    259166         byte_offset(0),
     
    270177    fdSrc = open(infilename, O_RDONLY);
    271178    if (fdSrc == -1) {
    272         fprintf(stderr, "Error: cannot open %s for processing.\n", infilename);
    273         exit(-1);
    274     }
     179      fprintf(stderr, "Error: cannot open %s for processing.\n", infilename);
     180      exit(-1);
     181    }
     182   
    275183    if (fstat(fdSrc, &infile_sb) == -1) {
    276         fprintf(stderr, "Error: cannot stat %s for processing.\n", infilename);
    277         exit(-1);
     184      fprintf(stderr, "Error: cannot stat %s for processing.\n", infilename);
     185      exit(-1);
    278186    }
    279187
    280188    buffer = (char *) mmap(NULL, infile_sb.st_size, PROT_READ, MAP_PRIVATE, fdSrc, 0);
    281     if (infile == MAP_FAILED) {
    282         fprintf(stderr, "Error: mmap of %s failure.\n", infilename);
    283         exit(-1);
    284     }
    285   }
    286  
    287   int read() {
    288  
    289     int buffer_size = 0;
    290  
    291     if((buffer_size = infile_sb.st_size) > 0 ) {
     189    if (buffer == MAP_FAILED) {
     190      fprintf(stderr, "Error: mmap of %s failure.\n", infilename);
     191      exit(-1);
     192    }
     193  }
     194 
     195  void read_infile() {
     196    if((s2k::BUFFER_SIZE = infile_sb.st_size) > 0 ) {
    292197      allocate();
    293       return buffer_size;
    294198    };
    295    
    296     return buffer_size;
    297199  }
    298200 
     
    304206    } 
    305207  }
    306  
     208
    307209  void close_infile() {
    308     close(fdSrc);
    309   }
    310  
     210    if(infile)  { close(fdSrc); infile=NULL; }
     211  }
     212
    311213  void close_outfile() {
    312214    if(outfile) { fclose(outfile); outfile=NULL;}
     
    327229  }
    328230 
    329   void process(int buffer_size) {
     231  void process() {
    330232   
    331233    // Segment-at-a-time variables
    332     int bytes_remaining = buffer_size;
     234    int bytes_remaining = s2k::BUFFER_SIZE;
    333235    int block_index     = 0;
    334236    int block_base      = 0;
    335     int block_count     = ((bytes_remaining % BLOCK_SIZE) == 0) // rounddown
     237    int block_count     = ((bytes_remaining % BLOCK_SIZE) == 0)
    336238                          ? (bytes_remaining/BLOCK_SIZE)
    337239                          : (bytes_remaining/BLOCK_SIZE) + 1;
     
    345247    line_ends_scanner.init(line_ends, block_count);
    346248     
    347      if(pattern_only_matching) {
    348        int match_offset = 0;
    349        while((match_offset = matches_scanner.scan_to_next()) > -1) {
    350          match_offset -= pattern_size;
    351          if(byte_offset) {
    352            int match_stream_offset = block_base + match_offset;
    353            fprintf(outfile, "%d:", match_stream_offset);
    354          }
    355 
    356          fwrite(&buffer[match_offset], 1, pattern_size, outfile); // lookahead
    357          fprintf(outfile, "\n");
    358    
    359        }
    360      } 
    361 
    362      if(line_matching) {
    363        int line_start_offset = 0;
    364        int line_end_offset = 0;
    365        
    366        while( ((line_start_offset = line_starts_scanner.scan_to_next()) > -1)
    367             &&
    368               ((line_end_offset = line_ends_scanner.scan_to_next()) > -1)
    369               ) {
    370              
    371          if(byte_offset) {
    372            fprintf(outfile, "%d:", block_base + line_start_offset,
    373                     block_base + line_end_offset);
    374          }
    375  
     249    if(pattern_only_matching) {
     250      int match_offset = 0;
     251      while((match_offset = matches_scanner.scan_to_next()) > -1) {
     252        match_offset -= pattern_size;
     253        if(byte_offset) {
     254          int match_stream_offset = block_base + match_offset;
     255          fprintf(outfile, "%d:", match_stream_offset);
     256        }
     257
     258        fwrite(&buffer[match_offset], 1, pattern_size, outfile); // lookahead
     259        fprintf(outfile, "\n");
     260      }
     261    } 
     262
     263    if(line_matching) {
     264      int line_start_offset = 0;
     265      int line_end_offset = 0;
     266
     267      while( ((line_start_offset = line_starts_scanner.scan_to_next()) > -1) &&
     268             ((line_end_offset = line_ends_scanner.scan_to_next()) > -1)) {
     269
     270        if(byte_offset) {
     271          fprintf(outfile, "%d:", block_base + line_start_offset,
     272          block_base + line_end_offset);
     273        }
     274
    376275        fwrite(&buffer[line_start_offset], 1,
    377276               line_end_offset - line_start_offset + 1, outfile);
    378        }
    379    
     277        }
    380278     }
    381      
    382279  }
    383280 
    384281  private:
    385282
    386   // \s2k bind helpers
     283  // helpers
    387284  void allocate() {
    388     line_starts   = simd_malloc<BitBlock>(infile_sb.st_size/8);
    389     line_ends     = simd_malloc<BitBlock>(infile_sb.st_size/8);
    390     match_follows = simd_malloc<BitBlock>(infile_sb.st_size/8);
     285    int size      = (s2k::BUFFER_SIZE + BLOCK_SIZE) / BLOCK_SIZE; 
     286    line_starts   = simd_malloc<BitBlock>(size);
     287    line_ends     = simd_malloc<BitBlock>(size);
     288    match_follows = simd_malloc<BitBlock>(size);
    391289  }
    392290 
     
    397295  }
    398296
     297  // members
    399298  int fdSrc;
    400299  struct stat infile_sb;
    401  
    402300  FILE *infile, * outfile;
    403301 
     
    429327        printf("\t-b,\tprint the byte offset with output lines\n");
    430328        printf("\t-o,\tshow only the part of a line matching PATTERN\n");
    431         printf("\t-V,\tprint version information and exit");           
     329        printf("\t-V,\tprint version information and exit");
    432330        exit(-1);
    433331    }
     
    436334  if (optind >= argc) {
    437335    printf ("Too few arguments\n");
    438     printf("Usage: %s [-c] [-v] <regex> <inputfile> [<outputfile>]\n", argv[0]);
     336    printf("Usage: %s [-b] [-o] <inputfile> [<outputfile>]\n", argv[0]);
    439337    exit(-1);
    440338  }
     
    445343  if(!(optind >= argc)) {
    446344    char * outfilename = argv[optind++];
     345    grep.open_outfile(outfilename);
     346    /*
    447347    if (optind != argc) {
    448348      printf ("Too many arguments\n");
     
    450350      exit(-1);
    451351    }
    452     grep.open_outfile(outfilename);
    453   }
    454 
    455 
    456   grep.open_infile(infilename);
    457   int segment_bytes = 0;
    458   if( (segment_bytes = grep.read()) > 0) {
    459     pipeline.process(segment_bytes);
    460     grep.process(segment_bytes);
    461   }
     352    */
     353  }
     354
     355  grep.read_infile();
     356  pipeline.process();
     357  grep.process();
    462358  grep.close_infile();
     359  grep.close_outfile();
    463360 
    464361  return 0;
Note: See TracChangeset for help on using the changeset viewer.