nvBWT.cu 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  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. // nvBWT.cu
  28. //
  29. #define NVBIO_CUDA_DEBUG
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <string>
  34. #include <vector>
  35. #include <algorithm>
  36. #include <crc/crc.h>
  37. #include <nvbio/basic/console.h>
  38. #include <nvbio/basic/exceptions.h>
  39. #include <nvbio/basic/bnt.h>
  40. #include <nvbio/basic/numbers.h>
  41. #include <nvbio/basic/timer.h>
  42. #include <nvbio/basic/packedstream.h>
  43. #include <nvbio/basic/thrust_view.h>
  44. #include <nvbio/basic/dna.h>
  45. #include <nvbio/basic/exceptions.h>
  46. #include <nvbio/basic/cuda/arch.h>
  47. #include <nvbio/fmindex/bwt.h>
  48. #include <nvbio/fasta/fasta.h>
  49. #include <nvbio/io/fmindex/fmindex.h>
  50. #include <nvbio/sufsort/sufsort.h>
  51. #include "filelist.h"
  52. // PAC File Type
  53. enum PacType { BPAC = 0, WPAC = 1 };
  54. using namespace nvbio;
  55. unsigned char nst_nt4_table[256] = {
  56. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  57. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  58. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5 /*'-'*/, 4, 4,
  59. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  60. 4, 0, 4, 1, 4, 4, 4, 2, 4, 4, 4, 4, 4, 4, 4, 4,
  61. 4, 4, 4, 4, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  62. 4, 0, 4, 1, 4, 4, 4, 2, 4, 4, 4, 4, 4, 4, 4, 4,
  63. 4, 4, 4, 4, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  64. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  65. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  66. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  67. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  68. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  69. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  70. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  71. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4
  72. };
  73. #define RAND 0
  74. #define RAND48 1
  75. #if (GENERATOR == RAND) || ((GENERATOR == RAND48) && defined(WIN32))
  76. // generate random base pairs using rand()
  77. inline void srand_bp(const unsigned int s) { srand(s); }
  78. inline float frand() { return float(rand()) / float(RAND_MAX); }
  79. inline uint8 rand_bp() { return uint8( frand() * 4 ) & 3; }
  80. #elif (GENERATOR == RAND48)
  81. // generate random base pairs using rand48()
  82. inline void srand_bp(const unsigned int s) { srand48(s); }
  83. inline uint8 rand_bp() { return uint8( drand48() * 4 ) & 3; }
  84. #endif
  85. struct Counter
  86. {
  87. Counter() : m_size(0), m_reads(0) {}
  88. void begin_read() { m_reads++; }
  89. void end_read() {}
  90. void id(const uint8 c) {}
  91. void read(const uint8 c) { m_size++; }
  92. uint64 m_size;
  93. uint32 m_reads;
  94. };
  95. template <typename stream_type>
  96. struct Writer
  97. {
  98. Writer(stream_type stream, const uint32 reads, const uint64 max_size) :
  99. m_max_size(max_size), m_size(0), m_stream( stream )
  100. {
  101. m_bntseq.seed = 11;
  102. m_bntseq.anns_data.resize( reads );
  103. m_bntseq.anns_info.resize( reads );
  104. srand_bp( m_bntseq.seed );
  105. for (uint32 i = 0; i < 4; ++i)
  106. m_freq[i] = 0;
  107. }
  108. void begin_read()
  109. {
  110. BNTAnnData& ann_data = m_bntseq.anns_data[ m_bntseq.n_seqs ];
  111. ann_data.len = 0;
  112. ann_data.gi = 0;
  113. ann_data.offset = m_size;
  114. ann_data.n_ambs = 0;
  115. BNTAnnInfo& ann_info = m_bntseq.anns_info[ m_bntseq.n_seqs ];
  116. ann_info.anno = "null";
  117. m_lasts = 0;
  118. }
  119. void end_read()
  120. {
  121. m_bntseq.n_seqs++;
  122. }
  123. void id(const uint8 c)
  124. {
  125. m_bntseq.anns_info[ m_bntseq.n_seqs ].name.push_back(char(c));
  126. }
  127. void read(const uint8 s)
  128. {
  129. if (m_size < m_max_size)
  130. {
  131. const uint8 c = nst_nt4_table[s];
  132. const uint8 sc = c < 4 ? c : rand_bp();
  133. m_stream[ m_size ] = sc;
  134. // keep track of the symbol frequencies
  135. ++m_freq[sc];
  136. if (c >= 4) // we have an N
  137. {
  138. if (m_lasts == s) // contiguous N
  139. {
  140. // increment length of the last hole
  141. ++m_bntseq.ambs.back().len;
  142. }
  143. else
  144. {
  145. // beginning of a new hole
  146. BNTAmb amb;
  147. amb.len = 1;
  148. amb.offset = m_size;
  149. amb.amb = s;
  150. m_bntseq.ambs.push_back( amb );
  151. ++m_bntseq.anns_data[ m_bntseq.n_seqs ].n_ambs;
  152. ++m_bntseq.n_holes;
  153. }
  154. }
  155. // save last symbol
  156. m_lasts = s;
  157. // update sequence length
  158. BNTAnnData& ann_data = m_bntseq.anns_data[ m_bntseq.n_seqs ];
  159. ann_data.len++;
  160. }
  161. m_bntseq.l_pac++;
  162. m_size++;
  163. }
  164. uint64 m_max_size;
  165. uint64 m_size;
  166. stream_type m_stream;
  167. BNTSeq m_bntseq;
  168. uint8 m_lasts;
  169. uint32 m_freq[4];
  170. };
  171. template <typename StreamType>
  172. bool save_stream(FILE* output_file, const uint64 seq_words, const StreamType* stream)
  173. {
  174. for (uint64 words = 0; words < seq_words; words += 1024)
  175. {
  176. const uint32 n_words = (uint32)nvbio::min( uint64(1024u), uint64(seq_words - words) );
  177. if (fwrite( stream + words, sizeof(StreamType), n_words, output_file ) != n_words)
  178. return false;
  179. }
  180. return true;
  181. }
  182. //
  183. // .wpac file
  184. //
  185. void save_wpac(const uint32 seq_length, const uint32* string_storage, const char* pac_name)
  186. {
  187. log_info(stderr, "\nwriting \"%s\"... started\n", pac_name);
  188. const uint32 seq_words = util::divide_ri( seq_length, 16 );
  189. FILE* output_file = fopen( pac_name, "wb" );
  190. if (output_file == NULL)
  191. {
  192. log_error(stderr, " could not open output file \"%s\"!\n", pac_name );
  193. exit(1);
  194. }
  195. // write the sequence length as a uint64
  196. const uint64 len = seq_length;
  197. fwrite( &len, sizeof(len), 1u, output_file );
  198. // save the uint32 stream
  199. if (save_stream( output_file, seq_words, string_storage ) == false)
  200. {
  201. log_error(stderr, " writing failed!\n");
  202. exit(1);
  203. }
  204. fclose( output_file );
  205. log_info(stderr, "writing \"%s\"... done\n", pac_name);
  206. }
  207. //
  208. // .pac file
  209. //
  210. void save_bpac(const uint32 seq_length, const uint32* string_storage, const char* pac_name)
  211. {
  212. typedef PackedStream<const uint32*,uint8,2,true,int64> stream_type;
  213. typedef PackedStream< uint8*, uint8,2,true,int64> pac_stream_type;
  214. log_info(stderr, "\nwriting \"%s\"... started\n", pac_name);
  215. const uint32 bps_per_byte = 4u;
  216. const uint64 seq_bytes = (seq_length + bps_per_byte - 1u) / bps_per_byte;
  217. FILE* output_file = fopen( pac_name, "wb" );
  218. if (output_file == NULL)
  219. {
  220. log_error(stderr, " could not open output file \"%s\"!\n", pac_name );
  221. exit(1);
  222. }
  223. // copy the uint32 packed stream into a uint8 pac stream
  224. thrust::host_vector<uint8> pac_storage( seq_bytes );
  225. pac_stream_type pac_string( nvbio::plain_view( pac_storage ) );
  226. stream_type string( string_storage );
  227. for (uint32 i = 0; i < seq_length; ++i)
  228. pac_string[i] = string[i];
  229. // save the uint8 stream
  230. if (save_stream( output_file, seq_bytes, nvbio::raw_pointer( pac_storage ) ) == false)
  231. {
  232. log_error(stderr, " writing failed!\n");
  233. exit(1);
  234. }
  235. // the following code makes the pac file size always (l_pac/4+1+1)
  236. if (seq_length % 4 == 0)
  237. {
  238. const uint8 ct = 0;
  239. fwrite( &ct, 1, 1, output_file );
  240. }
  241. {
  242. const uint8 ct = seq_length % 4;
  243. fwrite( &ct, 1, 1, output_file );
  244. }
  245. fclose( output_file );
  246. log_info(stderr, "writing \"%s\"... done\n", pac_name);
  247. }
  248. //
  249. // .pac | .wpac file
  250. //
  251. void save_pac(const uint32 seq_length, const uint32* string_storage, const char* pac_name, const PacType pac_type)
  252. {
  253. if (pac_type == BPAC)
  254. save_bpac( seq_length, string_storage, pac_name );
  255. else
  256. save_wpac( seq_length, string_storage, pac_name );
  257. }
  258. //
  259. // .bwt file
  260. //
  261. void save_bwt(const uint32 seq_length, const uint32 seq_words, const uint32 primary, const uint32* cumFreq, const uint32* h_bwt_storage, const char* bwt_name)
  262. {
  263. log_info(stderr, "\nwriting \"%s\"... started\n", bwt_name);
  264. FILE* output_file = fopen( bwt_name, "wb" );
  265. if (output_file == NULL)
  266. {
  267. log_error(stderr, " could not open output file \"%s\"!\n", bwt_name );
  268. exit(1);
  269. }
  270. fwrite( &primary, sizeof(uint32), 1, output_file );
  271. fwrite( cumFreq, sizeof(uint32), 4, output_file );
  272. if (save_stream( output_file, seq_words, h_bwt_storage ) == false)
  273. {
  274. log_error(stderr, " writing failed!\n");
  275. exit(1);
  276. }
  277. fclose( output_file );
  278. log_info(stderr, "writing \"%s\"... done\n", bwt_name);
  279. }
  280. //
  281. // .sa file
  282. //
  283. void save_ssa(const uint32 seq_length, const uint32 sa_intv, const uint32 ssa_len, const uint32 primary, const uint32* cumFreq, const uint32* h_ssa, const char* sa_name)
  284. {
  285. log_info(stderr, "\nwriting \"%s\"... started\n", sa_name);
  286. FILE* output_file = fopen( sa_name, "wb" );
  287. if (output_file == NULL)
  288. {
  289. log_error(stderr, " could not open output file \"%s\"!\n", sa_name );
  290. exit(1);
  291. }
  292. fwrite( &primary, sizeof(uint32), 1u, output_file );
  293. fwrite( &cumFreq, sizeof(uint32), 4u, output_file );
  294. fwrite( &sa_intv, sizeof(uint32), 1u, output_file );
  295. fwrite( &seq_length, sizeof(uint32), 1u, output_file );
  296. fwrite( &h_ssa[1], sizeof(uint32), ssa_len-1, output_file );
  297. fclose( output_file );
  298. log_info(stderr, "writing \"%s\"... done\n", sa_name);
  299. }
  300. int build(
  301. const char* input_name,
  302. const char* output_name,
  303. const char* pac_name,
  304. const char* rpac_name,
  305. const char* bwt_name,
  306. const char* rbwt_name,
  307. const char* sa_name,
  308. const char* rsa_name,
  309. const uint64 max_length,
  310. const PacType pac_type,
  311. const bool compute_crc)
  312. {
  313. std::vector<std::string> sortednames;
  314. list_files(input_name, sortednames);
  315. uint32 n_inputs = (uint32)sortednames.size();
  316. log_info(stderr, "\ncounting bps... started\n");
  317. // count entire sequence length
  318. Counter counter;
  319. for (uint32 i = 0; i < n_inputs; ++i)
  320. {
  321. log_info(stderr, " counting \"%s\"\n", sortednames[i].c_str());
  322. FASTA_inc_reader fasta( sortednames[i].c_str() );
  323. if (fasta.valid() == false)
  324. {
  325. log_error(stderr, " unable to open file\n");
  326. exit(1);
  327. }
  328. while (fasta.read( 1024, counter ) == 1024);
  329. }
  330. log_info(stderr, "counting bps... done\n");
  331. const uint64 seq_length = nvbio::min( (uint64)counter.m_size, (uint64)max_length );
  332. const uint32 bps_per_word = sizeof(uint32)*4u;
  333. const uint64 seq_words = (seq_length + bps_per_word - 1u) / bps_per_word;
  334. log_info(stderr, "\nstats:\n");
  335. log_info(stderr, " reads : %u\n", counter.m_reads );
  336. log_info(stderr, " sequence length : %llu bps (%.1f MB)\n",
  337. seq_length,
  338. float(seq_words*sizeof(uint32))/float(1024*1024));
  339. log_info(stderr, " buffer size : %.1f MB\n",
  340. 2*seq_words*sizeof(uint32)/1.0e6f );
  341. const uint32 sa_intv = nvbio::io::FMIndexData::SA_INT;
  342. const uint32 ssa_len = (seq_length + sa_intv) / sa_intv;
  343. // allocate the actual storage
  344. thrust::host_vector<uint32> h_string_storage( seq_words+1 );
  345. thrust::host_vector<uint32> h_bwt_storage( seq_words+1 );
  346. thrust::host_vector<uint32> h_ssa( ssa_len );
  347. typedef PackedStream<const uint32*,uint8,io::FMIndexData::BWT_BITS,io::FMIndexData::BWT_BIG_ENDIAN> const_stream_type;
  348. typedef PackedStream< uint32*,uint8,io::FMIndexData::BWT_BITS,io::FMIndexData::BWT_BIG_ENDIAN> stream_type;
  349. stream_type h_string( nvbio::plain_view( h_string_storage ) );
  350. uint32 cumFreq[4] = { 0, 0, 0, 0 };
  351. log_info(stderr, "\nbuffering bps... started\n");
  352. // read all files
  353. {
  354. Writer<stream_type> writer( h_string, counter.m_reads, seq_length );
  355. for (uint32 i = 0; i < n_inputs; ++i)
  356. {
  357. log_info(stderr, " buffering \"%s\"\n", sortednames[i].c_str());
  358. FASTA_inc_reader fasta( sortednames[i].c_str() );
  359. if (fasta.valid() == false)
  360. {
  361. log_error(stderr, " unable to open file!\n");
  362. exit(1);
  363. }
  364. while (fasta.read( 1024, writer ) == 1024);
  365. }
  366. save_bns( writer.m_bntseq, output_name );
  367. // compute the cumulative symbol frequencies
  368. cumFreq[0] = writer.m_freq[0];
  369. cumFreq[1] = writer.m_freq[1] + cumFreq[0];
  370. cumFreq[2] = writer.m_freq[2] + cumFreq[1];
  371. cumFreq[3] = writer.m_freq[3] + cumFreq[2];
  372. if (cumFreq[3] != seq_length)
  373. {
  374. log_error(stderr, " mismatching symbol frequencies!\n");
  375. log_error(stderr, " (%u, %u, %u, %u)\n", cumFreq[0], cumFreq[1], cumFreq[2], cumFreq[3]);
  376. exit(1);
  377. }
  378. }
  379. log_info(stderr, "buffering bps... done\n");
  380. if (compute_crc)
  381. {
  382. const uint32 crc = crcCalc( h_string, uint32(seq_length) );
  383. log_info(stderr, " crc: %u\n", crc);
  384. }
  385. try
  386. {
  387. BWTParams params;
  388. uint32 primary;
  389. thrust::device_vector<uint32> d_string_storage( h_string_storage );
  390. thrust::device_vector<uint32> d_bwt_storage( seq_words+1 );
  391. const_stream_type d_string( nvbio::plain_view( d_string_storage ) );
  392. stream_type d_bwt( nvbio::plain_view( d_bwt_storage ) );
  393. Timer timer;
  394. log_info(stderr, "\nbuilding forward BWT... started\n");
  395. timer.start();
  396. {
  397. StringBWTSSAHandler<const_stream_type,stream_type,uint32*> output(
  398. seq_length, // string length
  399. d_string, // string
  400. sa_intv, // SSA sampling interval
  401. d_bwt, // output bwt iterator
  402. nvbio::plain_view( h_ssa ) ); // output ssa iterator
  403. cuda::blockwise_suffix_sort(
  404. seq_length,
  405. d_string,
  406. output,
  407. &params );
  408. // remove the dollar symbol
  409. output.remove_dollar();
  410. primary = output.primary();
  411. }
  412. timer.stop();
  413. log_info(stderr, "building forward BWT... done: %um:%us\n", uint32(timer.seconds()/60), uint32(timer.seconds())%60);
  414. log_info(stderr, " primary: %u\n", primary);
  415. // save everything to disk
  416. {
  417. // copy to the host
  418. thrust::copy( d_bwt_storage.begin(),
  419. d_bwt_storage.begin() + seq_words,
  420. h_bwt_storage.begin() );
  421. if (compute_crc)
  422. {
  423. const_stream_type h_bwt( nvbio::plain_view( h_bwt_storage ) );
  424. const uint32 crc = crcCalc( h_bwt, uint32(seq_length) );
  425. log_info(stderr, " crc: %u\n", crc);
  426. }
  427. save_pac( seq_length, nvbio::plain_view( h_string_storage ), pac_name, pac_type );
  428. save_bwt( seq_length, seq_words, primary, cumFreq, nvbio::plain_view( h_bwt_storage ), bwt_name );
  429. save_ssa( seq_length, sa_intv, ssa_len, primary, cumFreq, nvbio::plain_view( h_ssa ), sa_name );
  430. }
  431. // reverse the string in h_string_storage
  432. {
  433. // reuse the bwt storage to build the reverse
  434. uint32* h_rbase_stream = nvbio::plain_view( h_bwt_storage );
  435. stream_type h_rstring( h_rbase_stream );
  436. // reverse the string
  437. for (uint32 i = 0; i < seq_length; ++i)
  438. h_rstring[i] = h_string[ seq_length - i - 1u ];
  439. // and now swap the vectors
  440. h_bwt_storage.swap( h_string_storage );
  441. h_string = stream_type( nvbio::plain_view( h_string_storage ) );
  442. // and copy back the new string to the device
  443. d_string_storage = h_string_storage;
  444. }
  445. log_info(stderr, "\nbuilding reverse BWT... started\n");
  446. timer.start();
  447. {
  448. StringBWTSSAHandler<const_stream_type,stream_type,uint32*> output(
  449. seq_length, // string length
  450. d_string, // string
  451. sa_intv, // SSA sampling interval
  452. d_bwt, // output bwt iterator
  453. nvbio::plain_view( h_ssa ) ); // output ssa iterator
  454. cuda::blockwise_suffix_sort(
  455. seq_length,
  456. d_string,
  457. output,
  458. &params );
  459. // remove the dollar symbol
  460. output.remove_dollar();
  461. primary = output.primary();
  462. }
  463. timer.stop();
  464. log_info(stderr, "building reverse BWT... done: %um:%us\n", uint32(timer.seconds()/60), uint32(timer.seconds())%60);
  465. log_info(stderr, " primary: %u\n", primary);
  466. // save everything to disk
  467. {
  468. // copy to the host
  469. thrust::copy( d_bwt_storage.begin(),
  470. d_bwt_storage.begin() + seq_words,
  471. h_bwt_storage.begin() );
  472. if (compute_crc)
  473. {
  474. const_stream_type h_bwt( nvbio::plain_view( h_bwt_storage ) );
  475. const uint32 crc = crcCalc( h_bwt, uint32(seq_length) );
  476. log_info(stderr, " crc: %u\n", crc);
  477. }
  478. save_pac( seq_length, nvbio::plain_view( h_string_storage ), rpac_name, pac_type );
  479. save_bwt( seq_length, seq_words, primary, cumFreq, nvbio::plain_view( h_bwt_storage ), rbwt_name );
  480. save_ssa( seq_length, sa_intv, ssa_len, primary, cumFreq, nvbio::plain_view( h_ssa ), rsa_name );
  481. }
  482. }
  483. catch (nvbio::cuda_error e)
  484. {
  485. log_error(stderr, "caught a nvbio::cuda_error exception:\n");
  486. log_error(stderr, " %s\n", e.what());
  487. }
  488. catch (nvbio::bad_alloc e)
  489. {
  490. log_error(stderr, "caught a nvbio::bad_alloc exception:\n");
  491. log_error(stderr, " %s\n", e.what());
  492. }
  493. catch (nvbio::logic_error e)
  494. {
  495. log_error(stderr, "caught a nvbio::logic_error exception:\n");
  496. log_error(stderr, " %s\n", e.what());
  497. }
  498. catch (nvbio::runtime_error e)
  499. {
  500. log_error(stderr, "caught a nvbio::runtime_error exception:\n");
  501. log_error(stderr, " %s\n", e.what());
  502. }
  503. catch (std::bad_alloc e)
  504. {
  505. log_error(stderr, "caught a std::bad_alloc exception:\n");
  506. log_error(stderr, " %s\n", e.what());
  507. }
  508. catch (std::logic_error e)
  509. {
  510. log_error(stderr, "caught a std::logic_error exception:\n");
  511. log_error(stderr, " %s\n", e.what());
  512. }
  513. catch (std::runtime_error e)
  514. {
  515. log_error(stderr, "caught a std::runtime_error exception:\n");
  516. log_error(stderr, " %s\n", e.what());
  517. }
  518. catch (...)
  519. {
  520. log_error(stderr,"unknown exception caught!\n");
  521. exit(1);
  522. }
  523. return 0;
  524. }
  525. int main(int argc, char* argv[])
  526. {
  527. crcInit();
  528. if (argc < 2)
  529. {
  530. log_info(stderr, "please specify input and output file names, e.g:\n");
  531. log_info(stderr, " nvBWT [options] myinput.*.fa output-prefix\n");
  532. log_info(stderr, " options:\n");
  533. log_info(stderr, " -v | --verbosity select verbosity\n");
  534. log_info(stderr, " -m | --max-length clamp input to max_length\n");
  535. log_info(stderr, " -b | --byte-packing output byte packed .pac\n");
  536. log_info(stderr, " -w | --word-packing output word packed .wpac\n");
  537. log_info(stderr, " -c | --crc compute crcs\n");
  538. log_info(stderr, " -d | --device cuda device\n");
  539. exit(0);
  540. }
  541. const char* file_names[2] = { NULL, NULL };
  542. uint64 max_length = uint64(-1);
  543. PacType pac_type = BPAC;
  544. bool crc = false;
  545. int cuda_device = -1;
  546. uint32 n_files = 0;
  547. for (int32 i = 1; i < argc; ++i)
  548. {
  549. const char* arg = argv[i];
  550. if ((strcmp( arg, "-m" ) == 0) ||
  551. (strcmp( arg, "--max-length" ) == 0))
  552. {
  553. max_length = atoi( argv[++i] );
  554. }
  555. else if ((strcmp( argv[i], "-v" ) == 0) ||
  556. (strcmp( argv[i], "-verbosity" ) == 0) ||
  557. (strcmp( argv[i], "--verbosity" ) == 0))
  558. {
  559. set_verbosity( Verbosity( atoi( argv[++i] ) ) );
  560. }
  561. else if ((strcmp( arg, "-b" ) == 0) ||
  562. (strcmp( arg, "--byte-packing" ) == 0))
  563. {
  564. pac_type = BPAC;
  565. }
  566. else if ((strcmp( arg, "-w" ) == 0) ||
  567. (strcmp( arg, "--word-packing" ) == 0))
  568. {
  569. pac_type = WPAC;
  570. }
  571. else if ((strcmp( arg, "-c" ) == 0) ||
  572. (strcmp( arg, "--crc" ) == 0))
  573. {
  574. crc = true;
  575. }
  576. else if ((strcmp( arg, "-d" ) == 0) ||
  577. (strcmp( arg, "--device" ) == 0))
  578. {
  579. cuda_device = atoi( argv[++i] );
  580. }
  581. else
  582. file_names[ n_files++ ] = argv[i];
  583. }
  584. const char* input_name = file_names[0];
  585. const char* output_name = file_names[1];
  586. std::string pac_string = std::string( output_name ) + (pac_type == BPAC ? ".pac" : ".wpac");
  587. const char* pac_name = pac_string.c_str();
  588. std::string rpac_string = std::string( output_name ) + (pac_type == BPAC ? ".rpac" : ".rwpac");
  589. const char* rpac_name = rpac_string.c_str();
  590. std::string bwt_string = std::string( output_name ) + ".bwt";
  591. const char* bwt_name = bwt_string.c_str();
  592. std::string rbwt_string = std::string( output_name ) + ".rbwt";
  593. const char* rbwt_name = rbwt_string.c_str();
  594. std::string sa_string = std::string( output_name ) + ".sa";
  595. const char* sa_name = sa_string.c_str();
  596. std::string rsa_string = std::string( output_name ) + ".rsa";
  597. const char* rsa_name = rsa_string.c_str();
  598. log_info(stderr, "max length : %lld\n", max_length);
  599. log_info(stderr, "input : \"%s\"\n", input_name);
  600. log_info(stderr, "output : \"%s\"\n", output_name);
  601. try
  602. {
  603. int device_count;
  604. cudaGetDeviceCount(&device_count);
  605. cuda::check_error("cuda-check");
  606. log_verbose(stderr, " cuda devices : %d\n", device_count);
  607. // inspect and select cuda devices
  608. if (device_count)
  609. {
  610. if (cuda_device == -1)
  611. {
  612. int best_device = 0;
  613. cudaDeviceProp best_device_prop;
  614. cudaGetDeviceProperties( &best_device_prop, best_device );
  615. for (int device = 0; device < device_count; ++device)
  616. {
  617. cudaDeviceProp device_prop;
  618. cudaGetDeviceProperties( &device_prop, device );
  619. log_verbose(stderr, " device %d has compute capability %d.%d\n", device, device_prop.major, device_prop.minor);
  620. log_verbose(stderr, " SM count : %u\n", device_prop.multiProcessorCount);
  621. log_verbose(stderr, " SM clock rate : %u Mhz\n", device_prop.clockRate / 1000);
  622. log_verbose(stderr, " memory clock rate : %.1f Ghz\n", float(device_prop.memoryClockRate) * 1.0e-6f);
  623. if (device_prop.major >= best_device_prop.major &&
  624. device_prop.minor >= best_device_prop.minor)
  625. {
  626. best_device_prop = device_prop;
  627. best_device = device;
  628. }
  629. }
  630. cuda_device = best_device;
  631. }
  632. log_verbose(stderr, " chosen device %d\n", cuda_device);
  633. {
  634. cudaDeviceProp device_prop;
  635. cudaGetDeviceProperties( &device_prop, cuda_device );
  636. log_verbose(stderr, " device name : %s\n", device_prop.name);
  637. log_verbose(stderr, " compute capability : %d.%d\n", device_prop.major, device_prop.minor);
  638. }
  639. cudaSetDevice( cuda_device );
  640. }
  641. size_t free, total;
  642. cudaMemGetInfo(&free, &total);
  643. NVBIO_CUDA_DEBUG_STATEMENT( log_info(stderr,"device mem : total: %.1f GB, free: %.1f GB\n", float(total)/float(1024*1024*1024), float(free)/float(1024*1024*1024)) );
  644. cuda::check_error("cuda-memory-check");
  645. return build( input_name, output_name, pac_name, rpac_name, bwt_name, rbwt_name, sa_name, rsa_name, max_length, pac_type, crc );
  646. }
  647. catch (nvbio::cuda_error e)
  648. {
  649. log_error(stderr, "caught a nvbio::cuda_error exception:\n");
  650. log_error(stderr, " %s\n", e.what());
  651. return 1;
  652. }
  653. catch (nvbio::bad_alloc e)
  654. {
  655. log_error(stderr, "caught a nvbio::bad_alloc exception:\n");
  656. log_error(stderr, " %s\n", e.what());
  657. return 1;
  658. }
  659. catch (nvbio::logic_error e)
  660. {
  661. log_error(stderr, "caught a nvbio::logic_error exception:\n");
  662. log_error(stderr, " %s\n", e.what());
  663. return 1;
  664. }
  665. catch (nvbio::runtime_error e)
  666. {
  667. log_error(stderr, "caught a nvbio::runtime_error exception:\n");
  668. log_error(stderr, " %s\n", e.what());
  669. return 1;
  670. }
  671. catch (thrust::system::system_error e)
  672. {
  673. log_error(stderr, "caught a thrust::system_error exception:\n");
  674. log_error(stderr, " %s\n", e.what());
  675. return 1;
  676. }
  677. catch (std::bad_alloc e)
  678. {
  679. log_error(stderr, "caught a std::bad_alloc exception:\n");
  680. log_error(stderr, " %s\n", e.what());
  681. return 1;
  682. }
  683. catch (std::logic_error e)
  684. {
  685. log_error(stderr, "caught a std::logic_error exception:\n");
  686. log_error(stderr, " %s\n", e.what());
  687. return 1;
  688. }
  689. catch (std::runtime_error e)
  690. {
  691. log_error(stderr, "caught a std::runtime_error exception:\n");
  692. log_error(stderr, " %s\n", e.what());
  693. return 1;
  694. }
  695. catch (...)
  696. {
  697. log_error(stderr, "caught an unknown exception!\n");
  698. return 1;
  699. }
  700. }