allocate.cc 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifdef WITH_GPU
  15. #include <cuda_runtime_api.h>
  16. #endif
  17. #include "ultra_infer/core/allocate.h"
  18. namespace ultra_infer {
  19. bool FDHostAllocator::operator()(void **ptr, size_t size) const {
  20. *ptr = malloc(size);
  21. return *ptr != nullptr;
  22. }
  23. void FDHostFree::operator()(void *ptr) const { free(ptr); }
  24. #ifdef WITH_GPU
  25. bool FDDeviceAllocator::operator()(void **ptr, size_t size) const {
  26. return cudaMalloc(ptr, size) == cudaSuccess;
  27. }
  28. void FDDeviceFree::operator()(void *ptr) const { cudaFree(ptr); }
  29. bool FDDeviceHostAllocator::operator()(void **ptr, size_t size) const {
  30. return cudaMallocHost(ptr, size) == cudaSuccess;
  31. }
  32. void FDDeviceHostFree::operator()(void *ptr) const { cudaFreeHost(ptr); }
  33. #endif
  34. } // namespace ultra_infer