NVBIO
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
scoring_queues_inl.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 namespace nvbio {
29 namespace bowtie2 {
30 namespace cuda {
31 
32 // alloc enough storage for max_size hits
33 //
34 inline
35 uint64 ReadHitsIndex::resize(const uint32 max_size, const bool do_alloc)
36 {
37  const uint32 n = 2u*max_size;
38  if (do_alloc) m_links.resize( n );
39  return n * sizeof(uint32);
40 }
41 
42 // setup the number of input reads
43 //
44 inline
45 void ReadHitsIndex::setup(const uint32 n_hits_per_read, const uint32 in_reads)
46 {
47  m_mode = n_hits_per_read > 1 ?
50 
51  m_stride = in_reads;
52 
53  if (n_hits_per_read == 1)
54  {
55  // fill the links structure
56  thrust::fill(
57  m_links.begin(),
58  m_links.begin() + in_reads,
59  uint32(1u) );
60 
61  // fill the links structure
63  thrust::make_counting_iterator(0u),
64  thrust::make_counting_iterator(in_reads),
65  m_links.begin() + in_reads );
66  }
67 }
68 
69 // return a view of the data structure
70 //
71 inline
73 {
76  m_stride );
77 }
78 
79 // setup all queues
80 //
81 inline
82 uint64 HitQueues::resize(const uint32 size, const bool do_alloc)
83 {
84  uint64 bytes = 0;
85  if (do_alloc) read_id.resize( size ); bytes += size * sizeof(uint32);
86  if (do_alloc) seed.resize( size ); bytes += size * sizeof(packed_seed);
87  if (do_alloc) ssa.resize( size ); bytes += size * sizeof(uint32);
88  if (do_alloc) loc.resize( size ); bytes += size * sizeof(uint32);
89  if (do_alloc) score.resize( size ); bytes += size * sizeof(int32);
90  if (do_alloc) sink.resize( size ); bytes += size * sizeof(uint32);
91  if (do_alloc) opposite_loc.resize( size ); bytes += size * sizeof(uint32);
92  if (do_alloc) opposite_score.resize( size ); bytes += size * sizeof(int32);
93  if (do_alloc) opposite_sink.resize( size ); bytes += size * sizeof(uint32);
94  if (do_alloc) opposite_score2.resize( size ); bytes += size * sizeof(int32);
95  if (do_alloc) opposite_sink2.resize( size ); bytes += size * sizeof(uint32);
96  return bytes;
97 }
98 
99 // return a view of the data structure
100 //
101 inline
103 {
104  return HitQueuesDeviceView(
116 }
117 
118 // constructor
119 //
122  index_storage_type _read_id, // hit -> read mapping
123  seed_storage_type _seed, // hit info
124  ssa_storage_type _ssa, // hit ssa info
125  loc_storage_type _loc, // hit locations
126  score_storage_type _score, // hit scores
127  sink_storage_type _sink, // hit sinks
128  loc_storage_type _opposite_loc, // hit locations, opposite mate
129  score_storage_type _opposite_score, // hit scores, opposite mate
130  sink_storage_type _opposite_sink, // hit sinks, opposite mate
131  score_storage_type _opposite_score2, // hit scores, opposite mate
132  sink_storage_type _opposite_sink2) : // hit sinks, opposite mate
133  read_id( _read_id ),
134  seed( _seed ),
135  ssa( _ssa ),
136  loc( _loc ),
137  score( _score ),
138  sink( _sink ),
139  opposite_loc( _opposite_loc ),
140  opposite_score( _opposite_score ),
141  opposite_sink( _opposite_sink ),
142  opposite_score2( _opposite_score2 ),
143  opposite_sink2( _opposite_sink2 )
144 {}
145 
146 // resize
147 //
148 inline
149 uint64 ScoringQueues::resize(const uint32 n_reads, const uint32 n_hits, const bool do_alloc)
150 {
151  uint64 bytes = 0;
152  bytes += active_reads.resize_arena( n_reads, do_alloc );
153  bytes += hits.resize( n_hits, do_alloc );
154  bytes += hits_index.resize( n_hits, do_alloc );
155  return bytes;
156 }
157 
158 // return a reference to a given hit
159 //
162 {
163  return HitReference<HitQueuesDeviceView>( *this, i );
164 }
165 
166 // return a reference to a given hit
167 //
170 {
171  return HitReference<HitQueuesDeviceView>( *const_cast<HitQueuesDeviceView*>( this ), i );
172 }
173 
174 // constructor
175 //
176 // \param hits hits container
177 // \param hit_index index of this hit
178 #pragma hd_warning_disable
179 template <typename HitQueuesType>
181 HitReference<HitQueuesType>::HitReference(HitQueuesType& hits, const uint32 hit_index) :
182  read_id ( hits.read_id[ hit_index ] ),
183  seed ( hits.seed[ hit_index ] ),
184  ssa ( hits.ssa[ hit_index ] ),
185  loc ( hits.loc[ hit_index ] ),
186  score ( hits.score[ hit_index ] ),
187  sink ( hits.sink[ hit_index ] ),
188  opposite_loc ( hits.opposite_loc[ hit_index ] ),
189  opposite_score ( hits.opposite_score[ hit_index ] ),
190  opposite_sink ( hits.opposite_sink[ hit_index ] ),
191  opposite_score2 ( hits.opposite_score2[ hit_index ] ),
192  opposite_sink2 ( hits.opposite_sink2[ hit_index ] )
193 {}
194 
195 // return a view of the data structure
196 //
197 inline
199 {
205 }
206 
207 // constructor
208 //
211  active_reads_storage_type _active_reads, // set of active reads
212  hits_storage_type _hits, // nested sequence of read hits
213  read_hits_index_type _hits_index, // read -> hits mapping
214  pool_type _hits_pool) : // pool counter
215  active_reads ( _active_reads ),
216  hits ( _hits ),
217  hits_index ( _hits_index ),
218  hits_pool ( _hits_pool )
219 {}
220 
221 // constructor
222 //
223 // \param index read -> hits index
224 // \param hits hits container
225 // \param read_index index of this read
226 template <typename ScoringQueuesType>
228 ReadHitsReference<ScoringQueuesType>::ReadHitsReference(ScoringQueuesType& queues, const uint32 read_index) :
229  m_queues( queues ),
230  m_read_index( read_index )
231 {}
232 
233 // size of the hits vector
234 //
235 template <typename ScoringQueuesType>
238 {
239  return m_queues.hits_index.hit_count( m_read_index );
240 }
241 
242 // return the i-th element
243 //
244 #pragma hd_warning_disable
245 template <typename ScoringQueuesType>
249 {
250  const uint32 hit_index = m_queues.hits_index( m_read_index, i );
251  return m_queues.hits[ hit_index ];
252 }
253 
254 // return the slot where the i-th element is stored
255 //
256 #pragma hd_warning_disable
257 template <typename ScoringQueuesType>
259 uint32
261 {
262  return m_queues.hits_index( m_read_index, i );
263 }
264 
265 // bind this read to its new output location
266 //
267 // \param read_index output index of this read
268 template <typename ScoringQueuesType>
271 {
272  m_read_index = read_index;
273 }
274 
275 // access the packed_read info in the selected queue
276 //
277 template <typename ScoringQueuesType>
280 {
281  return m_queues.active_reads.in_queue[ m_read_index ];
282 }
283 
284 
285 // constructor
286 //
287 // \param index read -> hits index
288 // \param hits hits container
289 // \param read_index index of this read
290 template <typename ScoringQueuesType>
292 ReadHitsBinder<ScoringQueuesType>::ReadHitsBinder(ScoringQueuesType& queues, const uint32 read_index) :
293  m_queues( queues ),
294  m_read_index( read_index )
295 {}
296 
297 // bind this read to its new output location
298 //
299 // \param read_index output index of this read
300 template <typename ScoringQueuesType>
303 {
304  m_read_index = read_index;
305 }
306 
307 // resize the hits vector
308 //
309 template <typename ScoringQueuesType>
312 {
313  m_queues.hits_index.set_hit_count( m_read_index, size );
314 }
315 
316 // size of the hits vector
317 //
318 template <typename ScoringQueuesType>
321 {
322  return m_queues.hits_index.hit_count( m_read_index );
323 }
324 
325 // return the i-th element
326 //
327 #pragma hd_warning_disable
328 template <typename ScoringQueuesType>
332 {
333  const uint32 hit_index = m_queues.hits_index( m_read_index, i );
334  return m_queues.hits[ hit_index ];
335 }
336 
337 // access the packed_read info in the selected queue
338 //
339 template <typename ScoringQueuesType>
342 {
343  return m_queues.active_reads.out_queue[ m_read_index ];
344 }
345 
346 // set the packed_read info
347 //
348 // \param read_index output index of this read
349 template <typename ScoringQueuesType>
352 {
353  m_queues.active_reads.out_queue[ m_read_index ] = info;
354 }
355 
356 // bind the i-th hit to a given location
357 //
358 // \param i index of the hit to bind relative to this read
359 // \param slot address of the bound hit in the HitQueues
360 template <typename ScoringQueuesType>
363 {
364  m_queues.hits_index( m_read_index, i ) = slot;
365 }
366 
367 } // namespace cuda
368 } // namespace bowtie2
369 } // namespace nvbio