NVBIO
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
packedstream_loader_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 #pragma once
29 
31 #include <nvbio/basic/types.h>
32 #include <nvbio/basic/numbers.h>
34 #include <nvbio/basic/cuda/arch.h>
35 #include <nvbio/basic/iterator.h>
36 
37 namespace nvbio {
38 
39 namespace priv {
40 
41 // copy the support of a packed string in local memory
42 template <typename StreamType,
43  typename SymbolType,
44  uint32 SYMBOL_SIZE_T,
45  bool BIG_ENDIAN_T,
46  typename W>
48 typename PackedStream<const_cached_iterator<const W*>,SymbolType,SYMBOL_SIZE_T,BIG_ENDIAN_T>::iterator
51  const uint32 N,
52  W* lmem)
53 {
54  typedef typename std::iterator_traits<StreamType>::value_type word_type;
55 
56  const StreamType in_storage = in_stream.stream();
57 
58  const uint32 SYMBOLS_PER_WORD = (sizeof(word_type)*8) / SYMBOL_SIZE_T;
59  const uint32 storage_offset = in_stream.index();
60  const uint32 word_offset = storage_offset & (SYMBOLS_PER_WORD-1);
61  const uint32 begin_word = storage_offset / SYMBOLS_PER_WORD;
62  const uint32 end_word = (storage_offset + N + SYMBOLS_PER_WORD-1) / SYMBOLS_PER_WORD;
63  //NVBIO_CUDA_DEBUG_ASSERT( (end_word - begin_word) <= LMEM_STRING_WORDS, "make_local_string(): out of bounds!\n (%u, %u)\n", begin_word, end_word );
64 
65  for (uint32 word = begin_word; word < end_word; ++word)
66  lmem[ word - begin_word ] = in_storage[ word ];
67 
68  typedef PackedStream<const_cached_iterator<const W*>,uint8,SYMBOL_SIZE_T,BIG_ENDIAN_T> const_stream_type;
69  const_stream_type clmem_stream( lmem );
70 
71  return clmem_stream.begin() + word_offset;
72 }
73 
74 // copy the support of a window of a packed string in local memory
75 template <typename StreamType,
76  typename SymbolType,
77  uint32 SYMBOL_SIZE_T,
78  bool BIG_ENDIAN_T,
79  typename W>
81 typename PackedStream<const_cached_iterator<const W*>,SymbolType,SYMBOL_SIZE_T,BIG_ENDIAN_T>::iterator
84  const uint32 N,
85  const uint2 substring_range,
86  const uint32 rev_flag,
87  W* lmem)
88 {
89  typedef typename std::iterator_traits<StreamType>::value_type word_type;
90 
91  const StreamType in_storage = in_stream.stream();
92 
93  const uint32 SYMBOLS_PER_WORD = (sizeof(word_type)*8) / SYMBOL_SIZE_T;
94  const uint32 storage_offset = in_stream.index();
95  const uint32 word_offset = storage_offset & (SYMBOLS_PER_WORD-1);
96  const uint32 base_word = storage_offset / SYMBOLS_PER_WORD;
97  const uint32 begin_word = (storage_offset + (rev_flag ? N - substring_range.y : substring_range.x)) / SYMBOLS_PER_WORD;
98  const uint32 end_word = (storage_offset + (rev_flag ? N - substring_range.x : substring_range.y) + SYMBOLS_PER_WORD-1) / SYMBOLS_PER_WORD;
99  //NVBIO_CUDA_DEBUG_ASSERT( (begin_word - base_word) < LMEM_STRING_WORDS && (end_word - base_word) <= LMEM_STRING_WORDS, "make_local_string(): out of bounds!\n (%u, %u)\n", begin_word, end_word );
100 
101  for (uint32 word = begin_word; word < end_word; ++word)
102  lmem[ word - base_word ] = in_storage[ word ];
103 
104  typedef PackedStream<const_cached_iterator<const W*>,uint8,SYMBOL_SIZE_T,BIG_ENDIAN_T> const_stream_type;
105  const_stream_type clmem_stream( lmem );
106 
107  return clmem_stream.begin() + word_offset;
108 }
109 
110 } // namespace priv
111 
112 //
113 // A utility wrapper to cache a packed-string into a local memory buffer and present a wrapper
114 // string iterator.
115 //
116 template <typename StorageIterator, uint32 SYMBOL_SIZE_T, bool BIG_ENDIAN_T, uint32 CACHE_SIZE>
118 typename PackedStringLoader<StorageIterator,SYMBOL_SIZE_T,BIG_ENDIAN_T,lmem_cache_tag<CACHE_SIZE> >::iterator
120 {
121  return priv::make_local_string( stream, length, lmem );
122 }
123 
124 template <typename StorageIterator, uint32 SYMBOL_SIZE_T, bool BIG_ENDIAN_T, uint32 CACHE_SIZE>
128  const input_stream stream,
129  const uint32 length,
130  const uint2 substring_range,
131  const uint32 rev_flag)
132 {
133  return priv::make_local_string( stream, length, substring_range, rev_flag, lmem );
134 }
135 
136 //
137 // A utility wrapper to cache a packed-string into a local memory buffer and present a wrapper
138 // string iterator.
139 //
140 template <typename StorageIterator, uint32 SYMBOL_SIZE_T, bool BIG_ENDIAN_T>
144 {
145  return stream;
146 }
147 
148 template <typename StorageIterator, uint32 SYMBOL_SIZE_T, bool BIG_ENDIAN_T>
152  const input_stream stream,
153  const uint32 length,
154  const uint2 substring_range,
155  const uint32 rev_flag)
156 {
157  return stream;
158 }
159 
160 } // namespace nvbio