b3OpenCLArray.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. #ifndef B3_OPENCL_ARRAY_H
  2. #define B3_OPENCL_ARRAY_H
  3. #include "Bullet3Common/b3AlignedObjectArray.h"
  4. #include "Bullet3OpenCL/Initialize/b3OpenCLInclude.h"
  5. template <typename T>
  6. class b3OpenCLArray
  7. {
  8. size_t m_size;
  9. size_t m_capacity;
  10. cl_mem m_clBuffer;
  11. cl_context m_clContext;
  12. cl_command_queue m_commandQueue;
  13. bool m_ownsMemory;
  14. bool m_allowGrowingCapacity;
  15. void deallocate()
  16. {
  17. if (m_clBuffer && m_ownsMemory)
  18. {
  19. clReleaseMemObject(m_clBuffer);
  20. }
  21. m_clBuffer = 0;
  22. m_capacity = 0;
  23. }
  24. b3OpenCLArray<T>& operator=(const b3OpenCLArray<T>& src);
  25. B3_FORCE_INLINE size_t allocSize(size_t size)
  26. {
  27. return (size ? size * 2 : 1);
  28. }
  29. public:
  30. b3OpenCLArray(cl_context ctx, cl_command_queue queue, size_t initialCapacity = 0, bool allowGrowingCapacity = true)
  31. : m_size(0), m_capacity(0), m_clBuffer(0), m_clContext(ctx), m_commandQueue(queue), m_ownsMemory(true), m_allowGrowingCapacity(true)
  32. {
  33. if (initialCapacity)
  34. {
  35. reserve(initialCapacity);
  36. }
  37. m_allowGrowingCapacity = allowGrowingCapacity;
  38. }
  39. ///this is an error-prone method with no error checking, be careful!
  40. void setFromOpenCLBuffer(cl_mem buffer, size_t sizeInElements)
  41. {
  42. deallocate();
  43. m_ownsMemory = false;
  44. m_allowGrowingCapacity = false;
  45. m_clBuffer = buffer;
  46. m_size = sizeInElements;
  47. m_capacity = sizeInElements;
  48. }
  49. // we could enable this assignment, but need to make sure to avoid accidental deep copies
  50. // b3OpenCLArray<T>& operator=(const b3AlignedObjectArray<T>& src)
  51. // {
  52. // copyFromArray(src);
  53. // return *this;
  54. // }
  55. cl_mem getBufferCL() const
  56. {
  57. return m_clBuffer;
  58. }
  59. virtual ~b3OpenCLArray()
  60. {
  61. deallocate();
  62. m_size = 0;
  63. m_capacity = 0;
  64. }
  65. B3_FORCE_INLINE bool push_back(const T& _Val, bool waitForCompletion = true)
  66. {
  67. bool result = true;
  68. size_t sz = size();
  69. if (sz == capacity())
  70. {
  71. result = reserve(allocSize(size()));
  72. }
  73. copyFromHostPointer(&_Val, 1, sz, waitForCompletion);
  74. m_size++;
  75. return result;
  76. }
  77. B3_FORCE_INLINE T forcedAt(size_t n) const
  78. {
  79. b3Assert(n >= 0);
  80. b3Assert(n < capacity());
  81. T elem;
  82. copyToHostPointer(&elem, 1, n, true);
  83. return elem;
  84. }
  85. B3_FORCE_INLINE T at(size_t n) const
  86. {
  87. b3Assert(n >= 0);
  88. b3Assert(n < size());
  89. T elem;
  90. copyToHostPointer(&elem, 1, n, true);
  91. return elem;
  92. }
  93. B3_FORCE_INLINE bool resize(size_t newsize, bool copyOldContents = true)
  94. {
  95. bool result = true;
  96. size_t curSize = size();
  97. if (newsize < curSize)
  98. {
  99. //leave the OpenCL memory for now
  100. }
  101. else
  102. {
  103. if (newsize > size())
  104. {
  105. result = reserve(newsize, copyOldContents);
  106. }
  107. //leave new data uninitialized (init in debug mode?)
  108. //for (size_t i=curSize;i<newsize;i++) ...
  109. }
  110. if (result)
  111. {
  112. m_size = newsize;
  113. }
  114. else
  115. {
  116. m_size = 0;
  117. }
  118. return result;
  119. }
  120. B3_FORCE_INLINE size_t size() const
  121. {
  122. return m_size;
  123. }
  124. B3_FORCE_INLINE size_t capacity() const
  125. {
  126. return m_capacity;
  127. }
  128. B3_FORCE_INLINE bool reserve(size_t _Count, bool copyOldContents = true)
  129. {
  130. bool result = true;
  131. // determine new minimum length of allocated storage
  132. if (capacity() < _Count)
  133. { // not enough room, reallocate
  134. if (m_allowGrowingCapacity)
  135. {
  136. cl_int ciErrNum;
  137. //create a new OpenCL buffer
  138. size_t memSizeInBytes = sizeof(T) * _Count;
  139. cl_mem buf = clCreateBuffer(m_clContext, CL_MEM_READ_WRITE, memSizeInBytes, NULL, &ciErrNum);
  140. if (ciErrNum != CL_SUCCESS)
  141. {
  142. b3Error("OpenCL out-of-memory\n");
  143. _Count = 0;
  144. result = false;
  145. }
  146. //#define B3_ALWAYS_INITIALIZE_OPENCL_BUFFERS
  147. #ifdef B3_ALWAYS_INITIALIZE_OPENCL_BUFFERS
  148. unsigned char* src = (unsigned char*)malloc(memSizeInBytes);
  149. for (size_t i = 0; i < memSizeInBytes; i++)
  150. src[i] = 0xbb;
  151. ciErrNum = clEnqueueWriteBuffer(m_commandQueue, buf, CL_TRUE, 0, memSizeInBytes, src, 0, 0, 0);
  152. b3Assert(ciErrNum == CL_SUCCESS);
  153. clFinish(m_commandQueue);
  154. free(src);
  155. #endif //B3_ALWAYS_INITIALIZE_OPENCL_BUFFERS
  156. if (result)
  157. {
  158. if (copyOldContents)
  159. copyToCL(buf, size());
  160. }
  161. //deallocate the old buffer
  162. deallocate();
  163. m_clBuffer = buf;
  164. m_capacity = _Count;
  165. }
  166. else
  167. {
  168. //fail: assert and
  169. b3Assert(0);
  170. deallocate();
  171. result = false;
  172. }
  173. }
  174. return result;
  175. }
  176. void copyToCL(cl_mem destination, size_t numElements, size_t firstElem = 0, size_t dstOffsetInElems = 0) const
  177. {
  178. if (numElements <= 0)
  179. return;
  180. b3Assert(m_clBuffer);
  181. b3Assert(destination);
  182. //likely some error, destination is same as source
  183. b3Assert(m_clBuffer != destination);
  184. b3Assert((firstElem + numElements) <= m_size);
  185. cl_int status = 0;
  186. b3Assert(numElements > 0);
  187. b3Assert(numElements <= m_size);
  188. size_t srcOffsetBytes = sizeof(T) * firstElem;
  189. size_t dstOffsetInBytes = sizeof(T) * dstOffsetInElems;
  190. status = clEnqueueCopyBuffer(m_commandQueue, m_clBuffer, destination,
  191. srcOffsetBytes, dstOffsetInBytes, sizeof(T) * numElements, 0, 0, 0);
  192. b3Assert(status == CL_SUCCESS);
  193. }
  194. void copyFromHost(const b3AlignedObjectArray<T>& srcArray, bool waitForCompletion = true)
  195. {
  196. size_t newSize = srcArray.size();
  197. bool copyOldContents = false;
  198. resize(newSize, copyOldContents);
  199. if (newSize)
  200. copyFromHostPointer(&srcArray[0], newSize, 0, waitForCompletion);
  201. }
  202. void copyFromHostPointer(const T* src, size_t numElems, size_t destFirstElem = 0, bool waitForCompletion = true)
  203. {
  204. b3Assert(numElems + destFirstElem <= capacity());
  205. if (numElems + destFirstElem)
  206. {
  207. cl_int status = 0;
  208. size_t sizeInBytes = sizeof(T) * numElems;
  209. status = clEnqueueWriteBuffer(m_commandQueue, m_clBuffer, 0, sizeof(T) * destFirstElem, sizeInBytes,
  210. src, 0, 0, 0);
  211. b3Assert(status == CL_SUCCESS);
  212. if (waitForCompletion)
  213. clFinish(m_commandQueue);
  214. }
  215. else
  216. {
  217. b3Error("copyFromHostPointer invalid range\n");
  218. }
  219. }
  220. void copyToHost(b3AlignedObjectArray<T>& destArray, bool waitForCompletion = true) const
  221. {
  222. destArray.resize(this->size());
  223. if (size())
  224. copyToHostPointer(&destArray[0], size(), 0, waitForCompletion);
  225. }
  226. void copyToHostPointer(T* destPtr, size_t numElem, size_t srcFirstElem = 0, bool waitForCompletion = true) const
  227. {
  228. b3Assert(numElem + srcFirstElem <= capacity());
  229. if (numElem + srcFirstElem <= capacity())
  230. {
  231. cl_int status = 0;
  232. status = clEnqueueReadBuffer(m_commandQueue, m_clBuffer, 0, sizeof(T) * srcFirstElem, sizeof(T) * numElem,
  233. destPtr, 0, 0, 0);
  234. b3Assert(status == CL_SUCCESS);
  235. if (waitForCompletion)
  236. clFinish(m_commandQueue);
  237. }
  238. else
  239. {
  240. b3Error("copyToHostPointer invalid range\n");
  241. }
  242. }
  243. void copyFromOpenCLArray(const b3OpenCLArray& src)
  244. {
  245. size_t newSize = src.size();
  246. resize(newSize);
  247. if (size())
  248. {
  249. src.copyToCL(m_clBuffer, size());
  250. }
  251. }
  252. };
  253. #endif //B3_OPENCL_ARRAY_H