STXXL  1.4.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
buf_istream.h
Go to the documentation of this file.
1 /***************************************************************************
2  * include/stxxl/bits/mng/buf_istream.h
3  *
4  * Part of the STXXL. See http://stxxl.sourceforge.net
5  *
6  * Copyright (C) 2002-2004 Roman Dementiev <[email protected]>
7  *
8  * Distributed under the Boost Software License, Version 1.0.
9  * (See accompanying file LICENSE_1_0.txt or copy at
10  * http://www.boost.org/LICENSE_1_0.txt)
11  **************************************************************************/
12 
13 #ifndef STXXL_MNG_BUF_ISTREAM_HEADER
14 #define STXXL_MNG_BUF_ISTREAM_HEADER
15 
16 #include <stxxl/bits/mng/config.h>
18 #include <stxxl/bits/noncopyable.h>
20 
22 
23 //! \addtogroup schedlayer
24 //! \{
25 
26 // a paranoid check
27 #define BUF_ISTREAM_CHECK_END
28 
29 //! Buffered input stream.
30 //!
31 //! Reads data records from the stream of blocks.
32 //! \remark Reading performed in the background, i.e. with overlapping of I/O and computation
33 template <typename BlockType, typename BidIteratorType>
34 class buf_istream : private noncopyable
35 {
36 public:
37  typedef BlockType block_type;
38  typedef BidIteratorType bid_iterator_type;
39 
40 private:
42 
43 protected:
49 #ifdef BUF_ISTREAM_CHECK_END
51 #endif
52 
53 public:
54  typedef typename block_type::reference reference;
56 
57  //! Constructs input stream object.
58  //! \param begin \c bid_iterator pointing to the first block of the stream
59  //! \param end \c bid_iterator pointing to the ( \b last + 1 ) block of the stream
60  //! \param nbuffers number of buffers for internal use
62  : current_elem(0)
64  , not_finished(true)
65 #endif
66  {
67  const unsigned_type ndisks = config::get_instance()->disks_number();
68  const unsigned_type mdevid = config::get_instance()->get_max_device_id();
69  const int_type seq_length = end - begin;
70  prefetch_seq = new int_type[seq_length];
71 
72  // obvious schedule
73  //for(int_type i = 0; i < seq_length; ++i)
74  // prefetch_seq[i] = i;
75 
76  // optimal schedule
77  nbuffers = STXXL_MAX(2 * ndisks, unsigned_type(nbuffers - 1));
78  compute_prefetch_schedule(begin, end, prefetch_seq,
79  nbuffers, mdevid);
80 
81  prefetcher = new prefetcher_type(begin, end, prefetch_seq, nbuffers);
82 
83  current_blk = prefetcher->pull_block();
84  }
85 
86  //! Input stream operator, reads in \c record.
87  //! \param record reference to the block record type,
88  //! contains value of the next record in the stream after the call of the operator
89  //! \return reference to itself (stream object)
90  self_type& operator >> (reference record)
91  {
92 #ifdef BUF_ISTREAM_CHECK_END
93  assert(not_finished);
94 #endif
95 
96  record = current_blk->elem[current_elem++];
97 
98  if (UNLIKELY(current_elem >= block_type::size))
99  {
100  current_elem = 0;
101 #ifdef BUF_ISTREAM_CHECK_END
102  not_finished = prefetcher->block_consumed(current_blk);
103 #else
104  prefetcher->block_consumed(current_blk);
105 #endif
106  }
107 
108  return (*this);
109  }
110 
111  //! Returns reference to the current record in the stream.
112  reference current() /* const */
113  {
114  return current_blk->elem[current_elem];
115  }
116 
117  //! Returns reference to the current record in the stream.
118  reference operator * () /* const */
119  {
120  return current_blk->elem[current_elem];
121  }
122 
123  //! Moves to the next record in the stream.
124  //! \return reference to itself after the advance
126  {
127 #ifdef BUF_ISTREAM_CHECK_END
128  assert(not_finished);
129 #endif
130 
131  current_elem++;
132 
133  if (UNLIKELY(current_elem >= block_type::size))
134  {
135  current_elem = 0;
136 #ifdef BUF_ISTREAM_CHECK_END
137  not_finished = prefetcher->block_consumed(current_blk);
138 #else
139  prefetcher->block_consumed(current_blk);
140 #endif
141  }
142  return *this;
143  }
144 
145  //! Frees used internal objects.
147  {
148  delete prefetcher;
149  delete[] prefetch_seq;
150  }
151 };
152 
153 //! \}
154 
156 
157 #endif // !STXXL_MNG_BUF_ISTREAM_HEADER
buf_istream< block_type, bid_iterator_type > self_type
Definition: buf_istream.h:55
int_type current_elem
Definition: buf_istream.h:46
#define BUF_ISTREAM_CHECK_END
Definition: buf_istream.h:27
uint_pair & operator++()
prefix increment operator (directly manipulates the integer parts)
Definition: uint_types.h:163
prefetcher_type * prefetcher
Definition: buf_istream.h:45
Encapsulates asynchronous prefetching engine.
choose_int_types< my_pointer_size >::int_type int_type
Definition: types.h:63
BlockType block_type
Definition: buf_istream.h:37
#define UNLIKELY(c)
Definition: utils.h:225
buf_istream(bid_iterator_type begin, bid_iterator_type end, unsigned_type nbuffers)
Constructs input stream object.
Definition: buf_istream.h:61
const Type & STXXL_MAX(const Type &a, const Type &b)
Definition: utils.h:153
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
Buffered input stream.
Definition: buf_istream.h:34
block_type * current_blk
Definition: buf_istream.h:47
BidIteratorType bid_iterator_type
Definition: buf_istream.h:38
int_type * prefetch_seq
Definition: buf_istream.h:48
choose_int_types< my_pointer_size >::unsigned_type unsigned_type
Definition: types.h:64
void compute_prefetch_schedule(const int_type *first, const int_type *last, int_type *out_first, int_type m, int_type D)
block_prefetcher< block_type, bid_iterator_type > prefetcher_type
Definition: buf_istream.h:44
reference current()
Returns reference to the current record in the stream.
Definition: buf_istream.h:112
~buf_istream()
Frees used internal objects.
Definition: buf_istream.h:146
#define STXXL_END_NAMESPACE
Definition: namespace.h:17
block_type::reference reference
Definition: buf_istream.h:54