-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cu
86 lines (63 loc) · 2.39 KB
/
main.cu
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <iostream>
#include <vector>
#include <random>
#include <thrust/sort.h>
#include <thrust/host_vector.h>
#include <thrust/device_vector.h>
#include <chrono>
#include "sorter.cuh"
#include "integrator.cuh"
const int POINT_RANGE = 100000;
const int NUM_POINTS = 1 << 10; //the program works for NUM_POINTS being a power of two
thrust::host_vector<float> pointgenerator(int num_points) {
// setup random generator
std::random_device rd;
std::mt19937 eng(rd());
std::uniform_real_distribution<> dist(0, 1);
// pushback random points to vector
thrust::host_vector<float> vec;
for (int i = 0; i < num_points; i++) {
float x = dist(eng) * POINT_RANGE + dist(eng);
vec.push_back(x);
}
return vec;
}
int main() {
std::cout.precision(15);
//generate random points
thrust::host_vector<float> vecx = pointgenerator(NUM_POINTS);
thrust::host_vector<float> vecy = pointgenerator(NUM_POINTS);
Sorter sorter;
Integrator integrator;
//variables for the result of integration
float integralGPU;
float integralCPU;
//allocate GPU memory
thrust::device_vector<float> dvecx = vecx;
thrust::device_vector<float> dvecy = vecy;
//GPU sorting
sorter.GPUsort(dvecx, dvecy);
cudaEvent_t start, stop;
float time;
cudaEventCreate(&start);
cudaEventCreate(&stop);
cudaEventRecord(start, 0);
//GPU integration
integralGPU = integrator.GPUintegrator(dvecx, dvecy);
cudaEventRecord(stop, 0);
cudaEventSynchronize(stop);
cudaEventElapsedTime(&time, start, stop);
printf ("GPU integration time: %f ms\n", time);
// cudaDeviceSynchronize();
std::cout << "integralGPU = " << integralGPU << std::endl;
//CPU sorting
sorter.CPUsort(vecx, vecy);
auto start1 = std::chrono::high_resolution_clock::now();
//CPU integration
integralCPU = integrator.CPUintegrator(vecx, vecy);
auto stop1 = std::chrono::high_resolution_clock::now();
auto durationmili1 = std::chrono::duration_cast<std::chrono::milliseconds>(stop1 - start1);
auto durationmicro1 = std::chrono::duration_cast<std::chrono::microseconds>(stop1 - start1);
std::cout << "CPU integration time: " << durationmili1.count() << "." << durationmicro1.count() - 1000*durationmili1.count() << " ms" << std::endl;
std::cout << "integralCPU = " << integralCPU << std::endl;
}