# DidaWiki

### Strumenti Sito

magistraleinformaticanetworking:spm:ffsieve

## Heratosthene Sieve in FastFlow

This computes the prime numbers using a variable number of stages. Printing is protected through a Pthread lock.

sievelock.cpp
#include <iostream>
#include <ff/pipeline.hpp>

using namespace ff;

class Sieve: public ff_node {
public:

Sieve() { filter = 0; }

void * svc(void * task) {
unsigned int * t = (unsigned int *)task;

if (filter == 0) {
filter = *t;
return GO_ON;
} else {
if(*t % filter == 0)
return GO_ON;
else
}
}

void svc_end() {
std::cout << "Prime(" << filter << ")\n";
return;
}

private:
int filter;
};

class Generate: public ff_node {
public:

Generate(int n) {
streamlen = n;
std::cout << "Generate object created" << std::endl;
return;
}

int svc_init() {
std::cout << "Sieve started. Generating a stream of " << streamlen <<
" elements, starting with " << task << std::endl;
return 0;
}

void * svc(void * tt) {
unsigned int * t = (unsigned int *)tt;

int * xi = (int *) calloc(1, sizeof(int));
return xi;
} else {
return NULL;
}
}
private:
int streamlen;
};

class Printer: public ff_node {

int svc_init() {
std::cout << "Printer started " << std::endl;
first = 0;
}

void * svc(void *t) {
int * xi = (int *) t;
if (first == 0) {
first = *xi;
}
return GO_ON;
}

void svc_end() {
std::cout << "Sieve terminating, prime numbers found up to " << first
<< std::endl;
}

private:
int first;
};

int main(int argc, char * argv[]) {
if (argc!=3) {
std::cerr << "use: "  << argv[0] << " nstages streamlen\n";
return -1;
}

ff_pipeline pipe;
int nstages = atoi(argv[1]);
for(int j=0; j<nstages; j++)

ffTime(START_TIME);
if (pipe.run_and_wait_end()<0) {
error("running pipeline\n");
return -1;
}
ffTime(STOP_TIME);

std::cerr << "DONE, pipe  time= " << pipe.ffTime() << " (ms)\n";
std::cerr << "DONE, total time= " << ffTime(GET_TIME) << " (ms)\n";
pipe.ffStats(std::cerr);
return 0;
}
magistraleinformaticanetworking/spm/ffsieve.txt · Ultima modifica: 21/03/2012 alle 18:55 (8 anni fa) da Marco Danelutto