manager.cc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. #include "ultra_infer/vision/common/processors/manager.h"
  15. namespace ultra_infer {
  16. namespace vision {
  17. ProcessorManager::~ProcessorManager() {
  18. #ifdef WITH_GPU
  19. if (stream_)
  20. cudaStreamDestroy(stream_);
  21. #endif
  22. }
  23. void ProcessorManager::UseCuda(bool enable_cv_cuda, int gpu_id) {
  24. #ifdef WITH_GPU
  25. if (gpu_id >= 0) {
  26. device_id_ = gpu_id;
  27. FDASSERT(cudaSetDevice(device_id_) == cudaSuccess,
  28. "[ERROR] Error occurs while setting cuda device.");
  29. }
  30. FDASSERT(cudaStreamCreate(&stream_) == cudaSuccess,
  31. "[ERROR] Error occurs while creating cuda stream.");
  32. proc_lib_ = ProcLib::CUDA;
  33. #else
  34. FDASSERT(false, "UltraInfer didn't compile with WITH_GPU.");
  35. #endif
  36. if (enable_cv_cuda) {
  37. #ifdef ENABLE_CVCUDA
  38. proc_lib_ = ProcLib::CVCUDA;
  39. #else
  40. FDASSERT(false, "UltraInfer didn't compile with CV-CUDA.");
  41. #endif
  42. }
  43. }
  44. bool ProcessorManager::CudaUsed() {
  45. return (proc_lib_ == ProcLib::CUDA || proc_lib_ == ProcLib::CVCUDA);
  46. }
  47. void ProcessorManager::PreApply(FDMatBatch *image_batch) {
  48. FDASSERT(image_batch->mats != nullptr, "The mats is empty.");
  49. FDASSERT(image_batch->mats->size() > 0,
  50. "The size of input images should be greater than 0.");
  51. if (image_batch->mats->size() > input_caches_.size()) {
  52. input_caches_.resize(image_batch->mats->size());
  53. output_caches_.resize(image_batch->mats->size());
  54. }
  55. image_batch->input_cache = &batch_input_cache_;
  56. image_batch->output_cache = &batch_output_cache_;
  57. image_batch->proc_lib = proc_lib_;
  58. if (CudaUsed()) {
  59. SetStream(image_batch);
  60. }
  61. for (size_t i = 0; i < image_batch->mats->size(); ++i) {
  62. FDMat *mat = &(image_batch->mats->at(i));
  63. mat->input_cache = &input_caches_[i];
  64. mat->output_cache = &output_caches_[i];
  65. mat->proc_lib = proc_lib_;
  66. if (mat->mat_type == ProcLib::CUDA) {
  67. // Make a copy of the input data ptr, so that the original data ptr of
  68. // FDMat won't be modified.
  69. auto fd_tensor = std::make_shared<FDTensor>();
  70. fd_tensor->SetExternalData(mat->Tensor()->shape, mat->Tensor()->Dtype(),
  71. mat->Tensor()->Data(), mat->Tensor()->device,
  72. mat->Tensor()->device_id);
  73. mat->SetTensor(fd_tensor);
  74. }
  75. }
  76. }
  77. void ProcessorManager::PostApply() {
  78. if (CudaUsed()) {
  79. SyncStream();
  80. }
  81. }
  82. bool ProcessorManager::Run(std::vector<FDMat> *images,
  83. std::vector<FDTensor> *outputs) {
  84. FDMatBatch image_batch(images);
  85. PreApply(&image_batch);
  86. bool ret = Apply(&image_batch, outputs);
  87. PostApply();
  88. return ret;
  89. }
  90. } // namespace vision
  91. } // namespace ultra_infer