2
0

ClassGpuAllocator.cpp 4.3 KB

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