magistraleinformaticanetworking:spm:skepu_sample
Questa è una vecchia versione del documento!
SPM SkePU sample code
Containers
Illustrates the usage of Vectors and Matrixes. To execute the code:
- on CPU: compile it using
g++ -I skepuinclude containers.cpp -o cpu_containers
- on GPU: uncomment the macro defining SKEPU_CUDA and compile with
nvcc -I skepuinclude containers.cu -o gpu_containers
- src="containers.cpp"
#include <iostream> //#define SKEPU_CUDA #include "skepu/vector.h" #include "skepu/matrix.h" #define N 16 #define M 16 using namespace std; int main() { skepu::Vector<float> v(N,0.0); skepu::Matrix<float> m(N,M,0.0); cout << "Initialized matrix and vector" << endl; for(int i=0; i<N;i++) { cout << "v[" << i << "]=" << v[i] << endl; } for(int i=0; i<N;i++) { for(int j=0; i<N;i++) { cout << "m[" << i << "," << j << "]=" << m(i,j) << endl; } } cout << "map +5 on matrix m" << endl; m += 5; for(int i=0; i<N;i++) { for(int j=0; i<N;i++) { cout << "m[" << i << "," << j << "]=" << m(i,j) << endl; } } return 0; }
Sample map
- 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; }
magistraleinformaticanetworking/spm/skepu_sample.1336741057.txt.gz · Ultima modifica: 11/05/2012 alle 12:57 (13 anni fa) da Marco Danelutto