26 void MemoryManager::DeviceBuffer::add_allocation(Allocation &allocation)
28 allocations.push_back(&allocation);
31 void MemoryManager::DeviceBuffer::update_device_memory(OpenCLDevice *device)
33 bool need_realloc =
false;
36 size_t total_size = 0;
38 for (
int i = allocations.size() - 1; i >= 0; i--) {
39 Allocation *allocation = allocations[i];
42 if (!allocation->mem || allocation->mem->memory_size() == 0) {
43 allocation->device_buffer =
NULL;
46 allocations.erase(allocations.begin() + i);
54 size_t alloc_size =
align_up(allocation->mem->memory_size(), 16);
56 if (allocation->size != alloc_size) {
58 allocation->size = alloc_size;
59 allocation->needs_copy_to_device =
true;
64 total_size += alloc_size;
68 total_size =
std::max(total_size, (
size_t)16);
71 cl_ulong max_buffer_size;
73 device->cdDevice, CL_DEVICE_MAX_MEM_ALLOC_SIZE,
sizeof(cl_ulong), &max_buffer_size,
NULL);
75 if (total_size > max_buffer_size) {
76 device->set_error(
"Scene too complex to fit in available memory.");
81 "memory manager buffer");
87 foreach (Allocation *allocation, allocations) {
88 if (allocation->needs_copy_to_device) {
90 opencl_device_assert(device,
91 clEnqueueWriteBuffer(device->cqCommandQueue,
95 allocation->mem->memory_size(),
96 allocation->mem->host_pointer,
101 allocation->needs_copy_to_device =
false;
105 opencl_device_assert(device,
106 clEnqueueCopyBuffer(device->cqCommandQueue,
107 CL_MEM_PTR(
buffer->device_pointer),
109 allocation->desc.offset,
111 allocation->mem->memory_size(),
117 allocation->desc.offset = offset;
118 offset += allocation->size;
126 assert(total_size ==
buffer->data_size);
130 foreach (Allocation *allocation, allocations) {
131 if (allocation->needs_copy_to_device) {
133 opencl_device_assert(device,
134 clEnqueueWriteBuffer(device->cqCommandQueue,
135 CL_MEM_PTR(
buffer->device_pointer),
138 allocation->mem->memory_size(),
139 allocation->mem->host_pointer,
144 allocation->needs_copy_to_device =
false;
147 offset += allocation->size;
152 clFinish(device->cqCommandQueue);
160 MemoryManager::DeviceBuffer *MemoryManager::smallest_device_buffer()
162 DeviceBuffer *smallest = device_buffers;
164 foreach (DeviceBuffer &device_buffer, device_buffers) {
165 if (device_buffer.size < smallest->size) {
166 smallest = &device_buffer;
175 foreach (DeviceBuffer &device_buffer, device_buffers) {
182 foreach (DeviceBuffer &device_buffer, device_buffers) {
183 device_buffer.free(device);
189 Allocation &allocation = allocations[name];
191 allocation.mem = &mem;
192 allocation.needs_copy_to_device =
true;
194 if (!allocation.device_buffer) {
195 DeviceBuffer *device_buffer = smallest_device_buffer();
196 allocation.device_buffer = device_buffer;
198 allocation.desc.device_buffer = device_buffer - device_buffers;
200 device_buffer->add_allocation(allocation);
210 foreach (AllocationsMap::value_type &value, allocations) {
211 Allocation &allocation = value.second;
212 if (allocation.mem == &mem) {
214 allocation.device_buffer->size -= mem.
memory_size();
216 allocation.mem =
NULL;
217 allocation.needs_copy_to_device =
false;
231 Allocation &allocation = allocations[name];
232 return allocation.desc;
243 foreach (DeviceBuffer &device_buffer, device_buffers) {
244 device_buffer.update_device_memory(device);
252 foreach (DeviceBuffer &device_buffer, device_buffers) {
253 if (device_buffer.buffer->device_pointer) {
254 device->kernel_set_args(kernel, (*narg)++, *device_buffer.buffer);
257 device->kernel_set_args(kernel, (*narg)++);
void BLI_kdtree_nd_() free(KDTree *tree)
void alloc(const char *name, device_memory &mem)
MemoryManager(OpenCLDevice *device)
BufferDescriptor get_descriptor(string name)
void set_kernel_arg_buffers(cl_kernel kernel, cl_uint *narg)
void update_device_memory()
device_ptr device_pointer
void alloc_to_device(size_t num, bool shrink_to_fit=true)
#define CCL_NAMESPACE_END
__kernel void ccl_constant KernelData ccl_global void ccl_global char ccl_global int ccl_global char ccl_global unsigned int ccl_global float * buffer
ccl_device_inline size_t align_up(size_t offset, size_t alignment)