/* -*- 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 pipeline. // It finds the first n prime numbers using a pipeline of n stages. // Please remember that in FastFlow pipeline stages are threads ! // Example: // pipe_primes 16 // // generator ---> stage(2) ---> stage(3) ---> stage(4) // ^ | // |___________________________________________| // // - The number of stages is floor(sqrt(n)) plus the generator. // - stage(i) sends out only those numbers x such that (x != i and (x % i) != 0) // - generators first generates all numbers, then prints all primes received // in input from the last stage. // #include #include #include #include using namespace ff; // stream generator struct generator: ff_node { generator(long n):n(n) {} void *svc(void *x) { if (x == nullptr) { // first time for(long x=2;x<=n;++x) ff_send_out((void*)x); ff_send_out(EOS); return GO_ON; } std::cout << reinterpret_cast(x) << " "; return GO_ON; } long n; }; // generic stage struct stage: ff_node_t { int svc_init() { myid = get_my_id() + 1 ; return 0; } long *svc(long *t) { long x = (long)t; if (x == myid) return t; if (x % myid) return t; return GO_ON; } long myid; }; int main(int argc, char *argv[]) { if (argc<2) { std::cerr << "use: " << argv[0] << " number (not too big !)\n"; return -1; } long n = atol(argv[1]); assert(n>1); // The number of stages (i.e. threads) is floor(sqrt(n))+1 || ff_pipe pipe(new generator(n)); long sq = sqrt(n); for(long i=2;i<=sq;++i) pipe.add_stage(new stage); pipe.cleanup_nodes(); pipe.wrap_around(); pipe.run_and_wait_end(); std::cout << "\n\n"; return 0; }