Skip to content

Commit 803bde0

Browse files
authored
add config option to keep allocated memories mapped (#801)
Some workloads on some devices can get huge performance improvement by keeping allocated memories mapped.
1 parent c400518 commit 803bde0

File tree

5 files changed

+33
-6
lines changed

5 files changed

+33
-6
lines changed

src/config.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ OPTION(uint32_t, enqueue_command_retry_sleep_us, UINT32_MAX) // UINT32_MAX meani
6666

6767
OPTION(bool, supports_filter_linear, true)
6868

69+
OPTION(bool, keep_memory_allocations_mapped, false)
70+
6971
OPTION(std::string, device_extensions, "")
7072
OPTION(std::string, device_extensions_masked, "")
7173

src/device.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,10 @@ struct cvk_device : public _cl_device_id,
699699
0;
700700
}
701701

702+
bool keep_memory_allocations_mapped() const {
703+
return m_clvk_properties->keep_memory_allocations_mapped();
704+
}
705+
702706
private:
703707
std::string version_desc() const {
704708
std::string ret = "CLVK on Vulkan v";

src/device_properties.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ struct cvk_device_properties {
7171
return no_disabled_formats;
7272
}
7373

74+
virtual bool keep_memory_allocations_mapped() const {
75+
return config.keep_memory_allocations_mapped();
76+
}
77+
7478
virtual ~cvk_device_properties() {}
7579
};
7680

src/memory.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ bool cvk_buffer::init() {
127127

128128
// Allocate memory
129129
m_memory = std::make_shared<cvk_memory_allocation>(
130-
vkdev, params.size, params.memory_type_index, params.memory_coherent);
130+
vkdev, params.size, params.memory_type_index, params.memory_coherent,
131+
device->keep_memory_allocations_mapped());
131132
res = m_memory->allocate(device->uses_physical_addressing());
132133

133134
if (res != VK_SUCCESS) {
@@ -441,7 +442,8 @@ bool cvk_image::init_vulkan_image() {
441442

442443
// Allocate memory
443444
m_memory = std::make_unique<cvk_memory_allocation>(
444-
vkdev, params.size, params.memory_type_index, params.memory_coherent);
445+
vkdev, params.size, params.memory_type_index, params.memory_coherent,
446+
device->keep_memory_allocations_mapped());
445447

446448
res = m_memory->allocate(device->uses_physical_addressing());
447449

src/memory.hpp

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,15 @@
2525
struct cvk_memory_allocation {
2626

2727
cvk_memory_allocation(VkDevice dev, VkDeviceSize size, uint32_t type_index,
28-
bool coherent)
28+
bool coherent, bool keep_mapping)
2929
: m_device(dev), m_size(size), m_memory(VK_NULL_HANDLE),
30-
m_memory_type_index(type_index), m_coherent(coherent) {}
30+
m_memory_type_index(type_index), m_coherent(coherent),
31+
m_keep_mapping(keep_mapping), m_map_ptr(nullptr) {}
3132

3233
~cvk_memory_allocation() {
34+
if (m_keep_mapping && m_map_ptr != nullptr) {
35+
vkUnmapMemory(m_device, m_memory);
36+
}
3337
if (m_memory != VK_NULL_HANDLE) {
3438
vkFreeMemory(m_device, m_memory, nullptr);
3539
}
@@ -73,10 +77,19 @@ struct cvk_memory_allocation {
7377
}
7478

7579
VkResult map(void** map_ptr) {
76-
return vkMapMemory(m_device, m_memory, 0, m_size, 0, map_ptr);
80+
VkResult result = VK_SUCCESS;
81+
if (!m_keep_mapping || m_map_ptr == nullptr) {
82+
result = vkMapMemory(m_device, m_memory, 0, m_size, 0, &m_map_ptr);
83+
}
84+
*map_ptr = m_map_ptr;
85+
return result;
7786
}
7887

79-
void unmap() { vkUnmapMemory(m_device, m_memory); }
88+
void unmap() {
89+
if (!m_keep_mapping) {
90+
vkUnmapMemory(m_device, m_memory);
91+
}
92+
}
8093

8194
VkDeviceMemory vulkan_memory() { return m_memory; }
8295

@@ -86,6 +99,8 @@ struct cvk_memory_allocation {
8699
VkDeviceMemory m_memory;
87100
uint32_t m_memory_type_index;
88101
bool m_coherent;
102+
bool m_keep_mapping;
103+
void* m_map_ptr;
89104
};
90105

91106
using cvk_mem_callback_pointer_type = void(CL_CALLBACK*)(cl_mem mem,

0 commit comments

Comments
 (0)