/* -*- 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 */ // SPM class exercise (Class Work 2) // Set up a FastFlow pipeline filtering a stream of integers from 2 to MAX, // (MAX is a command line argument) such that: // - the first stage generates integers from 2 to MAX // - the i-th stage: // - if it does not have any number stored, stores the first number received // and does not forward it to the next stage // - if it has a number stored: // - passes the received number only if it is not a multiple of the stored integer // otherwise discards the received number // - when EOS is received, each stage prints its stored number (a prime number) // // // generator ---> stage(2) ---> stage(3) ---> stage(5)... -->discard // // #include #include #include #include using namespace ff; // stream generator struct generator: ff_node_t { generator(long n):n(n) {} long *svc(long *) { for(long x=2;x<=n;++x) ff_send_out(reinterpret_cast(x)); return EOS; } long n; }; // generic stage struct stage: ff_node_t { int svc_init() { storage = 0 ; return 0; } long *svc(long *t) { long x = (long)t; if (!storage) storage = x; if (x % storage) return t; return GO_ON; } void svc_end() { if (storage) std::cout << storage << " "; } long storage; }; // task discarder struct discarder: ff_node { void *svc(void *t) { return GO_ON; } }; 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); ff_pipe pipe(new generator(n)); // adding the first stage for(long i=2;i<=n;++i) pipe.add_stage(new stage); // adding all "middle" stages pipe.add_stage(new discarder); // adding the last stage pipe.cleanup_nodes(); if (pipe.run_and_wait_end()<0) error("running pipe\n"); std::cout << "\n\n"; return 0; }