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  * \param ctx Reference to the Context object
76  *
77  * \param size Size of the output DIA
78  *
79  * \param generate_function Generator function, which maps `size_t` from
80  * `[0,size)` to elements. Input type has to be `size_t`.
81  *
82  * \ingroup dia_sources
83  */
84 template <typename GenerateFunction>
85 auto Generate(Context& ctx, size_t size,
86  const GenerateFunction& generate_function) {
87 
88  using GenerateResult =
89  typename common::FunctionTraits<GenerateFunction>::result_type;
90 
91  using GenerateNode =
93 
94  static_assert(
95  common::FunctionTraits<GenerateFunction>::arity == 1,
96  "GenerateFunction must take exactly one parameter");
97 
98  static_assert(
99  std::is_convertible<
100  size_t,
101  typename common::FunctionTraits<GenerateFunction>::template arg<0>
102  >::value,
103  "GenerateFunction needs a const unsigned long int& (aka. size_t) as input");
104 
105  auto node = tlx::make_counting<GenerateNode>(
106  ctx, generate_function, size);
107 
108  return DIA<GenerateResult>(node);
109 }
110 
111 /*!
112  * Generate is a Source-DOp, which creates a DIA of given size containing the
113  * size_t indexes `[0,size)`.
114  *
115  * \param ctx Reference to the Context object
116  *
117  * \param size Size of the output DIA
118  *
119  * \ingroup dia_sources
120  */
121 static inline
122 auto Generate(Context& ctx, size_t size) {
123  return Generate(ctx, size, [](const size_t& index) { return index; });
124 }
125 
126 } // namespace api
127 
128 //! imported from api namespace
129 using api::Generate;
130 
131 } // namespace thrill
132 
133 #endif // !THRILL_API_GENERATE_HEADER
134 
135 /******************************************************************************/
DIA is the interface between the user and the Thrill framework.
Definition: dia.hpp:133
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
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:40
The Context of a job is a unique instance per worker which holds references to all underlying parts o...
Definition: context.hpp:210
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:54
common::Range CalculateLocalRange(size_t global_size) const
Definition: context.hpp:341
size_t size_
Size of the output DIA.
Definition: generate.hpp:67
size_t begin
begin index
Definition: math.hpp:52
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