Skip to content

Commit 24108e8

Browse files
Cache the memory used for pixel coordinates and indices map to improve performance
1 parent a11bef3 commit 24108e8

File tree

1 file changed

+39
-8
lines changed

1 file changed

+39
-8
lines changed

cpp/visualmesh/engine/opencl/engine.hpp

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -503,14 +503,8 @@ namespace engine {
503503
}
504504

505505
// Create buffers for indices map
506-
cl::mem indices_map(
507-
::clCreateBuffer(context, CL_MEM_READ_ONLY, sizeof(cl_int) * points, nullptr, &error),
508-
::clReleaseMemObject);
509-
throw_cl_error(error, "Error allocating indices_map buffer");
510-
cl::mem pixel_coordinates(
511-
::clCreateBuffer(context, CL_MEM_READ_WRITE, sizeof(std::array<Scalar, 2>) * points, nullptr, &error),
512-
::clReleaseMemObject);
513-
throw_cl_error(error, "Error allocating pixel_coordinates buffer");
506+
cl::mem indices_map = get_indices_map_memory(points);
507+
cl::mem pixel_coordinates = get_pixel_coordinates_memory(points);
514508

515509
// Upload our indices map
516510
cl::event indices_event;
@@ -598,6 +592,33 @@ namespace engine {
598592
projected); // GPU event
599593
}
600594

595+
cl::mem get_indices_map_memory(const int& max_size) const {
596+
597+
if (indices_map_memory.max_size < max_size) {
598+
cl_int error;
599+
indices_map_memory.memory =
600+
cl::mem(::clCreateBuffer(context, CL_MEM_READ_WRITE, max_size * sizeof(int), nullptr, &error),
601+
::clReleaseMemObject);
602+
throw_cl_error(error, "Error allocating indices map buffer on device");
603+
indices_map_memory.max_size = max_size;
604+
}
605+
return indices_map_memory.memory;
606+
}
607+
608+
cl::mem get_pixel_coordinates_memory(const int& max_size) const {
609+
610+
if (pixel_coordinates_memory.max_size < max_size) {
611+
cl_int error;
612+
pixel_coordinates_memory.memory =
613+
cl::mem(::clCreateBuffer(
614+
context, CL_MEM_READ_WRITE, max_size * sizeof(std::array<Scalar, 2>), nullptr, &error),
615+
::clReleaseMemObject);
616+
throw_cl_error(error, "Error allocating pixel coordinates buffer on device");
617+
pixel_coordinates_memory.max_size = max_size;
618+
}
619+
return pixel_coordinates_memory.memory;
620+
}
621+
601622
cl::mem get_image_memory(vec2<int> dimensions, uint32_t format) const {
602623

603624
// If our dimensions and format haven't changed from last time we can reuse the same memory location
@@ -682,6 +703,16 @@ namespace engine {
682703
/// A list of kernels to run in sequence to run the network
683704
std::vector<std::pair<cl::kernel, size_t>> conv_layers;
684705

706+
mutable struct {
707+
int max_size = 0;
708+
cl::mem memory;
709+
} indices_map_memory;
710+
711+
mutable struct {
712+
int max_size = 0;
713+
cl::mem memory;
714+
} pixel_coordinates_memory;
715+
685716
mutable struct {
686717
vec2<int> dimensions = {0, 0};
687718
uint32_t format = 0;

0 commit comments

Comments
 (0)