Advertisement
Wow_Rasl

Untitled

May 25th, 2022
447
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.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <cstdlib>
  7. #include <time.h>
  8. #include <curand_kernel.h>
  9. #define pnt 100
  10. __global__ void mykernel(void) {
  11. }
  12. __global__ void addKernel(int* c, const int* a, const int* b) {
  13.     int i = threadIdx.x;
  14.     c[i] = a[i] + b[i];
  15. }
  16. __device__ float f(float x) {
  17.     return x;
  18. }
  19. __global__ void parallel_monte_carlo_integrate(curandStatePhilox4_32_10_t* state, int R, float* result) {
  20.     //srand(time(NULL));
  21.     //float* dev_state;
  22.     //cudaMalloc((void**)&dev_state, sizeof(float));
  23.     //curandStatePhilox4_32_10_t* dev_state;
  24.     float w = 1 / (1.0 * R);
  25.     int success = 0;
  26.     int cp = 0;
  27.     int i = threadIdx.x;
  28.     for (int j = 0; j < pnt; j++) {
  29.         float x = 1.0 * state;
  30.         x = 1.0 * state / RAND_MAX;
  31.         x = 1.0 * state / RAND_MAX + i * w;
  32.         x = 1.0 * state / RAND_MAX / R + i * w;
  33.         float y = 1.0 * state / RAND_MAX;
  34.         success = success + int(f(x) < y);
  35.  
  36.         //printf("x=%lf\n", x);
  37.         cp++;
  38.     }
  39.     *result = success * 1.0 / cp;
  40.     printf("%lf\n", *result);
  41. }
  42. int main(void) {
  43.     //printf("%d", RAND_MAX);
  44.     float result;
  45.     int R = 2;
  46.     float* dev_result;
  47.     cudaMalloc((void**)&dev_result, sizeof(float));
  48.     //cudaMemcpy(&result, &result_gpu, size_t count, cudaMemcpyHostToDevice = 1);
  49.     parallel_monte_carlo_integrate << <1, R >> > (curandStatePhilox4_32_10_t* state, R, dev_result);
  50.     cudaMemcpy(&result, dev_result, sizeof(float), cudaMemcpyDeviceToHost);
  51.     /*for (int R = 1; R < 100; R++) {
  52.         parallel_monte_carlo_integrate(R, &result);
  53.         printf("%x %lf\n", &result, result);
  54.     }*/
  55.     printf("%lf", result);
  56.     return 0;
  57. }
Advertisement
RAW Paste Data Copied
Advertisement