Elffuss BraidHub

kikocis/ncnn public

master · 2.1 KiBRenderedRawPermalink
1Mat structure is now allocator-aware via an extra allocator parameter with default zero value.
2
3The good-old ncnn::fastMalloc()/ncnn::fastFree() will be used for a null allocator.
4
5You could pass a custom allocator to delegate all memory allocation and deallocation.
6
7```cpp
8class Allocator
9{
10public:
11 virtual void* fastMalloc(size_t size) = 0;
12 virtual void fastFree(void* ptr) = 0;
13};
14```
15
16ncnn has already implemented two simple pooled Allocator class, with mutex lock or without it.
17
18```cpp
19ncnn::PoolAllocator locked_mempool;
20ncnn::UnlockedPoolAllocator unlocked_mempool;
21```
22
23the 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
32by default, all Extractor instance use the two allocator in the default option
33You can alter them by ncnn::set_default_option()
34or you can set them per Extractor by Extractor::set_blob_allocator()/Extractor::set_workspace_allocator()
35
36blob allocator is guaranteed to be called in-order in layer implementation during each Extractor lifecycle
37while workspace allocator may be called synchronously
38
39the 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)