Advertisement
Guest User

vector_projection.cu

a guest
Sep 27th, 2014
544
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.76 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. #include "helper_cuda.h"
  4. #include <cuda_runtime.h>
  5.  
  6. template<typename T> __global__ void vector_projection(T *dx, int n) {
  7.     int tid;
  8.     tid = threadIdx.x + blockIdx.x * blockDim.x;
  9.     if (tid < n) {
  10.         if (dx[tid] < 0)
  11.             dx[tid] = (T) 0;
  12.     }
  13. }
  14.  
  15. double X[10] = {1,-2,3,-4,5,-6,7,-8,9,-10};
  16.  
  17.  
  18. int main(void) {
  19.     int i;
  20.  
  21.     double *dev_X;
  22.     checkCudaErrors(cudaMalloc(&dev_X, 10*sizeof(double)));
  23.     checkCudaErrors(cudaMemcpy(dev_X, X, 10*sizeof(double), cudaMemcpyHostToDevice));
  24.  
  25.     vector_projection<double><<<1,10>>>(dev_X, 10);
  26.     getLastCudaError("vector_projection");
  27.  
  28.     checkCudaErrors(cudaMemcpy(X, dev_X, 10*sizeof(double), cudaMemcpyDeviceToHost));
  29.     for (i=0; i<10; i++)
  30.     {
  31.         printf("proj(X) = %g\n", i, X[i]);
  32.     }
  33.  
  34.     return 0;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement