proteinsw_qp.cu 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. // seeding.cu
  28. //
  29. #include <nvbio/basic/console.h>
  30. #include <nvbio/basic/timer.h>
  31. #include <nvbio/basic/vector.h>
  32. #include <nvbio/basic/cuda/ldg.h>
  33. #include <nvbio/strings/string.h>
  34. #include <nvbio/strings/alphabet.h>
  35. #include <nvbio/alignment/alignment.h>
  36. #include <nvbio/alignment/batched.h>
  37. #include <thrust/sequence.h>
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40. using namespace nvbio;
  41. int8 s_blosum62[] =
  42. {
  43. 4, 0, -2, -1, -2, 0, -2, -1, -1, -1, -1, -2, -4, -1, -1, -1, 1, 0, 0, -3, -2, -2, -1, 0,
  44. 0, 9, -3, -4, -2, -3, -3, -1, -3, -1, -1, -3, -4, -3, -3, -3, -1, -1, -1, -2, -2, -3, -3, -2,
  45. -2, -3, 6, 2, -3, -1, -1, -3, -1, -4, -3, 1, -4, -1, 0, -2, 0, -1, -3, -4, -3, 4, 1, -1,
  46. -1, -4, 2, 5, -3, -2, 0, -3, 1, -3, -2, 0, -4, -1, 2, 0, 0, -1, -2, -3, -2, 1, 4, -1,
  47. -2, -2, -3, -3, 6, -3, -1, 0, -3, 0, 0, -3, -4, -4, -3, -3, -2, -2, -1, 1, 3, -3, -3, -1,
  48. 0, -3, -1, -2, -3, 6, -2, -4, -2, -4, -3, 0, -4, -2, -2, -2, 0, -2, -3, -2, -3, -1, -2, -1,
  49. -2, -3, -1, 0, -1, -2, 8, -3, -1, -3, -2, 1, -4, -2, 0, 0, -1, -2, -3, -2, 2, 0, 0, -1,
  50. -1, -1, -3, -3, 0, -4, -3, 4, -3, 2, 1, -3, -4, -3, -3, -3, -2, -1, 3, -3, -1, -3, -3, -1,
  51. -1, -3, -1, 1, -3, -2, -1, -3, 5, -2, -1, 0, -4, -1, 1, 2, 0, -1, -2, -3, -2, 0, 1, -1,
  52. -1, -1, -4, -3, 0, -4, -3, 2, -2, 4, 2, -3, -4, -3, -2, -2, -2, -1, 1, -2, -1, -4, -3, -1,
  53. -1, -1, -3, -2, 0, -3, -2, 1, -1, 2, 5, -2, -4, -2, 0, -1, -1, -1, 1, -1, -1, -3, -1, -1,
  54. -2, -3, 1, 0, -3, 0, 1, -3, 0, -3, -2, 6, -4, -2, 0, 0, 1, 0, -3, -4, -2, 3, 0, -1,
  55. -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, 1, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4,
  56. -1, -3, -1, -1, -4, -2, -2, -3, -1, -3, -2, -2, -4, 7, -1, -2, -1, -1, -2, -4, -3, -2, -1, -2,
  57. -1, -3, 0, 2, -3, -2, 0, -3, 1, -2, 0, 0, -4, -1, 5, 1, 0, -1, -2, -2, -1, 0, 3, -1,
  58. -1, -3, -2, 0, -3, -2, 0, -3, 2, -2, -1, 0, -4, -2, 1, 5, -1, -1, -3, -3, -2, -1, 0, -1,
  59. 1, -1, 0, 0, -2, 0, -1, -2, 0, -2, -1, 1, -4, -1, 0, -1, 4, 1, -2, -3, -2, 0, 0, 0,
  60. 0, -1, -1, -1, -2, -2, -2, -1, -1, -1, -1, 0, -4, -1, -1, -1, 1, 5, 0, -2, -2, -1, -1, 0,
  61. 0, -1, -3, -2, -1, -3, -3, 3, -2, 1, 1, -3, -4, -2, -2, -3, -2, 0, 4, -3, -1, -3, -2, -1,
  62. -3, -2, -4, -3, 1, -2, -2, -3, -3, -2, -1, -4, -4, -4, -2, -3, -3, -2, -3, 11, 2, -4, -3, -2,
  63. -2, -2, -3, -2, 3, -3, 2, -1, -2, -1, -1, -2, -4, -3, -1, -2, -2, -2, -1, 2, 7, -3, -2, -1,
  64. -2, -3, 4, 1, -3, -1, 0, -3, 0, -4, -3, 3, -4, -2, 0, -1, 0, -1, -3, -4, -3, 4, 1, -1,
  65. -1, -3, 1, 4, -3, -2, 0, -3, 1, -3, -1, 0, -4, -1, 3, 0, 0, -1, -2, -3, -2, 1, 4, -1,
  66. 0, -2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -4, -2, -1, -1, 0, 0, -1, -2, -1, -1, -1, -1,
  67. };
  68. template <typename matrix_iterator>
  69. struct BlosumScheme
  70. {
  71. NVBIO_FORCEINLINE NVBIO_HOST_DEVICE BlosumScheme(const matrix_iterator m, const int32 gap_open, const int32 gap_ext) :
  72. m_matrix(m), m_gap_open(gap_open), m_gap_ext(gap_ext) {}
  73. NVBIO_FORCEINLINE NVBIO_HOST_DEVICE int32 match(const uint8 q = 0) const { return 11; };
  74. NVBIO_FORCEINLINE NVBIO_HOST_DEVICE int32 substitution(const uint32 r_i, const uint32 q_j, const uint8 r, const uint8 q, const uint8 qq = 0) const { return int8( m_matrix[ r + q*24 ] ); };
  75. NVBIO_FORCEINLINE NVBIO_HOST_DEVICE int32 pattern_gap_open() const { return m_gap_open; };
  76. NVBIO_FORCEINLINE NVBIO_HOST_DEVICE int32 pattern_gap_extension() const { return m_gap_ext; };
  77. NVBIO_FORCEINLINE NVBIO_HOST_DEVICE int32 text_gap_open() const { return m_gap_open; };
  78. NVBIO_FORCEINLINE NVBIO_HOST_DEVICE int32 text_gap_extension() const { return m_gap_ext; };
  79. const matrix_iterator m_matrix;
  80. const int32 m_gap_open;
  81. const int32 m_gap_ext;
  82. };
  83. template <typename matrix_iterator>
  84. struct QueryProfile
  85. {
  86. NVBIO_FORCEINLINE NVBIO_HOST_DEVICE QueryProfile(const matrix_iterator m, const int32 gap_open, const int32 gap_ext) :
  87. m_matrix(m), m_gap_open(gap_open), m_gap_ext(gap_ext) {}
  88. NVBIO_FORCEINLINE NVBIO_HOST_DEVICE int32 match(const uint8 q = 0) const { return 11; };
  89. NVBIO_FORCEINLINE NVBIO_HOST_DEVICE int32 substitution(const uint32 r_i, const uint32 q_j, const uint8 r, const uint8 q, const uint8 qq = 0) const { return int8( m_matrix[ r + q_j*24 ] ); };
  90. NVBIO_FORCEINLINE NVBIO_HOST_DEVICE int32 pattern_gap_open() const { return m_gap_open; };
  91. NVBIO_FORCEINLINE NVBIO_HOST_DEVICE int32 pattern_gap_extension() const { return m_gap_ext; };
  92. NVBIO_FORCEINLINE NVBIO_HOST_DEVICE int32 text_gap_open() const { return m_gap_open; };
  93. NVBIO_FORCEINLINE NVBIO_HOST_DEVICE int32 text_gap_extension() const { return m_gap_ext; };
  94. const matrix_iterator m_matrix;
  95. const int32 m_gap_open;
  96. const int32 m_gap_ext;
  97. };
  98. // main test entry point
  99. //
  100. int main(int argc, char* argv[])
  101. {
  102. log_info(stderr, "protein SW... started\n");
  103. const uint32 n_tests = 10;
  104. const uint32 n_strings = 50000;
  105. const uint32 P = 100;
  106. const uint32 T = 1000;
  107. // alloc a device vector for holding the BLOSUM62 scoring matrix
  108. nvbio::vector<device_tag,uint8> d_blosum62( 24*24 );
  109. // copy the matrix to the device
  110. thrust::copy( s_blosum62, s_blosum62 + 24*24, d_blosum62.begin() );
  111. // alloc the storage for the host strings
  112. nvbio::vector<host_tag,uint8> h_pattern_strings( P * n_strings );
  113. nvbio::vector<host_tag,uint8> h_pattern_profiles( P * n_strings * 24 );
  114. nvbio::vector<host_tag,uint8> h_text_strings( T * n_strings );
  115. // fill the strings with random characters
  116. LCG_random rand;
  117. for (uint32 i = 0; i < n_strings; ++i)
  118. {
  119. for (uint32 j = 0; j < P; ++j)
  120. {
  121. h_pattern_strings[i*P + j] = nvbio::min( uint8( rand.next() * 24.0f ), (uint8)23u );
  122. const uint8 q = h_pattern_strings[i*P + j];
  123. for (uint32 r = 0; r < 24; ++r)
  124. h_pattern_profiles[i*P + j*24 + r] = s_blosum62[ r + q*24 ];
  125. }
  126. }
  127. for (uint32 i = 0; i < T * n_strings; ++i)
  128. h_text_strings[i] = nvbio::min( uint8( rand.next() * 24.0f ), (uint8)23u );
  129. // copy to the device
  130. nvbio::vector<device_tag,uint8> d_pattern_strings( h_pattern_strings );
  131. nvbio::vector<device_tag,uint8> d_pattern_profiles( h_pattern_profiles );
  132. nvbio::vector<device_tag,uint8> d_text_strings( h_text_strings );
  133. nvbio::vector<device_tag,uint32> d_pattern_offsets( n_strings + 1 );
  134. nvbio::vector<device_tag,uint32> d_text_offsets( n_strings + 1 );
  135. // build the string offsets
  136. thrust::sequence( d_pattern_offsets.begin(), d_pattern_offsets.end(), 0u, P );
  137. thrust::sequence( d_text_offsets.begin(), d_text_offsets.end(), 0u, T );
  138. // prepare a vector of alignment sinks
  139. nvbio::vector< device_tag, aln::BestSink< uint32 > > sinks( n_strings );
  140. {
  141. const aln::SimpleGotohScheme scoring( 1, -1, -5, -3 );
  142. Timer timer;
  143. timer.start();
  144. for (uint32 i = 0; i < n_tests; ++i)
  145. {
  146. // and execute the batch alignment, on a GPU device
  147. aln::batch_alignment_score(
  148. aln::make_gotoh_aligner<aln::LOCAL,aln::TextBlockingTag>( scoring ),
  149. make_concatenated_string_set( n_strings, (const uint8*)raw_pointer( d_pattern_strings ), (const uint32*)raw_pointer( d_pattern_offsets ) ),
  150. make_concatenated_string_set( n_strings, (const uint8*)raw_pointer( d_text_strings ), (const uint32*)raw_pointer( d_text_offsets ) ),
  151. sinks.begin(),
  152. aln::DeviceThreadScheduler() );
  153. }
  154. cudaDeviceSynchronize();
  155. timer.stop();
  156. log_info(stderr, " GCUPS (Constant): %.1f\n", (1.0e-9f * float(P*T) * float(n_strings) * float(n_tests))/timer.seconds());
  157. }
  158. {
  159. const BlosumScheme< cuda::ldg_pointer<uint8> > scoring( cuda::make_ldg_pointer( raw_pointer( d_blosum62 ) ), -5, -3 );
  160. Timer timer;
  161. timer.start();
  162. for (uint32 i = 0; i < n_tests; ++i)
  163. {
  164. // and execute the batch alignment, on a GPU device
  165. aln::batch_alignment_score(
  166. aln::make_gotoh_aligner<aln::LOCAL,aln::TextBlockingTag>( scoring ),
  167. make_concatenated_string_set( n_strings, (const uint8*)raw_pointer( d_pattern_strings ), (const uint32*)raw_pointer( d_pattern_offsets ) ),
  168. make_concatenated_string_set( n_strings, (const uint8*)raw_pointer( d_text_strings ), (const uint32*)raw_pointer( d_text_offsets ) ),
  169. sinks.begin(),
  170. aln::DeviceThreadBlockScheduler<128,1>() );
  171. }
  172. cudaDeviceSynchronize();
  173. timer.stop();
  174. log_info(stderr, " GCUPS (Blosum62): %.1f\n", (1.0e-9f * float(P*T) * float(n_strings) * float(n_tests))/timer.seconds());
  175. }
  176. {
  177. //const QueryProfile< cuda::ldg_pointer<uint8> > scoring( cuda::make_ldg_pointer( raw_pointer( d_pattern_profiles ) ), -5, -3 );
  178. const QueryProfile<const uint8*> scoring( raw_pointer( d_pattern_profiles ), -5, -3 );
  179. Timer timer;
  180. timer.start();
  181. for (uint32 i = 0; i < n_tests; ++i)
  182. {
  183. // and execute the batch alignment, on a GPU device
  184. aln::batch_alignment_score(
  185. aln::make_gotoh_aligner<aln::LOCAL,aln::TextBlockingTag>( scoring ),
  186. make_concatenated_string_set( n_strings, thrust::make_constant_iterator<uint32>(0), (const uint32*)raw_pointer( d_pattern_offsets ) ),
  187. make_concatenated_string_set( n_strings, (const uint8*)raw_pointer( d_text_strings ), (const uint32*)raw_pointer( d_text_offsets ) ),
  188. sinks.begin(),
  189. aln::DeviceThreadBlockScheduler<128,1>() );
  190. }
  191. cudaDeviceSynchronize();
  192. timer.stop();
  193. log_info(stderr, " GCUPS (Blosum62): %.1f\n", (1.0e-9f * float(P*T) * float(n_strings) * float(n_tests))/timer.seconds());
  194. }
  195. log_info(stderr, "protein SW... done\n");
  196. return 0;
  197. }