Strumenti Utente

Strumenti Sito


magistraleinformaticanetworking:spm:spm1213_c_ff_load_balancing
sbil_ff.cpp
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
 
#include <omp.h>
#include "ff/farm.hpp"
 
using namespace std;
using namespace ff; 
 
float **a; 
int     n; 
 
float calc(float x) {
  long iter = (long) x; 
  for(long i=0; i<iter; i++) 
    x = sin(x); 
  return(x);
}
 
class Worker : public ff_node {
  void * svc(void * t) {
 
    float * task = (float *) t;
    float * sum = new float;
 
    for(int i=0; i<n; i++) {
      task[i] = calc(task[i]);
      *sum += task[i]; 
    }
    return ((void *) sum); 
  }
};
 
class Emitter : public ff_node {
  void * svc(void * t) {
    a = new float * [n];
    for(int i=0; i<n; i++) 
      a[i] = new float[n];
 
    for(int i=0; i<n; i++) 
      for(int j=0; j<n; j++) 
	a[i][j] = i* 100 + j * 10; 
 
    for(int i=0; i<n; i++) 
      ff_send_out((void *) a[i]);
 
    return(EOS); 
  }
};
 
 
class Collector : public ff_node {
private: 
  float sum; 
public: 
 
  int svc_init() {
    sum = 0.0; 
    return(0); 
  }
 
  void svc_end() {
    cout << "sum is " << sum << endl; 
    return; 
  }
 
  void * svc(void * t) {
    float * x = ((float *) t) ;
    sum += *x; 
    return GO_ON; 
  }
};
 
int main(int argc, char * argv[]) {
 
  if(argc == 1) {
    cout << "Usage is:\n" << argv[0] << " veclen numthread " << endl; 
    return(0);
  }
 
  n   = atoi(argv[1]); 
  int nw; 
  float tc1; 
 
  if(argc == 3) {
    nw  = atoi(argv[2]); 
  } 
  if(argc == 4) {
    nw = atoi(argv[3]); 
    tc1 = atof(argv[2]); 
  }
 
 
  float sum = 0.0;
  ff_farm<> farm; 
 
  vector<ff_node *> workers;
  for(int i=0;i<nw;++i) 
    workers.push_back(new Worker);
  farm.add_workers(workers); 
  farm.add_collector(new Collector); 
  farm.add_emitter(new Emitter); 
 
  float ts0 = omp_get_wtime();
  farm.run_and_wait_end();
  float ts1 = omp_get_wtime();
  float tc = ts1 - ts0; 
 
  cout << tc << " " ;
  if(argc==4) {
    cout << " sp = " << tc1 / tc;
  } 
  cout << endl; 
  return(0); 
 
}
magistraleinformaticanetworking/spm/spm1213_c_ff_load_balancing.txt · Ultima modifica: 22/04/2013 alle 16:19 (12 anni fa) da Marco Danelutto

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki