ret_0

Lab3. 2

Mar 6th, 2021
645
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "cuda_runtime.h"
  2. #include "device_launch_parameters.h"
  3. #include <cuda.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <ctime>
  7. #include <clocale>
  8. #include <cmath>
  9.  
  10. #define NUM_BLOCK 256
  11. #define NUM_THREAD 256
  12.  
  13. const double PI = 3.14159265358979323846;
  14.  
  15. __global__ void calc(double *res, double step, int blocks, int threads, int size)
  16. {
  17.     for(int idx = blockIdx.x * blockDim.x + threadIdx.x;
  18.         idx <= size;
  19.         idx += blocks * threads)
  20.     {
  21.         double x = step * idx;
  22.         res[idx] += sqrt(1 - x * x);
  23.     }
  24. }
  25.  
  26. int main()
  27. {
  28.     const int N = NUM_BLOCK * NUM_THREAD;
  29.     double pi = 0;
  30.     double *res, *dev_res;
  31.     double step = 1.0 / N;
  32.     size_t size = N * sizeof(double);
  33.     res = (double*)malloc(size);
  34.     cudaMalloc((void**)&dev_res, size);
  35.  
  36.     calc <<<NUM_BLOCK, NUM_THREAD>>> (dev_res, step, NUM_BLOCK, NUM_THREAD, N);
  37.     cudaMemcpy(res, dev_res, size, cudaMemcpyDeviceToHost);
  38.  
  39.     for (int i = 0; i < N; i++)
  40.         pi += res[i];
  41.     pi *= 4 * step;
  42.     pi /= N;
  43.  
  44.     printf("%d blocks, %d threads\n", NUM_BLOCK, NUM_THREAD);
  45.     printf("Defined PI    = %.20f\n", PI);
  46.     printf("Calculated PI = %.20f\n", pi);
  47.     printf("Error         = %.20f\n", abs(pi - PI));
  48.  
  49.     cudaFree(dev_res);
  50.     return 0;
  51. }
  52.  
  53.  
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×