system_allocator.h 3.4 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
L
liaogang 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

#pragma once

17
#include <stddef.h>  // for size_t
18
#include <string>
Y
Yi Wang 已提交
19

L
liaogang 已提交
20 21 22 23
namespace paddle {
namespace memory {
namespace detail {

L
liaogang 已提交
24
/**
C
chengduoZH 已提交
25 26 27
 * \brief SystemAllocator is the parent class of CPUAllocator,
 *        CUDAPinnedAllocator and GPUAllocator. A BuddyAllocator
 *        object uses a SystemAllocator* pointing to the
L
liaogang 已提交
28 29
 *        underlying system allocator.
 */
30
class SystemAllocator {
31
 public:
32
  virtual ~SystemAllocator() {}
Y
Yi Wang 已提交
33
  virtual void* Alloc(size_t* index, size_t size) = 0;
L
liaogang 已提交
34
  virtual void Free(void* p, size_t size, size_t index) = 0;
L
liaogang 已提交
35
  virtual bool UseGpu() const = 0;
Y
Yi Wang 已提交
36 37
};

38
class CPUAllocator : public SystemAllocator {
39
 public:
Y
Yi Wang 已提交
40
  virtual void* Alloc(size_t* index, size_t size);
L
liaogang 已提交
41
  virtual void Free(void* p, size_t size, size_t index);
L
liaogang 已提交
42
  virtual bool UseGpu() const;
L
liaogang 已提交
43 44
};

45
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
46 47
class GPUAllocator : public SystemAllocator {
 public:
C
chengduoZH 已提交
48 49
  explicit GPUAllocator(int gpu_id) : gpu_id_(gpu_id) {}

Y
Yi Wang 已提交
50
  virtual void* Alloc(size_t* index, size_t size);
L
liaogang 已提交
51
  virtual void Free(void* p, size_t size, size_t index);
L
liaogang 已提交
52
  virtual bool UseGpu() const;
L
liaogang 已提交
53

L
liaogang 已提交
54
 private:
55
  size_t gpu_alloc_size_ = 0;
C
chengduoZH 已提交
56
  int gpu_id_;
57
};
C
chengduoZH 已提交
58 59 60

class CUDAPinnedAllocator : public SystemAllocator {
 public:
Y
Yi Wang 已提交
61
  virtual void* Alloc(size_t* index, size_t size);
C
chengduoZH 已提交
62 63 64 65
  virtual void Free(void* p, size_t size, size_t index);
  virtual bool UseGpu() const;

 private:
C
chengduoZH 已提交
66
  size_t cuda_pinnd_alloc_size_ = 0;
C
chengduoZH 已提交
67
};
L
Luo Tao 已提交
68
#endif
L
liaogang 已提交
69

70 71 72 73 74 75 76 77 78 79 80 81 82 83
#ifdef PADDLE_WITH_ASCEND_CL

class NPUAllocator : public SystemAllocator {
 public:
  explicit NPUAllocator(int npu_id) : npu_id_(npu_id) {}

  virtual void* Alloc(size_t* index, size_t size);
  virtual void Free(void* p, size_t size, size_t index);
  virtual bool UseGpu() const;

 private:
  size_t npu_alloc_size_ = 0;
  int npu_id_;
};
84 85 86 87 88 89 90 91 92 93

class NPUPinnedAllocator : public SystemAllocator {
 public:
  virtual void* Alloc(size_t* index, size_t size);
  virtual void Free(void* p, size_t size, size_t index);
  virtual bool UseGpu() const;

 private:
  size_t npu_pinnd_alloc_size_ = 0;
};
94 95
#endif

F
fwenguang 已提交
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
#ifdef PADDLE_WITH_MLU
class MLUAllocator : public SystemAllocator {
 public:
  explicit MLUAllocator(int mlu_id) : mlu_id_(mlu_id) {}

  virtual void* Alloc(size_t* index, size_t size);
  virtual void Free(void* p, size_t size, size_t index);
  virtual bool UseGpu() const;

 private:
  size_t mlu_alloc_size_ = 0;
  int mlu_id_;
};
#endif

111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
#ifdef PADDLE_WITH_CUSTOM_DEVICE
class CustomAllocator : public SystemAllocator {
 public:
  explicit CustomAllocator(const std::string& device_type, size_t dev_id)
      : dev_type_(device_type), dev_id_(dev_id) {}

  virtual void* Alloc(size_t* index, size_t size);
  virtual void Free(void* p, size_t size, size_t index);
  virtual bool UseGpu() const;

 private:
  size_t plug_alloc_size = 0;
  std::string dev_type_;
  size_t dev_id_;
};
#endif

L
liaogang 已提交
128 129 130
}  // namespace detail
}  // namespace memory
}  // namespace paddle