You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

custom-allocator.md 2.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. Mat structure is now allocator-aware via an extra allocator parameter with default zero value.
  2. The good-old ncnn::fastMalloc()/ncnn::fastFree() will be used for a null allocator.
  3. You could pass a custom allocator to delegate all memory allocation and deallocation.
  4. ```cpp
  5. class Allocator
  6. {
  7. public:
  8. virtual void* fastMalloc(size_t size) = 0;
  9. virtual void fastFree(void* ptr) = 0;
  10. };
  11. ```
  12. ncnn has already implemented two simple pooled Allocator class, with mutex lock or without it.
  13. ```cpp
  14. ncnn::PoolAllocator locked_mempool;
  15. ncnn::UnlockedPoolAllocator unlocked_mempool;
  16. ```
  17. the two allocator types in ncnn
  18. * blob allocator
  19. used to allocate memory for all named blobs, which you could retrieve by Extractor::extract()
  20. * workspace allocator
  21. used to allocate memory for internal temporary use in layer implementation, such as the temp blob after padding in convolution
  22. by default, all Extractor instance use the two allocator in the default option
  23. You can alter them by ncnn::set_default_option()
  24. or you can set them per Extractor by Extractor::set_blob_allocator()/Extractor::set_workspace_allocator()
  25. blob allocator is guaranteed to be called in-order in layer implementation during each Extractor lifecycle
  26. while workspace allocator may be called synchronously
  27. the practical usage
  28. * one network, one-by-one inference
  29. shared unlocked blob allocator for all Extractor
  30. shared locked workspace allocator for all Extractor
  31. * one network, concurrent inference
  32. shared unlocked blob allocator for all Extractor in each thread
  33. shared locked workspace allocator for all Extractor among all threads
  34. * concurrent multiple networks, one-by-one inference for each network
  35. shared unlocked blob allocator for all Extractor of each network
  36. shared locked workspace allocator for all Extractor among all networks (for saving memory)
  37. * concurrent multiple networks, concurrent inference for each network
  38. shared unlocked blob allocator for all Extractor of each network in each thread
  39. shared locked workspace allocator for all Extractor among all networks (for saving memory)