NVBIO
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
qgroup.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>
36 #include <thrust/host_vector.h>
37 #include <thrust/device_vector.h>
38 #include <thrust/scatter.h>
39 #include <thrust/for_each.h>
40 #include <thrust/iterator/constant_iterator.h>
41 
59 
60 namespace nvbio {
61 
64 
77 
80 template <typename index_iterator>
82 {
83  static const uint32 WORD_SIZE = 32;
84 
85  // class typedefs
86  typedef index_iterator vector_type;
87  typedef uint32 coord_type;
88 
89  // plain view typedefs
92 
93  // unary functor typedefs
95  typedef uint2 result_type;
96 
101  const uint32 _Q = 0,
102  const uint32 _symbol_size = 0,
103  const uint32 _n_qgrams = 0,
104  const uint32 _n_unique_qgrams = 0,
105  vector_type _I = NULL,
106  vector_type _S = NULL,
107  vector_type _SS = NULL,
108  vector_type _P = NULL) :
109  Q (_Q),
110  symbol_size (_symbol_size),
111  n_qgrams (_n_qgrams),
112  n_unique_qgrams (_n_unique_qgrams),
113  I (_I),
114  S (_S),
115  SS (_SS),
116  P (_P) {}
117 
121  uint2 range(const uint64 g) const
122  {
123  const uint32 i = uint32( g / WORD_SIZE );
124  const uint32 j = uint32( g % WORD_SIZE );
125 
126  // check whether the j-th bit of I[i] is set
127  if ((I[i] & (1u << j)) == 0u)
128  return make_uint2( 0u, 0u );
129 
130  // compute j' such that bit j is the j'-th set bit in I[i]
131  const uint32 j_prime = popc( I[i] & ((1u << j) - 1u) );
132 
133  return make_uint2(
134  SS[ S[i] + j_prime ],
135  SS[ S[i] + j_prime + 1u ] );
136  }
137 
141  uint2 operator() (const uint64 g) const { return range( g ); }
142 
146  uint32 locate(const uint32 i) const { return P[i]; }
147 
148 
157 };
158 
161 
165 {
166  static const uint32 WORD_SIZE = 32;
167 
169 
170  typedef thrust::host_vector<uint32> vector_type;
174 
178  {
179  return I.size() * sizeof(uint32) +
180  S.size() * sizeof(uint32) +
181  SS.size() * sizeof(uint32) +
182  P.size() * sizeof(uint32);
183  }
184 
187  uint64 used_device_memory() const { return 0u; }
188 
196 };
197 
201 {
202  static const uint32 WORD_SIZE = 32;
203 
205 
206  typedef thrust::device_vector<uint32> vector_type;
209 
220  template <typename string_type>
221  void build(
222  const uint32 q,
223  const uint32 symbol_sz,
224  const uint32 string_len,
225  const string_type string);
226 
229  uint64 used_host_memory() const { return 0u; }
230 
234  {
235  return I.size() * sizeof(uint32) +
236  S.size() * sizeof(uint32) +
237  SS.size() * sizeof(uint32) +
238  P.size() * sizeof(uint32);
239  }
240 
249 };
250 
253 inline
254 QGroupIndexView plain_view(const QGroupIndexView qgram) { return qgram; }
255 
258 inline
260 
263 inline
265 {
266  return QGroupIndexView(
267  qgroup.Q,
268  qgroup.symbol_size,
269  qgroup.n_qgrams,
270  qgroup.n_unique_qgrams,
271  nvbio::plain_view( qgroup.I ),
272  nvbio::plain_view( qgroup.S ),
273  nvbio::plain_view( qgroup.SS ),
274  nvbio::plain_view( qgroup.P ) );
275 }
276 
279 inline
281 {
282  return ConstQGroupIndexView(
283  qgroup.Q,
284  qgroup.symbol_size,
285  qgroup.n_qgrams,
286  qgroup.n_unique_qgrams,
287  nvbio::plain_view( qgroup.I ),
288  nvbio::plain_view( qgroup.S ),
289  nvbio::plain_view( qgroup.SS ),
290  nvbio::plain_view( qgroup.P ) );
291 }
292 
293 template<> struct plain_view_subtype<QGroupIndexHost> { typedef QGroupIndexView type; };
297 
300 
301 } // namespace nvbio
302 
303 #include <nvbio/qgram/qgroup_inl.h>