NVBIO
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
prefetcher.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/basic/types.h>
34 #include <nvbio/strings/infix.h>
35 
36 
37 namespace nvbio {
38 
41 
52 
59 template <typename StringType, typename CacheTag>
61 {
62  typedef StringType input_string_type;
63  typedef StringType string_type;
64 
71  const string_type& load(const input_string_type& string) { return string; }
72 
80  const string_type& load(
81  const input_string_type& string,
82  const uint2 range) { return string; }
83 };
84 
93 template <typename StorageIterator, uint32 SYMBOL_SIZE_T, bool BIG_ENDIAN_T, uint32 CACHE_SIZE>
95  vector_view< PackedStream<StorageIterator,uint8,SYMBOL_SIZE_T,BIG_ENDIAN_T> >,
96  lmem_cache_tag<CACHE_SIZE> >
97 {
101 
109  {
110  return string_type(
111  string.size(),
112  loader.load( string.base(),
113  string.size() ) );
114  }
115 
124  const input_string_type& string,
125  const uint2 range)
126  {
127  return string_type(
128  string.size(),
129  loader.load( string.base(),
130  string.size(),
131  range,
132  false ) );
133  }
134 
136 };
137 
146 template <typename InfixCoordType, typename StorageIterator, uint32 SYMBOL_SIZE_T, bool BIG_ENDIAN_T, uint32 CACHE_SIZE>
148  Infix< PackedStream<StorageIterator,uint8,SYMBOL_SIZE_T,BIG_ENDIAN_T>,
149  InfixCoordType >,
150  lmem_cache_tag<CACHE_SIZE> >
151 {
153  InfixCoordType> input_string_type;
156 
164  {
165  return string_type(
166  string.size(),
167  loader.load( string.m_string + string.range().x,
168  string.size() ) );
169  }
170 
179  const input_string_type& string,
180  const uint2 range)
181  {
182  return string_type(
183  string.size(),
184  loader.load( string.m_string + string.range().x,
185  string.size(),
186  range,
187  false ) );
188  }
189 
191 };
192 
201 template <typename InfixCoordType, typename StorageIterator, uint32 SYMBOL_SIZE_T, bool BIG_ENDIAN_T, uint32 CACHE_SIZE>
203  Infix< vector_view< PackedStream<StorageIterator,uint8,SYMBOL_SIZE_T,BIG_ENDIAN_T> >,
204  InfixCoordType >,
205  lmem_cache_tag<CACHE_SIZE> >
206 {
208  InfixCoordType> input_string_type;
211 
219  {
220  return string_type(
221  string.size(),
222  loader.load( string.m_string.base() + string.range().x,
223  string.size() ) );
224  }
225 
234  const input_string_type& string,
235  const uint2 range)
236  {
237  return string_type(
238  string.size(),
239  loader.load( string.m_string.base() + string.range().x,
240  string.size(),
241  range,
242  false ) );
243  }
244 
246 };
247 
254 template <typename T, uint32 CACHE_SIZE>
256  vector_view<const T*>,
258 {
259  static const uint32 CACHE_BYTES = CACHE_SIZE * sizeof(uint32);
260  static const uint32 CACHE_ITEMS = CACHE_BYTES / sizeof(T);
261 
264 
271  string_type load(const input_string_type& string)
272  {
273  const uint32 L = string.size();
274 
275  // check whether the cache is too small
276  if (L > CACHE_ITEMS)
277  return string;
278 
279  for (uint32 i = 0; i < L; ++i)
280  cache[i] = string[i];
281 
282  return string_type( L, cache );
283  }
284 
292  string_type load(
293  const input_string_type& string,
294  const uint2 range)
295  {
296  // check whether the cache is too small, to prevent overflows
297  if (range.y - range.x > CACHE_ITEMS)
298  return string;
299 
300  for (uint32 i = range.x; i < range.y; ++i)
301  cache[i - range.x] = string[i];
302 
303  return string_type( string.size(), cache - range.x );
304  }
305 
306  T cache[CACHE_ITEMS];
307 };
308 
315 template <typename T, uint32 CACHE_SIZE>
317  vector_view<T*>,
319 {
320  static const uint32 CACHE_BYTES = CACHE_SIZE * sizeof(uint32);
321  static const uint32 CACHE_ITEMS = CACHE_BYTES / sizeof(T);
322 
325 
332  string_type load(const input_string_type& string)
333  {
334  const uint32 L = string.size();
335 
336  // check whether the cache is too small
337  if (L > CACHE_ITEMS)
338  return string_type( L, string.base() );
339 
340  for (uint32 i = 0; i < L; ++i)
341  cache[i] = string[i];
342 
343  return string_type( L, cache );
344  }
345 
353  string_type load(
354  const input_string_type& string,
355  const uint2 range)
356  {
357  // check whether the cache is too small, to prevent overflows
358  if (range.y - range.x > CACHE_ITEMS)
359  return string_type( string.size(), string.base() );
360 
361  for (uint32 i = range.x; i < range.y; ++i)
362  cache[i - range.x] = string[i];
363 
364  return string_type( string.size(), cache - range.x );
365  }
366 
367  T cache[CACHE_ITEMS];
368 };
369 
372 
373 } // namespace nvbio