32 double DistanceSquare(
const Point& b)
const {
33 return (x - b.x) * (x - b.x) + (y - b.y) * (y - b.y);
36 return Point { x + b.x, y + b.y };
38 Point operator / (
double s)
const {
39 return Point { x / s, y / s };
46 return os <<
'(' << p.x <<
',' << p.y <<
')';
51 struct ClosestCenter {
57 std::ostream&
operator << (std::ostream& os,
const ClosestCenter& cc) {
58 return os <<
'(' << cc.cluster_id
59 <<
':' << cc.point <<
':' << cc.count <<
')';
66 std::default_random_engine rng(std::random_device { } ());
67 std::uniform_real_distribution<double> dist(0.0, 1000.0);
74 return Point { dist(rng), dist(rng) };
79 points.Print(
"points");
82 auto centers = points.Sample( 10);
85 std::vector<Point> local_centers = centers.AllGather();
88 auto closest = points.Map(
89 [local_centers](
const Point& p) {
90 double min_dist = p.DistanceSquare(local_centers[0]);
91 size_t cluster_id = 0;
93 for (
size_t i = 1; i < local_centers.size(); ++i) {
94 double dist = p.DistanceSquare(local_centers[i]);
96 min_dist = dist, cluster_id = i;
99 return ClosestCenter { cluster_id, p, 1 };
103 closest.Print(
"closest");
107 auto reduced_centers =
111 [](
const ClosestCenter& cc) {
return cc.cluster_id; },
113 [](
const ClosestCenter& a,
const ClosestCenter& b) {
114 return ClosestCenter {
115 a.cluster_id, a.point + b.point, a.count + b.count
119 reduced_centers.Print(
"reduced_centers");
125 .Map([](
const ClosestCenter& cc) {
126 return cc.point / cc.count;
129 new_centers.Print(
"new_centers");
auto Generate(Context &ctx, size_t size, const GenerateFunction &generate_function)
Generate is a Source-DOp, which creates a DIA of given size using a generator function.
int Run(const std::function< void(Context &)> &job_startpoint)
Runs the given job startpoint with a Context instance.
thrill::common::Vector< D, double > Point
Compile-Time Fixed-Dimensional Points.
The Context of a job is a unique instance per worker which holds references to all underlying parts o...
void Process(thrill::Context &ctx)
[ClosestCenter class]
std::ostream & operator<<(std::ostream &os, const Point &p)
[Point class]