Thrill  0.1
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  auto save_fn = [this](const ValueType& input) {
46  writer_.Put(input);
47  };
48  auto lop_chain = parent.stack().push(save_fn).fold();
49  parent.node()->AddChild(this, lop_chain);
50  }
51 
52  bool OnPreOpFile(const data::File& file, size_t /* parent_index */) final {
53  if (!parent_stack_empty_) {
55  << "Cache rejected File from parent "
56  << "due to non-empty function stack.";
57  return false;
58  }
59  assert(file_.num_items() == 0);
60  file_ = file.Copy();
61  return true;
62  }
63 
64  void StopPreOp(size_t /* parent_index */) final {
65  // Push local elements to children
66  writer_.Close();
67  }
68 
69  void Execute() final { }
70 
71  void PushData(bool consume) final {
72  this->PushFile(file_, consume);
73  }
74 
75  void Dispose() final {
76  file_.Clear();
77  }
78 
79  size_t NumItems() const {
80  return file_.num_items();
81  }
82 
83 private:
84  //! Local data file
86  //! Data writer to local file (only active in PreOp).
88  //! Whether the parent stack is empty
89  const bool parent_stack_empty_;
90 };
91 
92 template <typename ValueType, typename Stack>
94  assert(IsValid());
95 
96 #if !defined(_MSC_VER)
97  // skip Cache if this is already a CacheNode. MSVC messes this up.
98  if (stack_empty &&
99  dynamic_cast<CacheNode<ValueType>*>(node_.get()) != nullptr) {
100  // return Collapse instead, automatically eliminates CollapseNode since
101  // the stack is empty.
102  return Collapse();
103  }
104 #endif
105  return DIA<ValueType>(
106  tlx::make_counting<api::CacheNode<ValueType> >(*this));
107 }
108 
109 } // namespace api
110 } // namespace thrill
111 
112 #endif // !THRILL_API_CACHE_HEADER
113 
114 /******************************************************************************/
DIA is the interface between the user and the Thrill framework.
Definition: dia.hpp:141
void PushData(bool consume) final
Virtual method for pushing data. Triggers actual pushing in sub-classes.
Definition: cache.hpp:71
size_t NumItems() const
Definition: cache.hpp:79
A File is an ordered sequence of Block objects for storing items.
Definition: file.hpp:56
void Clear()
Free all Blocks in the File and deallocate vectors.
Definition: file.cpp:57
data::File::Writer writer_
Data writer to local file (only active in PreOp).
Definition: cache.hpp:87
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
data::File file_
Local data file.
Definition: cache.hpp:85
A DIANode is a typed node representing and operation in Thrill.
Definition: dia_node.hpp:37
void StopPreOp(size_t) final
Virtual method for preparing end of PushData.
Definition: cache.hpp:64
const bool parent_stack_empty_
Whether the parent stack is empty.
Definition: cache.hpp:89
void Execute() final
Virtual execution method. Triggers actual computation in sub-classes.
Definition: cache.hpp:69
DIA< ValueType > Cache() const
Create a CacheNode which contains all items of a DIA in calculated plain format.
Definition: cache.hpp:93
data::File GetFile(size_t dia_id)
Returns a new File object containing a sequence of local Blocks.
Definition: context.hpp:283
bool OnPreOpFile(const data::File &file, size_t) final
Definition: cache.hpp:52
void Dispose() final
Virtual clear method. Triggers actual disposing in sub-classes.
Definition: cache.hpp:75
File Copy() const
Return a copy of the File (explicit copy-constructor)
Definition: file.cpp:42
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.
size_t num_items() const
Return the number of items in the file.
Definition: file.hpp:180
void Close()
Explicitly close the writer.
A DOpNode which caches all items in an external file.
Definition: cache.hpp:32
Writer GetWriter(size_t block_size=default_block_size)
Get BlockWriter.
Definition: file.cpp:63
void PushFile(data::File &file, bool consume) const
Definition: dia_node.hpp:156
Context & context_
associated Context
Definition: dia_base.hpp:293
#define LOGC(cond)
Explicitly specify the condition for logging.
Definition: logger.hpp:21