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