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

Go to the source code of this file.

Classes

class  MyInputFilter
class  MyOutputFilter
 Filter that writes each buffer to a file.
class  MyTransformFilter
 Filter that changes each decimal number to its square.
class  TextSlice
 Holds a slice of text.

Functions

int main (int argc, char *argv[])
int ParseCommandLine (int argc, char *argv[])
int run_pipeline (int nthreads)
void Usage ()

Variables

static const char * InputFileName = "input.txt"
static bool is_number_of_threads_set = false
const size_t MAX_CHAR_PER_INPUT_SLICE = 4000
static int NThread = tbb::task_scheduler_init::automatic
static const char * OutputFileName = "output.txt"

Function Documentation

int main ( int  argc,
char *  argv[] 
)

Definition at line 269 of file square.cpp.

References tbb::task_scheduler_init::default_num_threads(), is_number_of_threads_set, NThread, ParseCommandLine(), and run_pipeline().

                                   {
    if(!ParseCommandLine( argc, argv ))
        return 1;
    if (is_number_of_threads_set) {
        // Start task scheduler
        tbb::task_scheduler_init init( NThread );
        if(!run_pipeline (NThread))
            return 1;
    } else { // Number of threads wasn't set explicitly. Run serial and parallel version
        { // serial run
            tbb::task_scheduler_init init_serial(1);
            if(!run_pipeline (1))
                return 1;
        }
        { // parallel run (number of threads is selected automatically)
            tbb::task_scheduler_init init_parallel;
            if(!run_pipeline (init_parallel.default_num_threads()))
                return 1;
        }
    }
    return 0;
}
int ParseCommandLine ( int  argc,
char *  argv[] 
)

Definition at line 199 of file square.cpp.

References InputFileName, is_number_of_threads_set, NThread, OutputFileName, and Usage().

                                                {
    // Parse command line
    if( argc> 4 ){
        Usage();
        return 0;
    }
    if( argc>=2 ) InputFileName = argv[1];
    if( argc>=3 ) OutputFileName = argv[2];
    if( argc>=4 ) {
        NThread = strtol(argv[3],0,0);
        if( NThread<1 ) {
            fprintf(stderr,"nthread set to %d, but must be at least 1\n",NThread);
            return 0;
        }
        is_number_of_threads_set = true; //Number of threads is set explicitly
    }
    return 1;
}
int run_pipeline ( int  nthreads)

Definition at line 218 of file square.cpp.

References tbb::pipeline::add_filter(), InputFileName, is_number_of_threads_set, tbb::tick_count::now(), OutputFileName, tbb::pipeline::run(), t0, and Usage().

Referenced by main().

{
    FILE* input_file = fopen(InputFileName,"r");
    if( !input_file ) {
        perror( InputFileName );
        Usage();
        return 0;
    }
    FILE* output_file = fopen(OutputFileName,"w");
    if( !output_file ) {
        perror( OutputFileName );
        return 0;
    }

    // Create the pipeline
    tbb::pipeline pipeline;

    // Create file-reading writing stage and add it to the pipeline
    MyInputFilter input_filter( input_file );
    pipeline.add_filter( input_filter );

    // Create capitalization stage and add it to the pipeline
    MyTransformFilter transform_filter; 
    pipeline.add_filter( transform_filter );

    // Create file-writing stage and add it to the pipeline
    MyOutputFilter output_filter( output_file );
    pipeline.add_filter( output_filter );

    // Run the pipeline
    tbb::tick_count t0 = tbb::tick_count::now();
    // Need more than one token in flight per thread to keep all threads 
    // busy; 2-4 works
    pipeline.run( nthreads*4 );
    tbb::tick_count t1 = tbb::tick_count::now();

    fclose( output_file );
    fclose( input_file );

    if (is_number_of_threads_set) {
        printf("threads = %d time = %g\n", nthreads, (t1-t0).seconds());
    } else {
        if ( nthreads == 1 ){
            printf("serial run   time = %g\n", (t1-t0).seconds());
        } else {
            printf("parallel run time = %g\n", (t1-t0).seconds());
        }
    }
    return 1;
}
void Usage ( )

Definition at line 194 of file square.cpp.

{
    fprintf( stderr, "Usage:\tsquare [input-file [output-file [nthread]]]\n");
}

Variable Documentation

const char* InputFileName = "input.txt" [static]

Definition at line 83 of file square.cpp.

Referenced by ParseCommandLine(), and run_pipeline().

bool is_number_of_threads_set = false [static]

Definition at line 192 of file square.cpp.

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

const size_t MAX_CHAR_PER_INPUT_SLICE = 4000

Definition at line 82 of file square.cpp.

Definition at line 191 of file square.cpp.

Referenced by main(), and ParseCommandLine().

const char* OutputFileName = "output.txt" [static]

Definition at line 84 of file square.cpp.

Referenced by ParseCommandLine(), and run_pipeline().


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