### Build Kompute Example with CMake Source: https://github.com/komputeproject/kompute/blob/master/examples/array_multiplication/README.md Clone the Kompute repository, navigate to the array_multiplication example directory, create a build directory, and run CMake commands to build the project. This setup uses fetch_content to manage Kompute as a dependency. ```bash git clone https://github.com/KomputeProject/kompute.git cd kompute/examples/array_multiplication mkdir build cd build cmake .. cmake --build . ``` -------------------------------- ### Setup Kompute Python Test Environment Source: https://github.com/komputeproject/kompute/blob/master/examples/pi4_mesa_build/README.md Navigates to the Kompute Python test directory, creates a virtual environment, activates it, upgrades pip, and installs development dependencies including Kompute itself from a Git repository. ```bash cd kompute/python/test python3 -m venv .venv source .venv/bin/activate pip install --upgrade pip wheel pip install -r requirements-dev.txt pip install git+git://github.com/KomputeProject/kompute.git ``` -------------------------------- ### Build Kompute Example with CMake Source: https://github.com/komputeproject/kompute/blob/master/examples/vulkan_ext_printf/README.md Clone the Kompute repository and build the example using CMake. This process involves creating a build directory, configuring CMake, and then building the project. ```bash git clone https://github.com/KomputeProject/kompute.git cd kompute/examples/vulkan_ext_printf mkdir build cd build cmake .. cmake --build . ``` -------------------------------- ### Python Kompute Simple Example Source: https://github.com/komputeproject/kompute/blob/master/docs/overview/python-examples.rst A basic example demonstrating tensor initialization, synchronization, shader definition, and synchronous execution of a multiplication operation. ```python from kp import Manager, Tensor, OpTensorSyncDevice, OpTensorSyncLocal, OpAlgoDispatch from pyshader import python2shader, ivec3, f32, Array mgr = Manager() # Can be initialized with List[] or np.Array tensor_in_a = mgr.tensor([2, 2, 2]) tensor_in_b = mgr.tensor([1, 2, 3]) tensor_out = mgr.tensor([0, 0, 0]) sq = mgr.sequence() sq.eval(OpTensorSyncDevice([tensor_in_a, tensor_in_b, tensor_out])) # Define the function via PyShader or directly as glsl string or spirv bytes @python2shader def compute_shader_multiply(index=("input", "GlobalInvocationId", ivec3), data1=("buffer", 0, Array(f32)), data2=("buffer", 1, Array(f32)), data3=("buffer", 2, Array(f32))): i = index.x data3[i] = data1[i] * data2[i] algo = mgr.algorithm([tensor_in_a, tensor_in_b, tensor_out], compute_shader_multiply.to_spirv()) # Run shader operation synchronously sq.eval(OpAlgoDispatch(algo)) sq.eval(OpTensorSyncLocal([tensor_out])) assert tensor_out.data().tolist() == [2.0, 4.0, 6.0] ``` -------------------------------- ### Execute Kompute Debug Printf Example Source: https://github.com/komputeproject/kompute/blob/master/examples/vulkan_ext_printf/README.md Run the compiled Kompute example application from the build directory. This command executes the shader debugging functionality. ```bash ./example_shader_printf ``` -------------------------------- ### Install Shader Library and Headers Source: https://github.com/komputeproject/kompute/blob/master/src/shaders/glsl/CMakeLists.txt Installs the shader library and its associated header files when `KOMPUTE_OPT_INSTALL` is enabled. This ensures shaders are available in the installed package. ```cmake install(TARGETS kp_shader EXPORT komputeTargets) install(FILES $/ShaderOpMult.hpp DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) install(FILES $/ShaderLogisticRegression.hpp DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) ``` -------------------------------- ### Install Python Package from Production PyPI Source: https://github.com/komputeproject/kompute/blob/master/docs/overview/ci-tests.rst Install the Kompute Python package from the official PyPI repository. ```bash pip install kp ``` -------------------------------- ### Python Kompute Extended Example with Async Source: https://github.com/komputeproject/kompute/blob/master/docs/overview/python-examples.rst An extended example showing tensor initialization with explicit dtype, asynchronous shader execution, and multiple tensor synchronizations. ```python from kp import Manager, Tensor import kp from pyshader import python2shader, ivec3, f32, Array mgr = Manager(0, [2]) # Can be initialized with List[] or np.Array tensor_in_a = mgr.tensor([2, 2, 2]) tensor_in_b = mgr.tensor([1, 2, 3]) # By default, tensors use a float type, but that can be explicitly specified tensor_out = mgr.tensor_t([0, 0, 0], dtype=np.float32) assert(tensor_out.data_type() == kp.DataTypes.float) seq = mgr.sequence() seq.eval(kp.OpTensorSyncDevice([tensor_in_a, tensor_in_b, tensor_out])) # Define the function via PyShader or directly as glsl string or spirv bytes @python2shader def compute_shader_multiply(index=("input", "GlobalInvocationId", ivec3), data1=("buffer", 0, Array(f32)), data2=("buffer", 1, Array(f32)), data3=("buffer", 2, Array(f32))): i = index.x data3[i] = data1[i] * data2[i] algo = mgr.algorithm([tensor_in_a, tensor_in_b, tensor_out], compute_shader_multiply.to_spirv()) # Run shader operation asynchronously and then await seq.eval_async(kp.OpAlgoDispatch(algo)) seq.eval_await() seq.record(kp.OpTensorSyncLocal([tensor_in_a])) seq.record(kp.OpTensorSyncLocal([tensor_in_b])) seq.record(kp.OpTensorSyncLocal([tensor_out])) seq.eval() assert tensor_out.data().tolist() == [2.0, 4.0, 6.0] ``` -------------------------------- ### Kompute C++ Example Source: https://github.com/komputeproject/kompute/blob/master/README.md This example demonstrates initializing Kompute, creating tensors, defining a compute shader, dispatching workgroups, and synchronizing results. Ensure the shader is correctly compiled and linked. ```c++ void kompute(const std::string& shader) { // 1. Create Kompute Manager with default settings (device 0, first queue and no extensions) kp::Manager mgr; // 2. Create and initialise Kompute Tensors through manager // Default tensor constructor simplifies creation of float values auto tensorInA = mgr.tensor({ 2., 2., 2. }); auto tensorInB = mgr.tensor({ 1., 2., 3. }); // Explicit type constructor supports uint32, int32, double, float and bool auto tensorOutA = mgr.tensorT({ 0, 0, 0 }); auto tensorOutB = mgr.tensorT({ 0, 0, 0 }); std::vector> params = {tensorInA, tensorInB, tensorOutA, tensorOutB}; // 3. Create algorithm based on shader (supports buffers & push/spec constants) kp::Workgroup workgroup({3, 1, 1}); std::vector specConsts({ 2 }); std::vector pushConstsA({ 2.0 }); std::vector pushConstsB({ 3.0 }); auto algorithm = mgr.algorithm(params, // See documentation shader section for compileSource compileSource(shader), workgroup, specConsts, pushConstsA); // 4. Run operation synchronously using sequence mgr.sequence() ->record(params) ->record(algorithm) // Binds default push consts ->eval() // Evaluates the two recorded operations ->record(algorithm, pushConstsB) // Overrides push consts ->eval(); // Evaluates only last recorded operation // 5. Sync results from the GPU asynchronously auto sq = mgr.sequence(); sq->evalAsync(params); // ... Do other work asynchronously whilst GPU finishes sq->evalAwait(); // Prints the first output which is: { 4, 8, 12 } for (const float& elem : tensorOutA->vector()) std::cout << elem << " "; // Prints the second output which is: { 10, 10, 10 } for (const float& elem : tensorOutB->vector()) std::cout << elem << " "; } // Manages / releases all CPU and GPU memory resources int main() { // Define a raw string shader (or use the Kompute tools to compile to SPIRV / C++ header // files). This shader shows some of the main components including constants, buffers, etc std::string shader = (R"( #version 450 layout (local_size_x = 1) in; // The input tensors bind index is relative to index in parameter passed layout(set = 0, binding = 0) buffer buf_in_a { float in_a[]; }; layout(set = 0, binding = 1) buffer buf_in_b { float in_b[]; }; layout(set = 0, binding = 2) buffer buf_out_a { uint out_a[]; }; layout(set = 0, binding = 3) buffer buf_out_b { uint out_b[]; }; // Kompute supports push constants updated on dispatch layout(push_constant) uniform PushConstants { float val; } push_const; // Kompute also supports spec constants on initalization layout(constant_id = 0) const float const_one = 0; void main() { uint index = gl_GlobalInvocationID.x; out_a[index] += uint( in_a[index] * in_b[index] ); out_b[index] += uint( const_one * push_const.val ); } )"); // Run the function declared above with our raw string shader kompute(shader); } ``` -------------------------------- ### Install Python Package from TestPyPI Source: https://github.com/komputeproject/kompute/blob/master/docs/overview/ci-tests.rst Install the Kompute Python package from the TestPyPI repository. Use this to test the package before publishing to the production PyPI. ```bash python -m pip install --index-url https://test.pypi.org/simple/ --no-deps kp ``` -------------------------------- ### Build and Install Mesa Source: https://github.com/komputeproject/kompute/blob/master/examples/pi4_mesa_build/README.md Configures and builds the Mesa library using Meson and Ninja, specifying build options for X11, Broadcom Vulkan drivers, and V3D Gallium drivers. It then installs the built library. ```bash meson --libdir lib \ --prefix /mesa-install \ -D platforms=x11 \ -D vulkan-drivers=broadcom \ -D gallium-drivers=v3d \ -D dri-drivers=[] \ -D buildtype=debug \ build ninja -C build sudo ninja -C build install ``` -------------------------------- ### Build Kompute Logistic Regression Example Source: https://github.com/komputeproject/kompute/blob/master/examples/logistic_regression/README.md Clone the Kompute repository, navigate to the logistic regression example directory, create a build folder, and then use CMake to configure and build the project. ```bash git clone https://github.com/KomputeProject/kompute.git cd kompute/examples/logistic_regression mkdir build cd build cmake .. cmake --build . ``` -------------------------------- ### Install Kompute from Master Branch Source: https://github.com/komputeproject/kompute/blob/master/docs/overview/python-package.rst Install the Kompute package directly from the master branch of its GitHub repository using pip. ```python pip install git+git://github.com/KomputeProject/kompute.git@master ``` -------------------------------- ### Install fmt::fmt Target Source: https://github.com/komputeproject/kompute/blob/master/src/CMakeLists.txt Installs the fmt::fmt target when KOMPUTE_OPT_INSTALL is enabled and built-in fmt is used without spdlog. It unwraps the alias target and installs it. ```cmake if(KOMPUTE_OPT_INSTALL) if (NOT KOMPUTE_OPT_USE_SPDLOG AND KOMPUTE_OPT_USE_BUILT_IN_FMT) # We can't export fmt::fmt because it's alias target, so we unwrap the alias and install it. get_target_property(fmt_aliased_target fmt::fmt ALIASED_TARGET) install(TARGETS ${fmt_aliased_target} EXPORT komputeTargets #COMPONENT DO_NOT_INSTALL_THIS EXCLUDE_FROM_ALL) # Installation of headers changed in fmt from v8 to v10. In v10, cmake will try to find fmt headers in the # include dir of kompute, which will fail definitely. Add EXCLUDE_FROM_ALL to avoid this bug. endif () endif() ``` -------------------------------- ### Kompute Python Example Source: https://github.com/komputeproject/kompute/blob/master/README.md This example demonstrates a full Kompute workflow in Python, including tensor creation, algorithm dispatch with shaders, and asynchronous result synchronization. Ensure the shader is correctly compiled to SPIR-V. ```python from .utils import compile_source # using util function from python/test/utils def kompute(shader): # 1. Create Kompute Manager with default settings (device 0, first queue and no extensions) mgr = kp.Manager() # 2. Create and initialise Kompute Tensors through manager # Default tensor constructor simplifies creation of float values tensor_in_a = mgr.tensor([2, 2, 2]) tensor_in_b = mgr.tensor([1, 2, 3]) # Explicit type constructor supports uint32, int32, double, float and bool tensor_out_a = mgr.tensor_t(np.array([0, 0, 0], dtype=np.uint32)) tensor_out_b = mgr.tensor_t(np.array([0, 0, 0], dtype=np.uint32)) assert(t_data.data_type() == kp.DataTypes.uint) params = [tensor_in_a, tensor_in_b, tensor_out_a, tensor_out_b] # 3. Create algorithm based on shader (supports buffers & push/spec constants) workgroup = (3, 1, 1) spec_consts = [2] push_consts_a = [2] push_consts_b = [3] # See documentation shader section for compile_source spirv = compile_source(shader) algo = mgr.algorithm(params, spirv, workgroup, spec_consts, push_consts_a) # 4. Run operation synchronously using sequence (mgr.sequence() .record(kp.OpTensorSyncDevice(params)) .record(kp.OpAlgoDispatch(algo)) # Binds default push consts provided .eval() # evaluates the two recorded ops .record(kp.OpAlgoDispatch(algo, push_consts_b)) # Overrides push consts .eval()) # evaluates only the last recorded op # 5. Sync results from the GPU asynchronously sq = mgr.sequence() sq.eval_async(kp.OpTensorSyncLocal(params)) # ... Do other work asynchronously whilst GPU finishes sq.eval_await() # Prints the first output which is: { 4, 8, 12 } print(tensor_out_a) # Prints the first output which is: { 10, 10, 10 } print(tensor_out_b) if __name__ == "__main__": # Define a raw string shader (or use the Kompute tools to compile to SPIRV / C++ header # files). This shader shows some of the main components including constants, buffers, etc shader = """ #version 450 layout (local_size_x = 1) in; // The input tensors bind index is relative to index in parameter passed layout(set = 0, binding = 0) buffer buf_in_a { float in_a[]; }; layout(set = 0, binding = 1) buffer buf_in_b { float in_b[]; }; layout(set = 0, binding = 2) buffer buf_out_a { uint out_a[]; }; layout(set = 0, binding = 3) buffer buf_out_b { uint out_b[]; }; // Kompute supports push constants updated on dispatch layout(push_constant) uniform PushConstants { float val; } push_const; // Kompute also supports spec constants on initalization layout(constant_id = 0) const float const_one = 0; void main() { uint index = gl_GlobalInvocationID.x; out_a[index] += uint( in_a[index] * in_b[index] ); out_b[index] += uint( const_one * push_const.val ); } " kompute(shader) ``` -------------------------------- ### Install Kompute Python Package Source: https://github.com/komputeproject/kompute/blob/master/docs/overview/ci-tests.rst Install the Kompute Python package locally for development or testing. This command builds the distribution from source. ```bash pip install . ``` -------------------------------- ### Install Kompute Targets Source: https://github.com/komputeproject/kompute/blob/master/CMakeLists.txt Installs the Kompute targets, including CMake export files, when KOMPUTE_OPT_INSTALL is enabled. This is typically done last as other targets might be exported in subdirectories. ```cmake install(EXPORT komputeTargets FILE komputeTargets.cmake NAMESPACE kompute:: DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/kompute) ``` -------------------------------- ### Install pybind11::headers Target Source: https://github.com/komputeproject/kompute/blob/master/src/CMakeLists.txt Installs the pybind11::headers target when KOMPUTE_OPT_INSTALL, KOMPUTE_OPT_BUILD_PYTHON, and KOMPUTE_OPT_USE_BUILT_IN_PYBIND11 are enabled. It unwraps the alias target for installation. ```cmake if(KOMPUTE_OPT_INSTALL) if(KOMPUTE_OPT_BUILD_PYTHON AND KOMPUTE_OPT_USE_BUILT_IN_PYBIND11) # We can't export pybind11::headers because it's alias target, so we unwrap the alias and install it. get_target_property(pybind11_aliased_target pybind11::headers ALIASED_TARGET) install(TARGETS ${pybind11_aliased_target} EXPORT komputeTargets) endif () endif() ``` -------------------------------- ### Build Documentation with CMake Source: https://github.com/komputeproject/kompute/blob/master/docs/overview/ci-tests.rst Commands to clean the CMake build, reconfigure, and build the documentation. Ensure CI dependencies are installed first. ```bash make clean_cmake mk_cmake mk_build_docs ``` -------------------------------- ### Install Shader Conversion Requirements Source: https://github.com/komputeproject/kompute/blob/master/docs/overview/shaders-to-headers.rst Installs the necessary Python dependencies for the shader conversion utility script. Ensure Python 3 and pip3 are installed. ```bash python3 -m pip install -r scripts/requirements.txt ``` -------------------------------- ### Kompute Tensor Initialization and Setup Source: https://github.com/komputeproject/kompute/blob/master/docs/overview/python-examples.rst Initializes Kompute tensors for input features, target values, weights, biases, and gradients. This setup is crucial before dispatching the compute shader for training. Ensure tensors are synchronized to the device before use. ```python mgr = Manager() # First we create input and ouput tensors for shader tensor_x_i = mgr.tensor([0.0, 1.0, 1.0, 1.0, 1.0]) tensor_x_j = mgr.tensor([0.0, 0.0, 0.0, 1.0, 1.0]) tensor_y = mgr.tensor([0.0, 0.0, 0.0, 1.0, 1.0]) tensor_w_in = mgr.tensor([0.001, 0.001]) tensor_w_out_i = mgr.tensor([0.0, 0.0, 0.0, 0.0, 0.0]) tensor_w_out_j = mgr.tensor([0.0, 0.0, 0.0, 0.0, 0.0]) tensor_b_in = mgr.tensor([0.0]) tensor_b_out = mgr.tensor([0.0, 0.0, 0.0, 0.0, 0.0]) tensor_l_out = mgr.tensor([0.0, 0.0, 0.0, 0.0, 0.0]) tensor_m = mgr.tensor([ 5.0 ]) # We store them in an array for easier interaction params = [tensor_x_i, tensor_x_j, tensor_y, tensor_w_in, tensor_w_out_i, tensor_w_out_j, tensor_b_in, tensor_b_out, tensor_l_out, tensor_m] some_sequence.eval(kp.OpTensorSyncDevice(params)) ``` -------------------------------- ### Install Shader Compiler CMake Files Source: https://github.com/komputeproject/kompute/blob/master/CMakeLists.txt Installs CMake files required for the vulkan_compile_shader target when KOMPUTE_OPT_INSTALL is enabled. ```cmake install(FILES cmake/vulkan_shader_compiler.cmake cmake/bin_file_to_header.cmake cmake/bin2h.cmake DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/kompute) ``` -------------------------------- ### Fetch Kompute via CMake Source: https://github.com/komputeproject/kompute/blob/master/docs/overview/shaders-to-headers.rst Includes the Kompute library into a CMake project using `FetchContent`. This example demonstrates how to set up include directories for shader compilation. ```cmake # Consume Kompute via CMake fetch_content include(FetchContent) FetchContent_Declare(kompute GIT_REPOSITORY https://github.com/KomputeProject/kompute.git GIT_TAG 1344ece4ac278f9b3be3b4555ffaace7a032b91f) # The commit hash for a dev version before v0.9.0. Replace with the latest from: https://github.com/KomputeProject/kompute/releases FetchContent_MakeAvailable(kompute) include_directories(${kompute_SOURCE_DIR}/src/include) include_directories(${kompute_BINARY_DIR}/src/shaders/glsl) # Add to the list, so CMake can later find the code to compile shaders to header files list(APPEND CMAKE_PREFIX_PATH "${kompute_SOURCE_DIR}/cmake") # To add more shaders simply copy the vulkan_compile_shader command and replace it with your new shader vulkan_compile_shader(INFILE my_shader.comp ``` -------------------------------- ### Install Kompute and Mesa Build Dependencies Source: https://github.com/komputeproject/kompute/blob/master/examples/pi4_mesa_build/README.md Installs essential packages required for building the Mesa graphics library and running Kompute. This includes development tools, Python build tools, and various X11/Vulkan libraries. ```bash sudo apt-get install \ git build-essential cmake \ python3-dev python3-mako python3-venv \ flex bison meson ninja-build \ libxcb-shm0-dev libxcb1-dev libxcb-*-dev \ libx11-dev libx11-xcb-dev x11proto-dri2-dev x11proto-dri3-dev \ libdrm-dev libxshmfence-dev libxrandr-dev libxfixes-dev \ vulkan-tools libvulkan-dev ``` -------------------------------- ### Execute Kompute Array Multiplication Example on Windows Source: https://github.com/komputeproject/kompute/blob/master/examples/array_multiplication/README.md Run the compiled Kompute array multiplication executable from the build directory on Windows systems. ```bash .\Debug\kompute_array_mult.exe ``` -------------------------------- ### Install Vulkan::Headers Target Source: https://github.com/komputeproject/kompute/blob/master/src/CMakeLists.txt Installs the Vulkan::Headers target when KOMPUTE_OPT_INSTALL and KOMPUTE_OPT_USE_BUILT_IN_VULKAN_HEADER are enabled. It unwraps the alias target and resets its include directories for installation. ```cmake if(KOMPUTE_OPT_INSTALL) if(KOMPUTE_OPT_USE_BUILT_IN_VULKAN_HEADER) # We can't export Vulkan::Headers because it's alias target, so we unwrap the alias and install it. get_target_property(vk_header_target Vulkan::Headers ALIASED_TARGET) # Before installation, reset the INTERFACE_INCLUDE_DIRECTORIES to a generator expression # Otherwise INTERFACE_INCLUDE_DIRECTORIES will point to an absolute path even if the software package # is copied to another computer. If we don't do this, cmake will complains about it in the configuration step. # Get the build interface of include dir get_target_property(vk_header_include_dir ${vk_header_target} INTERFACE_INCLUDE_DIRECTORIES) #message(STATUS "vk_header_include_dir = ${vk_header_include_dir}") # Reset INTERFACE_INCLUDE_DIRECTORIES to a generator expression. set_target_properties(${vk_header_target} PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "$;$" ) #message(STATUS "vk_header_include_dir = ${vk_header_include_di r}") install(TARGETS ${vk_header_target} EXPORT komputeTargets) endif () endif() ``` -------------------------------- ### Custom Shader Dispatch Example Source: https://github.com/komputeproject/kompute/blob/master/docs/overview/advanced-examples.rst Demonstrates how to define and use a custom shader for GPU computation. This involves creating tensors, defining the shader code, and dispatching the algorithm. ```cpp uint index = gl_GlobalInvocationID.x; valuesOutput[index] = valuesLhs[index] * valuesRhs[index]; } ); algorithm->rebuild(tensors, spirv); } }; int main() { kp::Manager mgr; // Automatically selects Device 0 // Create 3 tensors of default type float auto tensorLhs = mgr.tensor({ 0., 1., 2. }); auto tensorRhs = mgr.tensor({ 2., 4., 6. }); auto tensorOut = mgr.tensor({ 0., 0., 0. }); mgr.sequence() ->record({tensorLhs, tensorRhs, tensorOut}) ->record({tensorLhs, tensorRhs, tensorOut}, mgr.algorithm()) ->record({tensorLhs, tensorRhs, tensorOut}) ->eval(); // Prints the output which is { 0, 4, 12 } std::cout << fmt::format("Output: {}", tensorOut->vector()) << std::endl; } ``` -------------------------------- ### Execute Kompute Array Multiplication Example on Linux Source: https://github.com/komputeproject/kompute/blob/master/examples/array_multiplication/README.md Run the compiled Kompute array multiplication executable from the build directory on Linux systems. ```bash ./kompute_array_mult ``` -------------------------------- ### Enable Install Parameters for glslang Source: https://github.com/komputeproject/kompute/blob/master/CMakeLists.txt Sets ENABLE_GLSLANG_INSTALL to ON when KOMPUTE_OPT_INSTALL is enabled, which also makes glslang libraries shared. ```cmake set(ENABLE_GLSLANG_INSTALL ON CACHE BOOL "Enables install of glslang" FORCE) ``` -------------------------------- ### Link Libraries Based on Logger Setup Source: https://github.com/komputeproject/kompute/blob/master/src/logger/CMakeLists.txt Links the appropriate libraries (spdlog or fmt) and sets compile definitions based on the chosen logging setup. This section handles spdlog-specific configurations like async mode. ```cmake # Link depending on how the logger should be setup if(NOT KOMPUTE_OPT_LOG_LEVEL_DISABLED) if(KOMPUTE_OPT_USE_SPDLOG) target_link_libraries(kp_logger PUBLIC spdlog::spdlog) target_compile_definitions(kp_logger INTERFACE SPDLOG_ACTIVE_LEVEL=SPDLOG_LEVEL_${KOMPUTE_OPT_LOG_LEVEL}) message(STATUS "setting SPDLOG_ACTIVE_LEVEL to SPDLOG_LEVEL_${KOMPUTE_OPT_LOG_LEVEL}") if(KOMPUTE_OPT_SPDLOG_ASYNC_MODE) target_compile_definitions(kp_logger INTERFACE KOMPUTE_SPDLOG_ASYNC_LOGGING=1) endif() else() target_link_libraries(kp_logger PUBLIC fmt::fmt) endif() endif() ``` -------------------------------- ### Update System Packages Source: https://github.com/komputeproject/kompute/blob/master/examples/pi4_mesa_build/README.md Ensure all installed packages are up-to-date before proceeding with installations. This is a standard first step for most Linux package management tasks. ```bash sudo apt-get update sudo apt-get upgrade ``` -------------------------------- ### Execute Kompute Logistic Regression Example on Windows Source: https://github.com/komputeproject/kompute/blob/master/examples/logistic_regression/README.md Run the compiled Kompute logistic regression executable from within the build directory on Windows systems. ```bash .\Debug\kompute_logistic_regression.exe ``` -------------------------------- ### Link Executable with Kompute and Shaders Source: https://github.com/komputeproject/kompute/blob/master/examples/vulkan_ext_printf/CMakeLists.txt Defines the main executable for the example and links it against the compiled shader library and the Kompute library. This makes the shader and Kompute functionalities available to the application. ```cmake # Setting up main example code add_executable(example_shader_printf src/main.cpp) target_link_libraries(example_shader_printf PRIVATE shader kompute::kompute) ``` -------------------------------- ### Windows DLL Installation for Benchmark Source: https://github.com/komputeproject/kompute/blob/master/benchmark/CMakeLists.txt Custom command to copy necessary DLLs to the benchmark executable directory on Windows when building shared libraries. This ensures the benchmark can run directly. ```cmake if(WIN32 AND BUILD_SHARED_LIBS) # Install dlls in the same directory as the executable on Windows so one can simply double click them add_custom_command(TARGET kompute_benchmark POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy $ $) add_custom_command(TARGET kompute_benchmark POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy $ $) add_custom_command(TARGET kompute_benchmark POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy $ $) add_custom_command(TARGET kompute_benchmark POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy $ $) endif() ``` -------------------------------- ### Enable Shader Extension in Kompute Source: https://github.com/komputeproject/kompute/blob/master/docs/overview/advanced-examples.rst This example shows how to enable the 'VK_EXT_shader_atomic_float' extension during Kompute Manager initialization to allow atomic float operations in shaders. Ensure the extension is available on your driver. ```cpp int main() { std::string shader(R"( #version 450 #extension GL_EXT_shader_atomic_float: enable layout(push_constant) uniform PushConstants { float x; float y; float z; } pcs; layout (local_size_x = 1) in; layout(set = 0, binding = 0) buffer a { float pa[]; }; void main() { atomicAdd(pa[0], pcs.x); atomicAdd(pa[1], pcs.y); atomicAdd(pa[2], pcs.z); })"); // See shader documentation section for compileSource std::vector spirv = compileSource(shader); std::shared_ptr sq = nullptr; { kp::Manager mgr(0, {}, { "VK_EXT_shader_atomic_float" }); std::shared_ptr tensor = mgr.tensor({ 0, 0, 0 }); std::shared_ptr algo = mgr.algorithm({ tensor }, spirv, kp::Workgroup({ 1 }), {}, { 0.0, 0.0, 0.0 }); sq = mgr.sequence() ->record({ tensor }) ->record(algo, std::vector{ 0.1, 0.2, 0.3 }) ->record(algo, std::vector{ 0.3, 0.2, 0.1 }) ->record({ tensor }) ->eval(); EXPECT_EQ(tensor->data(), std::vector({ 0.4, 0.4, 0.4 })); } } ``` -------------------------------- ### Execute Kompute Logistic Regression Example on Linux Source: https://github.com/komputeproject/kompute/blob/master/examples/logistic_regression/README.md Run the compiled Kompute logistic regression executable from within the build directory on Linux systems. ```bash ./kompute_logistic_regression ``` -------------------------------- ### Kompute CMake Build Configuration Source: https://github.com/komputeproject/kompute/blob/master/examples/array_multiplication/CMakeLists.txt This CMake script configures the build for the Kompute array multiplication example. It handles fetching Kompute from source or a Git repository and compiling shaders. ```cmake cmake_minimum_required(VERSION 3.20) project(kompute_array_mult) set(CMAKE_CXX_STANDARD 14) # Options option(KOMPUTE_OPT_GIT_TAG "The tag of the repo to use for the example" v0.9.0) option(KOMPUTE_OPT_FROM_SOURCE "Whether to build example from source or from git fetch repo" ON) if(KOMPUTE_OPT_FROM_SOURCE) add_subdirectory(../../ ${CMAKE_CURRENT_BINARY_DIR}/kompute_build) else() include(FetchContent) FetchContent_Declare(kompute GIT_REPOSITORY https://github.com/KomputeProject/kompute.git GIT_TAG ${KOMPUTE_OPT_GIT_TAG}) FetchContent_MakeAvailable(kompute) include_directories(${kompute_SOURCE_DIR}/src/include) endif() # Compiling shader # To add more shaders simply copy the vulkan_compile_shader command and replace it with your new shader vulkan_compile_shader( INFILE shader/my_shader.comp OUTFILE shader/my_shader.hpp NAMESPACE "shader") # Then add it to the library, so you can access it later in your code add_library(shader INTERFACE "shader/my_shader.hpp") target_include_directories(shader INTERFACE $) # Setting up main example code add_executable(kompute_array_mult src/main.cpp) target_link_libraries(kompute_array_mult PRIVATE shader kompute::kompute) ``` -------------------------------- ### Benchmark Naive Matrix Multiplication in Python Source: https://github.com/komputeproject/kompute/blob/master/docs/overview/matmul-benchmark.rst This script runs a naive implementation of matrix multiplication to evaluate performance. Ensure Python is installed. ```python import time import numpy as np def naive_matmul(A, B): C = np.zeros((A.shape[0], B.shape[1])) for i in range(A.shape[0]): for j in range(B.shape[1]): for k in range(A.shape[1]): C[i, j] += A[i, k] * B[k, j] return C if __name__ == "__main__": # Define matrix dimensions M = 128 N = 128 P = 128 # Create random matrices A = np.random.rand(M, N) B = np.random.rand(N, P) # Time the naive implementation start_time = time.time() C_naive = naive_matmul(A, B) end_time = time.time() print(f"Naive matmul took {end_time - start_time:.4f} seconds") # Verify with numpy's matmul C_numpy = np.matmul(A, B) if np.allclose(C_naive, C_numpy): print("Results match numpy's matmul.") else: print("Results do NOT match numpy's matmul.") ``` -------------------------------- ### Kompute CMake Configuration Source: https://github.com/komputeproject/kompute/blob/master/examples/vulkan_ext_printf/CMakeLists.txt Configures the CMake project for the Kompute Vulkan extensions printf example. It sets the C++ standard, defines options for building from source or Git, and includes the Kompute subdirectory or fetches it using FetchContent. ```cmake cmake_minimum_required(VERSION 3.20) project(kompute_vulkan_extensions_printf) set(CMAKE_CXX_STANDARD 14) # Options option(KOMPUTE_OPT_GIT_TAG "The tag of the repo to use for the example" v0.9.0) option(KOMPUTE_OPT_FROM_SOURCE "Whether to build example from source or from git fetch repo" ON) if(KOMPUTE_OPT_FROM_SOURCE) add_subdirectory(../../ ${CMAKE_CURRENT_BINARY_DIR}/kompute_build) else() include(FetchContent) FetchContent_Declare(kompute GIT_REPOSITORY https://github.com/KomputeProject/kompute.git GIT_TAG ${KOMPUTE_OPT_GIT_TAG}) FetchContent_MakeAvailable(kompute) include_directories(${kompute_SOURCE_DIR}/src/include) endif() ``` -------------------------------- ### Execute Operations in Parallel Across Multiple GPU Queues Source: https://context7.com/komputeproject/kompute/llms.txt Initialize the Kompute manager with multiple queue families to enable parallel execution of operations on different GPU queues. This example demonstrates running two distinct algorithms asynchronously on separate queues and then synchronizing the results. ```cpp #include std::vector compileSource(const std::string& source); int main() { // Initialize manager with multiple queue families // Family 0: Graphics+Compute, Family 2: Compute-only uint32_t deviceIndex = 0; std::vector familyIndices = {0, 2}; kp::Manager mgr(deviceIndex, familyIndices); // Create sequences on different queues auto sqOne = mgr.sequence(0); // Graphics+Compute queue auto sqTwo = mgr.sequence(1); // Compute-only queue auto tensorA = mgr.tensorT(10); auto tensorB = mgr.tensorT(10); std::string shader = R"( #version 450 layout(local_size_x = 1) in; layout(set = 0, binding = 0) buffer buf { float data[]; }; void main() { uint idx = gl_GlobalInvocationID.x; float result = 0.0; for (int i = 0; i < 100000000; i++) { result += 1.0; } data[idx] = result; } )"; auto spirv = compileSource(shader); auto algoOne = mgr.algorithm({tensorA}, spirv); auto algoTwo = mgr.algorithm({tensorB}, spirv); // Sync tensors to device mgr.sequence()->eval({tensorA, tensorB}); // Run parallel operations on different queues sqOne->evalAsync(algoOne); sqTwo->evalAsync(algoTwo); // Both operations execute in parallel on GPU // Wait for both to complete sqOne->evalAwait(); sqTwo->evalAwait(); // Sync results back mgr.sequence()->eval({tensorA, tensorB}); std::cout << "A[0]: " << tensorA->data()[0] << std::endl; std::cout << "B[0]: " << tensorB->data()[0] << std::endl; return 0; } ``` -------------------------------- ### Configure Kompute Logger to INFO Level Source: https://github.com/komputeproject/kompute/blob/master/docs/overview/python-package.rst Sets the Kompute logger to the INFO level, allowing informational messages from the C++ backend to be displayed. This example shows the output when creating a Manager instance. ```python >>> import kp >>> import logging >>> >>> kp_logger = logging.getLogger("kp") >>> kp_logger.setLevel(logging.INFO) >>> >>> kp.Manager() INFO:kp:Using physical device index {} found {} ``` -------------------------------- ### Define Custom Kompute Operation Source: https://github.com/komputeproject/kompute/blob/master/docs/overview/advanced-examples.rst This example demonstrates how to create a custom Kompute operation by inheriting from `kp::OpAlgoDispatch`. It defines a shader for element-wise multiplication of two input tensors into an output tensor. ```cpp class OpMyCustom : public kp::OpAlgoDispatch { public: OpMyCustom(std::vector> tensors, std::shared_ptr algorithm) : kp::OpAlgoDispatch(algorithm) { if (tensors.size() != 3) { throw std::runtime_error("Kompute OpMult expected 3 tensors but got " + tensors.size()); } // See shader documentation section for compileSource std::vector spirv = compileSource(R"( #version 450 layout(set = 0, binding = 0) buffer tensorLhs { float valuesLhs[ ]; }; layout(set = 0, binding = 1) buffer tensorRhs { float valuesRhs[ ]; }; layout(set = 0, binding = 2) buffer tensorOutput { float valuesOutput[ ]; }; layout (constant_id = 0) const uint LEN_LHS = 0; layout (constant_id = 1) const uint LEN_RHS = 0; layout (constant_id = 2) const uint LEN_OUT = 0; layout (local_size_x = 1, local_size_y = 1, local_size_z = 1) in; void main() { ``` -------------------------------- ### Create and Initialize kp::Image Objects Source: https://context7.com/komputeproject/kompute/llms.txt Demonstrates creating various kp::Image objects with different data types, dimensions, and tiling modes using the kp::Manager. Ensure correct data vector sizes match image dimensions and channel counts. ```cpp #include int main() { kp::Manager mgr; // Create float image: width=4, height=4, channels=3 (RGB) std::vector imageData(4 * 4 * 3, 0.0f); // 48 floats auto image = mgr.image(imageData, 4, 4, 3); // Create typed image auto imageUint = mgr.imageT( std::vector(64, 0), // data 8, 8, // width, height 1 // channels (grayscale) ); // Create image without initial data auto emptyImage = mgr.imageT( 256, 256, // width, height 4 // channels (RGBA) ); // With explicit tiling mode auto tiledImage = mgr.imageT( imageData, 4, 4, 3, vk::ImageTiling::eOptimal, kp::Memory::MemoryTypes::eDevice ); return 0; } ``` -------------------------------- ### Initialize and Manage Kompute Manager Source: https://context7.com/komputeproject/kompute/llms.txt Demonstrates initializing the kp::Manager with default or custom settings, listing available devices, and retrieving device properties. The Manager handles Vulkan boilerplate and resource lifecycle. ```cpp #include int main() { // Default constructor - selects device 0 with default queue kp::Manager mgr; // Select specific device with custom queue family indices uint32_t deviceIndex = 0; std::vector familyQueueIndices = {0, 2}; // Graphics + Compute queues kp::Manager mgrAdvanced(deviceIndex, familyQueueIndices); // Enable Vulkan extensions kp::Manager mgrWithExtensions(0, {}, {"VK_EXT_shader_atomic_float"}); // List available devices std::vector devices = mgr.listDevices(); // Get current device properties vk::PhysicalDeviceProperties props = mgr.getDeviceProperties(); std::cout << "Device: " << props.deviceName << std::endl; // Manager automatically cleans up all managed resources on destruction return 0; } ``` -------------------------------- ### Serve Documentation Locally Source: https://github.com/komputeproject/kompute/blob/master/docs/overview/ci-tests.rst Command to serve the built documentation locally. Requires the documentation to be built first. ```bash mk_run_docs ``` -------------------------------- ### Basic Kompute Build with CMake Source: https://github.com/komputeproject/kompute/blob/master/docs/overview/build-system.rst Standard procedure to clone the Kompute repository, create a build directory, and configure and build the project using CMake. This default build does not include extra tasks like shader compilation or optional dependencies. ```bash git clone https://github.com/KomputeProject/kompute.git cd kompute mkdir build cd build cmake .. cmake --build . ``` -------------------------------- ### Export Kompute Logger Target Source: https://github.com/komputeproject/kompute/blob/master/src/logger/CMakeLists.txt Creates an alias target for 'kompute::logger' and conditionally installs the logger target if installation is enabled. This facilitates easy linking in other projects. ```cmake # Nothing will be installed, this will only export kp_logger to komputeTargets add_library(kompute::logger ALIAS kp_logger) if(KOMPUTE_OPT_INSTALL) install(TARGETS kp_logger EXPORT komputeTargets) endif() ``` -------------------------------- ### Shader Conversion Utility Help Source: https://github.com/komputeproject/kompute/blob/master/docs/overview/shaders-to-headers.rst Displays the help message for the Python shader conversion utility, showing available command-line options. ```bash > python3 scripts/convert_shaders.py --help Usage: convert_shaders.py [OPTIONS] CLI function for shader generation Options: -p, --shader-path TEXT The path for the directory to build and convert shaders [required] -s, --shader-binary TEXT The path for the directory to build and convert shaders [required] -c, --header-path TEXT The (optional) output file for the cpp header files -v, --verbose Enable verbosity if flag is provided --help Show this message and exit. ``` -------------------------------- ### Find pybind11 Package Source: https://github.com/komputeproject/kompute/blob/master/CMakeLists.txt Finds an installed pybind11 package. This is used when KOMPUTE_OPT_USE_BUILT_IN_PYBIND11 is disabled. ```cmake find_package(pybind11 REQUIRED) ``` -------------------------------- ### Find GoogleTest Package Source: https://github.com/komputeproject/kompute/blob/master/CMakeLists.txt Finds an installed GoogleTest package. This is used when KOMPUTE_OPT_USE_BUILT_IN_GOOGLE_TEST is disabled. ```cmake find_package(GTest CONFIG REQUIRED) ``` -------------------------------- ### Add Kompute as a Subdirectory Source: https://github.com/komputeproject/kompute/blob/master/README.md Integrate Kompute into your project by adding it as a subdirectory using CMake. Refer to the Android example for a practical implementation. ```cmake add_subdirectory(kompute) ``` -------------------------------- ### Initialize Kompute Build Directory Source: https://github.com/komputeproject/kompute/blob/master/README.md Initialize the build directory for Kompute using CMake. This command sets up the build configuration for cross-platform builds. ```bash cmake -Bbuild ```