memory_manager.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. * Copyright 2011-2017 Blender Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifdef WITH_OPENCL
  17. # include "util/util_foreach.h"
  18. # include "device/opencl/opencl.h"
  19. # include "device/opencl/memory_manager.h"
  20. CCL_NAMESPACE_BEGIN
  21. void MemoryManager::DeviceBuffer::add_allocation(Allocation &allocation)
  22. {
  23. allocations.push_back(&allocation);
  24. }
  25. void MemoryManager::DeviceBuffer::update_device_memory(OpenCLDevice *device)
  26. {
  27. bool need_realloc = false;
  28. /* Calculate total size and remove any freed. */
  29. size_t total_size = 0;
  30. for (int i = allocations.size() - 1; i >= 0; i--) {
  31. Allocation *allocation = allocations[i];
  32. /* Remove allocations that have been freed. */
  33. if (!allocation->mem || allocation->mem->memory_size() == 0) {
  34. allocation->device_buffer = NULL;
  35. allocation->size = 0;
  36. allocations.erase(allocations.begin() + i);
  37. need_realloc = true;
  38. continue;
  39. }
  40. /* Get actual size for allocation. */
  41. size_t alloc_size = align_up(allocation->mem->memory_size(), 16);
  42. if (allocation->size != alloc_size) {
  43. /* Allocation is either new or resized. */
  44. allocation->size = alloc_size;
  45. allocation->needs_copy_to_device = true;
  46. need_realloc = true;
  47. }
  48. total_size += alloc_size;
  49. }
  50. if (need_realloc) {
  51. cl_ulong max_buffer_size;
  52. clGetDeviceInfo(
  53. device->cdDevice, CL_DEVICE_MAX_MEM_ALLOC_SIZE, sizeof(cl_ulong), &max_buffer_size, NULL);
  54. if (total_size > max_buffer_size) {
  55. device->set_error("Scene too complex to fit in available memory.");
  56. return;
  57. }
  58. device_only_memory<uchar> *new_buffer = new device_only_memory<uchar>(device,
  59. "memory manager buffer");
  60. new_buffer->alloc_to_device(total_size);
  61. size_t offset = 0;
  62. foreach (Allocation *allocation, allocations) {
  63. if (allocation->needs_copy_to_device) {
  64. /* Copy from host to device. */
  65. opencl_device_assert(device,
  66. clEnqueueWriteBuffer(device->cqCommandQueue,
  67. CL_MEM_PTR(new_buffer->device_pointer),
  68. CL_FALSE,
  69. offset,
  70. allocation->mem->memory_size(),
  71. allocation->mem->host_pointer,
  72. 0,
  73. NULL,
  74. NULL));
  75. allocation->needs_copy_to_device = false;
  76. }
  77. else {
  78. /* Fast copy from memory already on device. */
  79. opencl_device_assert(device,
  80. clEnqueueCopyBuffer(device->cqCommandQueue,
  81. CL_MEM_PTR(buffer->device_pointer),
  82. CL_MEM_PTR(new_buffer->device_pointer),
  83. allocation->desc.offset,
  84. offset,
  85. allocation->mem->memory_size(),
  86. 0,
  87. NULL,
  88. NULL));
  89. }
  90. allocation->desc.offset = offset;
  91. offset += allocation->size;
  92. }
  93. delete buffer;
  94. buffer = new_buffer;
  95. }
  96. else {
  97. assert(total_size == buffer->data_size);
  98. size_t offset = 0;
  99. foreach (Allocation *allocation, allocations) {
  100. if (allocation->needs_copy_to_device) {
  101. /* Copy from host to device. */
  102. opencl_device_assert(device,
  103. clEnqueueWriteBuffer(device->cqCommandQueue,
  104. CL_MEM_PTR(buffer->device_pointer),
  105. CL_FALSE,
  106. offset,
  107. allocation->mem->memory_size(),
  108. allocation->mem->host_pointer,
  109. 0,
  110. NULL,
  111. NULL));
  112. allocation->needs_copy_to_device = false;
  113. }
  114. offset += allocation->size;
  115. }
  116. }
  117. /* Not really necessary, but seems to improve responsiveness for some reason. */
  118. clFinish(device->cqCommandQueue);
  119. }
  120. void MemoryManager::DeviceBuffer::free(OpenCLDevice *)
  121. {
  122. buffer->free();
  123. }
  124. MemoryManager::DeviceBuffer *MemoryManager::smallest_device_buffer()
  125. {
  126. DeviceBuffer *smallest = device_buffers;
  127. foreach (DeviceBuffer &device_buffer, device_buffers) {
  128. if (device_buffer.size < smallest->size) {
  129. smallest = &device_buffer;
  130. }
  131. }
  132. return smallest;
  133. }
  134. MemoryManager::MemoryManager(OpenCLDevice *device) : device(device), need_update(false)
  135. {
  136. foreach (DeviceBuffer &device_buffer, device_buffers) {
  137. device_buffer.buffer = new device_only_memory<uchar>(device, "memory manager buffer");
  138. }
  139. }
  140. void MemoryManager::free()
  141. {
  142. foreach (DeviceBuffer &device_buffer, device_buffers) {
  143. device_buffer.free(device);
  144. }
  145. }
  146. void MemoryManager::alloc(const char *name, device_memory &mem)
  147. {
  148. Allocation &allocation = allocations[name];
  149. allocation.mem = &mem;
  150. allocation.needs_copy_to_device = true;
  151. if (!allocation.device_buffer) {
  152. DeviceBuffer *device_buffer = smallest_device_buffer();
  153. allocation.device_buffer = device_buffer;
  154. allocation.desc.device_buffer = device_buffer - device_buffers;
  155. device_buffer->add_allocation(allocation);
  156. device_buffer->size += mem.memory_size();
  157. }
  158. need_update = true;
  159. }
  160. bool MemoryManager::free(device_memory &mem)
  161. {
  162. foreach (AllocationsMap::value_type &value, allocations) {
  163. Allocation &allocation = value.second;
  164. if (allocation.mem == &mem) {
  165. allocation.device_buffer->size -= mem.memory_size();
  166. allocation.mem = NULL;
  167. allocation.needs_copy_to_device = false;
  168. need_update = true;
  169. return true;
  170. }
  171. }
  172. return false;
  173. }
  174. MemoryManager::BufferDescriptor MemoryManager::get_descriptor(string name)
  175. {
  176. update_device_memory();
  177. Allocation &allocation = allocations[name];
  178. return allocation.desc;
  179. }
  180. void MemoryManager::update_device_memory()
  181. {
  182. if (!need_update) {
  183. return;
  184. }
  185. need_update = false;
  186. foreach (DeviceBuffer &device_buffer, device_buffers) {
  187. device_buffer.update_device_memory(device);
  188. }
  189. }
  190. void MemoryManager::set_kernel_arg_buffers(cl_kernel kernel, cl_uint *narg)
  191. {
  192. update_device_memory();
  193. foreach (DeviceBuffer &device_buffer, device_buffers) {
  194. if (device_buffer.buffer->device_pointer) {
  195. device->kernel_set_args(kernel, (*narg)++, *device_buffer.buffer);
  196. }
  197. else {
  198. device->kernel_set_args(kernel, (*narg)++, device->null_mem);
  199. }
  200. }
  201. }
  202. CCL_NAMESPACE_END
  203. #endif /* WITH_OPENCL */