Skip to content

Commit

Permalink
Add official support for libsais library
Browse files Browse the repository at this point in the history
  • Loading branch information
ch4rr0 committed Nov 10, 2023
1 parent d32e734 commit b6bdafe
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
24 changes: 20 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,14 @@ else ifneq (,$(findstring $(ARCH), aarch64 arm64 s390x powerpc64 powerpc64le ppc
POPCNT_CAPABILITY ?= 0
endif

ifdef USE_SAIS
ifeq (1, $(if $(or $(USE_SAIS),$(USE_SAIS_OPENMP)),1))
CPPFLAGS += -Ithird_party/libsais/include
CXXFLAGS += -fopenmp -DUSE_SAIS
LDFLAGS += -Lthird_party/libsais/lib
LDLIBS += -lsais
ifdef USE_SAIS_OPENMP
BUILD_CXXFLAGS += -fopenmp
endif
BUILD_CXXFLAGS += -DUSE_SAIS
BUILD_LDFLAGS += -Lthird_party/libsais/lib
BUILD_LDLIBS += -lsais
endif

# msys will always be 32 bit so look at the cpu arch instead.
Expand Down Expand Up @@ -322,6 +325,9 @@ else
$(BOWTIE2_BIN_LIST_SAN): $(error "Compiler does not support...")
endif

bowtie2-build-%: CXXFLAGS += $(BUILD_CXXFLAGS)
bowtie2-build-%: LDFLAGS += $(BUILD_LDFLAGS)
bowtie2-build-%: LDLIBS += $(BUILD_LDLIBS)
#
# bowtie2-build targets
#
Expand Down Expand Up @@ -576,6 +582,16 @@ static-libs:
rm -rf zstd-1.5.5* ; \
fi

.PHONY: libsais
libsais:
if [ ! -d $(CURDIR)/third_party/libsais ]; then \
git submodule init && git submodule update --recursive ; \
fi ; \
if [ -n "$(USE_SAIS_OPENMP)" ]; then \
export CFLAGS="$(CFLAGS) -fopenmp -O2" ; \
fi ; \
cd $(CURDIR)/third_party/libsais && $(MAKE) -B CC=$(CC) && $(MAKE) install PREFIX=$$PWD ;

.PHONY: sra-deps
sra-deps:
DL=$$( ( which wget >/dev/null 2>&1 && echo "wget --no-check-certificate" ) || echo "curl -LOk") ; \
Expand Down
11 changes: 9 additions & 2 deletions blockwise_sa.h
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,18 @@ class SAISBlockwiseSA : public InorderBlockwiseSA<TStr> {
__text.wbuf()[__text.length()] = (char)127; // $ is larger than any character in the suffix array
_suffixes.resize(__text.length() + 1);
#ifdef BOWTIE_64BIT_INDEX
#ifdef _OPENMP
libsais64_omp((const uint8_t *)__text.buf(), (int64_t *)(_suffixes.ptr()), (int64_t)_suffixes.size(), 0, NULL, _nthreads);
#else
libsais64((const uint8_t *)__text.buf(), (int64_t *)(_suffixes.ptr()), (int64_t)_suffixes.size(), 0, NULL);
#endif

#else
#ifdef _OPENMP
libsais_omp((const uint8_t *)__text.buf(), (int32_t *)(_suffixes.ptr()), (int32_t)_suffixes.size(), 0, NULL, _nthreads);

#else
libsais((const uint8_t *)__text.buf(), (int32_t *)(_suffixes.ptr()), (int32_t)_suffixes.size(), 0, NULL);
#endif
#endif
_suffixes[__text.length()] = __text.length();
}
Expand All @@ -231,7 +238,7 @@ class SAISBlockwiseSA : public InorderBlockwiseSA<TStr> {
}

bool hasMoreBlocks() const {
return _i == _suffixes.size();
return _i < _suffixes.size();
}

private:
Expand Down
18 changes: 17 additions & 1 deletion bt2_idx.h
Original file line number Diff line number Diff line change
Expand Up @@ -1067,13 +1067,17 @@ class Ebwt {
bmax = (TIndexOffU)sqrt(s.length());
VMSG_NL("bmax defaulted to: " << bmax);
}
#ifndef USE_SAIS
int iter = 0;
bool first = true;
streampos out1pos = out1.tellp();
streampos out2pos = out2.tellp();
#endif

// Look for bmax/dcv parameters that work.
thread_pool pool(nthreads - 1);
while(true) {
#ifndef USE_SAIS
if(!first && bmax < 40 && _passMemExc) {
cerr << "Could not find approrpiate bmax/dcv settings for building this index." << endl;
if(!isPacked()) {
Expand Down Expand Up @@ -1107,7 +1111,9 @@ class Ebwt {
VMSG_NL(" --dcv " << dcv);
}
iter++;
#endif
try {
#ifndef USE_SAIS
{
VMSG_NL(" Doing ahead-of-time memory usage test");
// Make a quick-and-dirty attempt to force a bad_alloc iff
Expand Down Expand Up @@ -1135,10 +1141,12 @@ class Ebwt {
}
VMSG_NL("");
}
VMSG_NL("Constructing suffix-array element generator");
#endif
#ifdef USE_SAIS
VMSG_NL("Using SAIS algorithm for constructing suffix array");
SAISBlockwiseSA<TStr> bsa(s, bmax, nthreads);
#else
VMSG_NL("Constructing suffix-array element generator");
KarkkainenBlockwiseSA<TStr> bsa(s, bmax, nthreads, pool, dcv, seed, _sanity, _passMemExc, _verbose, outfile);
#endif
assert(bsa.suffixItrIsReset());
Expand All @@ -1161,15 +1169,23 @@ class Ebwt {
}
break;
} catch(bad_alloc& e) {
#ifdef USE_SAIS
cerr << "Out of memory while constructing suffix array. Please try using a smaller" << endl;
throw 1;
#else
if(_passMemExc) {
VMSG_NL(" Ran out of memory; automatically trying more memory-economical parameters.");
} else {
cerr << "Out of memory while constructing suffix array. Please try using a smaller" << endl
<< "number of blocks by specifying a smaller --bmax or a larger --bmaxdivn" << endl;
throw 1;
}
#endif
}
#ifndef USE_SAIS
first = false;
#endif

}
assert(repOk());
// Now write reference sequence names on the end
Expand Down

0 comments on commit b6bdafe

Please sign in to comment.