nvmem.cu 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. #include <nvbio/basic/console.h>
  28. #include <nvbio/basic/shared_pointer.h>
  29. #include <nvbio/basic/exceptions.h>
  30. #include <nvbio/basic/cuda/arch.h> // cuda::check_error
  31. #include <nvbio/io/fmindex/fmindex.h>
  32. #include <nvbio/io/output/output_file.h>
  33. #include <nvbio/io/sequence/sequence.h>
  34. #ifdef _OPENMP
  35. #include <omp.h>
  36. #endif
  37. #include "options.h"
  38. #include "util.h"
  39. #include "pipeline.h"
  40. #include "mem-search.h"
  41. #include "build-chains.h"
  42. #include "filter-chains.h"
  43. #include "align.h"
  44. using namespace nvbio;
  45. int run(int argc, char **argv)
  46. {
  47. parse_command_line(argc, argv);
  48. gpu_init();
  49. #ifdef _OPENMP
  50. // now set the number of CPU threads
  51. omp_set_num_threads( omp_get_num_procs() );
  52. #pragma omp parallel
  53. {
  54. log_verbose(stderr, " running on multiple threads (%d)\n", omp_get_thread_num());
  55. }
  56. #endif
  57. pipeline_state pipeline;
  58. // start the pipeline on the device
  59. pipeline.system = DEVICE;
  60. // load the fmindex and prepare the SMEM search
  61. mem_init(&pipeline);
  62. // open the input read file
  63. SharedPointer<io::SequenceDataStream> input = SharedPointer<io::SequenceDataStream>(
  64. io::open_sequence_file(
  65. command_line_options.input_file_name,
  66. io::Phred33,
  67. uint32(-1),
  68. uint32(-1),
  69. io::SequenceEncoding(io::FORWARD | io::REVERSE_COMPLEMENT) ) );
  70. if (input == NULL || input->is_ok() == false)
  71. {
  72. log_error(stderr, "failed to open read file %s\n", command_line_options.input_file_name);
  73. exit(1);
  74. }
  75. // open the output file
  76. pipeline.output = io::OutputFile::open(command_line_options.output_file_name,
  77. io::SINGLE_END,
  78. io::BNT(*pipeline.mem.reference_data_host));
  79. if (!pipeline.output)
  80. {
  81. log_error(stderr, "failed to open output file %s\n", command_line_options.output_file_name);
  82. exit(1);
  83. }
  84. Timer global_timer;
  85. Timer timer;
  86. io::SequenceDataHost host_reads;
  87. // go!
  88. for(;;)
  89. {
  90. global_timer.start();
  91. timer.start();
  92. // read the next batch
  93. if (io::next( DNA_N, &host_reads, input.get(), command_line_options.batch_size, uint32(-1) ) == 0)
  94. break; // EOF
  95. log_info(stderr, "processing reads [%llu,%llu)\n", pipeline.stats.n_reads, pipeline.stats.n_reads + host_reads.size()/2);
  96. // copy batch to the device
  97. const io::SequenceDataDevice device_reads( host_reads );
  98. timer.stop();
  99. pipeline.stats.io_time += timer.seconds();
  100. // search for MEMs
  101. mem_search(&pipeline, &device_reads);
  102. // now start a loop where we break the read batch into smaller chunks for
  103. // which we can locate all MEMs and build all chains
  104. for (uint32 read_begin = 0; read_begin < host_reads.size(); read_begin = pipeline.chunk.read_end)
  105. {
  106. // determine the next chunk of reads to process
  107. fit_read_chunk(&pipeline, &device_reads, read_begin);
  108. log_verbose(stderr, "processing chunk\n");
  109. log_verbose(stderr, " reads : [%u,%u)\n", pipeline.chunk.read_begin, pipeline.chunk.read_end);
  110. log_verbose(stderr, " mems : [%u,%u)\n", pipeline.chunk.mem_begin, pipeline.chunk.mem_end);
  111. // locate all MEMs in the current chunk
  112. mem_locate(&pipeline, &device_reads);
  113. // build the chains
  114. build_chains(&pipeline, &device_reads);
  115. // filter the chains
  116. filter_chains(&pipeline, &device_reads);
  117. log_verbose(stderr, " chains: %u\n", pipeline.chn.n_chains);
  118. // initialize the alignment sub-pipeline
  119. align_init(&pipeline, &device_reads);
  120. // and loop until there's work to do
  121. while (align(&pipeline, &host_reads, &device_reads))
  122. {
  123. log_verbose(stderr, "\r active: %u", pipeline.aln.n_active);
  124. }
  125. log_verbose_cont(stderr, "\n");
  126. }
  127. global_timer.stop();
  128. pipeline.stats.n_reads += host_reads.size()/2;
  129. pipeline.stats.time += global_timer.seconds();
  130. log_stats(stderr, " time: %5.1fs (%.1f K reads/s)\n", pipeline.stats.time, 1.0e-3f * float(pipeline.stats.n_reads)/pipeline.stats.time);
  131. log_stats(stderr, " io : %6.2f %%\n", 100.0f * pipeline.stats.io_time/pipeline.stats.time);
  132. log_stats(stderr, " search : %6.2f %%\n", 100.0f * pipeline.stats.search_time/pipeline.stats.time);
  133. log_stats(stderr, " locate : %6.2f %%\n", 100.0f * pipeline.stats.locate_time/pipeline.stats.time);
  134. log_stats(stderr, " chain : %6.2f %%\n", 100.0f * pipeline.stats.chain_time/pipeline.stats.time);
  135. }
  136. pipeline.output->close();
  137. return 0;
  138. }
  139. int main(int argc, char **argv)
  140. {
  141. try
  142. {
  143. return run( argc, argv );
  144. }
  145. catch (nvbio::cuda_error e)
  146. {
  147. log_error(stderr, "caught a nvbio::cuda_error exception:\n");
  148. log_error(stderr, " %s\n", e.what());
  149. }
  150. catch (nvbio::bad_alloc e)
  151. {
  152. log_error(stderr, "caught a nvbio::bad_alloc exception:\n");
  153. log_error(stderr, " %s\n", e.what());
  154. }
  155. catch (nvbio::logic_error e)
  156. {
  157. log_error(stderr, "caught a nvbio::logic_error exception:\n");
  158. log_error(stderr, " %s\n", e.what());
  159. }
  160. catch (nvbio::runtime_error e)
  161. {
  162. log_error(stderr, "caught a nvbio::runtime_error exception:\n");
  163. log_error(stderr, " %s\n", e.what());
  164. }
  165. catch (std::bad_alloc e)
  166. {
  167. log_error(stderr, "caught a std::bad_alloc exception:\n");
  168. log_error(stderr, " %s\n", e.what());
  169. }
  170. catch (std::logic_error e)
  171. {
  172. log_error(stderr, "caught a std::logic_error exception:\n");
  173. log_error(stderr, " %s\n", e.what());
  174. }
  175. catch (std::runtime_error e)
  176. {
  177. log_error(stderr, "caught a std::runtime_error exception:\n");
  178. log_error(stderr, " %s\n", e.what());
  179. }
  180. catch (...)
  181. {
  182. log_error(stderr, "caught an unknown exception!\n");
  183. }
  184. return 0;
  185. }