-
Notifications
You must be signed in to change notification settings - Fork 183
Adding simple example of using debug_printf #447
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
eokeeffe
wants to merge
28
commits into
KomputeProject:master
Choose a base branch
from
eokeeffe:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+186
−0
Open
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
798ba17
add datatype enum to python module (#404)
koubaa 82bc222
Add noexcept to all constructors to explicitly state no exception thr…
ThePseudo 08a5261
Remove copy operations from classes in Kompute (#412)
ThePseudo fcf0f34
Fix noexcept missing from OpMemoryBarrier (#413)
axsaucedo 9fb1876
414 support for 1 4 headers (#415)
axsaucedo b4f74cb
Fix syntax error (MSVC) in Manager.cpp (#418)
jsrgb 63b84b3
Fix compile error in android (#423)
zlaazlaa 5ad7972
Make spdlog and fmt mutually exclusive and support -Werror=missing-br…
zfergus 9dd9c71
Add missing eDeviceAndHost MemoryType to the Python Bindings (#424)
robquill 1304c0f
feat: Add log library linking to Android build (#428)
zlaazlaa 3160b6a
Fix python SpecConst and PushConst bugs (#430)
axsaucedo d76e86b
Update Python Matmul examples (#434)
n-jay 77a0a0d
update internal gtest to v1.17.0 (#437)
TinyTinni 2488e92
Fix incorrect creation of python arrays in Tensor.data (#440)
robquill ad03e3b
Updated to pybind 3.0.0 (#431)
axsaucedo 38a1a81
Clarify status of llama.cpp in README (#446)
axsaucedo 7f1edae
adding kompute example using debug printf
67a0a84
adding link to the draft article
9cc8eca
Updating the readme for the debug statement example
d2e316b
Adding in the example shader, gitignore targets it so needed to force in
2d69587
Modifying the comment to include bytes in the comment on message length
1030b8b
Correcting the readme for spelling mistakes in vulkan_ext_printf
19caffe
Fix family queue selection in tests (#448)
TinyTinni 8fbcef0
removes compile definition defining for spdlog target (#449)
TinyTinni 958a55e
adding kompute example using debug printf
fb328f7
adding link to the draft article
4d5f5b1
Updating the readme for the debug statement example
466aaed
Merge branch 'master' into master
eokeeffe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| 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() | ||
|
|
||
| # 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/example_shader.comp | ||
| OUTFILE shader/example_shader.hpp | ||
| NAMESPACE "shader") | ||
|
|
||
| # Then add it to the library, so you can access it later in your code | ||
| add_library(shader INTERFACE "shader/example_shader.hpp") | ||
| target_include_directories(shader INTERFACE $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>) | ||
|
|
||
| # Setting up main example code | ||
| add_executable(example_shader_printf src/main.cpp) | ||
| target_link_libraries(example_shader_printf PRIVATE shader kompute::kompute) | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| # Kompute Debugging GLSL Shader using DebugPrint | ||
|
|
||
| This folder contains an end to end Kompute Example that implements a GLSL with a debug print statement. | ||
| This example is structured such that you will be able to extend it for your project. | ||
| It contains a CMake build configuration that can be used in your production applications. | ||
|
|
||
| ## Further information | ||
| Debugging Vulkan shaders, especially compute shaders, can be very difficult to do even with the aid | ||
| of a powerful debugging tool like RenderDoc. Debug Printf is a recent Vulkan feature that allows | ||
| developers to debug their shaders by inserting Debug Print statements. This debug print statement operates | ||
| quite like the C printf statement, only that it is executed in multiple compute cores at the same time, so it needs some logic to allow semantic usage of the debug statement otherwise you can easily be overwelmed by debug messages. | ||
| <br> | ||
|
|
||
| This feature is now supported within RenderDoc in a way that allows for per-invocation inspection of values in a shader. | ||
| This article describes how to instrument your GLSL or HLSL shaders with Debug Printf and how to | ||
| inspect and debug with them in the terminal, using vkconfig, or with environment variables. | ||
|
|
||
| For a full walkthrough of the process please see my article at https://medium.com/@evanokeeffe/using-debug-printf-in-vulkan-kompute-shaders-2aaf30bdb96c | ||
|
|
||
| ## Building the example | ||
|
|
||
| You will notice that it's a standalone project, so you can re-use it for your application. | ||
| It uses CMake's [`fetch_content`](https://cmake.org/cmake/help/latest/module/FetchContent.html) to consume Kompute as a dependency. | ||
| To build you just need to run the CMake command in this folder as follows: | ||
|
|
||
| ```bash | ||
| git clone https://github.com/KomputeProject/kompute.git | ||
| cd kompute/examples/vulkan_ext_printf | ||
| mkdir build | ||
| cd build | ||
| cmake .. | ||
| cmake --build . | ||
| ``` | ||
|
|
||
| ## Executing | ||
|
|
||
| From inside the `build/` directory run: | ||
|
|
||
| ### Linux | ||
|
|
||
| ```bash | ||
| ./example_shader_printf | ||
eokeeffe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ``` | ||
|
|
||
| ## Pre-requisites | ||
|
|
||
| In order to run this example, you will need the following dependencies: | ||
|
|
||
| * REQUIRED | ||
| + The Vulkan SDK must be installed | ||
|
|
||
| For the Vulkan SDK, the simplest way to install it is through [their website](https://vulkan.lunarg.com/sdk/home). You just have to follow the instructions for the relevant platform. | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| #version 450 | ||
| // To use Debug Printf in GLSL shaders, you need to enable the GL_EXT_debug_printf extension. | ||
| // Then add debugPrintfEXT calls at the locations in your shader where you want to print | ||
| // messages and/or values | ||
| #extension GL_EXT_debug_printf : enable | ||
|
|
||
| // The execution structure | ||
| layout (local_size_x = 1) in; | ||
|
|
||
| // The buffers are provided via the tensors | ||
| layout(binding = 0) buffer bufA { float a[]; }; | ||
| layout(binding = 1) buffer bufB { float b[]; }; | ||
| layout(binding = 2) buffer bufOut { float o[]; }; | ||
|
|
||
| void main() { | ||
| uint index = gl_GlobalInvocationID.x; | ||
| o[index] = a[index] * b[index]; | ||
|
|
||
| // the debug statement operates much the same as printf in C | ||
| // do be wary of the size of each line as the default debug message size is | ||
| // 1024 bytes | ||
| debugPrintfEXT("The result of %f x %f = %f \n", a[index], b[index], o[index]); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| #include <iostream> | ||
| #include <memory> | ||
| #include <vector> | ||
| #include <string> | ||
| #include <random> | ||
| #include <bits/stdc++.h> | ||
| #include <functional> // std::multiplies | ||
| #include <algorithm> // std::transform | ||
|
|
||
| #include <kompute/Kompute.hpp> | ||
| #include <shader/example_shader.hpp> | ||
eokeeffe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| std::vector<float> generate_random_floats(int n, float min_val, float max_val) { | ||
| // create std::vector and pre-allocate the vector with n floats | ||
| std::vector<float> vectorF = std::vector<float>(n); | ||
| // 1. Obtain a random seed from the hardware | ||
| std::random_device rd; | ||
| // 2. Initialize the generator with the seed | ||
| std::mt19937 gen(rd()); | ||
| // 3. Define the distribution range [0, n) | ||
| std::uniform_real_distribution<float> dis(min_val, max_val); | ||
| //fill the vector with randomly distributed floats | ||
| for (int i = 0; i < n; i++) {vectorF[i] = dis(gen);} | ||
| //return the filled vector | ||
| return vectorF; | ||
| } | ||
|
|
||
| int main(int argc, char *argv[]) | ||
| { | ||
| int device_id = 0; | ||
|
|
||
| if(argc>1){ | ||
| device_id = atoi(argv[1]); | ||
| }else{ | ||
| std::cout<<"Using device 0"<<std::endl; | ||
| } | ||
|
|
||
| // make sure to add the extension, check vulkan_info to see if your GPU vulkan driver supports the extension | ||
| // vulkaninfo | grep VK_KHR_shader_non_semantic_info | ||
| const std::vector<std::string> desiredExtensions = std::vector<std::string>({ | ||
| "VK_KHR_shader_non_semantic_info", | ||
| }); | ||
| const std::vector<uint32_t> familyQueueIndices = std::vector<uint32_t>({}); | ||
|
|
||
| kp::Manager mgr(device_id, familyQueueIndices, desiredExtensions); | ||
|
|
||
| int vector_length = 10; | ||
|
|
||
| const std::vector<float> A = generate_random_floats(vector_length, 1.0, 10.0); | ||
| const std::vector<float> B = generate_random_floats(vector_length, 1.0, 10.0); | ||
| const std::vector<float> C = generate_random_floats(vector_length, 0.0, 0.0); | ||
|
|
||
| std::shared_ptr<kp::TensorT<float>> tensorInA = mgr.tensorT<float>(A); | ||
| std::shared_ptr<kp::TensorT<float>> tensorInB = mgr.tensorT<float>(B); | ||
| std::shared_ptr<kp::TensorT<float>> tensorOut = mgr.tensorT<float>(C); | ||
|
|
||
| const std::vector<std::shared_ptr<kp::Memory>> params = { tensorInA, | ||
| tensorInB, | ||
| tensorOut }; | ||
|
|
||
| kp::Workgroup workgroup = { vector_length, 1, 1 }; | ||
|
|
||
| const std::vector<uint32_t> shader = std::vector<uint32_t>( | ||
| shader::EXAMPLE_SHADER_COMP_SPV.begin(), shader::EXAMPLE_SHADER_COMP_SPV.end()); | ||
| std::shared_ptr<kp::Algorithm> algo = mgr.algorithm(params, shader, workgroup); | ||
|
|
||
| mgr.sequence() | ||
| ->record<kp::OpSyncDevice>(params) | ||
| ->record<kp::OpAlgoDispatch>(algo) | ||
| ->record<kp::OpSyncLocal>(params) | ||
| ->eval(); | ||
|
|
||
| std::cout << "Output: { "; | ||
| for (const float& elem : tensorOut->vector()) { std::cout << elem << " ";} | ||
| std::cout << "}" << std::endl; | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.