Thrill  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
generate.hpp
Go to the documentation of this file.
1 /*******************************************************************************
2  * thrill/api/generate.hpp
3  *
4  * DIANode for a generate operation. Performs the actual generate operation
5  *
6  * Part of Project Thrill - http://project-thrill.org
7  *
8  * Copyright (C) 2015 Alexander Noe <[email protected]>
9  *
10  * All rights reserved. Published under the BSD-2 license in the LICENSE file.
11  ******************************************************************************/
12 
13 #pragma once
14 #ifndef THRILL_API_GENERATE_HEADER
15 #define THRILL_API_GENERATE_HEADER
16 
17 #include <thrill/api/dia.hpp>
19 #include <thrill/common/logger.hpp>
20 
21 #include <random>
22 #include <type_traits>
23 
24 namespace thrill {
25 namespace api {
26 
27 /*!
28  * A DIANode which performs a Generate operation. Generate creates an DIA
29  * according to a generator function. This function is used to generate a DIA of
30  * a certain size by applying it to integers from 0 to size - 1.
31  *
32  * \tparam ValueType Output type of the Generate operation.
33  * \tparam GenerateNode Type of the generate function.
34  * \ingroup api_layer
35  */
36 template <typename ValueType, typename GenerateFunction>
37 class GenerateNode final : public SourceNode<ValueType>
38 {
39 public:
41  using Super::context_;
42 
43  /*!
44  * Constructor for a GenerateNode. Sets the Context, parents, generator
45  * function and file path.
46  */
48  GenerateFunction generate_function,
49  size_t size)
50  : Super(ctx, "Generate"),
51  generate_function_(generate_function),
52  size_(size)
53  { }
54 
55  void PushData(bool /* consume */) final {
57 
58  for (size_t i = local.begin; i < local.end; i++) {
59  this->PushItem(generate_function_(i));
60  }
61  }
62 
63 private:
64  //! The generator function which is applied to every index.
65  GenerateFunction generate_function_;
66  //! Size of the output DIA.
67  size_t size_;
68 };
69 
70 /*!
71  * Generate is a Source-DOp, which creates a DIA of given size using a
72  * generator function. The generator function called for each index in the range
73  * of `[0,size)` and must output exactly one item.
74  *
75  * \image html dia_ops/Generate.svg
76  *
77  * \param ctx Reference to the Context object
78  *
79  * \param size Size of the output DIA
80  *
81  * \param generate_function Generator function, which maps `size_t` from
82  * `[0,size)` to elements. Input type has to be `size_t`.
83  *
84  * \ingroup dia_sources
85  */
86 template <typename GenerateFunction>
87 auto Generate(Context& ctx, size_t size,
88  const GenerateFunction& generate_function) {
89 
90  using GenerateResult =
91  typename common::FunctionTraits<GenerateFunction>::result_type;
92 
93  using GenerateNode =
95 
96  static_assert(
97  common::FunctionTraits<GenerateFunction>::arity == 1,
98  "GenerateFunction must take exactly one parameter");
99 
100  static_assert(
101  std::is_convertible<
102  size_t,
103  typename common::FunctionTraits<GenerateFunction>::template arg<0>
104  >::value,
105  "GenerateFunction needs a const unsigned long int& (aka. size_t) as input");
106 
107  auto node = tlx::make_counting<GenerateNode>(
108  ctx, generate_function, size);
109 
110  return DIA<GenerateResult>(node);
111 }
112 
113 /*!
114  * Generate is a Source-DOp, which creates a DIA of given size containing the
115  * size_t indexes `[0,size)`.
116  *
117  * \image html dia_ops/Generate.svg
118  *
119  * \param ctx Reference to the Context object
120  *
121  * \param size Size of the output DIA
122  *
123  * \ingroup dia_sources
124  */
125 static inline
126 auto Generate(Context& ctx, size_t size) {
127  return Generate(ctx, size, [](const size_t& index) { return index; });
128 }
129 
130 } // namespace api
131 
132 //! imported from api namespace
133 using api::Generate;
134 
135 } // namespace thrill
136 
137 #endif // !THRILL_API_GENERATE_HEADER
138 
139 /******************************************************************************/
DIA is the interface between the user and the Thrill framework.
Definition: dia.hpp:141
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:87
GenerateFunction generate_function_
The generator function which is applied to every index.
Definition: generate.hpp:65
GenerateNode(Context &ctx, GenerateFunction generate_function, size_t size)
Constructor for a GenerateNode.
Definition: generate.hpp:47
represents a 1 dimensional range (interval) [begin,end)
Definition: math.hpp:41
The Context of a job is a unique instance per worker which holds references to all underlying parts o...
Definition: context.hpp:221
A DIANode which performs a Generate operation.
Definition: generate.hpp:37
void PushItem(const ValueType &item) const
Method for derived classes to Push a single item to all children.
Definition: dia_node.hpp:147
int value
Definition: gen_data.py:41
size_t end
end index
Definition: math.hpp:58
common::Range CalculateLocalRange(size_t global_size) const
Definition: context.hpp:339
size_t size_
Size of the output DIA.
Definition: generate.hpp:67
size_t begin
begin index
Definition: math.hpp:56
void PushData(bool) final
Virtual method for pushing data. Triggers actual pushing in sub-classes.
Definition: generate.hpp:55
Context & context_
associated Context
Definition: dia_base.hpp:293