/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */ /* *************************************************************************** * This program 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. * * This program 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 this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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. * **************************************************************************** */ /* * Author: Massimo Torquati * Date: November 2014 */ // // Toy example to test FastFlow farm. // It finds the prime numbers in the range (n1,n2) using a farm + feedback. // // |----> Worker -- // Emitter --|----> Worker --|-- // ^ |----> Worker -- | // |__________________________| // // - The Emitter generates all number between n1 and n2 and then waits // to receive primes from the Workers. // - The Worker check if the number is prime (by using the is_prime function), // if yes then it sends the prime number back to the Emitter. // // Usage example: // ./farm_primes 1900 2000 3 | tr -s ' ' '\n' | sort -n #include #include #include using namespace ff; // see http://en.wikipedia.org/wiki/Primality_test bool is_prime(unsigned long long n) { if (n <= 3) return n > 1; // 1 is not prime ! if (n % 2 == 0 || n % 3 == 0) return false; for (unsigned long long i = 5; i * i <= n; i += 6) { if (n % i == 0 || n % (i + 2) == 0) return false; } return true; } struct Emitter: ff_node_t { Emitter(ff_loadbalancer *lb, unsigned long long n1, unsigned long long n2) :lb(lb),n1(n1),n2(n2) {} long *svc(long *x) { if (x == nullptr) { // first time unsigned long long n; while( (n=n1++) <= n2 ) ff_send_out((void*)n); lb->broadcast_task(EOS); return GO_ON; } std::cout << reinterpret_cast(x) << " "; return GO_ON; } ff_loadbalancer *const lb; unsigned long long n1,n2; }; struct Worker: ff_node_t { long *svc(long *in) { if (is_prime(reinterpret_cast(in))) return in; return GO_ON; } }; int main(int argc, char *argv[]) { if (argc<4) { std::cerr << "use: " << argv[0] << " number1 number2 nworkers\n"; return -1; } unsigned long long n1 = atoll(argv[1]); unsigned long long n2 = atoll(argv[2]); const int nw = atoi(argv[3]); std::vector W; for(int i=0;i farm(W); // by default adds an emitter and a collector Emitter E(farm.getlb(), n1,n2); farm.add_emitter(&E); // replacing the default emitter farm.remove_collector(); // removing the default collector farm.wrap_around(); // adds feedback channel between worker and emitter farm.run_and_wait_end(); std::cout << "\n\n"; return 0; }