[PATCH 43/90] MapBuffer/MapImage: allocate the cl_mem backing buffer if required
authorMichal Babej <michal.babej@tuni.fi>
Thu, 25 Nov 2021 13:49:47 +0000 (15:49 +0200)
committerAndreas Beckmann <anbe@debian.org>
Fri, 7 Jan 2022 23:55:22 +0000 (23:55 +0000)
Gbp-Pq: Name 0043-MapBuffer-MapImage-allocate-the-cl_mem-backing-buffe.patch

lib/CL/clEnqueueMapBuffer.c
lib/CL/clEnqueueMapImage.c

index f342177c2d7eaa5629dd05c551cb7e5f9f986ca3..b277dd944bc5ff9477912d31ccebf2de7c2e83c7 100644 (file)
@@ -104,7 +104,17 @@ POname(clEnqueueMapBuffer)(cl_command_queue command_queue,
   mapping_info->offset = offset;
   mapping_info->size = size;
 
-  errcode = device->ops->get_mapping_ptr (device->data, mem_id, buffer,
+  /* because cl_mems are per-context, PoCL delays allocation of
+   * cl_mem backing memory until it knows which device will need it,
+   * so the cl_mem might not yet be allocated on this device. However,
+   * some drivers can avoid unnecessary host memory usage if we
+   * allocate it now. We can assume it will be used on this device. */
+  pocl_mem_identifier *p = &buffer->device_ptrs[device->global_mem_id];
+  if (p->mem_ptr == NULL)
+    errcode = device->ops->alloc_mem_obj (device, buffer, NULL);
+
+  if (errcode == CL_SUCCESS)
+    errcode = device->ops->get_mapping_ptr (device->data, mem_id, buffer,
                                           mapping_info);
   DL_APPEND (buffer->mappings, mapping_info);
   ++buffer->map_count;
index ea71b85db0d043efc0b982406b36959dcfdf70cd..c1f78c93f8cf33aad86b34e68be0b8a85de51186 100644 (file)
@@ -152,8 +152,18 @@ CL_API_SUFFIX__VERSION_1_0
   assert (start_offset <= end_offset);
   mapping_info->size = end_offset + 1 - start_offset;
 
-  errcode = device->ops->get_mapping_ptr (device->data, mem_id, image,
-                                          mapping_info);
+  /* because cl_mems are per-context, PoCL delays allocation of
+   * cl_mem backing memory until it knows which device will need it,
+   * so the cl_mem might not yet be allocated on this device. However,
+   * some drivers can avoid unnecessary host memory usage if we
+   * allocate it now. We can assume it will be used on this device. */
+  pocl_mem_identifier *p = &image->device_ptrs[device->global_mem_id];
+  if (p->mem_ptr == NULL)
+    errcode = device->ops->alloc_mem_obj (device, image, NULL);
+
+  if (errcode == CL_SUCCESS)
+    errcode = device->ops->get_mapping_ptr (device->data, mem_id, image,
+                                            mapping_info);
   DL_APPEND (image->mappings, mapping_info);
   ++image->map_count;
   POCL_UNLOCK_OBJ (image);