Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include "helper_cuda.h"
- #include <cuda_runtime.h>
- template<typename T> __global__ void vector_projection(T *dx, int n) {
- int tid;
- tid = threadIdx.x + blockIdx.x * blockDim.x;
- if (tid < n) {
- if (dx[tid] < 0)
- dx[tid] = (T) 0;
- }
- }
- double X[10] = {1,-2,3,-4,5,-6,7,-8,9,-10};
- int main(void) {
- int i;
- double *dev_X;
- checkCudaErrors(cudaMalloc(&dev_X, 10*sizeof(double)));
- checkCudaErrors(cudaMemcpy(dev_X, X, 10*sizeof(double), cudaMemcpyHostToDevice));
- vector_projection<double><<<1,10>>>(dev_X, 10);
- getLastCudaError("vector_projection");
- checkCudaErrors(cudaMemcpy(X, dev_X, 10*sizeof(double), cudaMemcpyDeviceToHost));
- for (i=0; i<10; i++)
- {
- printf("proj(X) = %g\n", i, X[i]);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement