-
Notifications
You must be signed in to change notification settings - Fork 25
/
main.cu
122 lines (105 loc) · 3.13 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include "cuNSearch.h"
#include "Timing.h"
#include <iostream>
#include <vector>
#include <array>
#include <cmath>
#include <limits>
#include <random>
using namespace cuNSearch;
using Real3 = std::array<Real, 3>;
std::vector<Real3> positions;
inline Real3 operator-(const Real3 & left, const Real3 & right)
{
return Real3{ left[0] - right[0], left[1] - right[1], left[2] - right[2] };
}
std::size_t const N = 120;
Real const r_omega = static_cast<Real>(0.15);
Real const r_omega2 = r_omega * r_omega;
Real const radius = static_cast<Real>(2.0) * (static_cast<Real>(2.0) * r_omega / static_cast<Real>(N - 1));
void testCuNSearch()
{
//Generate test data
Real min_x = std::numeric_limits<Real>::max();
Real max_x = std::numeric_limits<Real>::min();
positions.reserve(N * N * N);
for (unsigned int i = 0; i < N; ++i)
{
for (unsigned int j = 0; j < N; ++j)
{
for (unsigned int k = 0; k < N; ++k)
{
std::array<Real, 3> x = { {
r_omega * static_cast<Real>(2.0 * static_cast<double>(i) / static_cast<double>(N - 1) - 1.0),
r_omega * static_cast<Real>(2.0 * static_cast<double>(j) / static_cast<double>(N - 1) - 1.0),
r_omega * static_cast<Real>(2.0 * static_cast<double>(k) / static_cast<double>(N - 1) - 1.0) } };
Real l2 = x[0] * x[0] + x[1] * x[1] + x[2] * x[2];
if (l2 < r_omega2)
{
x[0] += static_cast<Real>(0.35);
x[1] += static_cast<Real>(0.35);
x[2] += static_cast<Real>(0.35);
positions.push_back(x);
if (min_x > x[0])
{
min_x = x[0];
}
if (max_x < x[0])
{
max_x = x[0];
}
}
}
}
}
std::random_shuffle(positions.begin(), positions.end());
printf("Number of particles: %d \n", static_cast<int>(positions.size()));
//Create neighborhood search instance
NeighborhoodSearch nsearch(radius);
//Add point set from the test data
auto pointSetIndex = nsearch.add_point_set(positions.front().data(), positions.size(), true, true);
for (size_t i = 0; i < 5; i++)
{
if (i != 0)
{
nsearch.z_sort();
nsearch.point_set(pointSetIndex).sort_field((Real3*)nsearch.point_set(pointSetIndex).GetPoints());
}
Timing::reset();
nsearch.find_neighbors();
Timing::printAverageTimes();
}
//Neighborhood search result test
auto &pointSet = nsearch.point_set(0);
auto points = pointSet.GetPoints();
std::cout << "Validate results" << std::endl;
for (unsigned int i = 0; i < pointSet.n_points(); i++)
{
Real3 point = ((Real3*)points)[i];
auto count = pointSet.n_neighbors(0, i);
for (unsigned int j = 0; j < count; j++)
{
auto neighbor = pointSet.neighbor(0, i, j);
auto diff = point - ((Real3*)points)[neighbor];
float squaredLength = diff[0] * diff[0] + diff[1] * diff[1] + diff[2] * diff[2];
float distance = sqrt(squaredLength);
if (distance > radius)
{
throw std::runtime_error("Not a neighbor");
}
}
}
}
int main(int argc, char* argv[])
{
#ifdef DEBUG
std::cout << "Debug Build:" << std::endl;
if(sizeof(Real) == 4)
std::cout << "Real = float" << std::endl;
else if (sizeof(Real) == 8)
std::cout << "Real = double" << std::endl;
#endif
testCuNSearch();
std::cout << "Finished Testing" << std::endl;
getchar();
}