Skip to content

Commit

Permalink
[aot] Warns the user when mapping device-only memory (#6417)
Browse files Browse the repository at this point in the history
Issue: #6406

### Brief Summary

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
PENGUINLIONG and pre-commit-ci[bot] authored Oct 25, 2022
1 parent 19e6ac7 commit b1a3583
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
19 changes: 19 additions & 0 deletions c_api/tests/c_api_interface_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,25 @@ TEST_F(CapiTest, DryRunMemoryAllocation) {
}
}

TEST_F(CapiTest, FailMapDeviceOnlyMemory) {
if (capi::utils::is_vulkan_available()) {
ti::Runtime runtime(TI_ARCH_VULKAN);

ti::Memory mem = runtime.allocate_memory(100);
mem.map();

char err_msg[1024]{0};
TiError err = ti_get_last_error(sizeof(err_msg), err_msg);

TI_ASSERT(err == TI_ERROR_INVALID_STATE);
TI_ASSERT(std::string(err_msg).find("host_read") != std::string::npos);
TI_ASSERT(std::string(err_msg).find("host_write") != std::string::npos);
TI_ASSERT(std::string(err_msg).find("host_access") != std::string::npos);

ti_set_last_error(TI_ERROR_SUCCESS, nullptr);
}
}

TEST_F(CapiTest, DryRunImageAllocation) {
if (capi::utils::is_vulkan_available()) {
{
Expand Down
21 changes: 15 additions & 6 deletions taichi/rhi/vulkan/vulkan_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1560,14 +1560,23 @@ void *VulkanDevice::map(DeviceAllocation alloc) {
TI_ASSERT_INFO(alloc_int.mapped == nullptr,
"Memory can not be mapped multiple times");

VkResult res;
if (alloc_int.buffer->allocator) {
vmaMapMemory(alloc_int.buffer->allocator, alloc_int.buffer->allocation,
&alloc_int.mapped);
res = vmaMapMemory(alloc_int.buffer->allocator,
alloc_int.buffer->allocation, &alloc_int.mapped);
} else {
vkMapMemory(device_, alloc_int.alloc_info.deviceMemory,
alloc_int.alloc_info.offset, alloc_int.alloc_info.size, 0,
&alloc_int.mapped);
}
res = vkMapMemory(device_, alloc_int.alloc_info.deviceMemory,
alloc_int.alloc_info.offset, alloc_int.alloc_info.size, 0,
&alloc_int.mapped);
}
if (alloc_int.mapped == nullptr || res == VK_ERROR_MEMORY_MAP_FAILED) {
TI_ERROR(
"cannot map memory, potentially because the memory is not "
"accessible from the host: ensure your memory is allocated with "
"`host_read=true` or `host_write=true` (or `host_access=true` in C++ "
"wrapper)");
}
BAIL_ON_VK_BAD_RESULT(res, "failed to map memory for unknown reasons");

return alloc_int.mapped;
}
Expand Down

0 comments on commit b1a3583

Please sign in to comment.