mirror of
https://github.com/browsermt/bergamot-translator.git
synced 2024-09-11 05:35:33 +03:00
Update aligned vector following intgemm 1b8cbd6f611c21011325cfe0312940f0635dea33 (#334)
Fixes memory leak ifdef for -fno-exceptions including clang-cl Move spacing back to intgemm upstream Co-authored-by: Jerin Philip <jerin.philip@research.iiit.ac.in>
This commit is contained in:
parent
c76e630e00
commit
a94725b20d
@ -2,70 +2,91 @@
|
||||
#include <cstdlib>
|
||||
#include <new>
|
||||
#ifdef _MSC_VER
|
||||
// Ensure _HAS_EXCEPTIONS is defined
|
||||
#include <vcruntime.h>
|
||||
#include <malloc.h>
|
||||
#endif
|
||||
|
||||
#if !((defined(_MSC_VER) && !defined(__clang__)) ? (_HAS_EXCEPTIONS) : (__EXCEPTIONS))
|
||||
#include <cstdlib>
|
||||
#endif
|
||||
|
||||
// Aligned simple vector.
|
||||
|
||||
namespace marian {
|
||||
namespace bergamot {
|
||||
|
||||
template <class T> class AlignedVector {
|
||||
public:
|
||||
AlignedVector() : mem_(nullptr), size_(0) {}
|
||||
public:
|
||||
AlignedVector() : mem_(nullptr), size_(0) {}
|
||||
|
||||
explicit AlignedVector(std::size_t size, std::size_t alignment = 64 /* CPU cares about this */)
|
||||
: size_(size) {
|
||||
explicit AlignedVector(std::size_t size, std::size_t alignment = 64 /* CPU cares about this */)
|
||||
: size_(size) {
|
||||
#ifdef _MSC_VER
|
||||
mem_ = static_cast<T*>(_aligned_malloc(size * sizeof(T), alignment));
|
||||
if (!mem_) throw std::bad_alloc();
|
||||
mem_ = static_cast<T*>(_aligned_malloc(size * sizeof(T), alignment));
|
||||
if (!mem_) {
|
||||
# if (defined(_MSC_VER) && !defined(__clang__)) ? (_HAS_EXCEPTIONS) : (__EXCEPTIONS)
|
||||
throw std::bad_alloc();
|
||||
# else
|
||||
std::abort();
|
||||
# endif
|
||||
}
|
||||
#else
|
||||
if (posix_memalign(reinterpret_cast<void **>(&mem_), alignment, size * sizeof(T))) {
|
||||
throw std::bad_alloc();
|
||||
if (posix_memalign(reinterpret_cast<void **>(&mem_), alignment, size * sizeof(T))) {
|
||||
# if (defined(_MSC_VER) && !defined(__clang__)) ? (_HAS_EXCEPTIONS) : (__EXCEPTIONS)
|
||||
throw std::bad_alloc();
|
||||
# else
|
||||
std::abort();
|
||||
# endif
|
||||
}
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
AlignedVector(AlignedVector &&from) : mem_(from.mem_), size_(from.size_) {
|
||||
from.mem_ = nullptr;
|
||||
from.size_ = 0;
|
||||
}
|
||||
AlignedVector(AlignedVector &&from) : mem_(from.mem_), size_(from.size_) {
|
||||
from.mem_ = nullptr;
|
||||
from.size_ = 0;
|
||||
}
|
||||
|
||||
AlignedVector &operator=(AlignedVector &&from) {
|
||||
mem_ = from.mem_;
|
||||
size_ = from.size_;
|
||||
from.mem_ = nullptr;
|
||||
from.size_ = 0;
|
||||
return *this;
|
||||
}
|
||||
AlignedVector &operator=(AlignedVector &&from) {
|
||||
if (this == &from) return *this;
|
||||
release();
|
||||
mem_ = from.mem_;
|
||||
size_ = from.size_;
|
||||
from.mem_ = nullptr;
|
||||
from.size_ = 0;
|
||||
return *this;
|
||||
}
|
||||
|
||||
AlignedVector(const AlignedVector&) = delete;
|
||||
AlignedVector& operator=(const AlignedVector&) = delete;
|
||||
AlignedVector(const AlignedVector&) = delete;
|
||||
AlignedVector& operator=(const AlignedVector&) = delete;
|
||||
|
||||
~AlignedVector() {
|
||||
~AlignedVector() { release(); }
|
||||
|
||||
std::size_t size() const { return size_; }
|
||||
|
||||
T &operator[](std::size_t offset) { return mem_[offset]; }
|
||||
const T &operator[](std::size_t offset) const { return mem_[offset]; }
|
||||
|
||||
T *begin() { return mem_; }
|
||||
const T *begin() const { return mem_; }
|
||||
T *end() { return mem_ + size_; }
|
||||
const T *end() const { return mem_ + size_; }
|
||||
|
||||
template <typename ReturnType>
|
||||
ReturnType *as() { return reinterpret_cast<ReturnType*>(mem_); }
|
||||
|
||||
private:
|
||||
T *mem_;
|
||||
std::size_t size_;
|
||||
|
||||
void release() {
|
||||
#ifdef _MSC_VER
|
||||
_aligned_free(mem_);
|
||||
_aligned_free(mem_);
|
||||
#else
|
||||
std::free(mem_);
|
||||
std::free(mem_);
|
||||
#endif
|
||||
}
|
||||
|
||||
std::size_t size() const { return size_; }
|
||||
|
||||
T &operator[](std::size_t offset) { return mem_[offset]; }
|
||||
const T &operator[](std::size_t offset) const { return mem_[offset]; }
|
||||
|
||||
T *begin() { return mem_; }
|
||||
const T *begin() const { return mem_; }
|
||||
T *end() { return mem_ + size_; }
|
||||
const T *end() const { return mem_ + size_; }
|
||||
|
||||
template <typename ReturnType>
|
||||
ReturnType *as() { return reinterpret_cast<ReturnType*>(mem_); }
|
||||
|
||||
private:
|
||||
T *mem_;
|
||||
std::size_t size_;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace bergamot
|
||||
} // namespace marian
|
||||
|
Loading…
Reference in New Issue
Block a user