memory.cc 5.0 KB
Newer Older
Y
Yan Chunwei 已提交
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
// Copyright (c) 2019 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.

#include "lite/core/memory.h"

namespace paddle {
namespace lite {

void* TargetMalloc(TargetType target, size_t size) {
  void* data{nullptr};
  switch (target) {
    case TargetType::kHost:
    case TargetType::kX86:
    case TargetType::kARM:
      data = TargetWrapper<TARGET(kHost)>::Malloc(size);
      break;
#ifdef LITE_WITH_CUDA
    case TargetType::kCUDA:
30
      data = TargetWrapper<TARGET(kCUDA)>::Malloc(size);
Y
Yan Chunwei 已提交
31 32 33 34 35 36 37 38 39 40 41 42
      break;
#endif  // LITE_WITH_CUDA
#ifdef LITE_WITH_OPENCL
    case TargetType::kOpenCL:
      data = TargetWrapperCL::Malloc(size);
      break;
#endif  // LITE_WITH_OPENCL
#ifdef LITE_WITH_FPGA
    case TargetType::kFPGA:
      data = TargetWrapper<TARGET(kFPGA)>::Malloc(size);
      break;
#endif  // LITE_WITH_OPENCL
43 44 45 46 47
#ifdef LITE_WITH_BM
    case TargetType::kBM:
      data = TargetWrapper<TARGET(kBM)>::Malloc(size);
      break;
#endif
48 49 50 51 52
#ifdef LITE_WITH_MLU
    case TargetType::kMLU:
      data = TargetWrapper<TARGET(kMLU)>::Malloc(size);
      break;
#endif  // LITE_WITH_MLU
53 54 55 56 57
#ifdef LITE_WITH_XPU
    case TargetType::kXPU:
      data = TargetWrapperXPU::Malloc(size);
      break;
#endif  // LITE_WITH_XPU
Y
Yan Chunwei 已提交
58 59 60 61 62 63
    default:
      LOG(FATAL) << "Unknown supported target " << TargetToStr(target);
  }
  return data;
}

64
void TargetFree(TargetType target, void* data, std::string free_flag) {
Y
Yan Chunwei 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78
  switch (target) {
    case TargetType::kHost:
    case TargetType::kX86:
    case TargetType::kARM:
      TargetWrapper<TARGET(kHost)>::Free(data);
      break;

#ifdef LITE_WITH_CUDA
    case TargetType::kCUDA:
      TargetWrapper<TARGET(kCUDA)>::Free(data);
      break;
#endif  // LITE_WITH_CUDA
#ifdef LITE_WITH_OPENCL
    case TargetType::kOpenCL:
79 80 81 82 83
      if (free_flag == "cl_use_image2d_") {
        TargetWrapperCL::FreeImage(data);
      } else {
        TargetWrapperCL::Free(data);
      }
Y
Yan Chunwei 已提交
84 85 86 87 88 89 90
      break;
#endif  // LITE_WITH_OPENCL
#ifdef LITE_WITH_FPGA
    case TargetType::kFPGA:
      TargetWrapper<TARGET(kFPGA)>::Free(data);
      break;
#endif  // LITE_WITH_CUDA
91 92 93 94 95
#ifdef LITE_WITH_BM
    case TargetType::kBM:
      TargetWrapper<TARGET(kBM)>::Free(data);
      break;
#endif
96 97 98 99 100
#ifdef LITE_WITH_MLU
    case TargetType::kMLU:
      TargetWrapper<TARGET(kMLU)>::Free(data);
      break;
#endif  // LITE_WITH_MLU
101 102 103 104 105
#ifdef LITE_WITH_XPU
    case TargetType::kXPU:
      TargetWrapperXPU::Free(data);
      break;
#endif  // LITE_WITH_XPU
Y
Yan Chunwei 已提交
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
    default:
      LOG(FATAL) << "Unknown type";
  }
}

void TargetCopy(TargetType target, void* dst, const void* src, size_t size) {
  switch (target) {
    case TargetType::kHost:
    case TargetType::kX86:
    case TargetType::kARM:
      TargetWrapper<TARGET(kHost)>::MemcpySync(
          dst, src, size, IoDirection::DtoD);
      break;

#ifdef LITE_WITH_CUDA
    case TargetType::kCUDA:
      TargetWrapper<TARGET(kCUDA)>::MemcpySync(
          dst, src, size, IoDirection::DtoD);
      break;
#endif
#ifdef LITE_WITH_FPGA
    case TargetType::kFPGA:
      TargetWrapper<TARGET(kFPGA)>::MemcpySync(
          dst, src, size, IoDirection::DtoD);
130 131 132 133 134
      break;
#endif
#ifdef LITE_WITH_BM
    case TargetType::kBM:
      TargetWrapper<TARGET(kBM)>::MemcpySync(dst, src, size, IoDirection::DtoD);
Y
Yan Chunwei 已提交
135 136
      break;
#endif
137 138 139 140
#ifdef LITE_WITH_MLU
    case TargetType::kMLU:
      TargetWrapper<TARGET(kMLU)>::MemcpySync(
          dst, src, size, IoDirection::HtoD);
141 142 143 144 145
      break;
#endif
#ifdef LITE_WITH_XPU
    case TargetType::kXPU:
      TargetWrapperXPU::MemcpySync(dst, src, size, IoDirection::HtoD);
146 147
      break;
#endif
Y
Yan Chunwei 已提交
148 149 150 151 152 153 154 155 156 157
#ifdef LITE_WITH_OPENCL
    case TargetType::kOpenCL:
      TargetWrapperCL::MemcpySync(dst, src, size, IoDirection::DtoD);
      break;
#endif  // LITE_WITH_OPENCL
    default:
      LOG(FATAL) << "unsupported type";
  }
}

158 159 160 161
#ifdef LITE_WITH_OPENCL
void TargetCopyImage2D(TargetType target,
                       void* dst,
                       const void* src,
162 163 164 165 166 167 168 169 170 171 172
                       const size_t cl_image2d_width,
                       const size_t cl_image2d_height,
                       const size_t cl_image2d_row_pitch,
                       const size_t cl_image2d_slice_pitch) {
  TargetWrapperCL::ImgcpySync(dst,
                              src,
                              cl_image2d_width,
                              cl_image2d_height,
                              cl_image2d_row_pitch,
                              cl_image2d_slice_pitch,
                              IoDirection::DtoD);
173 174 175
}
#endif

Y
Yan Chunwei 已提交
176 177
}  // namespace lite
}  // namespace paddle