NVBIO
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
input_thread.h
Go to the documentation of this file.
1 /*
2  * nvbio
3  * Copyright (c) 2011-2014, NVIDIA CORPORATION. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of the NVIDIA CORPORATION nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #pragma once
29 
32 #include <nvbio/basic/threads.h>
33 #include <nvbio/basic/timer.h>
35 #include <stack>
36 #include <deque>
37 
38 namespace nvbio {
39 namespace bowtie2 {
40 namespace cuda {
41 
42 //
43 // A class implementing a background input thread, providing
44 // a set of input read-streams which are read in parallel to the
45 // operations performed by the main thread.
46 //
47 
48 struct InputThreadSE : public Thread<InputThreadSE>
49 {
50  static const uint32 BUFFERS = 4;
51 
52  InputThreadSE(io::SequenceDataStream* read_data_stream, Stats& _stats, const uint32 batch_size, const uint32 read_length) :
53  m_read_data_stream( read_data_stream ), m_stats( _stats ), m_batch_size( batch_size ), m_read_length( read_length ), m_set(0), m_reads(0), m_done(false)
54  {}
55 
56  void run();
57 
58  // get a batch
59  //
60  io::SequenceDataHost* next(uint32* offset = NULL);
61 
62  // release a batch
63  //
64  void release(io::SequenceDataHost* read_data);
65 
66  // return the batch size
67  //
68  uint32 batch_size() const { return m_batch_size; }
69 
70 private:
71  io::SequenceDataStream* m_read_data_stream;
72  Stats& m_stats;
73  uint32 m_batch_size;
74  uint32 m_read_length;
75  uint32 m_set;
76  uint32 m_reads;
77 
78  io::SequenceDataHost m_read_data_storage[BUFFERS];
79 
80  Mutex m_free_pool_lock;
81  std::stack<io::SequenceDataHost*> m_free_pool;
82 
83  Mutex m_ready_pool_lock;
84  std::deque<io::SequenceDataHost*> m_ready_pool;
85  std::deque<uint32> m_ready_poolN;
86 
87  volatile bool m_done;
88 };
89 
90 //
91 // A class implementing a background input thread, providing
92 // a set of input read-streams which are read in parallel to the
93 // operations performed by the main thread.
94 //
95 
96 struct InputThreadPE : public Thread<InputThreadPE>
97 {
98  static const uint32 BUFFERS = 4;
99 
100  InputThreadPE(io::SequenceDataStream* read_data_stream1, io::SequenceDataStream* read_data_stream2, Stats& _stats, const uint32 batch_size, const uint32 read_length) :
101  m_read_data_stream1( read_data_stream1 ), m_read_data_stream2( read_data_stream2 ), m_stats( _stats ), m_batch_size( batch_size ), m_read_length( read_length ), m_set(0), m_reads(0), m_done(false)
102  {}
103 
104  void run();
105 
106  // get a batch
107  //
108  std::pair<io::SequenceDataHost*,io::SequenceDataHost*> next(uint32* offset = NULL);
109 
110  // release a batch
111  //
112  void release(std::pair<io::SequenceDataHost*,io::SequenceDataHost*> read_data);
113 
114  // return the batch size
115  //
116  uint32 batch_size() const { return m_batch_size; }
117 
118 private:
119  io::SequenceDataStream* m_read_data_stream1;
120  io::SequenceDataStream* m_read_data_stream2;
121  Stats& m_stats;
122  uint32 m_batch_size;
123  uint32 m_read_length;
124  uint32 m_set;
125  uint32 m_reads;
126 
127  io::SequenceDataHost m_read_data_storage1[BUFFERS];
128  io::SequenceDataHost m_read_data_storage2[BUFFERS];
129 
130  Mutex m_free_pool_lock;
131  std::stack<io::SequenceDataHost*> m_free_pool1;
132  std::stack<io::SequenceDataHost*> m_free_pool2;
133 
134  Mutex m_ready_pool_lock;
135  std::deque<io::SequenceDataHost*> m_ready_pool1;
136  std::deque<io::SequenceDataHost*> m_ready_pool2;
137  std::deque<uint32> m_ready_poolN;
138 
139  volatile bool m_done;
140 };
141 
142 } // namespace cuda
143 } // namespace bowtie2
144 } // namespace nvbio