memory_pool_impl.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #ifndef DEPTHGUIDE_MEMORY_POOL_IMPL_H
  2. #define DEPTHGUIDE_MEMORY_POOL_IMPL_H
  3. #include "core/memory_pool.h"
  4. #include <boost/asio/io_context.hpp>
  5. #include <list>
  6. #include <map>
  7. #include <mutex>
  8. #include <thread>
  9. struct memory_pool::impl {
  10. // reuse_length * reuse_threshold >= request_length
  11. static constexpr auto reuse_threshold = 0.5;
  12. enum memory_layout : uint8_t {
  13. MEM_LINEAR, MEM_PITCH
  14. };
  15. struct mem_info_type {
  16. void *ptr;
  17. memory_location loc;
  18. memory_layout lay;
  19. // for MEM_LINEAR and MEM_PITCH
  20. size_t count;
  21. cudaEvent_t event = nullptr;
  22. };
  23. using malloc_pool_type = std::map<void *, mem_info_type, std::greater<>>;
  24. malloc_pool_type malloc_pool;
  25. using reuse_pool_type = std::multimap<size_t, mem_info_type>;
  26. reuse_pool_type reuse_host_pool;
  27. reuse_pool_type reuse_cuda_pool;
  28. std::mutex mu;
  29. void reg_allocate(mem_info_type mem_info);
  30. void *try_reuse_host(size_t count);
  31. void *try_reuse_cuda(size_t count);
  32. void *direct_allocate_host(size_t count);
  33. void *direct_allocate_cuda(size_t count);
  34. void *allocate_host(size_t count);
  35. void *allocate_cuda(size_t count);
  36. void *allocate(size_t count, memory_location mem_loc);
  37. void *allocate_pitch(size_t width, size_t rows, memory_location mem_loc, size_t *pitch);
  38. cudaEvent_t get_event(void *ptr);
  39. static void system_deallocate(mem_info_type mem_info);
  40. void deallocate(void *ptr);
  41. void purge();
  42. };
  43. #endif //DEPTHGUIDE_MEMORY_POOL_IMPL_H