Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 1st, 2012  |  syntax: C  |  size: 8.92 KB  |  hits: 20  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /*******************************************************************************
  2.  ***  NAME         :  Tyler McEntee                                          ***
  3.  ***  CLASS        :  CSC 346                                                ***
  4.  ***  ASSIGNMENT   :  Assignment 8                                           ***
  5.  ***  DUE DATE     :  5/2/12                                                ***
  6.  ***  INSTRUCTOR   :  GAMRADT                                                ***
  7.  *******************************************************************************
  8.  ***  DESCRIPTION  :
  9.  *** nvcc –Xcompiler –fopenmp –lgomp –arch sm_13 mcenteet8.cu ***
  10.  ******************************************************************************/
  11.  
  12. #include <stdio.h>     
  13. #include <stdlib.h>
  14. #include <omp.h>
  15.  
  16. void intro(void);
  17. void fillZeroes(double *, int);
  18. void getParamaters(char *[], int *, int *, long int *);
  19. double reduceArray(double *, int);
  20. void displayResults(int, long int, double, double);
  21.  
  22. __device__ void f(double, double *);
  23. __global__ void calcTrapezoid(double *, long int, double, int);
  24.  
  25. int main(int argc, char *argv[]) {
  26.        
  27.         int blocks, threadsPerBlock, totalThreads, arraySize;
  28.         long int totalParts, partsPerThread;
  29.         double start, end;
  30.         double piEst, width, *hostPi, *devicePi, a = 0.0, b = 1.0;
  31.  
  32.         intro();
  33.  
  34.         /*getParamaters(argv, &blocks, &threadsPerBlock, &totalParts);*/
  35.  
  36.         blocks = 16;
  37.         threadsPerBlock = 512;
  38.         totalParts = 5242880000;
  39.        
  40.         start = omp_get_wtime();
  41.        
  42.  
  43.         totalThreads = blocks * threadsPerBlock;
  44.         partsPerThread = totalParts / totalThreads;
  45.         width = (b - a) / totalParts;
  46.  
  47.         arraySize = totalThreads * sizeof(double);
  48.         printf("width = %.16f\n", width);
  49.         printf("totalParts = %ld\n", totalParts);
  50.         printf("arraySize = %d", arraySize);
  51.  
  52.  
  53.         hostPi = (double *)malloc(arraySize);
  54.  
  55.         if (hostPi == NULL) {
  56.                 printf("malloc failed\n");
  57.         }
  58.  
  59.         else {
  60.                
  61.                 if (cudaMalloc((void**)&devicePi, arraySize) != cudaSuccess) {
  62.                         printf("cudaMalloc failed\n");
  63.                 }
  64.  
  65.                 else {
  66.  
  67.                         printf("2.\n");
  68.  
  69.                         fillZeroes(hostPi, threadsPerBlock);
  70.                
  71.                         cudaMemcpy(devicePi, hostPi, arraySize, cudaMemcpyHostToDevice);
  72.  
  73.                         calcTrapezoid<<<blocks, threadsPerBlock>>>(devicePi, totalParts, width, totalThreads);
  74.                
  75.                         cudaMemcpy(hostPi, devicePi, arraySize, cudaMemcpyDeviceToHost);
  76.                        
  77.                         printf("3.\n");
  78.                        
  79.                         piEst = reduceArray(hostPi, arraySize);
  80.  
  81.                         printf("PiEst = %f\n", piEst);
  82.                
  83.                         end = omp_get_wtime();
  84.                
  85.                         displayResults(totalThreads, totalParts, piEst, (end - start));
  86.  
  87.                         cudaFree(devicePi);
  88.                 }
  89.  
  90.                 free(hostPi);
  91.         }
  92.        
  93.         return 0;
  94. }
  95.  
  96. /*******************************************************************************
  97.  ***  FUNCTION Intro                                                         ***
  98.  *******************************************************************************
  99.  ***  DESCRIPTION  :  Introduction to the program.                           ***
  100.  ***  INPUT ARGS   :  none                                                   ***
  101.  ***  OUTPUT ARGS  :  none                                                   ***
  102.  ***  IN/OUT ARGS  :  none                                                   ***
  103.  ***  RETURN       :  none                                                   ***
  104.  ******************************************************************************/
  105. void intro( void ) {
  106.    
  107.     printf("Change\n");
  108.     printf("this\n");
  109.     printf("later\n");
  110.     printf("do it.\n");
  111.     printf("\n");
  112.     printf("\n\n");
  113.  
  114.  
  115.     return;
  116.  }
  117.  
  118. /*******************************************************************************
  119.  ***  FUNCTION getParameters                                                 ***
  120.  *******************************************************************************
  121.  ***  DESCRIPTION  :  Gets the filename from the user or commandline arg     ***
  122.  ***  INPUT ARGS   :  char **argv, int *parameters                           ***
  123.  ***  OUTPUT ARGS  :  none                                                   ***
  124.  ***  IN/OUT ARGS  :  none                                                   ***
  125.  ***  RETURN       :  none                                                   ***
  126.  ******************************************************************************/
  127. void getParamaters(char **argv, int *blocks, int *threadsPerBlock, long int *partitions) {
  128.  
  129.       *blocks = atoi(argv[1]);
  130.       *threadsPerBlock = atoi(argv[2]);
  131.       *partitions = atol(argv[3]);
  132.  
  133.     return;
  134.  
  135. }
  136.  
  137.  
  138. /*******************************************************************************
  139.  ***  FUNCTION fillZereos                                                        ***
  140.  *******************************************************************************
  141.  ***  DESCRIPTION  :  Gets the filename from the user or commandline arg     ***
  142.  ***  INPUT ARGS   :  char **argv, int *parameters                           ***
  143.  ***  OUTPUT ARGS  :  none                                                   ***
  144.  ***  IN/OUT ARGS  :  none                                                   ***
  145.  ***  RETURN       :  none                                                   ***
  146.  ******************************************************************************/
  147. void fillZeroes(double *array, int size) {
  148.         int i;
  149.        
  150.         #pragma omp parallel for default(none) private(i) shared(size, array)
  151.         for (i = 0; i < size; i++) {
  152.                 array[i] = 0;
  153.         }
  154.        
  155.         return;
  156. }
  157.  
  158.  
  159.  
  160. /*******************************************************************************
  161.  ***  FUNCTION f                                                             ***
  162.  *******************************************************************************
  163.  ***  DESCRIPTION  :  calcualtes the area of a given trapezoid               ***
  164.  ***  INPUT ARGS   :  double x                                               ***
  165.  ***  OUTPUT ARGS  :  none                                                   ***
  166.  ***  IN/OUT ARGS  :  none                                                   ***
  167.  ***  RETURN       :  4.0 / (1 + x * x)                                      ***
  168.  ******************************************************************************/
  169. __device__ void f(double x, double *xCalc) {
  170.         *xCalc=  4.0 / (1.0 + x * x);
  171. }
  172.  
  173. /****************************************************************************
  174. *****************************************************************************
  175.    Function:  calcTrapezoid
  176. *****************************************************************************
  177. Description:  Outputs an array containing a complete list of partial sums
  178.               for pi.
  179.  Input args:  totalParts, width, totalThreads
  180. Output args:  sum
  181. In/Out args:  none
  182.      Return:  void
  183. *****************************************************************************
  184. ****************************************************************************/
  185. __global__ void calcTrapezoid(double *sum, long int totalParts, double width, int totalThreads) {
  186.        
  187.         double approx;
  188.         int i, index = blockIdx.x * blockDim.x + threadIdx.x;
  189.        
  190.         for (i = index; i < totalParts; i += totalThreads) {
  191.  
  192.                 f(i * width, &approx);
  193.                
  194.                 sum[i] += approx;
  195.         }
  196. }
  197.  
  198. /****************************************************************************
  199. *****************************************************************************
  200.    Function:  reduceArray
  201. *****************************************************************************
  202. Description:  Returns the sum of all the elements of a specified array using
  203.               an OpenMP parallel for reduction statement.
  204.  Input args:  array, size
  205. Output args:  none
  206. In/Out args:  none
  207.      Return:  sum
  208. *****************************************************************************
  209. ****************************************************************************/
  210. double reduceArray(double array[], int size) {
  211.         int i;
  212.         double sum = 0.0;
  213.        
  214.         #pragma omp parallel for default(none) reduction(+: sum) private(i) shared(size, array)
  215.         for (i = 0; i < size; i++)
  216.                 sum += array[i];
  217.  
  218.         return sum;
  219. }
  220.  
  221. /****************************************************************************
  222. *****************************************************************************
  223.    Function:  displayResults
  224. *****************************************************************************
  225. Description:  Displays the thread count, paritition count, approximation for
  226.               pi, and the overall time taken to perform the calculation.
  227.  Input args:  threads, partitions, result, time
  228. Output args:  none
  229. In/Out args:  none
  230.      Return:  void
  231. *****************************************************************************
  232. ****************************************************************************/
  233. void displayResults(int threads, long int partitions, double result, double time) {
  234.         printf("\nThread count:                  %-10d\n", threads);
  235.         printf("Partition count:                 %-10ld\n", partitions);
  236.         printf("Approximate value of integral:   %.17f\n", result);
  237.         printf("Overall time:                    %.17f\n\n", time);
  238.  
  239.         return;
  240. }