Skip to content

Commit

Permalink
Changed OpenMP to be optional.
Browse files Browse the repository at this point in the history
  • Loading branch information
bo1929 committed Feb 17, 2025
1 parent 5bb7773 commit 706611b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 11 deletions.
27 changes: 19 additions & 8 deletions makefile
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
# compiler options
#--------------------------------------------
COMPILER = g++
WLCURL=1
WLCURL = 1
WOPENMP = 1
CSTATIC = 0

ifeq ($(WLCURL), 0)
LDLIBS= -lstdc++fs -lm -lz -lstdc++
CXXFLAGS = -std=c++17 -O3

ifeq ($(CSTATIC), 0)
LDLIBS = -lstdc++fs -lm -lz -lstdc++
else
LDLIBS= -lstdc++fs -lm -lz -lstdc++ -lcurl
LDLIBS = --static -lstdc++fs -lm -lz -static-libgcc -static-libstdc++
endif

ifneq ($(WLCURL), 0)
LDLIBS += -lcurl
endif

VARDEF= -D _WLCURL=$(WLCURL)
ifneq ($(WOPENMP), 0)
LDLIBS += -lgomp
CXXFLAGS += -fopenmp
endif

CXXFLAGS = -std=c++17 -O3 -fopenmp
VARDEF= -D _WLCURL=$(WLCURL) -D _WOPENMP=$(WOPENMP)

INC = -Iexternal/CLI11/include/CLI \
-Iexternal/parallel-hashmap \
Expand All @@ -37,10 +48,10 @@ all: $(PROGRAM)
# generic rule for compiling *.cpp -> *.o
build/%.o: src/%.cpp
@mkdir -p build
$(COMPILER) $(WFLAGS) $(CXXFLAGS) $(CPPFLAGS) $(LDLIBS) $(VARDEF) $(INC) -c src/$*.cpp -o build/$*.o
$(COMPILER) $(WFLAGS) $(CXXFLAGS) $(LDLIBS) $(VARDEF) $(INC) -c src/$*.cpp -o build/$*.o

$(PROGRAM): $(OBJECTS)
$(COMPILER) $(WFLAGS) $(CXXFLAGS) $+ $(LDLIBS) $(VARDEF) $(CPPFLAGS) $(LDFLAGS) $(INC) -o $@
$(COMPILER) $(WFLAGS) $(CXXFLAGS) $+ $(LDLIBS) $(VARDEF) $(LDFLAGS) $(INC) -o $@

clean:
rm -f $(PROGRAM) $(OBJECTS)
Expand Down
6 changes: 3 additions & 3 deletions src/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@
#include <vector>
#include <zlib.h>
#include <stdio.h>
#if defined(_OPENMP)
#if defined(_OPENMP) && _WOPENMP == 1
#include "omp.h"
#endif
#if defined _WLCURL && _WLCURL == 1
#if defined(_WLCURL) && _WLCURL == 1
#include <curl/curl.h>
#endif

#define VERSION "v0.4.1"
#define VERSION "v0.4.2"
#define PRINT_VERSION std::cerr << "krepp version: " << VERSION << std::endl;

extern uint32_t num_threads;
Expand Down
16 changes: 16 additions & 0 deletions src/krepp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,10 @@ void IndexMultiple::build_index()
{
record_sptr_t record = std::make_shared<Record>(tree);
dynht_sptr_t root_dynht = std::make_shared<DynHT>(nrows, tree, record);
#if defined(_OPENMP) && _WOPENMP == 1
omp_set_num_threads(num_threads);
omp_set_nested(1);
#endif
#pragma omp parallel
{
#pragma omp single
Expand Down Expand Up @@ -238,20 +240,28 @@ void IndexMultiple::build_for_subtree(node_sptr_t nd, dynht_sptr_t dynht)
} else {
assert(nd->get_nchildren() > 0);
vec<dynht_sptr_t> children_dynht_v;
#if defined(_OPENMP) && _WOPENMP == 1
omp_lock_t parent_lock;
omp_init_lock(&parent_lock);
#endif
for (tuint_t i = 0; i < nd->get_nchildren(); ++i) {
children_dynht_v.emplace_back(std::make_shared<DynHT>(nrows, tree, dynht->get_record()));
#pragma omp task untied shared(dynht)
{
build_for_subtree(*std::next(nd->get_children(), i), children_dynht_v[i]);
#if defined(_OPENMP) && _WOPENMP == 1
omp_set_lock(&parent_lock);
#endif
dynht->union_table(children_dynht_v[i]);
#if defined(_OPENMP) && _WOPENMP == 1
omp_unset_lock(&parent_lock);
#endif
}
}
#pragma omp taskwait
#if defined(_OPENMP) && _WOPENMP == 1
omp_destroy_lock(&parent_lock);
#endif
#pragma omp critical
{
std::cerr << "\33[2K\r" << std::flush;
Expand All @@ -278,7 +288,9 @@ void QuerySketch::seek_sequences()
{
strstream dreport_stream;
header_dreport(dreport_stream);
#if defined(_OPENMP) && _WOPENMP == 1
omp_set_num_threads(num_threads);
#endif
qseq_sptr_t qs = std::make_shared<QSeq>(query);
#pragma omp parallel shared(qs)
{
Expand All @@ -301,7 +313,9 @@ void QueryIndex::estimate_distances()
strstream dreport_stream;
header_dreport(dreport_stream);
(*output_stream) << dreport_stream.rdbuf();
#if defined(_OPENMP) && _WOPENMP == 1
omp_set_num_threads(num_threads);
#endif
qseq_sptr_t qs = std::make_shared<QSeq>(query);
#pragma omp parallel shared(qs)
{
Expand Down Expand Up @@ -348,7 +362,9 @@ void QueryIndex::place_sequences()
strstream jplace_stream;
begin_jplace(jplace_stream);
(*output_stream) << jplace_stream.rdbuf();
#if defined(_OPENMP) && _WOPENMP == 1
omp_set_num_threads(num_threads);
#endif
qseq_sptr_t qs = std::make_shared<QSeq>(query);
#pragma omp parallel shared(qs)
{
Expand Down

0 comments on commit 706611b

Please sign in to comment.