Looking into BWA's multithreading

April 27, 2012

I was/am in the process of adding multithreading capabilities to a tool I am working on (more on that soon). The tool is written in C/C++. My first option was using POSIX threads (pthreads) before going into higher level options like boost.

I first read this fantastic tutorial. (Make sure you read the links also). After that, I started to look into how pthreads is used by the masters. I read parts of bwa's source code. Concretely the aln step. In bwtaln.c:bwa_aln_core() is where the threads are setup and bwtaln.c:bwa_cal_sa_reg_gap() is the main routine for implementing the aln step.

Then I implemented a little toy project to make sure I understood the concepts. The idea is to look for a kmer in nextgen reads. This is what the code on this gist is about.

I have followed the same approach than bwa. It basically loads reads (concretely 0x40000 or 262144 in decimal) to memory. Then each thread processes reads based on the position of the read in memory and the thread id. The gem is here. I used that approach for one my recent projects.

Let me know if you find it useful.