Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cuda.h>
- #include <curand_kernel.h>
- #include <stdio.h>
- #include <time.h>
- #include<stdio.h>
- #include<math.h>
- #include<time.h>
- #include<stdlib.h>
- #define N 1000000
- #define n 3
- __global__ void setup_kernel(curandState * state, unsigned long seed)
- {
- int id = threadIdx.x;
- curand_init(seed, id, 0, &state[id]);
- }
- __global__ void generate(curandState* globalState, float* randomArray)
- {
- int ind = threadIdx.x;
- curandState localState = globalState[ind];
- float RANDOM = curand_uniform(&localState);
- randomArray[ind] = RANDOM;
- globalState[ind] = localState;
- }
- double f(double x[]) {
- return x[0]*x[1]*x[2];
- }
- float* r()
- {
- dim3 tpb(N, 1, 1);
- curandState* devStates;
- float* randomValues = new float[N];
- float* devRandomValues;
- cudaMalloc(&devStates, N * sizeof(curandState));
- cudaMalloc(&devRandomValues, N * sizeof(*randomValues));// setup seeds
- setup_kernel <<< 1, tpb >>> (devStates, time(NULL));
- //printf("%s\n", cudaGetErrorString(cudaGetLastError()));// generate random numbers
- generate <<< 1, tpb >>> (devStates, devRandomValues);
- //printf("%s\n", cudaGetErrorString(cudaGetLastError()));
- cudaMemcpy(randomValues, devRandomValues, N * sizeof(*randomValues), cudaMemcpyDeviceToHost);
- //cudaFree(devRandomValues);
- //cudaFree(devStates);
- return randomValues;
- }
- void integral(int number_of_points, double m[]) {
- srand(time(NULL));
- int in = 0;
- double k[n + 1];
- float* rr = r();
- for (int i = 0; i < number_of_points; i++)
- {
- printf("%d %lf \n", i, rr[i]);
- }
- for (int i = 0; i < number_of_points; i++)
- {
- for (int j = 0; j < n + 1; j++)
- {
- //double rr = (double)rand()/RAND_MAX;
- //double rr = r[i];
- k[j] = (m[2 * j + 1] - m[2 * j])*rr[i] + m[2 * j];
- printf("%lf \n", rr[i]);
- }
- if (f(k) < k[n] && k[n] < 0)
- {
- printf("%d kek", in);
- in--;
- }
- if (f(k) > k[n] && k[n] > 0)
- {
- printf("%d lol", in);
- in++;
- }
- }
- double v = 1;
- for (int i = 0; i < n + 1; i++)
- {
- v = v * (m[2 * i + 1] - m[2 * i]);
- }
- double real = 9176.7;
- double result = v * in / (double) number_of_points;
- printf(" result= %lf, error %lf\n ", result, fabs(result - real));
- }
- int main(int argc, char** argv)
- {
- //int n;
- //printf("enter n: \n");
- //scanf("%d", &n);
- double m[2 * (n + 1)];
- for (int i = 0; i < n; i++)
- {
- printf("enter min x[%d]: \n", i);
- scanf("%lf", &m[2 * i]);
- printf("enter max x[%d]: \n", i);
- scanf("%lf", &m[2 * i + 1]);
- }
- for (int i = 1; i < 2; i++)
- {
- printf("enter min y: \n");
- scanf("%lf", &m[2 * n]);
- printf("enter max y: \n");
- scanf("%lf", &m[2 * n + i]);
- }
- clock_t begin = clock();
- for (int number_of_points = 100; number_of_points < N; number_of_points *= 10) {
- printf("For %d points\n", number_of_points);
- integral(number_of_points, m);
- }
- clock_t end = clock();
- double t = (double)(end - begin) / CLOCKS_PER_SEC;
- printf("%lf", t);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement