31 double DistanceSquare(
const Point& b)
const {
32 return (x - b.x) * (x - b.x) + (y - b.y) * (y - b.y);
39 return os <<
'(' << p.x <<
',' << p.y <<
')';
44 struct ClosestCenter {
49 std::ostream&
operator << (std::ostream& os,
const ClosestCenter& cc) {
50 return os <<
'(' << cc.cluster_id <<
':' << cc.point <<
')';
56 std::default_random_engine rng(std::random_device { } ());
57 std::uniform_real_distribution<double> dist(0.0, 1000.0);
64 return Point { dist(rng), dist(rng) };
69 points.Print(
"points");
73 auto centers = points.Sample( 10);
78 std::vector<Point> local_centers = centers.AllGather();
81 auto closest = points.Map(
82 [local_centers](
const Point& p) {
83 double min_dist = p.DistanceSquare(local_centers[0]);
84 size_t cluster_id = 0;
86 for (
size_t i = 1; i < local_centers.size(); ++i) {
87 double dist = p.DistanceSquare(local_centers[i]);
89 min_dist = dist, cluster_id = i;
91 return ClosestCenter { cluster_id, p };
94 closest.Print(
"closest");
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.
std::ostream & operator<<(std::ostream &os, const Point &p)
[Point class]
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]