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

device.cpp « cpu « tensors « src - github.com/marian-nmt/marian.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b0fdae92e31ec45973849aaddce82d2549aa4469 (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
#include "tensors/device.h"
#include <iostream>

#ifdef _WIN32
#include <malloc.h>
#endif
#include <stdlib.h>

namespace marian {
namespace cpu {

Device::~Device() {
  free(data_);
  data_ = nullptr;
  size_ = 0;
}

// allocate function for tensor reserve() below. 
// Needed for AVX512, while not available on all compilers. It seems clang
// does not have aligned_alloc for all cstlib versions. If AVX512 is not used
// a simple malloc is probably fine. 
// Should generate a runtime error otherwise as we have a check in the AVX512 
// functions which tests for alignment. 
#ifdef _WIN32
#define MALLOC(size) _aligned_alloc(size, alignment_)
#elif __GNUC__
#define MALLOC(size) aligned_alloc(alignment_, size)
#else
#define MALLOC(size) malloc(size)
#endif

void Device::reserve(size_t size) {
  size = align(size);
  ABORT_IF(size < size_ || size == 0,
           "New size must be larger than old size and larger than 0");

  if(data_) {
    uint8_t *temp = static_cast<uint8_t*>(MALLOC(size));
    std::copy(data_, data_ + size_, temp);
    free(data_);
    data_ = temp;
  } else {
    data_ = static_cast<uint8_t*>(MALLOC(size));
  }
  size_ = size;
}
}  // namespace cpu
}  // namespace marian