int main(int argc, char *argv[]) { if (argc < 4) { printf("Usage: %s threads n repetitions\n",argv[0]); return 1; } dump_title(); int P = atoi(argv[1]); long n = atol(argv[2]); int T = atoi(argv[3]); // collect serial time long serial_result = 0; tbb::tick_count t0; for (int t = -1; t < T; ++t) { if (t == 0) t0 = tbb::tick_count::now(); serial_result += SerialFib(n); } double serial_elapsed = (tbb::tick_count::now() - t0).seconds(); output(0,n,0,T,serial_elapsed,serial_elapsed,serial_result); // perform search find_cutoff(P,n,T,serial_elapsed); return 0; }
tbb::task* execute() { if( n<CutOff ) { *sum = SerialFib(n); return NULL; } else { FibContinuation& c = *new( allocate_continuation() ) FibContinuation(sum); FibTask& b = *new( c.allocate_child() ) FibTask(n-1,&c.y); recycle_as_child_of(c); n -= 2; sum = &c.x; // Set ref_count to "two children". c.set_ref_count(2); c.spawn( b ); return this; } }
task* execute() { //Overrieds virtual function task:: execute if( n<CutOff) { *sum = SerialFib(n); } else { long x, y; FibTask& a = *new( allocate_child() ) FibTask(n-1, &x); FibTask& b = *new( allocate_child() ) FibTask(n-2, &y); //Set ref_count to "two children plus one for the wait". set_ref_count(3); //start b spawn(b); //Start a and wait for children spawn_and_wait_for_all(a); //Do the sum *sum = x+y; } return NULL; }
static volatile int Global; for( int k=0; k<10000; ++k ) for( int i=0; i<n; ++i ) ++Global; } int SerialFib( int n ) { int a=0, b=1; for( int i=0; i<n; ++i ) { b += a; a = b-a; } return a; } int F = SerialFib(N); int Fib ( int n ) { if( n < 2 ) { if ( g_sim ) { tbb::task_scheduler_init tsi(P_nested); } return n; } else { tbb::task_scheduler_init *tsi = NULL; tbb::task *cur = NULL; if ( g_sim ) { if ( n % 2 == 0 ) { if ( g_sim == tbbsched_auto_only || (g_sim == tbbsched_mixed && n % 4 == 0) ) { // Trigger TBB scheduler auto-initialization cur = &tbb::task::self();
long SerialFib( const long n ) { if( n<2 ) return n; else return SerialFib(n-1)+SerialFib(n-2); }