]> AND Private Git Repository - book_gpu.git/blob - BookGPU/Chapters/chapter5/code/ex1.cu
Logo AND Algorithmique Numérique Distribuée

Private GIT Repository
fin correct ch14
[book_gpu.git] / BookGPU / Chapters / chapter5 / code / ex1.cu
1 #include <gpulab/vector.h>
2
3 __global__ void add(double* a, double const* b, int N)
4 {
5         int i = blockDim.x*blockIdx.x + threadIdx.x;
6         if(i<N)
7                 a[i] += b[i];
8 }
9
10 int main(int argc, char *argv[])
11 {
12         int N = 1000;
13
14         // Basic CUDA example
15         double *a1, *b1;
16         cudaMalloc((void**)&a1, N*sizeof(double));
17         cudaMalloc((void**)&b1, N*sizeof(double));
18         cudaMemset(a1, 2.0, N);
19         cudaMemset(b1, 3.0, N);
20         int blocksize = 128;
21         add<<<(N+blocksize-1)/blocksize,blocksize>>>(a1, b1, N);
22
23         // gpulab example
24         gpulab::vector<double,gpulab::device_memory> a2(N, 2.0);
25         gpulab::vector<double,gpulab::device_memory> b2(N, 3.0);
26         a2.axpy(1.0, b2); // BLAS1: a2 = 1*b2 + a2
27         
28         return 0;
29 }