Skip to content
iliaskasmPublic

About

C++ wrapper for POLV library

Resources

Stars

1 star

Watchers

0 watching

Forks

Latest commit

 

History

2 Commits

Folders and files

Repository files navigation

POLV-C++

POLV-C++ is a C++11 wrapper around POLV Core, the low-level runtime of POLV-C. It lets C++ applications execute compute workloads on Vulkan-capable devices without managing Vulkan objects directly.

The library provides classes for devices, contexts, memory, and kernels. It uses RAII to manage resources automatically and supports both precompiled SPIR-V and GLSL compute shaders.

Dependencies

Building POLV-C++ requires:

  • a C compiler and a C++11 compiler (e.g., GCC or Clang)
  • GNU Make
  • Vulkan headers and loader development files
  • a Vulkan-capable device with a working driver to run compute kernels

Running GLSL samples or loading GLSL shaders also requires glslangValidator. Precompiled SPIR-V kernels do not require it at runtime.

On Debian or Ubuntu, the development tools can be installed with:

sudo apt install build-essential libvulkan-dev glslang-tools vulkan-tools

Check your Vulkan installation with:

vulkaninfo

Cloning

Clone the repository using:

git clone --recurse-submodules https://github.com/iliaskasm/POLV-Cpp.git
cd POLV-Cpp

...or without --recurse-submodules:

git clone https://github.com/iliaskasm/POLV-Cpp.git
cd POLV-Cpp
git submodule update --init --recursive

Installation

Build POLV-C++ with:

make

By default, build artifacts are placed under build/:

build/
    polv_core/
    polv_cpp/

To use a different build directory:

make BUILD_DIR=path/to/build

Optionally run the build checks:

make check

Local installation

By default, POLV-C++ installs into the install/ directory inside the repository:

make install

The installation contains:

install/
    include/
        polv
        polv.hpp
        polv_core.h
        polv_version.h
        polv_cpp_version.h
    lib/
        libpolvcore.so
        libpolvcpp.so

If you used a custom build directory, pass the same BUILD_DIR when installing:

make BUILD_DIR=path/to/build install

Build and run an example using the local installation:

make -C samples/02_vecadd
make -C samples/02_vecadd run

Installation under a different directory

POLV-C++ can also be installed under /usr/local, for example:

sudo make INSTALL_DIR=/usr/local/polv-cpp install

You can specify custom build and installation directories together:

sudo make \
    BUILD_DIR=path/to/build \
    INSTALL_DIR=/usr/local/polv-cpp \
    install

Add the include and library directories to the compiler, linker, and runtime library search paths:

export CPATH="/usr/local/polv-cpp/include${CPATH:+:$CPATH}"
export LIBRARY_PATH="/usr/local/polv-cpp/lib${LIBRARY_PATH:+:$LIBRARY_PATH}"
export LD_LIBRARY_PATH="/usr/local/polv-cpp/lib${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}"

To make these settings persistent for Bash, add them to ~/.bashrc. Afterward, a C++ application can be compiled with:

g++ -std=c++11 program.cpp -lpolvcpp -o program

libpolvcpp.so depends on libpolvcore.so, so both shared libraries must be available at runtime. Applications normally link against -lpolvcpp, without needing to specify -lpolvcore separately.

Device information

POLV-C++ provides device information without requiring applications to query Vulkan objects directly:

#include <polv>
#include <iostream>

int main()
{
	polv::Runtime runtime;

	for (int i = 0; i < runtime.getNumDevices(); ++i)
	{
		polv::Device device(i);
		const polv::DeviceInfo &info = device.getInfo();
		std::cout << i << ": " << info.name << '\n';
	}
}

DeviceInfo also exposes device type, identifiers, Vulkan API version, and memory-related information.

Samples

The C++ samples are located under samples/:

Sample Description
01_device_info Lists Vulkan-capable devices and their properties
02_vecadd Vector addition with device-local memory
03_vecadd_unified Vector addition with host-coherent memory
04_memcpy Host-to-device, device-to-host, and device-to-device copies
05_saxpy Single-precision SAXPY (alpha * x + y)
06_matadd Matrix addition
07_bandwidth H2D, D2H, and D2D bandwidth benchmark
08_matmul Simple matrix multiplication
09_sobel Sobel edge detection on a BMP image

Build and run a sample after installing the libraries:

make install
make -C samples/03_vecadd_unified
make -C samples/03_vecadd_unified run

..or build and run the complete sample set with:

make -C samples
make -C samples run

The bandwidth benchmark defaults to 64 MiB transfers and 20 timed iterations. You can override the transfer size (MiB) and iteration count:

./samples/07_bandwidth/bandwidth 256 50

Its H2D, D2H, and D2D figures represent end-to-end synchronous copy throughput, including runtime overhead, rather than raw PCIe or GPU memory bandwidth.

Example

The following example adds two arrays using a Vulkan compute shader and host-coherent memory. No explicit H2D or D2H copies are needed.

Compute shader

Create vecadd.comp:

#version 450

layout(local_size_x_id = 0,
       local_size_y_id = 1,
       local_size_z_id = 2) in;

layout(set = 0, binding = 0) readonly buffer A {
	float a[];
};

layout(set = 0, binding = 1) readonly buffer B {
	float b[];
};

layout(set = 0, binding = 2) writeonly buffer C {
	float c[];
};

void main()
{
	uint i = gl_GlobalInvocationID.x;

	if (i >= c.length())
		return;

	c[i] = a[i] + b[i];
}

Compile the shader to SPIR-V:

glslangValidator -V vecadd.comp -o vecadd.spv

C++ program

Create vecadd.cpp:

#include <polv>
#include <iostream>
#include <cstddef>
#include <cstdint>
#include <exception>

int main()
{
	const std::size_t n = 1024;
	const std::uint32_t threads = 128;
	const std::size_t bytes = n * sizeof(float);

	try
	{
		polv::Runtime runtime;
		polv::Device device(0);
		polv::Context context(device);
		context.setCurrent();

		polv::HostCoherentMemory d_a(bytes);
		polv::HostCoherentMemory d_b(bytes);
		polv::HostCoherentMemory d_c(bytes);

		float *a = static_cast<float *>(d_a.hostPointer());
		float *b = static_cast<float *>(d_b.hostPointer());
		float *c = static_cast<float *>(d_c.hostPointer());

		for (std::size_t i = 0; i < n; ++i)
		{
			a[i] = static_cast<float>(i);
			b[i] = static_cast<float>(2 * i);
			c[i] = 0.0f;
		}

		polv::SPIRVFileKernel kernel("vecadd.spv", 3);
		polv::Dim3 group{threads, 1, 1};
		polv::Dim3 grid{
			static_cast<std::uint32_t>((n + threads - 1) / threads), 1, 1
		};

		kernel.launch({d_a, d_b, d_c}, grid, group);

		for (std::size_t i = 0; i < n; ++i)
		{
			if (c[i] != a[i] + b[i])
			{
				std::cerr << "Incorrect result at index " << i << '\n';
				return 1;
			}
		}

		std::cout << "OK\n";
		return 0;
	}
	catch (const std::exception &e)
	{
		std::cerr << "Error: " << e.what() << '\n';
		return 1;
	}
}

Compile and run after installing POLV-C++ under /usr/local/polv-cpp and configuring the environment variables above:

g++ -std=c++11 vecadd.cpp -lpolvcpp -o vecadd
./vecadd

Expected output:

OK

For a local installation under ./install/ instead, add the explicit include, library, and runtime paths:

g++ -std=c++11 -I"$(pwd)/install/include" vecadd.cpp \
    -L"$(pwd)/install/lib" \
    -Wl,-rpath,"$(pwd)/install/lib" \
    -lpolvcpp -o vecadd
./vecadd

Run vecadd from the directory containing vecadd.spv.

Requirements

  • Compute execution requires a Vulkan-capable device and working driver.
  • Workgroup dimensions must be within the limits of the selected device.
  • The shader's workgroup dimensions use specialization constant IDs 0, 1, and 2.
  • Precompiled SPIR-V kernels can be loaded without glslangValidator; GLSL loaders require it at runtime.

Caching

POLV Core caches kernels under ~/.cache/polv. Disable caching with:

export POLV_CACHE=0

Current limitations

  • Kernel arguments are buffer-based; arbitrary push constants are not exposed.
  • Additional specialization constants beyond workgroup dimensions are not exposed by the wrapper.
  • The underlying runtime uses global state; concurrent access from multiple host threads is not guaranteed to be thread-safe.
  • Explicit multi-queue scheduling is not supported.

General notes

  • Construct Runtime, Device, and Context before creating memory or kernels.
  • Call Context::setCurrent() before allocating or launching in that context.
  • RAII destroys resources automatically in reverse construction order.
  • Host-coherent allocations expose host pointers; device-local allocations use explicit memory transfers.
  • #include <polv> and #include <polv.hpp> are both supported.
  • POLV Core and POLV-C++ are separate shared libraries with independent versions.

License

MIT. See LICENSE.

About

C++ wrapper for POLV library

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages