Advertisement
Wow_Rasl

Untitled

May 26th, 2022
710
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 <stdio.h>
  4. #include <stdlib.h>
  5. #include <cstdlib>
  6. #include <time.h>
  7. #include <curand_kernel.h>
  8. #define pnt 100
  9. #define SEED 9876543
  10. #define THREADS 80
  11. #define BLOCKS 10
  12. __global__ void mykernel(void) {
  13. }
  14. __global__ void addKernel(int* c, const int* a, const int* b) {
  15.     int i = threadIdx.x;
  16.     c[i] = a[i] + b[i];
  17. }
  18. __device__ float f(float x) {
  19.     return x;
  20. }
  21. // инициализация генератора
  22. __global__ void initfGENS(curandStatePhilox4_32_10_t* d_gen) {
  23.     for (int id = threadIdx.x + blockIdx.x * blockDim.x; id < THREADS * BLOCKS; id += THREADS * BLOCKS) {
  24.         curand_init(SEED, id, 0, d_gen + id);
  25.     }
  26.  
  27. }
  28. __global__ void parallel_monte_carlo_integrate(curandStatePhilox4_32_10_t* d_gen, int R, float* result) {
  29.     float w = 1 / (1.0 * R);
  30.     int success = 0;
  31.     int cp = 0;
  32.     int id = threadIdx.x + blockIdx.x * blockDim.x;
  33.     for (int j = 0; j < pnt; j++) {
  34.         float x = 1.0 * curand_uniform(&d_gen[id]) / R + w * id;
  35.         float y = 1.0 * curand_uniform(&d_gen[id]);
  36.         success = success + int(f(x) < y);
  37.         cp++;
  38.     }
  39.     *result = success * 1.0 / cp;
  40.     printf("%lf\n", *result);
  41. }
  42. int main() {
  43.     curandStatePhilox4_32_10_t* d_gen;
  44.     cudaMalloc((void**)&d_gen, THREADS * BLOCKS * sizeof(curandStatePhilox4_32_10_t));
  45.     initfGENS << < 1, 10 >> > (d_gen);
  46.     float result;
  47.     int R = 10;
  48.     float* dev_result;
  49.     cudaMalloc((void**)&dev_result, sizeof(float));
  50.     parallel_monte_carlo_integrate << <THREADS, BLOCKS >> > (d_gen, R, dev_result);
  51.     cudaMemcpy(&result, dev_result, sizeof(float), cudaMemcpyDeviceToHost);
  52.     printf("%lf", result);
  53.     //освобождение памяти
  54.     cudaFree(d_gen);
  55.     cudaFree(dev_result);
  56.     return 0;
  57. }
  58.  
Advertisement
RAW Paste Data Copied
Advertisement