inferutils.h 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #ifndef __TRT_INFER_H
  2. #define __TRT_INFER_H
  3. #define BATCH_SIZE 1
  4. #include "NvInfer.h"
  5. #include "NvOnnxParser.h"
  6. #include "NvInferRuntime.h"
  7. #include <cuda_runtime_api.h>
  8. #include <cuda_runtime.h>
  9. #include <cuda.h>
  10. #include <cmath>
  11. #include <experimental/filesystem>
  12. #include <cstdlib>
  13. #include <fstream>
  14. #include <iostream>
  15. #include <chrono>
  16. #include <string.h>
  17. /*
  18. This function allocates memory on host and provides a cpu pointer and
  19. gpu pointer for use with cuda functions. Since the memory on jetson
  20. is unified, the two pointers point to the same physical memory location.
  21. Notes:
  22. * This function is not x86 compatible, and works only an embedded platform
  23. */
  24. bool zero_copy_malloc(void** cpu_ptr, void** gpu_ptr, size_t size);
  25. bool file_exists(std::string filename);
  26. bool save_to_disk(nvinfer1::ICudaEngine* eg, std::string filename);
  27. //bool read_engine_from_disk(nvinfer1::ICudaEngine* eg, std::string enginepath);
  28. bool read_engine_from_disk(char* estream, size_t esize, const std::string enginepath);
  29. cudaStream_t create_cuda_stream(bool nonblocking);
  30. //struct iobinding
  31. class iobinding
  32. {
  33. public:
  34. nvinfer1::Dims dims;
  35. std::string name;
  36. float* cpu_ptr=nullptr;
  37. float* gpu_ptr=nullptr;
  38. uint32_t size=0;
  39. uint32_t binding;
  40. uint32_t get_size();
  41. void allocate_buffers();
  42. void destroy_buffers();
  43. void* tCPU;
  44. void* tGPU;
  45. };
  46. iobinding get_engine_bindings(nvinfer1::ICudaEngine* eg, const char* name, bool is_onnx);
  47. #endif