Thrill  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
k-means_step1.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2  * examples/tutorial/k-means_step1.cpp
3  *
4  * Part of Project Thrill - http://project-thrill.org
5  *
6  * Copyright (C) 2016 Timo Bingmann <[email protected]>
7  *
8  * All rights reserved. Published under the BSD-2 license in the LICENSE file.
9  ******************************************************************************/
10 
11 //! \example examples/tutorial/k-means_step1.cpp
12 //!
13 //! This example is part of the k-means tutorial. See \ref kmeans_tutorial_step1
14 
15 #include <thrill/api/cache.hpp>
16 #include <thrill/api/generate.hpp>
17 #include <thrill/api/print.hpp>
18 
19 #include <ostream>
20 #include <random>
21 
22 using thrill::DIA;
23 
24 //! [Point class]
25 //! A 2-dimensional point with double precision
26 struct Point {
27  //! point coordinates
28  double x, y;
29 };
30 //! [Point class]
31 
32 //! [Point ostream]
33 //! make ostream-able for Print()
34 std::ostream& operator << (std::ostream& os, const Point& p) {
35  return os << '(' << p.x << ',' << p.y << ')';
36 }
37 //! [Point ostream]
38 
39 //! [our main processing method]
40 void Process(thrill::Context& ctx) {
41 
42  std::default_random_engine rng(std::random_device { } ());
43  std::uniform_real_distribution<double> dist(0.0, 1000.0);
44 
45  // generate 100 random points using uniform distribution
46  DIA<Point> points =
47  Generate(
48  ctx, /* size */ 100,
49  [&](const size_t& /* index */) {
50  return Point { dist(rng), dist(rng) };
51  })
52  .Cache();
53 
54  // print out the points
55  points.Print("points");
56 }
57 //! [our main processing method]
58 
59 //! [Thrill Run launcher]
60 int main() {
61  // launch Thrill program: the lambda function will be run on each worker.
62  return thrill::Run(
63  [&](thrill::Context& ctx) { Process(ctx); });
64 }
65 //! [Thrill Run launcher]
66 
67 /******************************************************************************/
std::ostream & operator<<(std::ostream &os, const Point &p)
[Point class]
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.
Definition: generate.hpp:85
int Run(const std::function< void(Context &)> &job_startpoint)
Runs the given job startpoint with a Context instance.
Definition: context.cpp:887
thrill::common::Vector< D, double > Point
Compile-Time Fixed-Dimensional Points.
Definition: k-means.hpp:39
void Process(thrill::Context &ctx)
[Point ostream]
list x
Definition: gen_data.py:39
int main()
[our main processing method]