FastDeploy  latest
Fast & Easy to Deploy!
manager.h
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 
15 #pragma once
16 
17 #include "fastdeploy/utils/utils.h"
18 #include "fastdeploy/vision/common/processors/mat.h"
19 #include "fastdeploy/vision/common/processors/mat_batch.h"
20 #include "fastdeploy/vision/common/processors/base.h"
21 
22 namespace fastdeploy {
23 namespace vision {
24 
27 class FASTDEPLOY_DECL ProcessorManager {
28  public:
30 
37  void UseCuda(bool enable_cv_cuda = false, int gpu_id = -1);
38 
39  bool CudaUsed();
40 
41 #ifdef WITH_GPU
42  cudaStream_t Stream() const { return stream_; }
43 #endif
44 
45  void SetStream(FDMat* mat) {
46 #ifdef WITH_GPU
47  mat->SetStream(stream_);
48 #endif
49  }
50 
51  void SetStream(FDMatBatch* mat_batch) {
52 #ifdef WITH_GPU
53  mat_batch->SetStream(stream_);
54 #endif
55  }
56 
57  void SyncStream() {
58 #ifdef WITH_GPU
59  FDASSERT(cudaStreamSynchronize(stream_) == cudaSuccess,
60  "[ERROR] Error occurs while sync cuda stream.");
61 #endif
62  }
63 
64  int DeviceId() { return device_id_; }
65 
72  bool Run(std::vector<FDMat>* images, std::vector<FDTensor>* outputs);
73 
80  virtual bool Apply(FDMatBatch* image_batch,
81  std::vector<FDTensor>* outputs) = 0;
82 
83  void PreApply(FDMatBatch* image_batch);
84 
85  void PostApply();
86 
87  protected:
88  ProcLib proc_lib_ = ProcLib::DEFAULT;
89 
90  private:
91 #ifdef WITH_GPU
92  cudaStream_t stream_ = nullptr;
93 #endif
94  int device_id_ = -1;
95 
96  std::vector<FDTensor> input_caches_;
97  std::vector<FDTensor> output_caches_;
98  FDTensor batch_input_cache_;
99  FDTensor batch_output_cache_;
100 };
101 
102 } // namespace vision
103 } // namespace fastdeploy
FDTensor object used to represend data matrix.
Definition: fd_tensor.h:31
FDMat is a structure for replace cv::Mat.
Definition: mat.h:34
FDMatBatch contains batch data for preprocess.
Definition: mat_batch.h:28
ProcessorManager for Preprocess.
Definition: manager.h:27
All C++ FastDeploy APIs are defined inside this namespace.
Definition: option.h:16