manager.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. #pragma once
  15. #include "ultra_infer/utils/utils.h"
  16. #include "ultra_infer/vision/common/processors/base.h"
  17. #include "ultra_infer/vision/common/processors/mat.h"
  18. #include "ultra_infer/vision/common/processors/mat_batch.h"
  19. namespace ultra_infer {
  20. namespace vision {
  21. /*! @brief ProcessorManager for Preprocess
  22. */
  23. class ULTRAINFER_DECL ProcessorManager {
  24. public:
  25. ~ProcessorManager();
  26. /** \brief Use CUDA to boost the performance of processors
  27. *
  28. * \param[in] enable_cv_cuda true: use CV-CUDA, false: use CUDA only
  29. * \param[in] gpu_id GPU device id
  30. * \return true if the preprocess succeeded, otherwise false
  31. */
  32. void UseCuda(bool enable_cv_cuda = false, int gpu_id = -1);
  33. bool CudaUsed();
  34. #ifdef WITH_GPU
  35. cudaStream_t Stream() const { return stream_; }
  36. #endif
  37. void SetStream(FDMat *mat) {
  38. #ifdef WITH_GPU
  39. mat->SetStream(stream_);
  40. #endif
  41. }
  42. void SetStream(FDMatBatch *mat_batch) {
  43. #ifdef WITH_GPU
  44. mat_batch->SetStream(stream_);
  45. #endif
  46. }
  47. void SyncStream() {
  48. #ifdef WITH_GPU
  49. FDASSERT(cudaStreamSynchronize(stream_) == cudaSuccess,
  50. "[ERROR] Error occurs while sync cuda stream.");
  51. #endif
  52. }
  53. int DeviceId() { return device_id_; }
  54. /** \brief Process the input images and prepare input tensors for runtime
  55. *
  56. * \param[in] images The input image data list, all the elements are returned
  57. * by cv::imread() \param[in] outputs The output tensors which will feed in
  58. * runtime \return true if the preprocess succeeded, otherwise false
  59. */
  60. bool Run(std::vector<FDMat> *images, std::vector<FDTensor> *outputs);
  61. /** \brief Apply() is the body of Run() function, it needs to be implemented
  62. * by a derived class
  63. *
  64. * \param[in] image_batch The input image batch
  65. * \param[in] outputs The output tensors which will feed in runtime
  66. * \return true if the preprocess succeeded, otherwise false
  67. */
  68. virtual bool Apply(FDMatBatch *image_batch,
  69. std::vector<FDTensor> *outputs) = 0;
  70. void PreApply(FDMatBatch *image_batch);
  71. void PostApply();
  72. protected:
  73. ProcLib proc_lib_ = ProcLib::DEFAULT;
  74. private:
  75. #ifdef WITH_GPU
  76. cudaStream_t stream_ = nullptr;
  77. #endif
  78. int device_id_ = -1;
  79. std::vector<FDTensor> input_caches_;
  80. std::vector<FDTensor> output_caches_;
  81. FDTensor batch_input_cache_;
  82. FDTensor batch_output_cache_;
  83. };
  84. } // namespace vision
  85. } // namespace ultra_infer