| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2018 THL A29 Limited, a Tencent company. All rights reserved. |
| 4 | // |
| 5 | // Licensed under the BSD 3-Clause License (the "License"); you may not use this file except |
| 6 | // in compliance with the License. You may obtain a copy of the License at |
| 7 | // |
| 8 | // https://opensource.org/licenses/BSD-3-Clause |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software distributed |
| 11 | // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 12 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 13 | // specific language governing permissions and limitations under the License. |
| 14 | |
| 15 | #ifndef NCNN_ALLOCATOR_H |
| 16 | #define NCNN_ALLOCATOR_H |
| 17 | |
| 18 | #ifdef _WIN32 |
| 19 | #define WIN32_LEAN_AND_MEAN |
| 20 | #include <windows.h> |
| 21 | #endif |
| 22 | |
| 23 | #include "platform.h" |
| 24 | |
| 25 | #include <stdlib.h> |
| 26 | |
| 27 | #if NCNN_PLATFORM_API |
| 28 | #if __ANDROID_API__ >= 26 |
| 29 | #include <android/hardware_buffer.h> |
| 30 | #endif // __ANDROID_API__ >= 26 |
| 31 | #endif // NCNN_PLATFORM_API |
| 32 | |
| 33 | namespace ncnn { |
| 34 | |
| 35 | // the alignment of all the allocated buffers |
| 36 | #if NCNN_AVX512 |
| 37 | #define NCNN_MALLOC_ALIGN 64 |
| 38 | #elif NCNN_AVX |
| 39 | #define NCNN_MALLOC_ALIGN 32 |
| 40 | #else |
| 41 | #define NCNN_MALLOC_ALIGN 16 |
| 42 | #endif |
| 43 | |
| 44 | // we have some optimized kernels that may overread buffer a bit in loop |
| 45 | // it is common to interleave next-loop data load with arithmetic instructions |
| 46 | // allocating more bytes keeps us safe from SEGV_ACCERR failure |
| 47 | #define NCNN_MALLOC_OVERREAD 64 |
| 48 | |
| 49 | // Aligns a pointer to the specified number of bytes |
| 50 | // ptr Aligned pointer |
| 51 | // n Alignment size that must be a power of two |
| 52 | template<typename _Tp> |
| 53 | static NCNN_FORCEINLINE _Tp* alignPtr(_Tp* ptr, int n = (int)sizeof(_Tp)) |
| 54 | { |
| 55 | return (_Tp*)(((size_t)ptr + n - 1) & -n); |
| 56 | } |
| 57 | |
| 58 | // Aligns a buffer size to the specified number of bytes |
| 59 | // The function returns the minimum number that is greater or equal to sz and is divisible by n |
| 60 | // sz Buffer size to align |
| 61 | // n Alignment size that must be a power of two |
| 62 | static NCNN_FORCEINLINE size_t alignSize(size_t sz, int n) |
| 63 | { |
| 64 | return (sz + n - 1) & -n; |
| 65 | } |
| 66 | |
| 67 | static NCNN_FORCEINLINE void* fastMalloc(size_t size) |
| 68 | { |
| 69 | #if _MSC_VER |
| 70 | return _aligned_malloc(size, NCNN_MALLOC_ALIGN); |
| 71 | #elif (defined(__unix__) || defined(__APPLE__)) && _POSIX_C_SOURCE >= 200112L || (__ANDROID__ && __ANDROID_API__ >= 17) |
| 72 | void* ptr = 0; |
| 73 | if (posix_memalign(&ptr, NCNN_MALLOC_ALIGN, size + NCNN_MALLOC_OVERREAD)) |
| 74 | ptr = 0; |
| 75 | return ptr; |
| 76 | #elif __ANDROID__ && __ANDROID_API__ < 17 |
| 77 | return memalign(NCNN_MALLOC_ALIGN, size + NCNN_MALLOC_OVERREAD); |
| 78 | #else |
| 79 | unsigned char* udata = (unsigned char*)malloc(size + sizeof(void*) + NCNN_MALLOC_ALIGN + NCNN_MALLOC_OVERREAD); |
| 80 | if (!udata) |
| 81 | return 0; |
| 82 | unsigned char** adata = alignPtr((unsigned char**)udata + 1, NCNN_MALLOC_ALIGN); |
| 83 | adata[-1] = udata; |
| 84 | return adata; |
| 85 | #endif |
| 86 | } |
| 87 | |
| 88 | static NCNN_FORCEINLINE void fastFree(void* ptr) |
| 89 | { |
| 90 | if (ptr) |
| 91 | { |
| 92 | #if _MSC_VER |
| 93 | _aligned_free(ptr); |
| 94 | #elif (defined(__unix__) || defined(__APPLE__)) && _POSIX_C_SOURCE >= 200112L || (__ANDROID__ && __ANDROID_API__ >= 17) |
| 95 | free(ptr); |
| 96 | #elif __ANDROID__ && __ANDROID_API__ < 17 |
| 97 | free(ptr); |
| 98 | #else |
| 99 | unsigned char* udata = ((unsigned char**)ptr)[-1]; |
| 100 | free(udata); |
| 101 | #endif |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | #if NCNN_THREADS |
| 106 | // exchange-add operation for atomic operations on reference counters |
| 107 | #if defined __riscv && !defined __riscv_atomic |
| 108 | // riscv target without A extension |
| 109 | static NCNN_FORCEINLINE int NCNN_XADD(int* addr, int delta) |
| 110 | { |
| 111 | int tmp = *addr; |
| 112 | *addr += delta; |
| 113 | return tmp; |
| 114 | } |
| 115 | #elif defined __INTEL_COMPILER && !(defined WIN32 || defined _WIN32) |
| 116 | // atomic increment on the linux version of the Intel(tm) compiler |
| 117 | #define NCNN_XADD(addr, delta) (int)_InterlockedExchangeAdd(const_cast<void*>(reinterpret_cast<volatile void*>(addr)), delta) |
| 118 | #elif defined __GNUC__ |
| 119 | #if defined __clang__ && __clang_major__ >= 3 && !defined __ANDROID__ && !defined __EMSCRIPTEN__ && !defined(__CUDACC__) |
| 120 | #ifdef __ATOMIC_ACQ_REL |
| 121 | #define NCNN_XADD(addr, delta) __c11_atomic_fetch_add((_Atomic(int)*)(addr), delta, __ATOMIC_ACQ_REL) |
| 122 | #else |
| 123 | #define NCNN_XADD(addr, delta) __atomic_fetch_add((_Atomic(int)*)(addr), delta, 4) |
| 124 | #endif |
| 125 | #else |
| 126 | #if defined __ATOMIC_ACQ_REL && !defined __clang__ |
| 127 | // version for gcc >= 4.7 |
| 128 | #define NCNN_XADD(addr, delta) (int)__atomic_fetch_add((unsigned*)(addr), (unsigned)(delta), __ATOMIC_ACQ_REL) |
| 129 | #else |
| 130 | #define NCNN_XADD(addr, delta) (int)__sync_fetch_and_add((unsigned*)(addr), (unsigned)(delta)) |
| 131 | #endif |
| 132 | #endif |
| 133 | #elif defined _MSC_VER && !defined RC_INVOKED |
| 134 | #define NCNN_XADD(addr, delta) (int)_InterlockedExchangeAdd((long volatile*)addr, delta) |
| 135 | #else |
| 136 | // thread-unsafe branch |
| 137 | static NCNN_FORCEINLINE int NCNN_XADD(int* addr, int delta) |
| 138 | { |
| 139 | int tmp = *addr; |
| 140 | *addr += delta; |
| 141 | return tmp; |
| 142 | } |
| 143 | #endif |
| 144 | #else // NCNN_THREADS |
| 145 | static NCNN_FORCEINLINE int NCNN_XADD(int* addr, int delta) |
| 146 | { |
| 147 | int tmp = *addr; |
| 148 | *addr += delta; |
| 149 | return tmp; |
| 150 | } |
| 151 | #endif // NCNN_THREADS |
| 152 | |
| 153 | class NCNN_EXPORT Allocator |
| 154 | { |
| 155 | public: |
| 156 | virtual ~Allocator(); |
| 157 | virtual void* fastMalloc(size_t size) = 0; |
| 158 | virtual void fastFree(void* ptr) = 0; |
| 159 | }; |
| 160 | |
| 161 | class PoolAllocatorPrivate; |
| 162 | class NCNN_EXPORT PoolAllocator : public Allocator |
| 163 | { |
| 164 | public: |
| 165 | PoolAllocator(); |
| 166 | ~PoolAllocator(); |
| 167 | |
| 168 | // ratio range 0 ~ 1 |
| 169 | // default cr = 0 |
| 170 | void set_size_compare_ratio(float scr); |
| 171 | |
| 172 | // budget drop threshold |
| 173 | // default threshold = 10 |
| 174 | void set_size_drop_threshold(size_t); |
| 175 | |
| 176 | // release all budgets immediately |
| 177 | void clear(); |
| 178 | |
| 179 | virtual void* fastMalloc(size_t size); |
| 180 | virtual void fastFree(void* ptr); |
| 181 | |
| 182 | private: |
| 183 | PoolAllocator(const PoolAllocator&); |
| 184 | PoolAllocator& operator=(const PoolAllocator&); |
| 185 | |
| 186 | private: |
| 187 | PoolAllocatorPrivate* const d; |
| 188 | }; |
| 189 | |
| 190 | class UnlockedPoolAllocatorPrivate; |
| 191 | class NCNN_EXPORT UnlockedPoolAllocator : public Allocator |
| 192 | { |
| 193 | public: |
| 194 | UnlockedPoolAllocator(); |
| 195 | ~UnlockedPoolAllocator(); |
| 196 | |
| 197 | // ratio range 0 ~ 1 |
| 198 | // default cr = 0 |
| 199 | void set_size_compare_ratio(float scr); |
| 200 | |
| 201 | // budget drop threshold |
| 202 | // default threshold = 10 |
| 203 | void set_size_drop_threshold(size_t); |
| 204 | |
| 205 | // release all budgets immediately |
| 206 | void clear(); |
| 207 | |
| 208 | virtual void* fastMalloc(size_t size); |
| 209 | virtual void fastFree(void* ptr); |
| 210 | |
| 211 | private: |
| 212 | UnlockedPoolAllocator(const UnlockedPoolAllocator&); |
| 213 | UnlockedPoolAllocator& operator=(const UnlockedPoolAllocator&); |
| 214 | |
| 215 | private: |
| 216 | UnlockedPoolAllocatorPrivate* const d; |
| 217 | }; |
| 218 | |
| 219 | #if NCNN_VULKAN |
| 220 | |
| 221 | class VulkanDevice; |
| 222 | |
| 223 | class NCNN_EXPORT VkBufferMemory |
| 224 | { |
| 225 | public: |
| 226 | VkBuffer buffer; |
| 227 | |
| 228 | // the base offset assigned by allocator |
| 229 | size_t offset; |
| 230 | size_t capacity; |
| 231 | |
| 232 | VkDeviceMemory memory; |
| 233 | void* mapped_ptr; |
| 234 | |
| 235 | // buffer state, modified by command functions internally |
| 236 | mutable VkAccessFlags access_flags; |
| 237 | mutable VkPipelineStageFlags stage_flags; |
| 238 | |
| 239 | // initialize and modified by mat |
| 240 | int refcount; |
| 241 | }; |
| 242 | |
| 243 | class NCNN_EXPORT VkImageMemory |
| 244 | { |
| 245 | public: |
| 246 | VkImage image; |
| 247 | VkImageView imageview; |
| 248 | |
| 249 | // underlying info assigned by allocator |
| 250 | int width; |
| 251 | int height; |
| 252 | int depth; |
| 253 | VkFormat format; |
| 254 | |
| 255 | VkDeviceMemory memory; |
| 256 | void* mapped_ptr; |
| 257 | |
| 258 | // the base offset assigned by allocator |
| 259 | size_t bind_offset; |
| 260 | size_t bind_capacity; |
| 261 | |
| 262 | // image state, modified by command functions internally |
| 263 | mutable VkAccessFlags access_flags; |
| 264 | mutable VkImageLayout image_layout; |
| 265 | mutable VkPipelineStageFlags stage_flags; |
| 266 | |
| 267 | // in-execution state, modified by command functions internally |
| 268 | mutable int command_refcount; |
| 269 | |
| 270 | // initialize and modified by mat |
| 271 | int refcount; |
| 272 | }; |
| 273 | |
| 274 | class NCNN_EXPORT VkAllocator |
| 275 | { |
| 276 | public: |
| 277 | explicit VkAllocator(const VulkanDevice* _vkdev); |
| 278 | virtual ~VkAllocator(); |
| 279 | |
| 280 | virtual void clear(); |
| 281 | |
| 282 | virtual VkBufferMemory* fastMalloc(size_t size) = 0; |
| 283 | virtual void fastFree(VkBufferMemory* ptr) = 0; |
| 284 | virtual int flush(VkBufferMemory* ptr); |
| 285 | virtual int invalidate(VkBufferMemory* ptr); |
| 286 | |
| 287 | virtual VkImageMemory* fastMalloc(int w, int h, int c, size_t elemsize, int elempack) = 0; |
| 288 | virtual void fastFree(VkImageMemory* ptr) = 0; |
| 289 | |
| 290 | public: |
| 291 | const VulkanDevice* vkdev; |
| 292 | uint32_t buffer_memory_type_index; |
| 293 | uint32_t image_memory_type_index; |
| 294 | uint32_t reserved_type_index; |
| 295 | bool mappable; |
| 296 | bool coherent; |
| 297 | |
| 298 | protected: |
| 299 | VkBuffer create_buffer(size_t size, VkBufferUsageFlags usage); |
| 300 | VkDeviceMemory allocate_memory(size_t size, uint32_t memory_type_index); |
| 301 | VkDeviceMemory allocate_dedicated_memory(size_t size, uint32_t memory_type_index, VkImage image, VkBuffer buffer); |
| 302 | |
| 303 | VkImage create_image(int width, int height, int depth, VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage); |
| 304 | VkImageView create_imageview(VkImage image, VkFormat format); |
| 305 | }; |
| 306 | |
| 307 | class VkBlobAllocatorPrivate; |
| 308 | class NCNN_EXPORT VkBlobAllocator : public VkAllocator |
| 309 | { |
| 310 | public: |
| 311 | explicit VkBlobAllocator(const VulkanDevice* vkdev, size_t preferred_block_size = 16 * 1024 * 1024); // 16M |
| 312 | virtual ~VkBlobAllocator(); |
| 313 | |
| 314 | public: |
| 315 | // release all budgets immediately |
| 316 | virtual void clear(); |
| 317 | |
| 318 | virtual VkBufferMemory* fastMalloc(size_t size); |
| 319 | virtual void fastFree(VkBufferMemory* ptr); |
| 320 | virtual VkImageMemory* fastMalloc(int w, int h, int c, size_t elemsize, int elempack); |
| 321 | virtual void fastFree(VkImageMemory* ptr); |
| 322 | |
| 323 | private: |
| 324 | VkBlobAllocator(const VkBlobAllocator&); |
| 325 | VkBlobAllocator& operator=(const VkBlobAllocator&); |
| 326 | |
| 327 | private: |
| 328 | VkBlobAllocatorPrivate* const d; |
| 329 | }; |
| 330 | |
| 331 | class VkWeightAllocatorPrivate; |
| 332 | class NCNN_EXPORT VkWeightAllocator : public VkAllocator |
| 333 | { |
| 334 | public: |
| 335 | explicit VkWeightAllocator(const VulkanDevice* vkdev, size_t preferred_block_size = 8 * 1024 * 1024); // 8M |
| 336 | virtual ~VkWeightAllocator(); |
| 337 | |
| 338 | public: |
| 339 | // release all blocks immediately |
| 340 | virtual void clear(); |
| 341 | |
| 342 | public: |
| 343 | virtual VkBufferMemory* fastMalloc(size_t size); |
| 344 | virtual void fastFree(VkBufferMemory* ptr); |
| 345 | virtual VkImageMemory* fastMalloc(int w, int h, int c, size_t elemsize, int elempack); |
| 346 | virtual void fastFree(VkImageMemory* ptr); |
| 347 | |
| 348 | private: |
| 349 | VkWeightAllocator(const VkWeightAllocator&); |
| 350 | VkWeightAllocator& operator=(const VkWeightAllocator&); |
| 351 | |
| 352 | private: |
| 353 | VkWeightAllocatorPrivate* const d; |
| 354 | }; |
| 355 | |
| 356 | class VkStagingAllocatorPrivate; |
| 357 | class NCNN_EXPORT VkStagingAllocator : public VkAllocator |
| 358 | { |
| 359 | public: |
| 360 | explicit VkStagingAllocator(const VulkanDevice* vkdev); |
| 361 | virtual ~VkStagingAllocator(); |
| 362 | |
| 363 | public: |
| 364 | // ratio range 0 ~ 1 |
| 365 | // default cr = 0.75 |
| 366 | void set_size_compare_ratio(float scr); |
| 367 | |
| 368 | // release all budgets immediately |
| 369 | virtual void clear(); |
| 370 | |
| 371 | virtual VkBufferMemory* fastMalloc(size_t size); |
| 372 | virtual void fastFree(VkBufferMemory* ptr); |
| 373 | virtual VkImageMemory* fastMalloc(int w, int h, int c, size_t elemsize, int elempack); |
| 374 | virtual void fastFree(VkImageMemory* ptr); |
| 375 | |
| 376 | private: |
| 377 | VkStagingAllocator(const VkStagingAllocator&); |
| 378 | VkStagingAllocator& operator=(const VkStagingAllocator&); |
| 379 | |
| 380 | private: |
| 381 | VkStagingAllocatorPrivate* const d; |
| 382 | }; |
| 383 | |
| 384 | class VkWeightStagingAllocatorPrivate; |
| 385 | class NCNN_EXPORT VkWeightStagingAllocator : public VkAllocator |
| 386 | { |
| 387 | public: |
| 388 | explicit VkWeightStagingAllocator(const VulkanDevice* vkdev); |
| 389 | virtual ~VkWeightStagingAllocator(); |
| 390 | |
| 391 | public: |
| 392 | virtual VkBufferMemory* fastMalloc(size_t size); |
| 393 | virtual void fastFree(VkBufferMemory* ptr); |
| 394 | virtual VkImageMemory* fastMalloc(int w, int h, int c, size_t elemsize, int elempack); |
| 395 | virtual void fastFree(VkImageMemory* ptr); |
| 396 | |
| 397 | private: |
| 398 | VkWeightStagingAllocator(const VkWeightStagingAllocator&); |
| 399 | VkWeightStagingAllocator& operator=(const VkWeightStagingAllocator&); |
| 400 | |
| 401 | private: |
| 402 | VkWeightStagingAllocatorPrivate* const d; |
| 403 | }; |
| 404 | |
| 405 | #if NCNN_PLATFORM_API |
| 406 | #if __ANDROID_API__ >= 26 |
| 407 | class NCNN_EXPORT VkAndroidHardwareBufferImageAllocator : public VkAllocator |
| 408 | { |
| 409 | public: |
| 410 | VkAndroidHardwareBufferImageAllocator(const VulkanDevice* _vkdev, AHardwareBuffer* _hb); |
| 411 | virtual ~VkAndroidHardwareBufferImageAllocator(); |
| 412 | |
| 413 | public: |
| 414 | virtual VkBufferMemory* fastMalloc(size_t size); |
| 415 | virtual void fastFree(VkBufferMemory* ptr); |
| 416 | virtual VkImageMemory* fastMalloc(int w, int h, int c, size_t elemsize, int elempack); |
| 417 | virtual void fastFree(VkImageMemory* ptr); |
| 418 | |
| 419 | private: |
| 420 | VkAndroidHardwareBufferImageAllocator(const VkAndroidHardwareBufferImageAllocator&); |
| 421 | VkAndroidHardwareBufferImageAllocator& operator=(const VkAndroidHardwareBufferImageAllocator&); |
| 422 | |
| 423 | public: |
| 424 | int init(); |
| 425 | |
| 426 | int width() const; |
| 427 | int height() const; |
| 428 | uint64_t external_format() const; |
| 429 | |
| 430 | public: |
| 431 | AHardwareBuffer* hb; |
| 432 | AHardwareBuffer_Desc bufferDesc; |
| 433 | VkAndroidHardwareBufferFormatPropertiesANDROID bufferFormatProperties; |
| 434 | VkAndroidHardwareBufferPropertiesANDROID bufferProperties; |
| 435 | VkSamplerYcbcrConversionKHR samplerYcbcrConversion; |
| 436 | }; |
| 437 | #endif // __ANDROID_API__ >= 26 |
| 438 | #endif // NCNN_PLATFORM_API |
| 439 | |
| 440 | #endif // NCNN_VULKAN |
| 441 | |
| 442 | } // namespace ncnn |
| 443 | |
| 444 | #endif // NCNN_ALLOCATOR_H |