Skip to content

Commit 025f02f

Browse files
author
swfly
committed
memory query feature for cuda and metal (metal can only query total memory)
1 parent 33955dd commit 025f02f

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/backends/cuda/cuda_device.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1186,6 +1186,22 @@ string CUDADevice::query(luisa::string_view property) noexcept {
11861186
if (property == "device_name") {
11871187
return "cuda";
11881188
}
1189+
if (property == "total_memory") {
1190+
string memory = "0";
1191+
with_handle([&memory]{
1192+
size_t free_mem, total_mem;
1193+
cuMemGetInfo(&free_mem, &total_mem);
1194+
memory = to_string(total_mem);
1195+
});
1196+
return memory;
1197+
}
1198+
if (property == "free_memory") {
1199+
string memory = "0";
1200+
with_handle([&memory]{
1201+
size_t free_mem, total_mem;
1202+
cuMemGetInfo(&free_mem, &total_mem);
1203+
memory = to_string(free_mem);
1204+
});
11891205
LUISA_WARNING_WITH_LOCATION("Unknown device property '{}'.", property);
11901206
return {};
11911207
}

src/backends/metal/metal_device.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,17 @@ void MetalDevice::destroy_accel(uint64_t handle) noexcept {
670670
}
671671

672672
string MetalDevice::query(luisa::string_view property) noexcept {
673+
674+
if (property == "total_memory") {
675+
string memory = "0";
676+
memory = std::to_string(_handle->recommendedMaxWorkingSetSize());
677+
return memory;
678+
}
679+
if (property == "free_memory") {
680+
string memory = "0";
681+
memory = std::to_string(_handle->recommendedMaxWorkingSetSize());
682+
return memory;
683+
}
673684
LUISA_WARNING_WITH_LOCATION("Device property \"{}\" is not supported on Metal.", property);
674685
return {};
675686
}

0 commit comments

Comments
 (0)