From a8e5ce79036185b94dd642fb47bc38b282c351aa Mon Sep 17 00:00:00 2001 From: Alejandro Saucedo Date: Sat, 20 Mar 2021 08:49:27 +0000 Subject: [PATCH 1/4] Added functionality to get list of devices --- src/CMakeLists.txt | 6 ++++-- src/Manager.cpp | 6 ++++++ src/include/kompute/Manager.hpp | 12 +++++++++++- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index d67af1c01..c1ab2d3eb 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -51,8 +51,10 @@ endif() target_include_directories( kompute PUBLIC - ${CMAKE_CURRENT_SOURCE_DIR}/include - ${PROJECT_SOURCE_DIR}/single_include + $ + $ + $ + $ ) if(NOT KOMPUTE_OPT_ANDOID_BUILD) diff --git a/src/Manager.cpp b/src/Manager.cpp index 807d4832f..a78cc7a34 100644 --- a/src/Manager.cpp +++ b/src/Manager.cpp @@ -453,4 +453,10 @@ Manager::getDeviceProperties() const return this->mPhysicalDevice->getProperties(); } +std::vector +Manager::listDevices() const +{ + return this->mInstance->enumeratePhysicalDevices(); +} + } diff --git a/src/include/kompute/Manager.hpp b/src/include/kompute/Manager.hpp index e9e284155..03c0c8c1d 100644 --- a/src/include/kompute/Manager.hpp +++ b/src/include/kompute/Manager.hpp @@ -154,10 +154,20 @@ class Manager void clear(); /** - * Return a struct containing information about the device. + * Information about the current device. + * + * @return vk::PhysicalDeviceProperties containing information about the device **/ vk::PhysicalDeviceProperties getDeviceProperties() const; + /** + * List the devices available in the current vulkan instance. + * + * @return vector of physical devices containing their respective properties + **/ + std::vector listDevices() const; + + private: // -------------- OPTIONALLY OWNED RESOURCES std::shared_ptr mInstance = nullptr; From d23ea95e64a9fa1ad1a8c10a2814b3fbba588436 Mon Sep 17 00:00:00 2001 From: Alejandro Saucedo Date: Sat, 20 Mar 2021 08:49:36 +0000 Subject: [PATCH 2/4] Updated single include --- single_include/kompute/Kompute.hpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/single_include/kompute/Kompute.hpp b/single_include/kompute/Kompute.hpp index cd313e6e7..0093eddc1 100755 --- a/single_include/kompute/Kompute.hpp +++ b/single_include/kompute/Kompute.hpp @@ -2156,10 +2156,19 @@ class Manager void clear(); /** - * Return a struct containing information about the device. + * Information about the current device. + * + * @return vk::PhysicalDeviceProperties containing information about the device **/ vk::PhysicalDeviceProperties getDeviceProperties() const; + /** + * List the devices available in the current vulkan instance. + * + * @return vector of physical devices containing their respective properties + **/ + std::vector listDevices() const; + private: // -------------- OPTIONALLY OWNED RESOURCES std::shared_ptr mInstance = nullptr; From a6457ee1ec2499549119d3ae72570d49ec74b490 Mon Sep 17 00:00:00 2001 From: Alejandro Saucedo Date: Sat, 20 Mar 2021 08:49:51 +0000 Subject: [PATCH 3/4] Added list_devices function and separated new utils function --- python/src/main.cpp | 25 +++++++++++-------------- python/src/utils.hpp | 26 ++++++++++++++++++++++++++ python/test/test_kompute.py | 12 ++++++++++++ 3 files changed, 49 insertions(+), 14 deletions(-) create mode 100644 python/src/utils.hpp diff --git a/python/src/main.cpp b/python/src/main.cpp index acf4308aa..0bbae2d78 100644 --- a/python/src/main.cpp +++ b/python/src/main.cpp @@ -6,10 +6,10 @@ #include "fmt/ranges.h" +#include "utils.hpp" #include "docstrings.hpp" namespace py = pybind11; -using namespace pybind11::literals; // for the `_a` literal //used in Core.hpp py::object kp_debug, kp_info, kp_warning, kp_error; @@ -220,21 +220,18 @@ PYBIND11_MODULE(kp, m) { py::arg("workgroup") = kp::Workgroup(), py::arg("spec_consts") = kp::Constants(), py::arg("push_consts") = kp::Constants()) + .def("list_devices", [](kp::Manager& self){ + const std::vector devices = self.listDevices(); + py::list list; + for (const vk::PhysicalDevice& device : devices) { + list.append(kp::py::vkPropertiesToDict(device.getProperties())); + } + return list; + }, "Return a dict containing information about the device") .def("get_device_properties", [](kp::Manager& self){ - const auto properties = self.getDeviceProperties(); - py::dict py_props( - "device_name"_a = std::string(properties.deviceName.data()), - "max_work_group_count"_a = py::make_tuple(properties.limits.maxComputeWorkGroupCount[0], - properties.limits.maxComputeWorkGroupCount[1], - properties.limits.maxComputeWorkGroupCount[2]), - "max_work_group_invocations"_a = properties.limits.maxComputeWorkGroupInvocations, - "max_work_group_size"_a = py::make_tuple(properties.limits.maxComputeWorkGroupSize[0], - properties.limits.maxComputeWorkGroupSize[1], - properties.limits.maxComputeWorkGroupSize[2]), - "timestamps_supported"_a = (bool)properties.limits.timestampComputeAndGraphics - ); + const vk::PhysicalDeviceProperties properties = self.getDeviceProperties(); - return py_props; + return kp::py::vkPropertiesToDict(properties); }, "Return a dict containing information about the device"); diff --git a/python/src/utils.hpp b/python/src/utils.hpp new file mode 100644 index 000000000..367871344 --- /dev/null +++ b/python/src/utils.hpp @@ -0,0 +1,26 @@ + +#include +#include + +using namespace pybind11::literals; // for the `_a` literal + +namespace kp { +namespace py { +static pybind11::dict vkPropertiesToDict(const vk::PhysicalDeviceProperties& properties) { + + pybind11::dict pyDict( + "device_name"_a = std::string(properties.deviceName.data()), + "max_work_group_count"_a = pybind11::make_tuple(properties.limits.maxComputeWorkGroupCount[0], + properties.limits.maxComputeWorkGroupCount[1], + properties.limits.maxComputeWorkGroupCount[2]), + "max_work_group_invocations"_a = properties.limits.maxComputeWorkGroupInvocations, + "max_work_group_size"_a = pybind11::make_tuple(properties.limits.maxComputeWorkGroupSize[0], + properties.limits.maxComputeWorkGroupSize[1], + properties.limits.maxComputeWorkGroupSize[2]), + "timestamps_supported"_a = (bool)properties.limits.timestampComputeAndGraphics + ); + + return pyDict; +} +} +} diff --git a/python/test/test_kompute.py b/python/test/test_kompute.py index e1bcee940..d99dd4485 100644 --- a/python/test/test_kompute.py +++ b/python/test/test_kompute.py @@ -227,3 +227,15 @@ def test_workgroup(): assert np.all(tensor_a.data() == np.stack([np.arange(16)]*8, axis=1).ravel()) assert np.all(tensor_b.data() == np.stack([np.arange(8)]*16, axis=0).ravel()) +def test_mgr_utils(): + mgr = kp.Manager() + + props = mgr.get_device_properties() + + assert "device_name" in props + + devices = mgr.list_devices() + + assert len(devices) == 1 + assert "device_name" in devices[0] + From b7aa097b294ef29884cbdea4093d652167159508 Mon Sep 17 00:00:00 2001 From: Alejandro Saucedo Date: Sat, 20 Mar 2021 08:52:07 +0000 Subject: [PATCH 4/4] Added cpp test --- test/TestManager.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/test/TestManager.cpp b/test/TestManager.cpp index 6d6756d41..c0e4e2942 100644 --- a/test/TestManager.cpp +++ b/test/TestManager.cpp @@ -66,6 +66,14 @@ TEST(TestManager, TestMultipleSequences) TEST(TestManager, TestDeviceProperties) { kp::Manager mgr; - const auto properties = mgr.getDeviceProperties(); + const vk::PhysicalDeviceProperties properties = mgr.getDeviceProperties(); EXPECT_GT(properties.deviceName.size(), 0); } + +TEST(TestManager, TestListDevices) +{ + kp::Manager mgr; + const std::vector devices = mgr.listDevices(); + EXPECT_GT(devices.size(), 0); + EXPECT_GT(devices[0].getProperties().deviceName.size(), 0); +}