ClassGpuAllocator.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. // Copyright (C) 2009-2020, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <anki/gr/utils/ClassGpuAllocator.h>
  6. #include <tests/framework/Framework.h>
  7. #include <random>
  8. #include <algorithm>
  9. namespace anki
  10. {
  11. class Mem : public ClassGpuAllocatorMemory
  12. {
  13. public:
  14. void* m_mem = nullptr;
  15. PtrSize m_size = 0;
  16. };
  17. class Interface final : public ClassGpuAllocatorInterface
  18. {
  19. public:
  20. class Class
  21. {
  22. public:
  23. Class(PtrSize slot, PtrSize cluster)
  24. : m_slotSize(slot)
  25. , m_clusterSize(cluster)
  26. {
  27. }
  28. PtrSize m_slotSize;
  29. PtrSize m_clusterSize;
  30. };
  31. std::vector<Class> m_classes;
  32. PtrSize m_maxSize = 128 * 1024 * 1024;
  33. PtrSize m_crntSize = 0;
  34. Interface()
  35. {
  36. m_classes.push_back(Class(256, 16 * 1024));
  37. m_classes.push_back(Class(4 * 1024, 256 * 1024));
  38. m_classes.push_back(Class(128 * 1024, 8 * 1024 * 1024));
  39. m_classes.push_back(Class(1 * 1024 * 1024, 32 * 1024 * 1024));
  40. m_classes.push_back(Class(16 * 1024 * 1024, 128 * 1024 * 1024));
  41. m_classes.push_back(Class(64 * 1024 * 1024, 256 * 1024 * 1024));
  42. m_classes.push_back(Class(128 * 1024 * 1024, 256 * 1024 * 1024));
  43. }
  44. ANKI_USE_RESULT Error allocate(U32 classIdx, ClassGpuAllocatorMemory*& mem)
  45. {
  46. PtrSize size = m_classes[classIdx].m_clusterSize;
  47. if(m_crntSize + size > m_maxSize)
  48. {
  49. return Error::OUT_OF_MEMORY;
  50. }
  51. PtrSize alignment = 256;
  52. Mem* m = new Mem();
  53. m_crntSize += size;
  54. m->m_mem = mallocAligned(size, alignment);
  55. m->m_size = size;
  56. mem = m;
  57. return Error::NONE;
  58. }
  59. void free(ClassGpuAllocatorMemory* mem)
  60. {
  61. Mem* m = static_cast<Mem*>(mem);
  62. m_crntSize -= m->m_size;
  63. freeAligned(m->m_mem);
  64. delete m;
  65. }
  66. U32 getClassCount() const
  67. {
  68. return U32(m_classes.size());
  69. }
  70. void getClassInfo(U32 classIdx, PtrSize& slotSize, PtrSize& chunkSize) const
  71. {
  72. slotSize = m_classes[classIdx].m_slotSize;
  73. chunkSize = m_classes[classIdx].m_clusterSize;
  74. }
  75. };
  76. static inline U32 floorPow2(U32 v)
  77. {
  78. v |= v >> 16;
  79. v |= v >> 8;
  80. v |= v >> 4;
  81. v |= v >> 2;
  82. v |= v >> 1;
  83. v++;
  84. return v >> 1;
  85. }
  86. ANKI_TEST(Gr, ClassGpuAllocator)
  87. {
  88. HeapAllocator<U8> alloc(allocAligned, nullptr);
  89. Interface iface;
  90. ClassGpuAllocator calloc;
  91. calloc.init(alloc, &iface);
  92. std::mt19937 gen(0);
  93. const U SHIFT = 15;
  94. std::discrete_distribution<U> dis(16 * SHIFT, 0.0, F32(SHIFT), [](F32 c) { return exp2(-0.5 * c); });
  95. auto nextAllocSize = [&]() -> U {
  96. U size = U(256.0 * exp2(F64(dis(gen)) / 16.0));
  97. return size;
  98. };
  99. std::vector<ClassGpuAllocatorHandle> handles;
  100. const U TEST_COUNT = 100;
  101. const U ITERATIONS = 20;
  102. for(U tests = 0; tests < TEST_COUNT; ++tests)
  103. {
  104. for(U i = 0; i < ITERATIONS; ++i)
  105. {
  106. // Fill up the heap.
  107. while(1)
  108. {
  109. ClassGpuAllocatorHandle handle;
  110. PtrSize size = nextAllocSize();
  111. if(calloc.allocate(size, 1, handle))
  112. {
  113. break;
  114. }
  115. handles.push_back(handle);
  116. }
  117. std::shuffle(handles.begin(), handles.end(), gen);
  118. U halfSize = (handles.size() * 3) / 4;
  119. for(U i = halfSize; i < handles.size(); ++i)
  120. {
  121. calloc.free(handles[i]);
  122. }
  123. handles.erase(handles.begin() + halfSize, handles.end());
  124. }
  125. // The heap should be roughly half-full now, so test fragmentation.
  126. U32 freeSize = U32(iface.m_maxSize - iface.m_crntSize);
  127. U32 baseFreeSize = floorPow2(freeSize);
  128. const U32 BASE_SIZE = 256;
  129. const F32 BIAS = 0.0;
  130. const F32 POWER = 1.2f;
  131. const F32 OFFSET = -1.0f;
  132. F32 bestCase = 0.0;
  133. {
  134. // Best case is when we can allocate once for every bit that is set in the pow2 structure.
  135. U32 freeBits = freeSize / BASE_SIZE;
  136. for(U32 bit = 0; bit < 32; bit++)
  137. {
  138. if(freeBits & (1u << bit))
  139. {
  140. bestCase += (pow(POWER, F32(BIAS + F32(bit))) + OFFSET) * F32(BASE_SIZE << bit);
  141. }
  142. }
  143. }
  144. F32 score = 0.0;
  145. while(baseFreeSize >= BASE_SIZE)
  146. {
  147. ClassGpuAllocatorHandle handle;
  148. while(calloc.allocate(baseFreeSize, 1, handle) == Error::NONE)
  149. {
  150. score += (pow(POWER, (log2(F32(baseFreeSize / BASE_SIZE)) + BIAS)) + OFFSET) * F32(baseFreeSize);
  151. handles.push_back(handle);
  152. handle = {};
  153. }
  154. baseFreeSize >>= 1;
  155. }
  156. printf("Score: %.3f\n", score / bestCase);
  157. // Cleanup
  158. for(ClassGpuAllocatorHandle& h : handles)
  159. {
  160. calloc.free(h);
  161. }
  162. handles.clear();
  163. }
  164. }
  165. } // end namespace anki