wavelet_test.cu 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. // wavelet_test.cu
  28. //
  29. #include <nvbio/basic/timer.h>
  30. #include <nvbio/basic/console.h>
  31. #include <nvbio/basic/packedstream.h>
  32. #include <nvbio/strings/wavelet_tree.h>
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. namespace nvbio {
  36. template <typename BitStreamIterator, typename IndexIterator>
  37. struct text_functor
  38. {
  39. typedef uint32 argument_type;
  40. typedef uint8 result_type;
  41. // constructor
  42. NVBIO_HOST_DEVICE
  43. text_functor(
  44. WaveletTree<BitStreamIterator,IndexIterator> _tree) : tree(_tree) {}
  45. // unary operator
  46. NVBIO_HOST_DEVICE
  47. uint8 operator() (const uint32 i) const { return text( tree, i ); }
  48. WaveletTree<BitStreamIterator,IndexIterator> tree;
  49. };
  50. template <typename BitStreamIterator, typename IndexIterator>
  51. text_functor<BitStreamIterator,IndexIterator> make_text_functor(WaveletTree<BitStreamIterator,IndexIterator> _tree)
  52. {
  53. return text_functor<BitStreamIterator,IndexIterator>( _tree );
  54. }
  55. int wavelet_test(int argc, char* argv[])
  56. {
  57. uint32 text_len = 100000;
  58. for (int i = 0; i < argc; ++i)
  59. {
  60. if (strcmp( argv[i], "-length" ) == 0)
  61. text_len = atoi( argv[++i] )*1000;
  62. }
  63. try
  64. {
  65. log_info(stderr, "wavelet test... started\n");
  66. nvbio::vector<host_tag,uint8> h_text( text_len );
  67. h_text[0] = 41;
  68. h_text[1] = 59;
  69. h_text[2] = 59;
  70. h_text[3] = 41;
  71. h_text[4] = 59;
  72. for (uint32 i = 5; i < text_len; ++i)
  73. h_text[i] = (rand() & 255);
  74. nvbio::vector<device_tag,uint8> d_text( h_text );
  75. typedef WaveletTreeStorage<device_tag> wavelet_tree_type;
  76. // build a wavelet tree
  77. wavelet_tree_type wavelet_tree;
  78. // setup the wavelet tree
  79. setup( text_len, d_text.begin(), wavelet_tree );
  80. nvbio::vector<host_tag,uint32> occ( wavelet_tree.m_occ );
  81. // extract the text
  82. nvbio::transform<device_tag>(
  83. text_len,
  84. thrust::make_counting_iterator<uint32>(0),
  85. d_text.begin(),
  86. make_text_functor( plain_view(wavelet_tree) ) );
  87. // and copy it back to the host
  88. nvbio::vector<host_tag,uint8> h_extracted_text( d_text );
  89. for (uint32 i = 0; i < text_len; ++i)
  90. {
  91. const uint32 c = h_extracted_text[i];
  92. const uint32 r = h_text[i];
  93. if (c != r)
  94. {
  95. log_error(stderr, "error in text(%u): expected %u, got %u!\n", i, r, c);
  96. return 1;
  97. }
  98. }
  99. // do a quick ranking test against a bunch of known results
  100. const uint32 p[7] = { 0, 1, 2, 2, 3, 4, 5 };
  101. const uint32 c[7] = { 41, 59, 59, 41, 41, 59, 0 };
  102. const uint32 r[7] = { 1, 1, 2, 1, 2, 3, 0 };
  103. for (uint32 i = 0; i < 7; ++i)
  104. {
  105. const uint32 n = rank( plain_view(wavelet_tree), p[i], c[i] );
  106. if (n != r[i])
  107. {
  108. log_error(stderr, "error in rank(%u,%u): expected %u, got %u!\n", p[i], c[i], r[i], n);
  109. return 1;
  110. }
  111. }
  112. log_info(stderr, "wavelet test... done\n");
  113. }
  114. catch (...)
  115. {
  116. log_error(stderr, "error: unknown exception caught!\n");
  117. }
  118. return 0;
  119. }
  120. } // namespace nvbio