File: parallel_preorder.cpp

package info (click to toggle)
tbb 3.0%2Br035-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 11,620 kB
  • ctags: 27,241
  • sloc: cpp: 60,220; ansic: 5,220; asm: 802; makefile: 622; sh: 89
file content (181 lines) | stat: -rw-r--r-- 5,874 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
    Copyright 2005-2010 Intel Corporation.  All Rights Reserved.

    This file is part of Threading Building Blocks.

    Threading Building Blocks is free software; you can redistribute it
    and/or modify it under the terms of the GNU General Public License
    version 2 as published by the Free Software Foundation.

    Threading Building Blocks is distributed in the hope that it will be
    useful, but WITHOUT ANY WARRANTY; without even the implied warranty
    of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Threading Building Blocks; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

    As a special exception, you may use this file as part of a free software
    library without restriction.  Specifically, if other files instantiate
    templates or use macros or inline functions from this file, or you compile
    this file and link it with other files to produce an executable, this
    file does not by itself cause the resulting executable to be covered by
    the GNU General Public License.  This exception does not however
    invalidate any other reasons why the executable file might be covered by
    the GNU General Public License.
*/

/* Example program that shows how to use parallel_do to do parallel preorder 
   traversal of a directed acyclic graph. */

#include "tbb/parallel_do.h"
#include "tbb/atomic.h"
#include <vector>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include "Graph.h"

using namespace std;

//! Number of trials. Can be changed from command line
int ntrial = 50;

class Body {
public:
    Body() {};

    //------------------------------------------------------------------------
    // Following signatures are required by parallel_do
    //------------------------------------------------------------------------
    typedef Cell* argument_type;

    void operator()( Cell* c, tbb::parallel_do_feeder<Cell*>& feeder ) const {
        c->update();
        // Restore ref_count in preparation for subsequent traversal.
        c->ref_count = ArityOfOp[c->op];
        for( size_t k=0; k<c->successor.size(); ++k ) {
            Cell* successor = c->successor[k];
            if( 0 == --(successor->ref_count) ) {
                feeder.add( successor );
            }
        }
    }
};   

void ParallelPreorderTraversal( const vector<Cell*>& root_set ) {
    tbb::parallel_do(root_set.begin(), root_set.end(),Body());
}

//------------------------------------------------------------------------
// Test driver
//------------------------------------------------------------------------

#include <cctype>
#include "tbb/task_scheduler_init.h"
#include "tbb/tick_count.h"

//! A closed range of int.
struct IntRange {
    int low;
    int high;
    void set_from_string( const char* s );
    IntRange( int low_, int high_ ) : low(low_), high(high_) {}
};

void IntRange::set_from_string( const char* s ) {
    char* end;
    high = low = strtol(s,&end,0);
    switch( *end ) {
    case ':': 
        high = strtol(end+1,0,0); 
        break;
    case '\0':
        break;
    default:
        printf("unexpected character = %c\n",*end);
    }
}

//! Number of threads to use.
static IntRange NThread(1,4);

//! If true, then at end wait for user to hit return
static bool PauseFlag = false;

//! Displays usage message
void Usage(char * argv0) {
    fprintf(stderr, "Usage: %s [nthread [ntrials ['pause']]]\n", argv0);
    fprintf(stderr, "where nthread is a non-negative integer, or range of the form low:high [%d:%d]\n", NThread.low, NThread.high);
    fprintf(stderr, "ntrials is a positive integer. Default value is 50, reduce it (e.g. to 5) to shorten example run time\n");
    fprintf(stderr, "The application waits for user to hit return if 'pause' is specified\n");
}

//! Parse the command line.
static void ParseCommandLine( int argc, char* argv[] ) {
    int i = 1;
        if( i<argc && !isdigit(argv[i][0]) ) { 
        // Command line is garbled.
        Usage(argv[0]);
        exit(1);
    }
    if( i<argc )
        NThread.set_from_string(argv[i++]);
    if( i<argc && !isdigit(argv[i][0]) ) { 
        // Command line is garbled.
        Usage(argv[0]);
        exit(1);
    }
    if (i<argc) {
        ntrial = strtol(argv[i++], 0, 0);
    }
    if (ntrial == 0) {
        // Command line is garbled.
        Usage(argv[0]);
        exit(1);
    }
    if (i<argc && strcmp( argv[i], "pause" )==0 ) {
        PauseFlag = true;
    }
}

int main( int argc, char* argv[] ) {
    ParseCommandLine(argc,argv);

    // Start scheduler with given number of threads.
    for( int p=NThread.low; p<=NThread.high; ++p ) {
        tbb::task_scheduler_init init(p);
        srand(2);
        tbb::tick_count::interval_t interval;
        size_t total_root_set_size = 0;
        for( int trial=0; trial<ntrial; ++trial ) {
            Graph g;
            g.create_random_dag(1000);
            vector<Cell*> root_set;
            g.get_root_set(root_set);
            total_root_set_size += root_set.size();

            tbb::tick_count t0 = tbb::tick_count::now();
            for( int i=0; i<10; ++i ) {
                ParallelPreorderTraversal(root_set);
            }
            tbb::tick_count t1 = tbb::tick_count::now();

            interval += t1-t0;
        }
        printf("%g seconds using %d threads (average of %g nodes in root_set)\n",interval.seconds(),p,(double)total_root_set_size/ntrial);
    }

    if (PauseFlag) {
        printf ("Press return key to exit");
        char c;
        int n = scanf("%c", &c);
        if( n!=1 ) {
            fprintf(stderr,"Fatal error: unexpected end of input\n");
            exit(1);
        }
    }

    return 0;
}