/* -*- 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) // Consider the possibility to list all prime numbers in an interval [MIn, MAX] by: // // - split the interval in a number of given subintervals nw (n workers) // - assig each subinterval to a farm worker // - the worker // for each i in the interval // checks if i is prime if yes it packs the value in an array // finally the array is sent to the gatherer thread // - the gatherer threads gets all results and produces an ordered lists // of all primes // // (primality check in each partition) // // |----> Worker -->| // Emitter --|----> Worker -->|--Collector // |----> Worker -->| // (splits input array (gathers all sub-partitions) // in partitions) // #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; } // stream type struct Task_t { Task_t(unsigned long long n1, unsigned long long n2):n1(n1),n2(n2) {} const unsigned long long n1, n2; // input range std::vector V; // output data }; // splits the range [n1,n2] in nw sub-ranges each one of size ~(n2-n1)/nw struct Emitter: ff_node_t { Emitter(const ff_loadbalancer *lb, unsigned long long n1, unsigned long long n2) :lb(lb),n1(n1),n2(n2) {} Task_t *svc(Task_t *) { const int nw = lb->getNWorkers(); // gets the total number of workers added to the farm const size_t size = (n2 - n1) / nw; ssize_t more = (n2-n1) % nw; unsigned long long start = n1; unsigned long long stop = n1; for(int i=0; i0 ? 1:0); --more; Task_t *task = new Task_t(start, stop); lb->ff_send_out_to(task, i); } return EOS; // broadcast EOS to all workers } const ff_loadbalancer *lb; unsigned long long n1, n2; }; struct Worker: ff_node_t { Task_t *svc(Task_t *in) { auto &V = in->V; unsigned long long n, n1(in->n1), n2(in->n2); while( (n=n1++) < n2 ) if (is_prime(n)) V.push_back(n); return in; } }; struct Collector: ff_node_t { Task_t *svc(Task_t *in) { auto V = in->V; if (V.size()) // I may receive empty sub-partitions primes.insert(std::upper_bound(primes.begin(), primes.end(), V[0]), V.begin(), V.end()); delete in; return GO_ON; } void svc_end() { for(size_t i=0;i primes; }; 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, nullptr, &C); Emitter E(farm.getlb(), n1,n2); farm.add_emitter(&E); // replacing default emitter farm.cleanup_workers(); if (farm.run_and_wait_end()<0) error("running farm\n"); return 0; }