reduce.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. ///\file reduce.h
  29. ///
  30. #pragma once
  31. #include <nvBowtie/bowtie2/cuda/defs.h>
  32. #include <nvBowtie/bowtie2/cuda/scoring.h>
  33. #include <nvBowtie/bowtie2/cuda/params.h>
  34. namespace nvbio {
  35. namespace bowtie2 {
  36. namespace cuda {
  37. struct ParamsPOD;
  38. template <typename ScoringScheme> struct BaseScoringPipelineState;
  39. template <typename ScoringScheme> struct BestApproxScoringPipelineState;
  40. ///@addtogroup nvBowtie
  41. ///@{
  42. ///@addtogroup Reduce
  43. ///@{
  44. ///
  45. /// A context class for the score_reduce_kernel to be used in best-approx pipeline.
  46. ///
  47. /// \details
  48. /// Implements the basic extension bail-out mechanisms of Bowtie2, namely stopping
  49. /// when a given number of extensions of a read failed in a row.
  50. /// This is done keeping a vector of per-read extension 'trys' counters, which
  51. /// start from the maximum allowed number, and get decremented towards zero upon
  52. /// each failure, or reset upon successful extensions.
  53. ///
  54. struct ReduceBestApproxContext
  55. {
  56. /// constructor
  57. ///
  58. /// \param trys trys vector
  59. /// \param n_ext total number of extensions (i.e. extension loops) already performed
  60. ///
  61. ReduceBestApproxContext(uint32* trys, const uint32 n_ext) : m_trys( trys ), m_ext( n_ext ) {}
  62. /// this method is called from score_reduce_kernel to report updates to the best score.
  63. ///
  64. NVBIO_FORCEINLINE NVBIO_DEVICE
  65. void best_score(const uint32 read_id, const ParamsPOD& params) const
  66. {
  67. // reset the try counter
  68. m_trys[ read_id ] = params.max_effort;
  69. }
  70. /// this method is called from score_reduce_kernel to report updates to the second best score.
  71. ///
  72. NVBIO_FORCEINLINE NVBIO_DEVICE
  73. void second_score(const uint32 read_id, const ParamsPOD& params) const
  74. {
  75. // reset the try counter
  76. m_trys[ read_id ] = params.max_effort;
  77. }
  78. /// this method is called from score_reduce_kernel to report extension failures.
  79. ///
  80. NVBIO_FORCEINLINE NVBIO_DEVICE
  81. bool failure(const uint32 idx, const uint32 read_id, const uint32 top_flag, const ParamsPOD& params) const
  82. {
  83. if (m_trys[ read_id ] > 0)
  84. {
  85. if (((m_ext+idx >= params.min_ext) && (top_flag == 0) && (--m_trys[ read_id ] == 0)) || // bowtie2 does 1 more alignment than effort limit, we don't
  86. (m_ext+idx >= params.max_ext))
  87. return true;
  88. }
  89. return false;
  90. }
  91. private:
  92. uint32* m_trys;
  93. uint32 m_ext;
  94. };
  95. ///
  96. /// A context class for the score_reduce_kernel to be used in best-exact pipeline.
  97. ///
  98. /// \details
  99. /// A trivial implementation, that never bails out.
  100. ///
  101. struct ReduceBestExactContext
  102. {
  103. ReduceBestExactContext() {}
  104. /// this method is called from score_reduce_kernel to report updates to the best score.
  105. ///
  106. NVBIO_FORCEINLINE NVBIO_DEVICE
  107. void best_score(const uint32 read_id, const ParamsPOD& params) const {}
  108. /// this method is called from score_reduce_kernel to report updates to the second best score.
  109. ///
  110. NVBIO_FORCEINLINE NVBIO_DEVICE
  111. void second_score(const uint32 read_id, const ParamsPOD& params) const {}
  112. /// this method is called from score_reduce_kernel to report extension failures.
  113. ///
  114. NVBIO_FORCEINLINE NVBIO_DEVICE
  115. bool failure(const uint32 idx, const uint32 read_id, const uint32 top_flag, const ParamsPOD& params) const { return false; }
  116. };
  117. ///
  118. /// Reduce the scores associated to each read in the scoring queue to find the best 2 alignments.
  119. ///
  120. void score_reduce(
  121. const ReduceBestApproxContext context,
  122. const BestApproxScoringPipelineState<EditDistanceScoringScheme>& pipeline,
  123. const ParamsPOD& params);
  124. ///
  125. /// Reduce the scores associated to each read in the scoring queue to find the best 2 alignments.
  126. ///
  127. void score_reduce(
  128. const ReduceBestApproxContext context,
  129. const BestApproxScoringPipelineState<SmithWatermanScoringScheme<> >& pipeline,
  130. const ParamsPOD& params);
  131. ///
  132. /// Reduce the scores associated to each paired-end read in the scoring queue to find the best 2 alignments.
  133. ///
  134. void score_reduce_paired(
  135. const ReduceBestApproxContext context,
  136. const BestApproxScoringPipelineState<EditDistanceScoringScheme>& pipeline,
  137. const ParamsPOD& params);
  138. ///
  139. /// Reduce the scores associated to each paired-end read in the scoring queue to find the best 2 alignments.
  140. ///
  141. void score_reduce_paired(
  142. const ReduceBestApproxContext context,
  143. const BestApproxScoringPipelineState<SmithWatermanScoringScheme<> >& pipeline,
  144. const ParamsPOD& params);
  145. ///@} // group Reduce
  146. ///@} // group nvBowtie
  147. } // namespace cuda
  148. } // namespace bowtie2
  149. } // namespace nvbio
  150. #include <nvBowtie/bowtie2/cuda/reduce_inl.h>