-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.cpp
147 lines (119 loc) · 4.06 KB
/
main.cpp
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <chrono>
#include <cmath>
#include <fstream>
#include <future>
#include <iostream>
#include <set>
#include <string>
#include <thread>
#include <vector>
class Point {
public:
Point(int id, double x, double y, double z) : id(id), x(x), y(y), z(z){};
Point() { Point(-1, 0, 0, 0); };
double euclideanDistance(const Point& point) const {
double x = this->x - point.x;
double y = this->y - point.y;
double z = this->z - point.z;
return sqrt(x * x + y * y + z * z);
}
friend std::ostream& operator<<(std::ostream& out, const Point& point);
friend std::istream& operator>>(std::istream& in, Point& point);
int id;
double x, y, z;
};
std::istream& operator>>(std::istream& is, Point& point) {
is >> point.x >> point.y >> point.z;
return is;
}
std::ostream& operator<<(std::ostream& out, const Point& point) {
out << point.x << " " << point.y << " " << point.z;
return out;
}
using ResultGrid = std::vector<std::vector<int>>;
int main(int argc, char* argv[]) {
if (argc < 3) {
std::cout
<< "Wrong input. Use ./main <points_file.txt> <result_file.txt>"
<< std::endl;
return 0;
}
std::ifstream inFile;
inFile.open(argv[1]);
std::ofstream outFile;
outFile.open(argv[2]);
if (!inFile.is_open() || !outFile.is_open()) {
std::cout << "Wrong files." << std::endl;
return 0;
}
int neighbors, threads, points;
std::cout << "Enter threads: ";
std::cin >> threads;
std::cout << "Enter neighbors: ";
std::cin >> neighbors;
inFile >> points;
std::cout << "Start processing..." << std::endl;
auto startTime = std::chrono::system_clock::now();
int perThread = points / threads;
int lastThread = perThread + points % threads;
std::vector<int> threadRows(threads, perThread);
threadRows.back() = lastThread;
std::shared_ptr<std::vector<Point>> ptrPoints(
new std::vector<Point>(points));
std::shared_ptr<ResultGrid> ptrResultGrid(new ResultGrid(points));
int pointIndex = 1;
for (auto& point : *ptrPoints) {
inFile >> point;
point.id = pointIndex++;
}
auto calcKNearestNeighbors = [ptrPoints, ptrResultGrid, &neighbors](
int indexFrom, int indexTo) mutable {
std::set<std::pair<double, int>> minSet;
double distance = 0;
int pointIndex = -1;
int curNeighbor = 0;
for (int point = indexFrom; point < indexTo; ++point) {
minSet.clear();
pointIndex = -1;
for (auto& pointTo : *ptrPoints) {
++pointIndex;
if (point == pointIndex) continue;
distance = (*ptrPoints)[point].euclideanDistance(pointTo);
minSet.insert({distance, pointTo.id});
}
curNeighbor = 0;
for (auto setIter = minSet.begin();
setIter != minSet.end() && curNeighbor < neighbors;
++setIter, ++curNeighbor) {
(*ptrResultGrid)[point].push_back(setIter->second);
}
}
};
std::vector<std::future<void>> futuresResult;
int startIndex = 0;
for (auto& threadRow : threadRows) {
futuresResult.push_back(std::async(std::launch::async,
calcKNearestNeighbors, startIndex,
startIndex + threadRow));
startIndex += threadRow;
}
for (auto& future : futuresResult) {
future.get();
}
for (auto& neighborsList : *ptrResultGrid) {
for (auto& neighbors : neighborsList) {
outFile << neighbors << " ";
}
outFile << std::endl;
}
inFile.close();
outFile.close();
auto delta = std::chrono::system_clock::now() - startTime;
std::cout << "Done." << std::endl;
std::cout
<< "Time: "
<< std::chrono::duration_cast<std::chrono::milliseconds>(delta).count()
<< " milliseconds" << std::endl;
return 0;
}
// g++ -std=c++17 main.cpp -o main