Advertisement
Maverick82

TestingPTX.cu

May 16th, 2013
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.41 KB | None | 0 0
  1. #include "stdio.h"
  2. #include <cuda.h>
  3. #include <cuda_runtime.h>
  4.  
  5. #if defined(__CUDA_ARCH__) && (__CUDA_ARCH__ < 200)
  6.     #define printf(f, ...) ((void)(f, __VA_ARGS__),0)
  7. #endif
  8.  
  9.  
  10. #define NUM_BLOCKS 16
  11. #define BLOCK_WIDTH 1
  12.  
  13. __global__ void hello(float * d_out, float * d_in)
  14. {  
  15.     int idx=blockIdx.x;        
  16.     d_out[idx]=d_in[idx];
  17.     //printf("Hello world! I'm a thread in block %d\n", blockIdx.x);
  18.     //printf("The number d_out is %f\n", d_in[idx]);
  19.  
  20. }
  21.  
  22.  
  23.  
  24. int main(int argc,char **argv)
  25. {   const int ARRAY_SIZE = NUM_BLOCKS;
  26.     const int ARRAY_BYTES = ARRAY_SIZE * sizeof(float);
  27.    
  28.     float * d_in;
  29.     float * d_out;
  30.     float h_in[NUM_BLOCKS];
  31.     float h_out[NUM_BLOCKS];
  32.     for (int i = 0; i < NUM_BLOCKS; i++) {
  33.         h_in[i] = float(i);
  34.         //printf("The number h_in is %f\n", h_in[i]);
  35.     }
  36.  
  37.     //allocate memory
  38.     cudaMalloc((void**) &d_in, ARRAY_BYTES);
  39.     cudaMalloc((void**) &d_out, ARRAY_BYTES);
  40.     //Transfer array to GPU
  41.     cudaMemcpy(d_in,h_in,ARRAY_BYTES, cudaMemcpyHostToDevice);
  42.     // launch the kernel
  43.     hello<<<NUM_BLOCKS, BLOCK_WIDTH>>>(d_out,d_in);
  44.     //Copy back
  45.     cudaMemcpy(h_out,d_out, ARRAY_BYTES, cudaMemcpyDeviceToHost);
  46.  
  47.     // force the printf()s to flush
  48.     cudaDeviceSynchronize();
  49.  
  50.     //printf("That's all!\n");
  51.  
  52.         // print out the resulting array
  53.     for (int i =0; i < NUM_BLOCKS; i++) {
  54.         //printf("%f\n", h_out[i]);
  55.     }
  56.  
  57.     //cudaFree(d_in);
  58.     //cudaFree(d_out);
  59.  
  60.     return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement