Fix wrong track of the memory when doing device vector resize before freeing it

This is rather legit case which happens i.e. when having persistent images enabled
and session is updating the lookup tables.

Now device_memory keeps track of amount of memory being allocated on the device,
which makes freeing using the proper allocated size, not the CPU side buffer
size.
This commit is contained in:
Sergey Sharybin
2014-09-04 17:22:40 +06:00
parent 5e3b63a22b
commit fbed2047c8
5 changed files with 30 additions and 15 deletions

View File

@@ -73,8 +73,8 @@ public:
void mem_alloc(device_memory& mem, MemoryType type)
{
mem.device_pointer = mem.data_pointer;
stats.mem_alloc(mem.memory_size());
mem.device_size = mem.memory_size();
stats.mem_alloc(mem.device_size);
}
void mem_copy_to(device_memory& mem)
@@ -94,9 +94,11 @@ public:
void mem_free(device_memory& mem)
{
mem.device_pointer = 0;
stats.mem_free(mem.memory_size());
if(mem.device_pointer) {
mem.device_pointer = 0;
stats.mem_free(mem.device_size);
mem.device_size = 0;
}
}
void const_copy_to(const char *name, void *host, size_t size)
@@ -108,15 +110,17 @@ public:
{
kernel_tex_copy(&kernel_globals, name, mem.data_pointer, mem.data_width, mem.data_height, mem.data_depth, interpolation);
mem.device_pointer = mem.data_pointer;
stats.mem_alloc(mem.memory_size());
mem.device_size = mem.memory_size();
stats.mem_alloc(mem.device_size);
}
void tex_free(device_memory& mem)
{
mem.device_pointer = 0;
stats.mem_free(mem.memory_size());
if(mem.device_pointer) {
mem.device_pointer = 0;
stats.mem_free(mem.device_size);
mem.device_size = 0;
}
}
void *osl_memory()

View File

@@ -334,6 +334,7 @@ public:
size_t size = mem.memory_size();
cuda_assert(cuMemAlloc(&device_pointer, size));
mem.device_pointer = (device_ptr)device_pointer;
mem.device_size = size;
stats.mem_alloc(size);
cuda_pop_context();
}
@@ -381,7 +382,8 @@ public:
mem.device_pointer = 0;
stats.mem_free(mem.memory_size());
stats.mem_free(mem.device_size);
mem.device_size = 0;
}
}
@@ -473,6 +475,7 @@ public:
cuda_assert(cuTexRefSetFlags(texref, CU_TRSF_NORMALIZED_COORDINATES));
mem.device_pointer = (device_ptr)handle;
mem.device_size = size;
stats.mem_alloc(size);
}
@@ -540,7 +543,8 @@ public:
tex_interp_map.erase(tex_interp_map.find(mem.device_pointer));
mem.device_pointer = 0;
stats.mem_free(mem.memory_size());
stats.mem_free(mem.device_size);
mem.device_size = 0;
}
else {
tex_interp_map.erase(tex_interp_map.find(mem.device_pointer));
@@ -790,7 +794,8 @@ public:
mem.device_pointer = pmem.cuTexId;
pixel_mem_map[mem.device_pointer] = pmem;
stats.mem_alloc(mem.memory_size());
mem.device_size = mem.memory_size();
stats.mem_alloc(mem.device_size);
return;
}
@@ -847,7 +852,8 @@ public:
pixel_mem_map.erase(pixel_mem_map.find(mem.device_pointer));
mem.device_pointer = 0;
stats.mem_free(mem.memory_size());
stats.mem_free(mem.device_size);
mem.device_size = 0;
return;
}

View File

@@ -167,6 +167,7 @@ public:
int data_elements;
device_ptr data_pointer;
size_t data_size;
size_t device_size;
size_t data_width;
size_t data_height;
size_t data_depth;
@@ -194,6 +195,7 @@ public:
data_elements = device_type_traits<T>::num_elements;
data_pointer = 0;
data_size = 0;
device_size = 0;
data_width = 0;
data_height = 0;
data_depth = 0;

View File

@@ -794,6 +794,7 @@ public:
opencl_assert_err(ciErr, "clCreateBuffer");
stats.mem_alloc(size);
mem.device_size = size;
}
void mem_copy_to(device_memory& mem)
@@ -825,7 +826,8 @@ public:
opencl_assert(clReleaseMemObject(CL_MEM_PTR(mem.device_pointer)));
mem.device_pointer = 0;
stats.mem_free(mem.memory_size());
stats.mem_free(mem.device_size);
mem.device_size = 0;
}
}

View File

@@ -30,6 +30,7 @@ public:
}
void mem_free(size_t size) {
assert(mem_used >= size);
mem_used -= size;
}