Shadowrun: Awakened 29 September 2011 - Build 871
sub_string_finder_extended.cpp
Go to the documentation of this file.
00001 /*
00002     Copyright 2005-2010 Intel Corporation.  All Rights Reserved.
00003 
00004     This file is part of Threading Building Blocks.
00005 
00006     Threading Building Blocks is free software; you can redistribute it
00007     and/or modify it under the terms of the GNU General Public License
00008     version 2 as published by the Free Software Foundation.
00009 
00010     Threading Building Blocks is distributed in the hope that it will be
00011     useful, but WITHOUT ANY WARRANTY; without even the implied warranty
00012     of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013     GNU General Public License for more details.
00014 
00015     You should have received a copy of the GNU General Public License
00016     along with Threading Building Blocks; if not, write to the Free Software
00017     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018 
00019     As a special exception, you may use this file as part of a free software
00020     library without restriction.  Specifically, if other files instantiate
00021     templates or use macros or inline functions from this file, or you compile
00022     this file and link it with other files to produce an executable, this
00023     file does not by itself cause the resulting executable to be covered by
00024     the GNU General Public License.  This exception does not however
00025     invalidate any other reasons why the executable file might be covered by
00026     the GNU General Public License.
00027 */
00028 
00029 #include <iostream>
00030 #include <string>
00031 #include <algorithm>
00032 
00033 #include "tbb/parallel_for.h"
00034 #include "tbb/blocked_range.h"
00035 #include "tbb/tick_count.h"
00036 
00037 using namespace tbb;
00038 using namespace std;
00039 static const size_t N = 22;
00040 
00041 void SerialSubStringFinder ( const string &str, size_t *max_array, size_t *pos_array) {
00042  for ( size_t i = 0; i < str.size(); ++i ) {
00043    size_t max_size = 0, max_pos = 0;
00044    for (size_t j = 0; j < str.size(); ++j)
00045     if (j != i) {
00046      size_t limit = str.size()-( i > j ? i : j );
00047      for (size_t k = 0; k < limit; ++k) {
00048       if (str[i + k] != str[j + k]) break;
00049       if (k > max_size) {
00050        max_size = k;
00051        max_pos = j;
00052       }
00053      }
00054     }
00055    max_array[i] = max_size;
00056    pos_array[i] = max_pos;
00057   }
00058 }
00059 
00060 class SubStringFinder {
00061  const string str;
00062  size_t *max_array;
00063  size_t *pos_array;
00064  public:
00065  void operator() ( const blocked_range<size_t>& r ) const {
00066   for ( size_t i = r.begin(); i != r.end(); ++i ) {
00067    size_t max_size = 0, max_pos = 0;
00068    for (size_t j = 0; j < str.size(); ++j) 
00069     if (j != i) {
00070      size_t limit = str.size()-( i > j ? i : j );
00071      for (size_t k = 0; k < limit; ++k) {
00072       if (str[i + k] != str[j + k]) break;
00073       if (k > max_size) {
00074        max_size = k;
00075        max_pos = j;
00076       }
00077      }
00078     }
00079    max_array[i] = max_size;
00080    pos_array[i] = max_pos;
00081   }
00082  }
00083  SubStringFinder(string &s, size_t *m, size_t *p) : 
00084   str(s), max_array(m), pos_array(p) { }
00085 };
00086 
00087 int main(int argc, char *argv[]) {
00088  
00089 
00090  string str[N] = { string("a"), string("b") };
00091  for (size_t i = 2; i < N; ++i) str[i] = str[i-1]+str[i-2];
00092  string &to_scan = str[N-1]; 
00093 
00094  size_t *max = new size_t[to_scan.size()];
00095  size_t *max2 = new size_t[to_scan.size()];
00096  size_t *pos = new size_t[to_scan.size()];
00097  size_t *pos2 = new size_t[to_scan.size()];
00098  cout << " Done building string." << endl;
00099 
00100  
00101  tick_count serial_t0 = tick_count::now();
00102  SerialSubStringFinder(to_scan, max2, pos2);
00103  tick_count serial_t1 = tick_count::now();
00104  cout << " Done with serial version." << endl;
00105 
00106  tick_count parallel_t0 = tick_count::now();
00107  parallel_for(blocked_range<size_t>(0, to_scan.size(), 100),
00108        SubStringFinder( to_scan, max, pos ) );
00109  tick_count parallel_t1 = tick_count::now();
00110  cout << " Done with parallel version." << endl;
00111 
00112  for (size_t i = 0; i < to_scan.size(); ++i) {
00113    if (max[i] != max2[i] || pos[i] != pos2[i]) {
00114      cout << "ERROR: Serial and Parallel Results are Different!" << endl;
00115    }
00116  }
00117  cout << " Done validating results." << endl;
00118 
00119  cout << "Serial version ran in " << (serial_t1 - serial_t0).seconds() << " seconds" << endl
00120            << "Parallel version ran in " <<  (parallel_t1 - parallel_t0).seconds() << " seconds" << endl
00121            << "Resulting in a speedup of " << (serial_t1 - serial_t0).seconds() / (parallel_t1 - parallel_t0).seconds() << endl;
00122  delete[] max;
00123  delete[] pos;
00124  delete[] max2;
00125  delete[] pos2;
00126  return 0;
00127 }
00128 

Copyright © 2007-2010 by The Shadowrun: Awakened Team. This work is licensed under the GNU Lesser General Public License 3.

GNU Lesser General Public License 3 Sourceforge.net