Skip to content

Commit 61af4bc

Browse files
eokeeffekoubaakoubaaThePseudoaxsaucedo
authored
Adding simple example of using debug_printf (#447)
* add datatype enum to python module (#404) * add datatype enum to python module Signed-off-by: koubaa <koubaa@github.com> * add datatype enum to python module Signed-off-by: koubaa <koubaa@github.com> * add doc Signed-off-by: koubaa <koubaa@github.com> --------- Signed-off-by: koubaa <koubaa@github.com> Co-authored-by: koubaa <koubaa@github.com> Signed-off-by: evanokeeffe <evan.okeeffe@tas-2.com> * Add noexcept to all constructors to explicitly state no exception thrown #44 (#411) * Sequence con/destructor is noexcept Signed-off-by: Andrea Calabrese <andrea.calabrese@amarulasolutions.com> Signed-off-by: Andrea Calabrese <andrea1995.c@live.com> * Operations are noexcept Signed-off-by: Andrea Calabrese <andrea.calabrese@amarulasolutions.com> Signed-off-by: Andrea Calabrese <andrea1995.c@live.com> * Algorithm is noexcept Signed-off-by: Andrea Calabrese <andrea.calabrese@amarulasolutions.com> Signed-off-by: Andrea Calabrese <andrea1995.c@live.com> --------- Signed-off-by: Andrea Calabrese <andrea.calabrese@amarulasolutions.com> Signed-off-by: Andrea Calabrese <andrea1995.c@live.com> Signed-off-by: evanokeeffe <evan.okeeffe@tas-2.com> * Remove copy operations from classes in Kompute (#412) * Operations are non-copy As for Issue #20, operations should be non-copy. Instead of creating a base NonCopyable class, I prefer to keep this inside the classes themselves, so that we do not create new inherited structures. Signed-off-by: Andrea Calabrese <andrea.calabrese@amarulasolutions.com> Signed-off-by: Andrea Calabrese <andrea1995.c@live.com> * Make Algorithm uncopyable Same as previous commit Signed-off-by: Andrea Calabrese <andrea.calabrese@amarulasolutions.com> Signed-off-by: Andrea Calabrese <andrea1995.c@live.com> * Image is non-copy Signed-off-by: Andrea Calabrese <andrea.calabrese@amarulasolutions.com> Signed-off-by: Andrea Calabrese <andrea1995.c@live.com> * Manager is non-copy Signed-off-by: Andrea Calabrese <andrea.calabrese@amarulasolutions.com> Signed-off-by: Andrea Calabrese <andrea1995.c@live.com> * Memory is non-copy Signed-off-by: Andrea Calabrese <andrea.calabrese@amarulasolutions.com> Signed-off-by: Andrea Calabrese <andrea1995.c@live.com> * Sequence is non-copy Signed-off-by: Andrea Calabrese <andrea.calabrese@amarulasolutions.com> Signed-off-by: Andrea Calabrese <andrea1995.c@live.com> * Tensor is non-copy Signed-off-by: Andrea Calabrese <andrea.calabrese@amarulasolutions.com> Signed-off-by: Andrea Calabrese <andrea1995.c@live.com> --------- Signed-off-by: Andrea Calabrese <andrea.calabrese@amarulasolutions.com> Signed-off-by: Andrea Calabrese <andrea1995.c@live.com> Signed-off-by: evanokeeffe <evan.okeeffe@tas-2.com> * Fix noexcept missing from OpMemoryBarrier (#413) Signed-off-by: Alejandro Saucedo <alejandro.saucedo@zalando.de> Signed-off-by: evanokeeffe <evan.okeeffe@tas-2.com> * 414 support for 1 4 headers (#415) * Added guard for vk 1.4 headers Signed-off-by: Alejandro Saucedo <alejandro.saucedo@zalando.de> * CI test for 1.4 compatibility Signed-off-by: Alejandro Saucedo <alejandro.saucedo@zalando.de> CI test for 1.4 compatibility Signed-off-by: Alejandro Saucedo <alejandro.saucedo@zalando.de> CI test for 1.4 compatibility Signed-off-by: Alejandro Saucedo <alejandro.saucedo@zalando.de> CI test for 1.4 compatibility Signed-off-by: Alejandro Saucedo <alejandro.saucedo@zalando.de> CI test for 1.4 compatibility Signed-off-by: Alejandro Saucedo <alejandro.saucedo@zalando.de> CI test for 1.4 compatibility Signed-off-by: Alejandro Saucedo <alejandro.saucedo@zalando.de> Updated cast to match vk namespace Signed-off-by: Alejandro Saucedo <alejandro.saucedo@zalando.de> Updated cast to match vk namespace Signed-off-by: Alejandro Saucedo <alejandro.saucedo@zalando.de> * Updated debugMessageCallback func to vk:: ns Signed-off-by: Alejandro Saucedo <alejandro.saucedo@zalando.de> * Updated debugMessageCallback func to vk:: ns Signed-off-by: Alejandro Saucedo <alejandro.saucedo@zalando.de> --------- Signed-off-by: Alejandro Saucedo <alejandro.saucedo@zalando.de> Signed-off-by: evanokeeffe <evan.okeeffe@tas-2.com> * Fix syntax error (MSVC) in Manager.cpp (#418) Change VKAPI_PTR to VKAPI_ATTR Tested only on: MSVC version: `Microsoft (R) C/C++ Optimizing Compiler Version 19.43.34808 for x86` VulkanSDK version: 1.4.309.0 Latest kompute commit: 299b11f Signed-off-by: jamorphy <maps@jax2.net> Signed-off-by: evanokeeffe <evan.okeeffe@tas-2.com> * Fix compile error in android (#423) Signed-off-by: zlaazlaa <2889827787@qq.com> Signed-off-by: evanokeeffe <evan.okeeffe@tas-2.com> * Make spdlog and fmt mutually exclusive and support -Werror=missing-braces (#419) * Update bin2h.cmake to avoid -Werror=missing-braces Signed-off-by: zachferguson <zach.ferguson@clo3d.com> Signed-off-by: Zachary Ferguson <zach.ferguson@clo3d.com> * Use the fmt inside spdlog if using spdlog Signed-off-by: zachferguson <zach.ferguson@clo3d.com> Signed-off-by: Zachary Ferguson <zach.ferguson@clo3d.com> * Fix python build by linking fmt when KOMPUTE_OPT_LOG_LEVEL_DISABLED Signed-off-by: Zachary Ferguson <zach.ferguson@clo3d.com> --------- Signed-off-by: zachferguson <zach.ferguson@clo3d.com> Signed-off-by: Zachary Ferguson <zach.ferguson@clo3d.com> Signed-off-by: evanokeeffe <evan.okeeffe@tas-2.com> * Add missing eDeviceAndHost MemoryType to the Python Bindings (#424) * Add eDeviceAndHost to Python bindings for MemoryTypes. Update docstrings so that the build works. Signed-off-by: Robert Quill <robert.quill@imgtec.com> * Typo fix Signed-off-by: Robert Quill <robert.quill@imgtec.com> --------- Signed-off-by: Robert Quill <robert.quill@imgtec.com> Signed-off-by: evanokeeffe <evan.okeeffe@tas-2.com> * feat: Add log library linking to Android build (#428) Signed-off-by: zlaazlaa <2889827787@qq.com> Signed-off-by: evanokeeffe <evan.okeeffe@tas-2.com> * Fix python SpecConst and PushConst bugs (#430) * Fix push const inconsistent type bug Signed-off-by: Alejandro Saucedo <alejandro.saucedo@zalando.de> * Fix push const bug Signed-off-by: Alejandro Saucedo <alejandro.saucedo@zalando.de> * Added test to consts Signed-off-by: Alejandro Saucedo <alejandro.saucedo@zalando.de> * Added tests for const types@ Signed-off-by: Alejandro Saucedo <alejandro.saucedo@zalando.de> * Added test to consts Signed-off-by: Alejandro Saucedo <alejandro.saucedo@zalando.de> * Added test to consts Signed-off-by: Alejandro Saucedo <alejandro.saucedo@zalando.de> * Added test to consts Signed-off-by: Alejandro Saucedo <alejandro.saucedo@zalando.de> --------- Signed-off-by: Alejandro Saucedo <alejandro.saucedo@zalando.de> Signed-off-by: evanokeeffe <evan.okeeffe@tas-2.com> * Update Python Matmul examples (#434) * feat: Add utility function to compile GLSL source to SPIR-V Signed-off-by: n-jay <nuwanjayawardene@gmail.com> * refactor: Replace kp.Shader.compile_source with utility function compile_source Signed-off-by: n-jay <nuwanjayawardene@gmail.com> --------- Signed-off-by: n-jay <nuwanjayawardene@gmail.com> Signed-off-by: evanokeeffe <evan.okeeffe@tas-2.com> * update internal gtest to v1.17.0 (#437) Signed-off-by: Matthias Möller <m_moeller@live.de> Signed-off-by: evanokeeffe <evan.okeeffe@tas-2.com> * Fix incorrect creation of python arrays in Tensor.data (#440) Previously the Python array would just be filled with the first element of the data repeated to fill the array. Fixes a number of the Python tests. Signed-off-by: Robert Quill <robert.quill@imgtec.com> Signed-off-by: evanokeeffe <evan.okeeffe@tas-2.com> * Updated to pybind 3.0.0 (#431) Signed-off-by: Alejandro Saucedo <alejandro.saucedo@zalando.de> Signed-off-by: evanokeeffe <evan.okeeffe@tas-2.com> * Clarify status of llama.cpp in README (#446) Updated description for llama.cpp to indicate it is now decommissioned. Signed-off-by: Alejandro Saucedo <alejandro.saucedo@zalando.de> Signed-off-by: evanokeeffe <evan.okeeffe@tas-2.com> * adding kompute example using debug printf Signed-off-by: evanokeeffe <evan.okeeffe@tas-2.com> * adding link to the draft article Signed-off-by: evanokeeffe <evan.okeeffe@tas-2.com> * Updating the readme for the debug statement example Signed-off-by: evanokeeffe <evan.okeeffe@tas-2.com> * Adding in the example shader, gitignore targets it so needed to force in Signed-off-by: evanokeeffe <evan.okeeffe@tas-2.com> * Modifying the comment to include bytes in the comment on message length Signed-off-by: evanokeeffe <evan.okeeffe@tas-2.com> * Correcting the readme for spelling mistakes in vulkan_ext_printf Signed-off-by: evanokeeffe <evan.okeeffe@tas-2.com> * Fix family queue selection in tests (#448) * checks if given family queue has compute capability Signed-off-by: Matthias Möller <m_moeller@live.de> * test will search for queues with compute capabilities Signed-off-by: Matthias Möller <m_moeller@live.de> --------- Signed-off-by: Matthias Möller <m_moeller@live.de> Signed-off-by: evanokeeffe <evan.okeeffe@tas-2.com> * removes compile definition defining for spdlog target (#449) spdlog is not build when the CMake option "KOMPUTE_OPT_USE_BUILT_IN_SPDLOG" is set to false. When activating spdlog for logging via CMake option "KOMPUTE_OPT_USE_SPDLOG", CMake will deliver an error, as the current CMake file tries to add a definition to the spdlog target. Defining the compiler definition "SPDLOG_ACTIVE_LEVEL" has no effect on spdlog itself, it just has an effect on spdlog target consumers. Therefore, it can be removed so that the project can be build with the mentioned CMake options. Signed-off-by: Matthias Möller <m_moeller@live.de> Signed-off-by: evanokeeffe <evan.okeeffe@tas-2.com> * adding kompute example using debug printf Signed-off-by: evanokeeffe <evan.okeeffe@tas-2.com> * adding link to the draft article Signed-off-by: evanokeeffe <evan.okeeffe@tas-2.com> * Updating the readme for the debug statement example Signed-off-by: evanokeeffe <evan.okeeffe@tas-2.com> --------- Signed-off-by: koubaa <koubaa@github.com> Signed-off-by: evanokeeffe <evan.okeeffe@tas-2.com> Signed-off-by: Andrea Calabrese <andrea.calabrese@amarulasolutions.com> Signed-off-by: Andrea Calabrese <andrea1995.c@live.com> Signed-off-by: Alejandro Saucedo <alejandro.saucedo@zalando.de> Signed-off-by: jamorphy <maps@jax2.net> Signed-off-by: zlaazlaa <2889827787@qq.com> Signed-off-by: zachferguson <zach.ferguson@clo3d.com> Signed-off-by: Zachary Ferguson <zach.ferguson@clo3d.com> Signed-off-by: Robert Quill <robert.quill@imgtec.com> Signed-off-by: n-jay <nuwanjayawardene@gmail.com> Signed-off-by: Matthias Möller <m_moeller@live.de> Co-authored-by: Mohamed Koubaa <koubaa.m@gmail.com> Co-authored-by: koubaa <koubaa@github.com> Co-authored-by: Andrea Calabrese <18246320+ThePseudo@users.noreply.github.com> Co-authored-by: Alejandro Saucedo <axsauze@gmail.com> Co-authored-by: jamorphy <148006242+jamorphy@users.noreply.github.com> Co-authored-by: zlaa <72907523+zlaazlaa@users.noreply.github.com> Co-authored-by: Zachary Ferguson <zy.fergus@gmail.com> Co-authored-by: Robert Quill <robert.quill@imgtec.com> Co-authored-by: Nuwan Jayawardene <nuwanjayawardene@gmail.com> Co-authored-by: Matthias Möller <m_moeller@live.de> Co-authored-by: evanokeeffe <evan.okeeffe@tas-2.com>
1 parent e4b583f commit 61af4bc

File tree

4 files changed

+186
-0
lines changed

4 files changed

+186
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
cmake_minimum_required(VERSION 3.20)
2+
project(kompute_vulkan_extensions_printf)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
# Options
7+
option(KOMPUTE_OPT_GIT_TAG "The tag of the repo to use for the example" v0.9.0)
8+
option(KOMPUTE_OPT_FROM_SOURCE "Whether to build example from source or from git fetch repo" ON)
9+
10+
if(KOMPUTE_OPT_FROM_SOURCE)
11+
add_subdirectory(../../ ${CMAKE_CURRENT_BINARY_DIR}/kompute_build)
12+
else()
13+
include(FetchContent)
14+
FetchContent_Declare(kompute GIT_REPOSITORY https://github.com/KomputeProject/kompute.git
15+
GIT_TAG ${KOMPUTE_OPT_GIT_TAG})
16+
FetchContent_MakeAvailable(kompute)
17+
include_directories(${kompute_SOURCE_DIR}/src/include)
18+
endif()
19+
20+
# Compiling shader
21+
# To add more shaders simply copy the vulkan_compile_shader command and replace it with your new shader
22+
vulkan_compile_shader(
23+
INFILE shader/example_shader.comp
24+
OUTFILE shader/example_shader.hpp
25+
NAMESPACE "shader")
26+
27+
# Then add it to the library, so you can access it later in your code
28+
add_library(shader INTERFACE "shader/example_shader.hpp")
29+
target_include_directories(shader INTERFACE $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>)
30+
31+
# Setting up main example code
32+
add_executable(example_shader_printf src/main.cpp)
33+
target_link_libraries(example_shader_printf PRIVATE shader kompute::kompute)
34+
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Kompute Debugging GLSL Shader using DebugPrint
2+
3+
This folder contains an end to end Kompute Example that implements a GLSL with a debug print statement.
4+
This example is structured such that you will be able to extend it for your project.
5+
It contains a CMake build configuration that can be used in your production applications.
6+
7+
## Further information
8+
Debugging Vulkan shaders, especially compute shaders, can be very difficult to do even with the aid
9+
of a powerful debugging tool like RenderDoc. Debug Printf is a recent Vulkan feature that allows
10+
developers to debug their shaders by inserting Debug Print statements. This debug print statement operates
11+
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.
12+
<br>
13+
14+
This feature is now supported within RenderDoc in a way that allows for per-invocation inspection of values in a shader.
15+
This article describes how to instrument your GLSL or HLSL shaders with Debug Printf and how to
16+
inspect and debug with them in the terminal, using vkconfig, or with environment variables.
17+
18+
For a full walkthrough of the process please see my article at https://medium.com/@evanokeeffe/using-debug-printf-in-vulkan-kompute-shaders-2aaf30bdb96c
19+
20+
## Building the example
21+
22+
You will notice that it's a standalone project, so you can re-use it for your application.
23+
It uses CMake's [`fetch_content`](https://cmake.org/cmake/help/latest/module/FetchContent.html) to consume Kompute as a dependency.
24+
To build you just need to run the CMake command in this folder as follows:
25+
26+
```bash
27+
git clone https://github.com/KomputeProject/kompute.git
28+
cd kompute/examples/vulkan_ext_printf
29+
mkdir build
30+
cd build
31+
cmake ..
32+
cmake --build .
33+
```
34+
35+
## Executing
36+
37+
From inside the `build/` directory run:
38+
39+
### Linux
40+
41+
```bash
42+
./example_shader_printf
43+
```
44+
45+
## Pre-requisites
46+
47+
In order to run this example, you will need the following dependencies:
48+
49+
* REQUIRED
50+
+ The Vulkan SDK must be installed
51+
52+
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.
53+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#version 450
2+
// To use Debug Printf in GLSL shaders, you need to enable the GL_EXT_debug_printf extension.
3+
// Then add debugPrintfEXT calls at the locations in your shader where you want to print
4+
// messages and/or values
5+
#extension GL_EXT_debug_printf : enable
6+
7+
// The execution structure
8+
layout (local_size_x = 1) in;
9+
10+
// The buffers are provided via the tensors
11+
layout(binding = 0) buffer bufA { float a[]; };
12+
layout(binding = 1) buffer bufB { float b[]; };
13+
layout(binding = 2) buffer bufOut { float o[]; };
14+
15+
void main() {
16+
uint index = gl_GlobalInvocationID.x;
17+
o[index] = a[index] * b[index];
18+
19+
// the debug statement operates much the same as printf in C
20+
// do be wary of the size of each line as the default debug message size is
21+
// 1024 bytes
22+
debugPrintfEXT("The result of %f x %f = %f \n", a[index], b[index], o[index]);
23+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#include <iostream>
2+
#include <memory>
3+
#include <vector>
4+
#include <string>
5+
#include <random>
6+
#include <bits/stdc++.h>
7+
#include <functional> // std::multiplies
8+
#include <algorithm> // std::transform
9+
10+
#include <kompute/Kompute.hpp>
11+
#include <shader/example_shader.hpp>
12+
13+
std::vector<float> generate_random_floats(int n, float min_val, float max_val) {
14+
// create std::vector and pre-allocate the vector with n floats
15+
std::vector<float> vectorF = std::vector<float>(n);
16+
// 1. Obtain a random seed from the hardware
17+
std::random_device rd;
18+
// 2. Initialize the generator with the seed
19+
std::mt19937 gen(rd());
20+
// 3. Define the distribution range [0, n)
21+
std::uniform_real_distribution<float> dis(min_val, max_val);
22+
//fill the vector with randomly distributed floats
23+
for (int i = 0; i < n; i++) {vectorF[i] = dis(gen);}
24+
//return the filled vector
25+
return vectorF;
26+
}
27+
28+
int main(int argc, char *argv[])
29+
{
30+
int device_id = 0;
31+
32+
if(argc>1){
33+
device_id = atoi(argv[1]);
34+
}else{
35+
std::cout<<"Using device 0"<<std::endl;
36+
}
37+
38+
// make sure to add the extension, check vulkan_info to see if your GPU vulkan driver supports the extension
39+
// vulkaninfo | grep VK_KHR_shader_non_semantic_info
40+
const std::vector<std::string> desiredExtensions = std::vector<std::string>({
41+
"VK_KHR_shader_non_semantic_info",
42+
});
43+
const std::vector<uint32_t> familyQueueIndices = std::vector<uint32_t>({});
44+
45+
kp::Manager mgr(device_id, familyQueueIndices, desiredExtensions);
46+
47+
int vector_length = 10;
48+
49+
const std::vector<float> A = generate_random_floats(vector_length, 1.0, 10.0);
50+
const std::vector<float> B = generate_random_floats(vector_length, 1.0, 10.0);
51+
const std::vector<float> C = generate_random_floats(vector_length, 0.0, 0.0);
52+
53+
std::shared_ptr<kp::TensorT<float>> tensorInA = mgr.tensorT<float>(A);
54+
std::shared_ptr<kp::TensorT<float>> tensorInB = mgr.tensorT<float>(B);
55+
std::shared_ptr<kp::TensorT<float>> tensorOut = mgr.tensorT<float>(C);
56+
57+
const std::vector<std::shared_ptr<kp::Memory>> params = { tensorInA,
58+
tensorInB,
59+
tensorOut };
60+
61+
kp::Workgroup workgroup = { vector_length, 1, 1 };
62+
63+
const std::vector<uint32_t> shader = std::vector<uint32_t>(
64+
shader::EXAMPLE_SHADER_COMP_SPV.begin(), shader::EXAMPLE_SHADER_COMP_SPV.end());
65+
std::shared_ptr<kp::Algorithm> algo = mgr.algorithm(params, shader, workgroup);
66+
67+
mgr.sequence()
68+
->record<kp::OpSyncDevice>(params)
69+
->record<kp::OpAlgoDispatch>(algo)
70+
->record<kp::OpSyncLocal>(params)
71+
->eval();
72+
73+
std::cout << "Output: { ";
74+
for (const float& elem : tensorOut->vector()) { std::cout << elem << " ";}
75+
std::cout << "}" << std::endl;
76+
}

0 commit comments

Comments
 (0)