Thrill  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cache.hpp
Go to the documentation of this file.
1 /*******************************************************************************
2  * thrill/api/cache.hpp
3  *
4  * Part of Project Thrill - http://project-thrill.org
5  *
6  * Copyright (C) 2015 Sebastian Lamm <[email protected]>
7  *
8  * All rights reserved. Published under the BSD-2 license in the LICENSE file.
9  ******************************************************************************/
10 
11 #pragma once
12 #ifndef THRILL_API_CACHE_HEADER
13 #define THRILL_API_CACHE_HEADER
14 
15 #include <thrill/api/collapse.hpp>
16 #include <thrill/api/dia.hpp>
17 #include <thrill/api/dia_node.hpp>
18 #include <thrill/data/file.hpp>
19 
20 #include <string>
21 #include <vector>
22 
23 namespace thrill {
24 namespace api {
25 
26 /*!
27  * A DOpNode which caches all items in an external file.
28  *
29  * \ingroup api_layer
30  */
31 template <typename ValueType>
32 class CacheNode final : public DIANode<ValueType>
33 {
34 public:
36  using Super::context_;
37 
38  /*!
39  * Constructor for a LOpNode. Sets the Context, parents and stack.
40  */
41  template <typename ParentDIA>
42  explicit CacheNode(const ParentDIA& parent)
43  : Super(parent.ctx(), "Cache", { parent.id() }, { parent.node() }),
44  parent_stack_empty_(ParentDIA::stack_empty)
45  {
46  auto save_fn = [this](const ValueType& input) {
47  writer_.Put(input);
48  };
49  auto lop_chain = parent.stack().push(save_fn).fold();
50  parent.node()->AddChild(this, lop_chain);
51  }
52 
53  bool OnPreOpFile(const data::File& file, size_t /* parent_index */) final {
54  if (!parent_stack_empty_) {
56  << "Cache rejected File from parent "
57  << "due to non-empty function stack.";
58  return false;
59  }
60  assert(file_.num_items() == 0);
61  file_ = file.Copy();
62  return true;
63  }
64 
65  void StopPreOp(size_t /* id */) final {
66  // Push local elements to children
67  writer_.Close();
68  }
69 
70  void Execute() final { }
71 
72  void PushData(bool consume) final {
73  this->PushFile(file_, consume);
74  }
75 
76  void Dispose() final {
77  file_.Clear();
78  }
79 
80  size_t NumItems() const {
81  return file_.num_items();
82  }
83 
84 private:
85  //! Local data file
86  data::File file_ { context_.GetFile(this) };
87  //! Data writer to local file (only active in PreOp).
88  data::File::Writer writer_ { file_.GetWriter() };
89  //! Whether the parent stack is empty
90  const bool parent_stack_empty_;
91 };
92 
93 template <typename ValueType, typename Stack>
95  assert(IsValid());
96 
97 #if !defined(_MSC_VER)
98  // skip Cache if this is already a CacheNode. MSVC messes this up.
99  if (stack_empty &&
100  dynamic_cast<CacheNode<ValueType>*>(node_.get()) != nullptr) {
101  // return Collapse instead, automatically eliminates CollapseNode since
102  // the stack is empty.
103  return Collapse();
104  }
105 #endif
106  return DIA<ValueType>(
107  tlx::make_counting<api::CacheNode<ValueType> >(*this));
108 }
109 
110 } // namespace api
111 } // namespace thrill
112 
113 #endif // !THRILL_API_CACHE_HEADER
114 
115 /******************************************************************************/
void PushFile(data::File &file, bool consume) const
Definition: dia_node.hpp:156
virtual void Dispose()
Virtual clear method. Triggers actual disposing in sub-classes.
Definition: dia_base.hpp:188
DIA is the interface between the user and the Thrill framework.
Definition: dia.hpp:141
data::File::Writer writer_
Data writer to local file (only active in PreOp).
Definition: cache.hpp:88
BlockWriter contains a temporary Block object into which a) any serializable item can be stored or b)...
static constexpr bool g_debug_push_file
Definition: config.hpp:44
virtual void PushData(bool consume)=0
Virtual method for pushing data. Triggers actual pushing in sub-classes.
A DIANode is a typed node representing and operation in Thrill.
Definition: dia_node.hpp:37
virtual void StopPreOp(size_t)
Virtual method for preparing end of PushData.
Definition: dia_base.hpp:173
const bool parent_stack_empty_
Whether the parent stack is empty.
Definition: cache.hpp:90
#define LOGC(cond)
Explicitly specify the condition for logging.
Definition: logger.hpp:167
virtual void Execute()=0
Virtual execution method. Triggers actual computation in sub-classes.
data::File GetFile(size_t dia_id)
Returns a new File object containing a sequence of local Blocks.
Definition: context.hpp:280
DIA< ValueType > Cache() const
Create a CacheNode which contains all items of a DIA in calculated plain format.
Definition: cache.hpp:94
CacheNode(const ParentDIA &parent)
Constructor for a LOpNode.
Definition: cache.hpp:42
TLX_ATTRIBUTE_ALWAYS_INLINE BlockWriter & Put(const T &x)
Put appends a complete item, or fails with a FullException.
virtual bool OnPreOpFile(const data::File &, size_t)
Definition: dia_base.hpp:168
void Close()
Explicitly close the writer.
A DOpNode which caches all items in an external file.
Definition: cache.hpp:32
Context & context_
associated Context
Definition: dia_base.hpp:293