NVBIO
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
sw_warp_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 
30 #include <nvbio/basic/cuda/arch.h>
31 #include <nvbio/basic/cuda/scan.h>
32 #include <nvbio/alignment/sink.h>
33 #include <nvbio/alignment/utils.h>
36 
37 
38 namespace nvbio {
39 namespace aln {
40 namespace priv {
41 
42 // warp-parallel version of classic smith-waterman
43 // aligns str (horizontal) to ref (vertical) using a warp
44 template <
47  typename scoring_type,
48  typename string_type,
49  typename qual_type,
50  typename ref_type,
51  typename column_type>
54  const scoring_type& scoring,
55  string_type str,
56  qual_type quals,
57  ref_type ref,
58  const int32 min_score,
59  uint2* sink,
60  column_type temp)
61 {
62 #if __CUDA_ARCH__ >= 350
63  typedef int32 score_type;
64  typedef alignment_result<score_type> alignment;
65 
66  const uint32 WARP_SIZE = 1u << cuda::Arch::LOG_WARP_SIZE;
68 
69  const uint32 M = str.length();
70  const uint32 N = ref.length();
71 
72  const score_type SCORE_GAP = scoring.deletion();
73  const score_type SCORE_INSERTION = scoring.insertion();
74 
75  // local scores
76  score_type h_top, h_left, h_diag, hi;
77 
78  // local maximum score
79  alignment best_alignment = alignment::minimum_value();
80 
81  // current reference string character
82  uint8 r_j;
83 
84  // per-thread cache for temp values and reference string characters
85  // each thread loads a different value; cache values are shuffled down the warp at each iteration
86  score_type temp_cache;
87  uint8 reference_cache;
88 
89  // width of the current warp-block stripe of the DP matrix (always WARP_SIZE except for the last stripe)
90  uint32 warp_block_width;
91 
92  // compute warp-block horizontal coordinate in DP matrix for this thread
93  const uint32 wi = warp_tid() + 1;
94 
95  // initialize the leftmost matrix column
96  for (uint32 i = warp_tid(); i <= N; i += WARP_SIZE)
97  temp[i] = (TYPE == GLOBAL ? SCORE_GAP * (i + 1) : 0);
98 
99  for (uint32 warp_block = 0; warp_block < M; warp_block += WARP_SIZE)
100  {
101  // width of this block
102  warp_block_width = (warp_block + WARP_SIZE >= M ? M % WARP_SIZE : WARP_SIZE);
103  // compute the horizontal coordinate of the current thread in the DP matrix (including border column)
104  const uint32 i = wi + warp_block;
105 
106  // set top boundary values
107  h_top = (TYPE != LOCAL ? SCORE_GAP * i : 0);
108  // initialize diagonal
109  h_diag = (TYPE != LOCAL ? SCORE_GAP * (i - 1) : 0);
110 
111  // load the query string character for the current thread
112  const uint8 s_i = (i <= M ? str[i - 1] : 0);
113  const uint8 q_i = (i <= M ? quals[i - 1] : 0);
114 
115  // loop over all DP anti-diagonals, excluding the border row/column
116  for (uint32 block_diag = 2; block_diag <= warp_block_width + N; block_diag += WARP_SIZE)
117  {
118  // reload caches every WARP_SIZE diagonals
119  temp_cache = (block_diag - 2) + warp_tid() < N ? temp[(block_diag - 2) + warp_tid()] : 0;
120  reference_cache = (block_diag - 2) + warp_tid() < N ? ref[(block_diag - 2) + warp_tid()] : 0;
121 
122  for (uint32 diag = block_diag; diag < block_diag + WARP_SIZE; diag++)
123  {
124  // compute the length of this anti-diagonal (excluding border row/column)
125  const uint32 diag_len = nvbio::min3(diag - 1, WARP_SIZE, warp_block_width);
126  // compute vertical coordinate of the current cell in the DP matrix (including border column)
127  const uint32 j = diag - wi;
128 
129  // is the current cell inside the DP matrix?
130  if (wi <= diag_len && j <= N)
131  {
132  if (wi == 1)
133  {
134  // load new temp and reference values
135  r_j = reference_cache;
136  // initialize cell to the left of the current cell
137  h_left = temp_cache;
138  }
139 
140  // compute the match/mismatch score
141  const score_type S_ij = (r_j == s_i) ? scoring.match(q_i) : scoring.mismatch(q_i);
142 
143  // determine the current cell score
144  hi = nvbio::max3(h_diag + S_ij,
145  h_top + SCORE_GAP,
146  h_left + SCORE_INSERTION);
147 
148  if (TYPE == LOCAL)
149  {
150  // clamp score to zero
151  hi = nvbio::max(hi, score_type(0));
152  }
153 
154  // save off the last column
155  if (wi == WARP_SIZE)
156  temp[j - 1] = hi;
157 
158  // save the best score across the entire matrix for local scoring
159  // save the best score across the last column for semi-global scoring
160  if ((TYPE == LOCAL) ||
161  (TYPE == SEMI_GLOBAL && i == M))
162  {
163  if (hi > best_alignment.score)
164  best_alignment = alignment(hi, make_uint2(j, i));
165  }
166 
167  // current left becomes diagonal for next iteration on this lane
168  h_diag = h_left;
169  // current value becomes h_top for next iteration on this lane
170  h_top = hi;
171  }
172 
173  // move previous cell reference value across the warp
174  r_j = __shfl_up(r_j, 1);
175  // hi becomes h_left on the next lane
176  h_left = __shfl_up(hi, 1);
177 
178  // push temp_cache and reference_cache values down the warp
179  temp_cache = __shfl_down(temp_cache, 1);
180  reference_cache = __shfl_down(reference_cache, 1);
181  }
182  }
183  }
184 
185  if (TYPE == LOCAL || TYPE == SEMI_GLOBAL)
186  {
187  // do a warp-wide max-scan to find the largest score (TODO: use a reduction instead)
188  __shared__ volatile alignment sm_red [WARP_SIZE * NUM_WARPS * 2];
189  volatile alignment *sm_warp_red = sm_red + WARP_SIZE * warp_id() * 2;
190  cuda::scan<32>(best_alignment, alignment::max_operator(), alignment::minimum_value(), sm_warp_red);
191  best_alignment = cuda::scan_total<32>(sm_warp_red);
192  }
193 
194  if (TYPE == GLOBAL)
195  {
196  best_alignment.score = __shfl(hi, warp_block_width - 1);
197  best_alignment.sink = make_uint2(N, M);
198  }
199 
200  *sink = best_alignment.sink;
201  return best_alignment.score;
202 #else
203  // unsupported on compute capability < 3.5
204  return 0;
205 #endif
206 }
207 
208 // private dispatcher for the warp-parallel version of classic smith-waterman
209 template <
212  typename scoring_type,
213  typename pattern_string,
214  typename qual_string,
215  typename text_string,
216  typename column_type>
220  const pattern_string pattern,
221  const qual_string quals,
222  const text_string text,
223  const int32 min_score,
224  uint2* sink,
226 {
227 #if defined(NVBIO_DEVICE_COMPILATION)
228  return sw_alignment_score<BLOCKDIM,TYPE>(
229  aligner.scheme,
230  pattern,
231  quals,
232  text,
233  min_score,
234  sink,
235  column );
236 #else
237  return Field_traits<int32>::min();
238 #endif
239 }
240 
241 } // namespace priv
242 } // namespace aln
243 } // namespace nvbio