/* Copyright 2005-2016 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 computes number of prime numbers up to n, // where n is a command line argument. The algorithm here is a // fairly efficient version of the sieve of Eratosthenes. // The parallel version demonstrates how to use parallel_reduce, // and in particular how to exploit lazy splitting. #include "primes.h" #if __TBB_MIC_OFFLOAD #pragma offload_attribute (target(mic)) #endif // __TBB_MIC_OFFLOAD #include #include #include #include #include #include #include #include "tbb/parallel_reduce.h" #include "tbb/task_scheduler_init.h" using namespace std; //! If true, then print primes on stdout. static bool printPrimes = false; class Multiples { inline NumberType strike( NumberType start, NumberType limit, NumberType stride ) { // Hoist "my_is_composite" into register for sake of speed. bool* is_composite = my_is_composite; assert( stride>=2 ); for( ;start=1 ); my_is_composite = new bool[m/2]; my_striker = new NumberType[m/2]; for( size_t k=0; k=2; if( n>=3 ) { Multiples multiples(n); count += multiples.n_factor; if( printPrimes ) printf("---\n"); NumberType window_size = multiples.m; for( NumberType j=multiples.m; j<=n; j+=window_size ) { if( j+window_size>n+1 ) window_size = n+1-j; count += multiples.find_primes_in_window( j, window_size ); } } return count; } //! Range of a sieve window. class SieveRange { //! Width of full-size window into sieve. const NumberType my_stride; //! Always multiple of my_stride NumberType my_begin; //! One past last number in window. NumberType my_end; //! Width above which it is worth forking. const NumberType my_grainsize; bool assert_okay() const { assert( my_begin%my_stride==0 ); assert( my_begin<=my_end ); assert( my_stride<=my_grainsize ); return true; } public: //------------------------------------------------------------------------ // Begin signatures required by parallel_reduce //------------------------------------------------------------------------ bool is_divisible() const {return my_end-my_begin>my_grainsize;} bool empty() const {return my_end<=my_begin;} SieveRange( SieveRange& r, tbb::split ) : my_stride(r.my_stride), my_grainsize(r.my_grainsize), my_end(r.my_end) { assert( r.is_divisible() ); assert( r.assert_okay() ); NumberType middle = r.my_begin + (r.my_end-r.my_begin+r.my_stride-1)/2; middle = middle/my_stride*my_stride; my_begin = middle; r.my_end = middle; assert( assert_okay() ); assert( r.assert_okay() ); } //------------------------------------------------------------------------ // End of signatures required by parallel_reduce //------------------------------------------------------------------------ NumberType begin() const {return my_begin;} NumberType end() const {return my_end;} SieveRange( NumberType begin, NumberType end, NumberType stride, NumberType grainsize ) : my_begin(begin), my_end(end), my_stride(stride), my_grainsize(grainsizer.end() ) window_size = r.end()-j; count += multiples.find_primes_in_window( j, window_size ); } } void join( Sieve& other ) { count += other.count; // Final value of multiples needs to final value of other.mulitiples, // so that *this can correcty process next window to right. multiples.move( other.multiples ); } Sieve( Sieve& other, tbb::split ) : multiples(other.multiples,tbb::split()), count(0) {} //------------------------------------------------------------------------ // End of signatures required by parallel_reduce //------------------------------------------------------------------------ }; //! Count number of primes between 0 and n /** This is the parallel version. */ NumberType ParallelCountPrimes( NumberType n , int number_of_threads, NumberType grain_size ) { tbb::task_scheduler_init init(number_of_threads); // Two is special case NumberType count = n>=2; if( n>=3 ) { Sieve s(n); count += s.multiples.n_factor; if( printPrimes ) printf("---\n"); using namespace tbb; // Explicit grain size and simple_partitioner() used here instead of automatic grainsize // determination becase we want SieveRange to be decomposed down to grainSize or smaller. // Doing so improves odds that the working set fits in cache when evaluating Sieve::operator(). parallel_reduce( SieveRange( s.multiples.m, n, s.multiples.m, grain_size ), s, simple_partitioner() ); count += s.count; } return count; }