Thrill  0.1
size.hpp
Go to the documentation of this file.
1 /*******************************************************************************
2  * thrill/api/size.hpp
3  *
4  * Part of Project Thrill - http://project-thrill.org
5  *
6  * Copyright (C) 2015 Matthias Stumpp <[email protected]>
7  * Copyright (C) 2015 Sebastian Lamm <[email protected]>
8  *
9  * All rights reserved. Published under the BSD-2 license in the LICENSE file.
10  ******************************************************************************/
11 
12 #pragma once
13 #ifndef THRILL_API_SIZE_HEADER
14 #define THRILL_API_SIZE_HEADER
15 
17 #include <thrill/api/cache.hpp>
18 #include <thrill/api/dia.hpp>
19 #include <thrill/net/group.hpp>
20 
21 namespace thrill {
22 namespace api {
23 
24 /*!
25  * \ingroup api_layer
26  */
27 template <typename ValueType>
28 class SizeNode final : public ActionResultNode<size_t>
29 {
30  static constexpr bool debug = false;
31 
33  using Super::context_;
34 
35 public:
36  template <typename ParentDIA>
37  explicit SizeNode(const ParentDIA& parent)
38  : Super(parent.ctx(), "Size", { parent.id() }, { parent.node() }),
39  parent_stack_empty_(ParentDIA::stack_empty) {
40 
41  // Hook PreOp(s)
42  auto pre_op_fn = [this](const ValueType&) { ++local_size_; };
43 
44  auto lop_chain = parent.stack().push(pre_op_fn).fold();
45  parent.node()->AddChild(this, lop_chain);
46  }
47 
48  //! Receive a whole data::File of ValueType, but only if our stack is empty.
49  bool OnPreOpFile(const data::File& file, size_t /* parent_index */) final {
50  if (!parent_stack_empty_) {
52  << "Size rejected File from parent "
53  << "due to non-empty function stack.";
54  return false;
55  }
56  local_size_ = file.num_items();
58  << "CacheNode." << this->dia_id() << "::OnPreOpFile()"
59  << " accepted file with " << local_size_ << " items";
60  return true;
61  }
62 
63  //! Executes the size operation.
64  void Execute() final {
65  // get the number of elements that are stored on this worker
66  LOG << "MainOp processing, sum: " << local_size_;
67 
68  // process the reduce, default argument is SumOp.
69  global_size_ = context_.net.AllReduce(local_size_);
70  }
71 
72  //! Returns result of global size.
73  const size_t& result() const final {
74  return global_size_;
75  }
76 
77 private:
78  //! Whether the parent stack is empty
79  const bool parent_stack_empty_;
80  // Local size to be used.
81  size_t local_size_ = 0;
82  // Global size resulting from all reduce.
83  size_t global_size_ = 0;
84 };
85 
86 template <typename ValueType, typename Stack>
88  assert(IsValid());
89 
91  auto node = tlx::make_counting<SizeNode>(*this);
92  node->RunScope();
93  return node->result();
94 }
95 
96 template <typename ValueType, typename Stack>
98  assert(IsValid());
99 
101  auto node = tlx::make_counting<SizeNode>(*this);
102  return Future<size_t>(node);
103 }
104 
105 } // namespace api
106 } // namespace thrill
107 
108 #endif // !THRILL_API_SIZE_HEADER
109 
110 /******************************************************************************/
net::FlowControlChannel & net
Definition: context.hpp:446
A File is an ordered sequence of Block objects for storing items.
Definition: file.hpp:56
size_t global_size_
Definition: size.hpp:83
static constexpr bool g_debug_push_file
Definition: config.hpp:44
T TLX_ATTRIBUTE_WARN_UNUSED_RESULT AllReduce(const T &value, const BinarySumOp &sum_op=BinarySumOp())
Reduces a value of a serializable type T over all workers given a certain reduce function.
const bool parent_stack_empty_
Whether the parent stack is empty.
Definition: size.hpp:79
size_t local_size_
Definition: size.hpp:81
SizeNode(const ParentDIA &parent)
Definition: size.hpp:37
void Execute() final
Executes the size operation.
Definition: size.hpp:64
The return type class for all ActionFutures.
Definition: action_node.hpp:83
bool OnPreOpFile(const data::File &file, size_t) final
Receive a whole data::File of ValueType, but only if our stack is empty.
Definition: size.hpp:49
const size_t & dia_id() const
return unique id of DIANode subclass as stored by StatsNode
Definition: dia_base.hpp:213
Future< size_t > SizeFuture() const
Lazily computes the total size of all elements across all workers.
Definition: size.hpp:97
static constexpr bool debug
Definition: size.hpp:30
size_t Size() const
Computes the total size of all elements across all workers.
Definition: size.hpp:87
#define LOG
Default logging method: output if the local debug variable is true.
Definition: logger.hpp:24
Context & context_
associated Context
Definition: dia_base.hpp:293
#define LOGC(cond)
Explicitly specify the condition for logging.
Definition: logger.hpp:21
const size_t & result() const final
Returns result of global size.
Definition: size.hpp:73