NVBIO
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
filter.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 
30 #include <nvbio/qgram/qgram.h>
31 #include <nvbio/basic/types.h>
32 #include <nvbio/basic/numbers.h>
33 #include <nvbio/basic/vector.h>
34 #include <nvbio/basic/algorithms.h>
35 #include <nvbio/basic/exceptions.h>
36 #include <nvbio/basic/cuda/sort.h>
38 #include <thrust/sort.h>
39 #include <thrust/scan.h>
40 #include <thrust/iterator/constant_iterator.h>
41 #include <thrust/iterator/counting_iterator.h>
42 
43 namespace nvbio {
44 
47 
66 template <typename system_tag, typename qgram_index_type, typename query_iterator, typename index_iterator>
67 struct QGramFilter {};
68 
69 
92 template <typename qgram_index_type, typename query_iterator, typename index_iterator>
93 struct QGramFilter<host_tag, qgram_index_type, query_iterator, index_iterator>
94 {
96 
98 
99  typedef typename qgram_index_type::coord_type coord_type;
100 
106 
119  uint64 rank(
120  const qgram_index_type& qgram_index,
121  const uint32 n_queries,
122  const query_iterator queries,
123  const index_iterator indices);
124 
129  template <typename hits_iterator>
130  void locate(
131  const uint64 begin,
132  const uint64 end,
133  hits_iterator hits);
134 
145  template <typename hits_iterator, typename output_iterator>
146  void diagonals(
147  const uint32 n_hits,
148  const hits_iterator hits,
149  output_iterator diags,
150  const uint32 interval = 1);
151 
168  template <typename hits_iterator, typename output_iterator, typename count_iterator>
169  uint32 merge(
170  const uint32 interval,
171  const uint32 n_hits,
172  const hits_iterator hits,
173  output_iterator merged_hits,
174  count_iterator merged_counts);
175 
178  const uint2* ranges() const { return nvbio::plain_view( m_ranges ); }
179 
183  const uint64* ranks() const { return nvbio::plain_view( m_slots ); }
184 
186  query_iterator m_queries;
187  index_iterator m_indices;
190  thrust::host_vector<uint2> m_ranges;
191  thrust::host_vector<uint64> m_slots;
192  thrust::host_vector<diagonal_type> m_diags;
193 };
194 
213 template <typename qgram_index_type, typename query_iterator, typename index_iterator>
214 struct QGramFilter<device_tag, qgram_index_type, query_iterator, index_iterator>
215 {
217 
219 
220  typedef typename qgram_index_type::coord_type coord_type;
221 
227 
238  uint64 rank(
239  const qgram_index_type& qgram_index,
240  const uint32 n_queries,
241  const query_iterator queries,
242  const index_iterator indices);
243 
248  template <typename hits_iterator>
249  void locate(
250  const uint64 begin,
251  const uint64 end,
252  hits_iterator hits);
253 
264  template <typename hits_iterator, typename output_iterator>
265  void diagonals(
266  const uint32 n_hits,
267  const hits_iterator hits,
268  output_iterator diags,
269  const uint32 interval = 1);
270 
287  template <typename hits_iterator, typename output_iterator, typename count_iterator>
288  uint32 merge(
289  const uint32 interval,
290  const uint32 n_hits,
291  const hits_iterator hits,
292  output_iterator merged_hits,
293  count_iterator merged_counts);
294 
297  const uint2* ranges() const { return nvbio::plain_view( m_ranges ); }
298 
302  const uint64* ranks() const { return nvbio::plain_view( m_slots ); }
303 
305  query_iterator m_queries;
306  index_iterator m_indices;
309  thrust::device_vector<uint2> m_ranges;
310  thrust::device_vector<uint64> m_slots;
311  thrust::device_vector<diagonal_type> m_diags;
312  thrust::device_vector<uint8> d_temp_storage;
313 };
314 
315 template <typename qgram_index_type, typename query_iterator, typename index_iterator>
316 struct QGramFilterHost : public QGramFilter<host_tag, qgram_index_type, query_iterator, index_iterator> {};
317 
318 template <typename qgram_index_type, typename query_iterator, typename index_iterator>
319 struct QGramFilterDevice : public QGramFilter<device_tag, qgram_index_type, query_iterator, index_iterator> {};
320 
322 
323 } // namespace nvbio
324 
325 #include <nvbio/qgram/filter_inl.h>