sit/tests/libs/fastflow/matrix.cpp

85 lines
1.3 KiB
C++
Raw Permalink Normal View History

2012-03-06 15:07:26 +00:00
#include <iostream>
#include <fstream>
using namespace std;
#define N 1024
int A[N][N], B[N][N], C[N][N];
#ifdef FASTFLOW
#include <ff/farm.hpp>
struct task_t {
task_t(int i, int j):i(i),j(j) {}
int i;
int j;
};
class Worker : public ff::ff_node{
public:
void * svc(void * task){
task_t * t = (task_t *) task;
int _C = 0;
for(int k = 0; k < N; k++)
_C += A[t->i][k]*B[k][t->j];
C[t->i][t->j] = _C;
delete t;
return GO_ON;
}
};
#endif
int main() {
#ifdef FASTFLOW
ff::ff_farm<> farm(true);
vector<ff::ff_node *> w;
cout << "Using " << NWORKERS << " workers." << endl;
for(int i = 0; i < NWORKERS; i++)
w.push_back(new Worker);
farm.add_workers(w);
farm.run_then_freeze();
#endif
for(int i = 0; i < N; i++) {
for(int j = 0; j < N; j++) {
#ifdef FASTFLOW
task_t * task = new task_t(i,j);
farm.offload(task);
#else
int _C = 0;
for(int k = 0; k < N; k++)
_C += A[i][k]*B[k][j];
C[i][j] = _C;
#endif
}
}
#ifdef FASTFLOW
farm.offload((void *) ff::FF_EOS);
farm.wait();
#endif
string filename;
#ifdef FASTFLOW
filename = "matrix-ff.dat";
#else
filename = "matrix.dat";
#endif
fstream off;
off.open(filename.c_str(), fstream::out);
for(int i = 0; i < N; i ++) {
for( int j = 0; j < N; j++)
off << C[i][j];
}
off.close();
return 0;
}