Shadowrun: Awakened 29 September 2011 - Build 871
Classes | Typedefs | Functions | Variables
count_strings.cpp File Reference
#include <string>
#include <cstring>
#include <cctype>
#include <cstdlib>
#include <cstdio>
#include "tbb/concurrent_hash_map.h"
#include "tbb/blocked_range.h"
#include "tbb/parallel_for.h"
#include "tbb/tick_count.h"
#include "tbb/task_scheduler_init.h"
#include "tbb/tbb_allocator.h"
Include dependency graph for count_strings.cpp:

Go to the source code of this file.

Classes

struct  Sound
 Generator of random words.
struct  Tally
 Function object for counting occurrences of strings.

Typedefs

typedef std::basic_string
< char, std::char_traits< char >
, tbb::tbb_allocator< char > > 
MyString
 String type with scalable allocator.
typedef concurrent_hash_map
< MyString, int > 
StringTable
 A concurrent hash table that maps strings to ints.

Functions

static void CountOccurrences (int nthreads)
int CountRateSum (Sound sounds[], const int num, const int part)
static void CreateData ()
const char * GetLetters (int type, const int part)
int main (int argc, char *argv[])
static void ParseCommandLine (int argc, char *argv[])
 Main Driver.

Variables

Sound Consonants []
const int ConsonantsNumber = sizeof(Consonants)/sizeof(Sound)
int ConsonantsRatesSum [3] = {0,0,0}
static MyStringData
static bool is_number_of_threads_set = false
 Indicates if the number of threads wasn't set explicitly.
long N = 1000000
 Problem size.
static int NThread = 1
 Working threads count.
const int size_factor = 2
static bool Verbose = false
 Set to true to counts.
Sound Vowels []
const int VowelsNumber = sizeof(Vowels)/sizeof(Sound)
int VowelsRatesSum [3] = {0,0,0}

Typedef Documentation

typedef std::basic_string<char,std::char_traits<char>,tbb::tbb_allocator<char> > MyString

On platforms with non-scalable default memory allocators, the example scales better if the string allocator is changed to tbb::tbb_allocator<char>.

Definition at line 49 of file count_strings.cpp.

typedef concurrent_hash_map<MyString,int> StringTable

Definition at line 67 of file count_strings.cpp.


Function Documentation

static void CountOccurrences ( int  nthreads) [static]

Definition at line 84 of file count_strings.cpp.

References Data, is_number_of_threads_set, N, tbb::tick_count::now(), tbb::parallel_for(), t0, and Verbose.

Referenced by main().

                                           {
    StringTable table;

    tick_count t0 = tick_count::now();
    parallel_for( blocked_range<MyString*>( Data, Data+N, 1000 ), Tally(table) );
    tick_count t1 = tick_count::now();

    int n = 0;
    for( StringTable::iterator i=table.begin(); i!=table.end(); ++i ) {
        if( Verbose && nthreads )
            printf("%s %d\n",i->first.c_str(),i->second);
        n += i->second;
    }

    if (is_number_of_threads_set) {
        printf("threads = %d  total = %d  unique = %u  time = %g\n", nthreads, n, unsigned(table.size()), (t1-t0).seconds());
    } else {
        if ( nthreads == 1 ) {
            printf("serial run   total = %d  unique = %u  time = %g\n", n, unsigned(table.size()), (t1-t0).seconds());
        } else {
            printf("parallel run total = %d  unique = %u  time = %g\n", n, unsigned(table.size()), (t1-t0).seconds());
        }
    }
}
int CountRateSum ( Sound  sounds[],
const int  num,
const int  part 
)

Definition at line 165 of file count_strings.cpp.

Referenced by CreateData().

{
    int sum = 0;
    for(int i = 0; i < num; i++)
        sum += sounds[i].rates[part];
    return sum;
}
static void CreateData ( ) [static]

Definition at line 186 of file count_strings.cpp.

References Consonants, ConsonantsNumber, ConsonantsRatesSum, CountRateSum(), Data, GetLetters(), N, size_factor, Vowels, VowelsNumber, and VowelsRatesSum.

Referenced by main().

                         {
    for(int i = 0; i < 3; i++) {
        ConsonantsRatesSum[i] = CountRateSum(Consonants, ConsonantsNumber, i);
        VowelsRatesSum[i] = CountRateSum(Vowels, VowelsNumber, i);
    }
    for( int i=0; i<N; ++i ) {
        int type = rand();
        Data[i] = GetLetters(type++, 0);
        for( int j = 0; j < type%size_factor; ++j )
            Data[i] += GetLetters(type++, 1);
        Data[i] += GetLetters(type, 2);
    }
    MyString planet = Data[12]; planet[0] = toupper(planet[0]);
    MyString helloworld = Data[0]; helloworld[0] = toupper(helloworld[0]);
    helloworld += ", "+Data[1]+" "+Data[2]+" "+Data[3]+" "+Data[4]+" "+Data[5];
    printf("Message from planet '%s': %s!\nAnalyzing whole text...\n", planet.c_str(), helloworld.c_str());
}
const char* GetLetters ( int  type,
const int  part 
)

Definition at line 173 of file count_strings.cpp.

References Sound::chars, Consonants, ConsonantsRatesSum, Sound::rates, Vowels, and VowelsRatesSum.

Referenced by CreateData().

{
    Sound *sounds; int rate, i = 0;
    if(type & 1)
        sounds = Vowels, rate = rand() % VowelsRatesSum[part];
    else
        sounds = Consonants, rate = rand() % ConsonantsRatesSum[part];
    do {
        rate -= sounds[i++].rates[part];
    } while(rate > 0);
    return sounds[--i].chars;
}
int main ( int  argc,
char *  argv[] 
)

Definition at line 229 of file count_strings.cpp.

References CountOccurrences(), CreateData(), Data, is_number_of_threads_set, N, NThread, and ParseCommandLine().

                                   {
    srand(2);
    ParseCommandLine( argc, argv );
    Data = new MyString[N];
    CreateData();
    if (is_number_of_threads_set) {
        task_scheduler_init init(NThread);
        CountOccurrences(NThread);
    } else { // Number of threads wasn't set explicitly. Run serial and parallel version
        { // serial run
            task_scheduler_init init_serial(1);
            CountOccurrences(1);
        }
        { // parallel run (number of threads is selected automatically)
            task_scheduler_init init_parallel;
            CountOccurrences(0);
        }
    }
    delete[] Data;
}
static void ParseCommandLine ( int  argc,
char *  argv[] 
) [static]

Definition at line 206 of file count_strings.cpp.

References is_number_of_threads_set, N, NThread, and Verbose.

Referenced by main().

                                                       {
    int i = 1;
    if( i<argc && strcmp( argv[i], "verbose" )==0 ) {
        Verbose = true;
        ++i;
    }
    if( i<argc )
        if( !isdigit(argv[i][0]) ) {
            fprintf(stderr,"Usage: %s [verbose] [number-of-strings] [number-of-threads]\n",argv[0]);
            exit(1);
        } else {
            N = strtol(argv[i++],0,0);
        }
    if( i<argc )
        if( !isdigit(argv[i][0]) ) {
            fprintf(stderr,"Usage: %s [verbose] [number-of-strings] [number-of-threads]\n",argv[0]);
            exit(1);
        } else {
            NThread = strtol(argv[i++],0,0);
            is_number_of_threads_set = true;
        }
}

Variable Documentation

Definition at line 126 of file count_strings.cpp.

Referenced by CreateData(), and GetLetters().

const int ConsonantsNumber = sizeof(Consonants)/sizeof(Sound)

Definition at line 162 of file count_strings.cpp.

Referenced by CreateData().

int ConsonantsRatesSum[3] = {0,0,0}

Definition at line 163 of file count_strings.cpp.

Referenced by CreateData(), and GetLetters().

MyString* Data [static]

Definition at line 82 of file count_strings.cpp.

Referenced by CountOccurrences(), CreateData(), and main().

bool is_number_of_threads_set = false [static]

Definition at line 65 of file count_strings.cpp.

Referenced by CountOccurrences(), main(), and ParseCommandLine().

long N = 1000000
int NThread = 1 [static]

Definition at line 58 of file count_strings.cpp.

Referenced by main(), and ParseCommandLine().

const int size_factor = 2

Definition at line 62 of file count_strings.cpp.

Referenced by CreateData().

bool Verbose = false [static]

Definition at line 55 of file count_strings.cpp.

Referenced by CountOccurrences(), main(), Measure(), ParseCommandLine(), and partial_solve().

Initial value:
 {
    {"e", {445,6220,1762}}, {"a", {704,5262,514}}, {"i", {402,5224,162}}, {"o", {248,3726,191}},
    {"u", {155,1669,23}}, {"y", {4,400,989}}, {"io", {5,512,18}}, {"ia", {1,329,111}},
    {"ea", {21,370,16}}, {"ou", {32,298,4}}, {"ie", {0,177,140}}, {"ee", {2,183,57}},
    {"ai", {17,206,7}}, {"oo", {1,215,7}}, {"au", {40,111,2}}, {"ua", {0,102,4}},
    {"ui", {0,104,1}}, {"ei", {6,94,3}}, {"ue", {0,67,28}}, {"ay", {1,42,52}},
    {"ey", {1,14,80}}, {"oa", {5,84,3}}, {"oi", {2,81,1}}, {"eo", {1,71,5}},
    {"iou", {0,61,0}}, {"oe", {2,46,9}}, {"eu", {12,43,0}}, {"iu", {0,45,0}},
    {"ya", {12,19,5}}, {"ae", {7,18,10}}, {"oy", {0,10,13}}, {"ye", {8,7,7}},
    {"ion", {0,0,20}}, {"ing", {0,0,20}}, {"ium", {0,0,10}}, {"er", {0,0,20}}
}

Definition at line 115 of file count_strings.cpp.

Referenced by CreateData(), and GetLetters().

const int VowelsNumber = sizeof(Vowels)/sizeof(Sound)

Definition at line 161 of file count_strings.cpp.

Referenced by CreateData().

int VowelsRatesSum[3] = {0,0,0}

Definition at line 163 of file count_strings.cpp.

Referenced by CreateData(), and GetLetters().


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