Strumenti Utente

Strumenti Sito


magistraleinformaticanetworking:spm:spm1213_c_ff_with_skepu

Compile with

 g++ -DSKEPU_CUDA -DSKEPU_OPENMP_THREADS=4 
      -fopenmp -o cpu -I . -I include pipemap.cpp -lpthread

to target CPU cores via OpenMP SKEPU backend or

nvcc -o cuda -I. -Iinclude pipemap.cu 

to target GPU via CUDA backend.

pipemap.cpp
#include <iostream>
#include <ff/pipeline.hpp>
#include <math.h>
 
#include <skepu/vector.h>
#include <skepu/map.h>
 
 
 
using namespace ff;
 
int N = 10; 
 
class Source: public ff_node {
public:
  Source(unsigned int streamlen):streamlen(streamlen)  {}
 
  void * svc(void * task) {
    if(streamlen != 0) {
      skepu::Vector<float> * v = new skepu::Vector<float>(N,(float)streamlen);
      streamlen--;
      task = (void *) v;
#ifdef DEBUG
      std::cout << "Source delivering:" << *v << std::endl;
#endif
    } else {
      task = NULL; 
    }
    return task;
  }
private:
  unsigned int streamlen;
};
 
class Drain: public ff_node {
  void * svc(void * task) {
    skepu::Vector<float> * v = (skepu::Vector<float> *) task;
#ifdef DEBUG
    std::cout << "Drain got " << *v << std::endl;
#endif
    return(GO_ON);
  }
}; 
 
#define ITERNO 800000
 
UNARY_FUNC(iters, float, a,
           for(int _i=0; _i<ITERNO; _i++) a = sin(a); return(a);
           )
 
int NN = 10; 
 
// UNARY_FUNC(f, float, a, return(a+1); )
 
class MapStage:public ff_node {
 
 
  void *svc(void * task) {
 
    skepu::Vector<float> * v = (skepu::Vector<float> *) task;
 
#ifdef DEBUG
    std::cout << "MapStage got: " << *v << std::endl; 
#endif
 
    skepu::Vector<float> * r = new skepu::Vector<float>(NN);
    skepu::Map<iters> skepumap(new iters);
 
    skepumap(*v, *r);n
 
#ifdef DEBUG
    std::cout << "MapStage delivering: " << *r << std::endl; 
#endif
    return((void *) r);
  }
};
 
 
int main(int argc, char * argv[]) {
  if (argc!=3) {
    std::cerr << "use: "  << argv[0] << " streamlen veclen\n";
    return -1;
  }
  N = atoi(argv[2]);
 
  // bild a 2-stage pipeline
  ff_pipeline pipe;
 
  pipe.add_stage(new Source(atoi(argv[1])));
 
  pipe.add_stage(new MapStage());
 
  pipe.add_stage(new Drain());
 
 
  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/spm1213_c_ff_with_skepu.txt · Ultima modifica: 30/04/2013 alle 16:51 (12 anni fa) da Marco Danelutto

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki