sja91

Untitled

Jan 16th, 2013
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.98 KB | None | 0 0
  1. // MP 5 Scan
  2. // Given a list (lst) of length n
  3. // Output its prefix sum = {lst[0], lst[0] + lst[1], lst[0] + lst[1] + ... + lst[n-1]}
  4. // Due Tuesday, January 22, 2013 at 11:59 p.m. PST
  5.  
  6. #include    <wb.h>
  7.  
  8. #define BLOCK_SIZE 512 //@@ You can change this
  9.  
  10. #define wbCheck(stmt) do {                                 \
  11.         cudaError_t err = stmt;                            \
  12.         if (err != cudaSuccess) {                          \
  13.             wbLog(ERROR, "Failed to run stmt ", #stmt);    \
  14.             return -1;                                     \
  15.         }                                                  \
  16.     } while(0)
  17.  
  18. __global__ void scan(float * input, float * output, int len) {
  19.     //@@ Modify the body of this function to complete the functionality of
  20.     //@@ the scan on the device
  21.     //@@ You may need multiple kernel calls; write your kernels before this
  22.     //@@ function and call them from here
  23. }
  24.  
  25. int main(int argc, char ** argv) {
  26.     wbArg_t args;
  27.     float * hostInput; // The input 1D list
  28.     float * hostOutput; // The output list
  29.     float * deviceInput;
  30.     float * deviceOutput;
  31.     int numElements; // number of elements in the list
  32.  
  33.     args = wbArg_read(argc, argv);
  34.  
  35.     wbTime_start(Generic, "Importing data and creating memory on host");
  36.     hostInput = (float *) wbImport(wbArg_getInputFile(args, 0), &numElements);
  37.     hostOutput = (float*) malloc(numElements * sizeof(float));
  38.     wbTime_stop(Generic, "Importing data and creating memory on host");
  39.  
  40.     wbLog(TRACE, "The number of input elements in the input is ", numElements);
  41.  
  42.     wbTime_start(GPU, "Allocating GPU memory.");
  43.     wbCheck(cudaMalloc((void**)&deviceInput, numElements*sizeof(float)));
  44.     wbCheck(cudaMalloc((void**)&deviceOutput, numElements*sizeof(float)));
  45.     wbTime_stop(GPU, "Allocating GPU memory.");
  46.  
  47.     wbTime_start(GPU, "Clearing output memory.");
  48.     wbCheck(cudaMemset(deviceOutput, 0, numElements*sizeof(float)));
  49.     wbTime_stop(GPU, "Clearing output memory.");
  50.  
  51.     wbTime_start(GPU, "Copying input memory to the GPU.");
  52.     wbCheck(cudaMemcpy(deviceInput, hostInput, numElements*sizeof(float), cudaMemcpyHostToDevice));
  53.     wbTime_stop(GPU, "Copying input memory to the GPU.");
  54.  
  55.     //@@ Initialize the grid and block dimensions here
  56.  
  57.     wbTime_start(Compute, "Performing CUDA computation");
  58.     //@@ Modify this to complete the functionality of the scan
  59.     //@@ on the deivce
  60.  
  61.     cudaDeviceSynchronize();
  62.     wbTime_stop(Compute, "Performing CUDA computation");
  63.  
  64.     wbTime_start(Copy, "Copying output memory to the CPU");
  65.     wbCheck(cudaMemcpy(hostOutput, deviceOutput, numElements*sizeof(float), cudaMemcpyDeviceToHost));
  66.     wbTime_stop(Copy, "Copying output memory to the CPU");
  67.  
  68.     wbTime_start(GPU, "Freeing GPU Memory");
  69.     cudaFree(deviceInput);
  70.     cudaFree(deviceOutput);
  71.     wbTime_stop(GPU, "Freeing GPU Memory");
  72.  
  73.     wbSolution(args, hostOutput, numElements);
  74.  
  75.     free(hostInput);
  76.     free(hostOutput);
  77.  
  78.     return 0;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment