Welcome to mirror list, hosted at ThFree Co, Russian Federation.

memory_manager.cpp « opencl « device « cycles « intern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b67dfef88aa44349f00c075c02ebcb189c8a21ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/*
 * Copyright 2011-2017 Blender Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifdef WITH_OPENCL

#include "util/util_foreach.h"

#include "device/opencl/opencl.h"
#include "device/opencl/memory_manager.h"

CCL_NAMESPACE_BEGIN

void MemoryManager::DeviceBuffer::add_allocation(Allocation& allocation)
{
	allocations.push_back(&allocation);
}

void MemoryManager::DeviceBuffer::update_device_memory(OpenCLDeviceBase *device)
{
	bool need_realloc = false;

	/* Calculate total size and remove any freed. */
	size_t total_size = 0;

	for(int i = allocations.size()-1; i >= 0; i--) {
		Allocation* allocation = allocations[i];

		/* Remove allocations that have been freed. */
		if(!allocation->mem || allocation->mem->memory_size() == 0) {
			allocation->device_buffer = NULL;
			allocation->size = 0;

			allocations.erase(allocations.begin()+i);

			need_realloc = true;

			continue;
		}

		/* Get actual size for allocation. */
		size_t alloc_size = align_up(allocation->mem->memory_size(), 16);

		if(allocation->size != alloc_size) {
			/* Allocation is either new or resized. */
			allocation->size = alloc_size;
			allocation->needs_copy_to_device = true;

			need_realloc = true;
		}

		total_size += alloc_size;
	}

	if(need_realloc) {
		cl_ulong max_buffer_size;
		clGetDeviceInfo(device->cdDevice, CL_DEVICE_MAX_MEM_ALLOC_SIZE, sizeof(cl_ulong), &max_buffer_size, NULL);

		if(total_size > max_buffer_size) {
			device->set_error("Scene too complex to fit in available memory.");
			return;
		}

		device_memory *new_buffer = new device_memory;

		new_buffer->resize(total_size);
		device->mem_alloc(string_printf("buffer_%p", this).data(), *new_buffer, MEM_READ_ONLY);

		size_t offset = 0;

		foreach(Allocation* allocation, allocations) {
			if(allocation->needs_copy_to_device) {
				/* Copy from host to device. */
				opencl_device_assert(device, clEnqueueWriteBuffer(device->cqCommandQueue,
					CL_MEM_PTR(new_buffer->device_pointer),
					CL_FALSE,
					offset,
					allocation->mem->memory_size(),
					(void*)allocation->mem->data_pointer,
					0, NULL, NULL
				));

				allocation->needs_copy_to_device = false;
			}
			else {
				/* Fast copy from memory already on device. */
				opencl_device_assert(device, clEnqueueCopyBuffer(device->cqCommandQueue,
					CL_MEM_PTR(buffer->device_pointer),
					CL_MEM_PTR(new_buffer->device_pointer),
					allocation->desc.offset,
					offset,
					allocation->mem->memory_size(),
					0, NULL, NULL
				));
			}

			allocation->desc.offset = offset;
			offset += allocation->size;
		}

		device->mem_free(*buffer);
		delete buffer;

		buffer = new_buffer;
	}
	else {
		assert(total_size == buffer->data_size);

		size_t offset = 0;

		foreach(Allocation* allocation, allocations) {
			if(allocation->needs_copy_to_device) {
				/* Copy from host to device. */
				opencl_device_assert(device, clEnqueueWriteBuffer(device->cqCommandQueue,
					CL_MEM_PTR(buffer->device_pointer),
					CL_FALSE,
					offset,
					allocation->mem->memory_size(),
					(void*)allocation->mem->data_pointer,
					0, NULL, NULL
				));

				allocation->needs_copy_to_device = false;
			}

			offset += allocation->size;
		}
	}

	/* Not really necessary, but seems to improve responsiveness for some reason. */
	clFinish(device->cqCommandQueue);
}

void MemoryManager::DeviceBuffer::free(OpenCLDeviceBase *device)
{
	device->mem_free(*buffer);
}

MemoryManager::DeviceBuffer* MemoryManager::smallest_device_buffer()
{
	DeviceBuffer* smallest = device_buffers;

	foreach(DeviceBuffer& device_buffer, device_buffers) {
		if(device_buffer.size < smallest->size) {
			smallest = &device_buffer;
		}
	}

	return smallest;
}

MemoryManager::MemoryManager(OpenCLDeviceBase *device) : device(device), need_update(false)
{
}

void MemoryManager::free()
{
	foreach(DeviceBuffer& device_buffer, device_buffers) {
		device_buffer.free(device);
	}
}

void MemoryManager::alloc(const char *name, device_memory& mem)
{
	Allocation& allocation = allocations[name];

	allocation.mem = &mem;
	allocation.needs_copy_to_device = true;

	if(!allocation.device_buffer) {
		DeviceBuffer* device_buffer = smallest_device_buffer();
		allocation.device_buffer = device_buffer;

		allocation.desc.device_buffer = device_buffer - device_buffers;

		device_buffer->add_allocation(allocation);

		device_buffer->size += mem.memory_size();
	}

	need_update = true;
}

bool MemoryManager::free(device_memory& mem)
{
	foreach(AllocationsMap::value_type& value, allocations) {
		Allocation& allocation = value.second;
		if(allocation.mem == &mem) {

			allocation.device_buffer->size -= mem.memory_size();

			allocation.mem = NULL;
			allocation.needs_copy_to_device = false;

			need_update = true;
			return true;
		}
	}

	return false;
}

MemoryManager::BufferDescriptor MemoryManager::get_descriptor(string name)
{
	update_device_memory();

	Allocation& allocation = allocations[name];
	return allocation.desc;
}

void MemoryManager::update_device_memory()
{
	if(!need_update) {
		return;
	}

	need_update = false;

	foreach(DeviceBuffer& device_buffer, device_buffers) {
		device_buffer.update_device_memory(device);
	}
}

void MemoryManager::set_kernel_arg_buffers(cl_kernel kernel, cl_uint *narg)
{
	update_device_memory();

	foreach(DeviceBuffer& device_buffer, device_buffers) {
		if(device_buffer.buffer->device_pointer) {
			device->kernel_set_args(kernel, (*narg)++, *device_buffer.buffer);
		}
		else {
			device->kernel_set_args(kernel, (*narg)++, device->null_mem);
		}
	}
}

CCL_NAMESPACE_END

#endif  /* WITH_OPENCL */