Skip to content

Commit

Permalink
Fix TBB allocator used even if HAVE_TBB_MALLOC not available
Browse files Browse the repository at this point in the history
  • Loading branch information
epruesse committed Dec 13, 2020
1 parent dbd21de commit 14a17e9
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 12 deletions.
6 changes: 5 additions & 1 deletion src/align.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ using std::exception;
#include <algorithm>
using std::find_if;

#ifdef HAVE_TBB
#ifdef HAVE_TBB_MALLOC
# include "tbb/tbb_allocator.h"
#endif

Expand Down Expand Up @@ -481,7 +481,11 @@ sina::do_align(cseq& c, const cseq& orig, MASTER &m,
cnsts_type cns(tr);

// create the alignment "mesh" (not quite a matrix)
#ifdef HAVE_TBB_MALLOC
using mesh_t = mesh<MASTER, cseq, data_type, tbb::tbb_allocator<data_type>>;
#else
using mesh_t = mesh<MASTER, cseq, data_type>;
#endif
logger->debug("Allocating {}MB for alignment matrix", mesh_t::guessMem(m, c)/1024/1024);
mesh_t A(m, c);

Expand Down
32 changes: 30 additions & 2 deletions src/buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,15 @@ for the parts of ARB used as well as that of the covered work.
#ifndef _BUFFER_H_
#define _BUFFER_H_

#include <cstddef>
#include <cstdint>
#include <new>

#ifdef HAVE_TBB_MALLOC
#include <tbb/scalable_allocator.h>
#else
#include <stdlib.h>
#endif

namespace sina {

Expand All @@ -38,10 +46,18 @@ class buffer {
public:
using size_type = size_t;
explicit buffer(size_type size) {
#ifdef HAVE_TBB_MALLOC
_start = (T*)scalable_malloc(sizeof(T) * size);
#else
_start = (T*)malloc(sizeof(T) * size);
#endif
}
~buffer() {
#ifdef HAVE_TBB_MALLOC
scalable_free(_start);
#else
free(_start);
#endif
}
inline T& operator[](size_type idx) {
return _start[idx];
Expand All @@ -54,23 +70,35 @@ template<typename T, size_t ALIGN=64>
class aligned_buffer {
public:
using size_type = size_t;
using offset_t = uint16_t;
explicit aligned_buffer(size_type size) {
size_type alloc = sizeof(T) * size + sizeof(offset_t) + ALIGN - 1;
#ifdef HAVE_TBB_MALLOC
void *ptr = scalable_malloc(alloc);
if (!ptr) { throw std::bad_alloc(); }
#else
void *ptr = malloc(alloc);
#endif
if (!ptr) {
throw std::bad_alloc();
}
size_t res = ((size_t)ptr + sizeof(offset_t) + ALIGN - 1) & ~(ALIGN -1);
*((offset_t*)res - 1) = (offset_t)((size_t)res - (size_t)ptr);
_start = (T*)res;
}

~aligned_buffer() {
offset_t offset = *((offset_t*)_start - 1);
#ifdef HAVE_TBB_MALLOC
scalable_free((char*)_start - offset);
#else
free((char*)_start - offset);
#endif
}

inline T& operator[](size_type idx) {
return _start[idx];
}
private:
using offset_t = uint16_t;
T* _start;
};

Expand Down
10 changes: 2 additions & 8 deletions src/idset.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ for the parts of ARB used as well as that of the covered work.
#include <vector>
#include <iostream>

#include <tbb/scalable_allocator.h>
#include <tbb/cache_aligned_allocator.h>

#include "helpers.h"

/** Abstract base class for container of ids
Expand All @@ -43,8 +40,6 @@ for the parts of ARB used as well as that of the covered work.
class idset {
public:
using value_type = uint32_t;
// using inc_t = std::vector<int16_t, tbb::cache_aligned_allocator<int16_t>>;
//using data_t = std::vector<uint8_t, tbb::cache_aligned_allocator<uint8_t>>;
using inc_t = std::vector<int16_t>;
using data_t = std::vector<uint8_t>;

Expand Down Expand Up @@ -96,7 +91,7 @@ class bitmap : public idset {
}

/* set bit at @id */
void set(value_type id) {
void set(value_type id) const {
data[block_index(id)] |= 1 << (block_offset(id));
}

Expand All @@ -123,7 +118,6 @@ class bitmap : public idset {
set(id);
}


int increment(inc_t& t) const override {
for (value_type i=0; i < data.size(); i++) {
block_type block = data[i];
Expand All @@ -150,7 +144,7 @@ class imap_abs : public idset {
data_t::const_iterator _it;
public:
explicit const_iterator(const data_t::const_iterator& it) : _it(it) {}
value_type operator*() {
value_type operator*() const {
return *(value_type*)(&*_it);
}
const_iterator& operator++() {
Expand Down
1 change: 0 additions & 1 deletion src/kmer_search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ using std::string;
namespace fs = boost::filesystem;

#include <tbb/tbb.h>
#include <tbb/cache_aligned_allocator.h>
#include <cstdio>
#include <sys/stat.h>

Expand Down

0 comments on commit 14a17e9

Please sign in to comment.