Welcome to mirror list, hosted at ThFree Co, Russian Federation.

aligned.h « intgemm - github.com/marian-nmt/intgemm/intgemm.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6fda3697848b06d5c103dce6dace1267b6f86c39 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#pragma once
#include <cstdlib>
#include <new>
#ifdef _MSC_VER
#include <malloc.h>
#endif

// Aligned simple vector.

namespace intgemm {

template <class T> class AlignedVector {
  public:
    AlignedVector() : mem_(nullptr), size_(0) {}

    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();
#else      
      if (posix_memalign(reinterpret_cast<void **>(&mem_), alignment, size * sizeof(T))) {
        throw std::bad_alloc();
      }
#endif
    }

    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(const AlignedVector&) = delete;
    AlignedVector& operator=(const AlignedVector&) = delete;

    ~AlignedVector() {
#ifdef _MSC_VER
      _aligned_free(mem_);
#else
      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 intgemm