phi_allocator.h 1.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/* Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
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

14
#include "paddle/fluid/memory/malloc.h"
15
#include "paddle/phi/core/allocator.h"
16

W
Wilber 已提交
17 18 19 20
#ifdef INFRT_WITH_GPU
#include <cuda_runtime.h>
#endif

21 22 23
namespace infrt {
namespace backends {

24
class CpuPhiAllocator : public phi::Allocator {
25
 public:
26
  static void deleter(phi::Allocation* ptr) { ::operator delete(ptr); }
27 28 29

  AllocationPtr Allocate(size_t bytes_size) {
    return AllocationPtr(
30 31 32
        new phi::Allocation(::operator new(bytes_size),
                            bytes_size,
                            phi::Place(phi::AllocationType::CPU)),
33 34 35 36
        deleter);
  }
};

W
Wilber 已提交
37 38 39 40 41 42 43
#ifdef INFRT_WITH_GPU
// TODO(wilber): Just for demo test. we need a more efficient gpu allocator.
class GpuPhiAllocator : public phi::Allocator {
 public:
  static void deleter(phi::Allocation* ptr) { cudaFree(ptr->ptr()); }

  AllocationPtr Allocate(size_t bytes_size) {
44 45
    return paddle::memory::Alloc(phi::Place(phi::AllocationType::GPU),
                                 bytes_size);
W
Wilber 已提交
46 47 48 49
  }
};
#endif

50 51
}  // namespace backends
}  // namespace infrt