Strumenti Utente

Strumenti Sito


magistraleinformaticanetworking:spm:skepu_sample

Differenze

Queste sono le differenze tra la revisione selezionata e la versione attuale della pagina.

Link a questa pagina di confronto

Entrambe le parti precedenti la revisione Revisione precedente
Prossima revisione
Revisione precedente
magistraleinformaticanetworking:spm:skepu_sample [11/05/2012 alle 12:55 (12 anni fa)]
Marco Danelutto
magistraleinformaticanetworking:spm:skepu_sample [11/05/2012 alle 13:00 (12 anni fa)] (versione attuale)
Marco Danelutto
Linea 7: Linea 7:
 <code lang="C", src="containers.cpp"> <code lang="C", src="containers.cpp">
 #include <iostream> #include <iostream>
 +
 +//#define SKEPU_CUDA
  
 #include "skepu/vector.h" #include "skepu/vector.h"
Linea 40: Linea 42:
 } }
 </code> </code>
 +
 +=== Sample map ===
 +
 +<code lang="C", src="map.cu">
 +#include <iostream>
 +
 +
 +#define SKEPU_CUDA
 +
 +#include "skepu/vector.h"
 +#include "skepu/matrix.h"
 +#include "skepu/map.h"
 +
 +using namespace std;
 +
 +// definition of functions to be used in the map and reduce
 +
 +UNARY_FUNC(inc,  int, a, return(a+1); )
 +UNARY_FUNC(dec,  int, a, return(a-1); )
 +UNARY_FUNC(sq,   int, a, return(a*a); )
 +BINARY_FUNC(add, int, a, b, return(a+b); )
 +BINARY_FUNC(sub, int, a, b, return(a-b); )
 +BINARY_FUNC(mul, int, a, b, return(a*b); )
 +BINARY_FUNC(div, int, a, b, return(a/b); )
 +
 +#define N 4
 +
 +int main()
 +{
 +  // declare vectors 
 +  skepu::Vector<float> v0(N);
 +  skepu::Vector<float> v1(N);
 +  skepu::Vector<float> v2(N);
 +  
 +  // initialize the vector(s) to some meaningful value
 +  for(int i=0; i<N;i++) {
 +    v0[i]=i+1;
 +    v1[i]=-(i+1);
 +  }
 +  cout << "Initial array: " << v0 << endl;  
 +    
 +  // one input map: forall i compute f(v0[i])
 +  skepu::Map<sq> sqmap(new sq);
 +  sqmap(v0,v2);
 +  cout << "After sqmap:   " << v2 << endl; 
 +
 +  // two input map: forall i compute f(v0[i],v1[i])
 +  // same as: alpha(f) o zip
 +  skepu::Map<add> mapzip(new add);
 +  mapzip(v0,v1,v2);
 +  cout << "After mapzip:  " << v2 << endl; 
 +
 +  return 0;
 +}
 +</code>
 +
 +=== Sample reduce ====
 +<code lang="C",src="reduce.cpp">
 +#include <iostream>
 +
 +#include "skepu/vector.h"
 +#include "skepu/matrix.h"
 +#include "skepu/reduce.h"
 +
 +using namespace std;
 +
 +// definition of functions to be used in the map and reduce
 +
 +UNARY_FUNC(inc,  int, a, return(a+1); )
 +UNARY_FUNC(dec,  int, a, return(a-1); )
 +UNARY_FUNC(sq,   int, a, return(a*a); )
 +BINARY_FUNC(add, int, a, b, return(a+b); )
 +BINARY_FUNC(sub, int, a, b, return(a-b); )
 +BINARY_FUNC(mul, int, a, b, return(a*b); )
 +BINARY_FUNC(div, int, a, b, return(a/b); )
 +
 +#define N 4
 +
 +int main()
 +{
 +  // declare vectors 
 +  skepu::Vector<float> v0(N);
 +  skepu::Matrix<float> m(N,N);
 +  
 +  // initialize the vector(s) to some meaningful value
 +  for(int i=0; i<N;i++) {
 +    v0[i]=i+1;
 +  }
 +  cout << "Initial  array is " << v0 << endl;   
 +  for(int i=0; i<N; i++) 
 +    for(int j=0; j<N; j++) 
 +      m(i,j) = (i*N)+j;
 +  cout << "Initial matrix is " << m << endl; 
 +    
 +  skepu::Reduce<add> reduceadd(new add);
 +  int i = reduceadd(v0);
 +  cout << "After reduceadd : " << i << endl; 
 +
 +  skepu::Reduce<add> reduceaddmat(new add); 
 +  i = reduceaddmat(m);
 +  cout << "After reduceaddmat : " << i << endl;
 +  return 0;
 +}
 +</code>
 +
 +=== Sample stencil (mapoverlap) ====
 +<code lang="C",src="stencil.cpp">
 +#include <iostream>
 +
 +#include "skepu/vector.h"
 +#include "skepu/matrix.h"
 +#include "skepu/mapoverlap.h"
 +
 +using namespace std;
 +
 +OVERLAP_FUNC(avg, float, 1, a, return((a[-1]+a[0]+a[1])/3); )
 +
 +#define N 8
 +
 +int main()
 +{
 +  // declare vectors 
 +  skepu::Vector<float> v0(N);
 +  skepu::Vector<float> v2(N);
 +  
 +  // initialize the vector(s) to some meaningful value
 +  for(int i=0; i<N;i++) {
 +    v0[i]=i;
 +  }
 +  cout << "Initial array:                 " << v0 << endl;  
 +    
 +  // compute things in stencil map mode
 +  skepu::MapOverlap<avg> average(new avg);
 +  //    in vecs,  missing boundary filled with 1 (CONSTANT)
 +  average(v0,v2,skepu::CONSTANT, (float)1 );
 +  cout << "After average (boundary = 1):  " << v2 << endl; 
 +  //            consider the vector as a torus
 +  average(v0,v2,skepu::CYCLIC);
 +  cout << "After average (cyclic):        " << v2 << endl; 
 +
 +  return 0;
 +}
 +</code>
 +
 +=== Sample maparray ====
 +<code lang="C",src="maparray.cpp">
 +#include <iostream>
 +
 +#include "skepu/vector.h"
 +#include "skepu/matrix.h"
 +#include "skepu/maparray.h"
 +
 +using namespace std;
 +
 +// definition of functions to be used in the map and reduce
 +
 +#define N 4
 +
 +ARRAY_FUNC(f,int, a, b, 
 +         { int s = 0; \
 +    for(int i=0; i<N; i++) { \
 +      s += a[i]; \
 +           } \
 +           return(s*b); \
 +          )
 +
 +int main()
 +{
 +  // declare vectors 
 +  skepu::Vector<int> v0(N);
 +  skepu::Vector<int> v1(N);
 +  skepu::Vector<int> v2(N);
 +  
 +  // initialize the vector(s) to some meaningful value
 +  for(int i=0; i<N;i++) {
 +    v0[i]=i+1;
 +    v1[i]=i;
 +  }
 +  cout << "Initial array: " << v0 << endl;  
 +  cout << "               " << v1 << endl;  
 +    
 +  skepu::MapArray<f> ff(new f);
 +  ff(v0,v1,v2);
 +  cout << "After ff:      " << v2 << endl; 
 +
 +  return 0;
 +}
 +</code>
 +
 +=== Sample scan ====
 +<code lang="C",src="scan.cpp">
 +#include <iostream>
 +
 +#include "skepu/vector.h"
 +#include "skepu/matrix.h"
 +#include "skepu/scan.h"
 +
 +using namespace std;
 +
 +// definition of functions to be used in the map and reduce
 +
 +BINARY_FUNC(add, float, a, b, return(a+b); )
 +BINARY_FUNC(mul, float, a, b, return(a*b); )
 +
 +#define N 4
 +
 +int main()
 +{
 +  // declare vectors 
 +  skepu::Vector<float> v0(N);
 +  skepu::Vector<float> v1(N);
 +  skepu::Vector<float> v2(N);
 +  
 +  // initialize the vector(s) to some meaningful value
 +  for(int i=0; i<N;i++) {
 +    v0[i]=i+1;
 +    v1[i]=-(i+1);
 +  }
 +  cout << "Initial array: " << v0 << endl;  
 +    
 +  // one input map: forall i compute f(v0[i])
 +  skepu::Scan<add> scanadd(new add);
 +  // at pos i sum of items up to (i)
 +  scanadd(v0,v2,skepu::INCLUSIVE);
 +  cout << "After scanadd: " << v2 << endl; 
 +  // at pos i sum of items up to (i-1)
 +  scanadd(v0,v2,skepu::EXCLUSIVE);
 +  cout << "After scanadd: " << v2 << endl; 
 +
 +  return 0;
 +}
 +</code>
 +
magistraleinformaticanetworking/spm/skepu_sample.1336740958.txt.gz · Ultima modifica: 11/05/2012 alle 12:55 (12 anni fa) da Marco Danelutto