From bfd5e1840fa275fdf8abad6f397c35283d52ad0d Mon Sep 17 00:00:00 2001 From: Heng Li Date: Wed, 15 Oct 2014 15:44:06 -0400 Subject: [PATCH] shm works on small files, but not large ones I don't know why. SHMMAX, SHMALL and SHMMNI are large enough. --- bwa.c | 21 +++++++++++++++------ bwa.h | 2 ++ fastmap.c | 6 +++++- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/bwa.c b/bwa.c index 65e3c16..431cc49 100644 --- a/bwa.c +++ b/bwa.c @@ -227,7 +227,7 @@ bwt_t *bwa_idx_load_bwt(const char *hint) return bwt; } -bwaidx_t *bwa_idx_load(const char *hint, int which) +bwaidx_t *bwa_idx_load_from_disk(const char *hint, int which) { bwaidx_t *idx; char *prefix; @@ -256,6 +256,11 @@ bwaidx_t *bwa_idx_load(const char *hint, int which) return idx; } +bwaidx_t *bwa_idx_load(const char *hint, int which) +{ + return bwa_idx_load_from_disk(hint, which); +} + void bwa_idx_destroy(bwaidx_t *idx) { if (idx == 0) return; @@ -263,7 +268,10 @@ void bwa_idx_destroy(bwaidx_t *idx) if (idx->bwt) bwt_destroy(idx->bwt); if (idx->bns) bns_destroy(idx->bns); if (idx->pac) free(idx->pac); - } else if (!idx->is_shm) free(idx->mem); + } else { + free(idx->bwt); free(idx->bns->anns); free(idx->bns); + if (!idx->is_shm) free(idx->mem); + } free(idx); } @@ -273,19 +281,20 @@ int bwa_mem2idx(int64_t l_mem, uint8_t *mem, bwaidx_t *idx) int i; // generate idx->bwt - x = sizeof(bwt_t); idx->bwt = (bwt_t*)(mem + k); k += x; + x = sizeof(bwt_t); idx->bwt = malloc(x); memcpy(idx->bwt, mem + k, x); k += x; x = idx->bwt->n_sa * sizeof(bwtint_t); idx->bwt->sa = (bwtint_t*)(mem + k); k += x; x = idx->bwt->bwt_size * 4; idx->bwt->bwt = (uint32_t*)(mem + k); k += x; // generate idx->bns and idx->pac - x = sizeof(bntseq_t); idx->bns = (bntseq_t*)(mem + k); k += x; + x = sizeof(bntseq_t); idx->bns = malloc(x); memcpy(idx->bns, mem + k, x); k += x; x = idx->bns->n_holes * sizeof(bntamb1_t); idx->bns->ambs = (bntamb1_t*)(mem + k); k += x; - x = idx->bns->n_seqs * sizeof(bntann1_t); idx->bns->anns = (bntann1_t*)(mem + k); k += x; + x = idx->bns->n_seqs * sizeof(bntann1_t); idx->bns->anns = malloc(x); memcpy(idx->bns->anns, mem + k, x); k += x; for (i = 0; i < idx->bns->n_seqs; ++i) { idx->bns->anns[i].name = (char*)(mem + k); k += strlen(idx->bns->anns[i].name) + 1; idx->bns->anns[i].anno = (char*)(mem + k); k += strlen(idx->bns->anns[i].anno) + 1; } - idx->pac = (uint8_t*)(mem + k); + idx->pac = (uint8_t*)(mem + k); k += idx->bns->l_pac/4+1; + assert(k == l_mem); idx->l_mem = k; idx->mem = mem; return 0; diff --git a/bwa.h b/bwa.h index 094d79b..5d40aae 100644 --- a/bwa.h +++ b/bwa.h @@ -44,6 +44,8 @@ extern "C" { char *bwa_idx_infer_prefix(const char *hint); bwt_t *bwa_idx_load_bwt(const char *hint); + bwaidx_t *bwa_idx_load_from_shm(const char *hint); + bwaidx_t *bwa_idx_load_from_disk(const char *hint, int which); bwaidx_t *bwa_idx_load(const char *hint, int which); void bwa_idx_destroy(bwaidx_t *idx); int bwa_idx2mem(bwaidx_t *idx); diff --git a/fastmap.c b/fastmap.c index 43ddfd3..e949da3 100644 --- a/fastmap.c +++ b/fastmap.c @@ -230,7 +230,11 @@ int main_mem(int argc, char *argv[]) } else update_a(opt, &opt0); bwa_fill_scmat(opt->a, opt->b, opt->mat); - if ((idx = bwa_idx_load(argv[optind], BWA_IDX_ALL)) == 0) return 1; // FIXME: memory leak + idx = bwa_idx_load_from_shm(argv[optind]); + if (idx == 0) { + if ((idx = bwa_idx_load(argv[optind], BWA_IDX_ALL)) == 0) return 1; // FIXME: memory leak + } else if (bwa_verbose >= 3) + fprintf(stderr, "[M::%s] load the bwa index from shared memory\n", __func__); if (ignore_alt) for (i = 0; i < idx->bns->n_seqs; ++i) idx->bns->anns[i].is_alt = 0;