Lightweight OpenCL-Wrapper to greatly simplify OpenCL software development with C++ while keeping functionality and performance

Overview

OpenCL-Wrapper

OpenCL is the most powerful programming language ever created. Yet the OpenCL C++ bindings are very cumbersome and the code overhead prevents many people from getting started. I created this lightweight OpenCL-Wrapper to greatly simplify OpenCL software development with C++ while keeping functionality and performance.

Works in both Windows and Linux with C++17.

Key simplifications:

  1. select a Device with 1 line
    • automatically select fastest device / device with most memory / device with specified ID from a list of all devices
    • easily get device information (performance in TFLOPs/s, amount of memory and cache, FP64/FP16 capabilities, etc.)
    • automatic OpenCL C code compilation when creating the Device object
      • automatically enable FP64/FP16 capabilities in OpenCL C code
      • automatically print log to console if there are compile errors
      • easy option to generate PTX assembly and save that in a .ptx file
  2. create a Memory object with 1 line
    • one object for both host and device memory
    • easy host <-> device memory transfer
    • easy handling of multi-dimensional vectors
    • can also be used to only allocate memory on host or only allocate memory on device
  3. create a Kernel with 1 line
    • memory objects are linked to OpenCL C kernel parameters during kernel creation
    • easy kernel execution
  4. OpenCL C code is embedded into C++
    • syntax highlighting in the code editor is retained

No need to:

  • have code overhead for selecting a platform/device, passing the OpenCL C code, etc.
  • keep track of global/local ranges for buffers and kernels
  • have duplicate code for host and device buffers
  • bother with Queue, Context, Source, Program
  • load a .cl file at runtime

Example (OpenCL vector addition)

main.cpp

A(device, N); // allocate memory on both host and device Memory B(device, N); Memory C(device, N); for(uint n=0u; n
#include "opencl.hpp"

int main() {
	const Device device(select_device_with_most_flops()); // compile OpenCL C code for the fastest available device

	const uint N = 1024u; // size of vectors

	Memory<float> A(device, N); // allocate memory on both host and device
	Memory<float> B(device, N);
	Memory<float> C(device, N);

	for(uint n=0u; n
      3.
      0f; 
      // initialize memory
		B[n] = 
      2.
      0f;
		C[n] = 
      1.
      0f;
	}

	
      const Kernel 
      add_kernel(device, N, 
      "add_kernel", A, B, C); 
      // kernel that runs on the device

	
      print_info(
      "Value before kernel execution: C[0] = "+
      to_string(C[
      0]));

	A.
      write_to_device(); 
      // copy data from host memory to device memory
	B.
      write_to_device(); 
      // copy data from host memory to device memory
	add_kernel.
      run(); 
      // run add_kernel on the device
	C.
      read_from_device(); 
      // copy data from device memory to host memory

	
      print_info(
      "Value after kernel execution: C[0] = "+
      to_string(C[
      0]));

	
      wait();
	
      return 
      0;
}
     

kernel.cpp

#include "kernel.hpp" // note: string literals can't be arbitrarily long, so periodically interrupt them with )+R( before every #ifdef, #else, #endif, #if, #elif
string opencl_c_container() { return R( // ########################## begin of OpenCL C code ####################################################################



kernel void add_kernel(global float* A, global float* B, global float* C) { // equivalent to "for(int n=0; n
   
	
    const 
    uint n = 
    get_global_id(
    0);
	C[n] = A[n]+B[n];
}



);} 
    // ############################################################### end of OpenCL C code #####################################################################
   
You might also like...
GAMEDEV - Lightweight crash reporter for Unreal Engine

Mayday Mayday is a lightweight crash reporter for Unreal Engine games that uploads the crash context and minidump to an HTTP(S) server of your choosin

Graphlite is a lightweight C++ generic graph library

Introduction Graphlite is a lightweight generic graph library that supports node properties edge properties directed/undirected graphs multi-edges & s

An immediate-mode, renderer agnostic, lightweight debug drawing API for C++
An immediate-mode, renderer agnostic, lightweight debug drawing API for C++

Debug Draw An immediate-mode, renderer agnostic, lightweight debug drawing API for C++. License This software is in the public domain. Where that dedi

SPIRV-Reflect is a lightweight library that provides a C/C++ reflection API for SPIR-V shader bytecode in Vulkan applications.

SPIRV-Reflect SPIRV-Reflect is a lightweight library that provides a C/C++ reflection API for SPIR-V shader bytecode in Vulkan applications. SPIRV-Ref

A C++ commandline for use in servers and chat software. Provides very simple asynchronous input/output.
A C++ commandline for use in servers and chat software. Provides very simple asynchronous input/output.

commandline A C++ commandline for use in servers and terminal chat software. Provides very simple asynchronous input/output. Supports reading and writ

Pencil2D is an animation/drawing software for Windows, macOS, Linux, and FreeBSD.

Pencil2D is an animation/drawing software for Windows, macOS, Linux, and FreeBSD. It lets you create traditional hand-drawn animation (cartoon) using both bitmap and vector graphics. Pencil2D is free and open source.

Software ray tracer written from scratch in C that can run on CPU or GPU with emphasis on ease of use and trivial setup
Software ray tracer written from scratch in C that can run on CPU or GPU with emphasis on ease of use and trivial setup

A minimalist and platform-agnostic interactive/real-time raytracer. Strong emphasis on simplicity, ease of use and almost no setup to get started with

A library for high-performance, modern 2D graphics with SDL written in C.

SDL_gpu, a library for making hardware-accelerated 2D graphics easy. by Jonathan Dearborn SDL_gpu is licensed under the terms of the MIT License. See

Utility on top of the Flutter Driver API that facilitates measuring the performance of your app in an automated way created by Very Good Ventures 🦄
Utility on top of the Flutter Driver API that facilitates measuring the performance of your app in an automated way created by Very Good Ventures 🦄

Very Good Performance Developed with 💙 by Very Good Ventures 🦄 Utility on top of the Flutter Driver API that facilitates measuring the performance o

Owner
Moritz Lehmann
M.Sc. / physics PhD candidate at SFB1357 microplastics / ENB elite study program biological physics / FluidX3D OpenCL GPU developer / DLR_Graduate_Program
Moritz Lehmann
A heterogeneous OpenCL implementation of AutoDock Vina

Vina-GPU A heterogeneous OpenCL implementation of AutoDock Vina Compiling and Running Note: at least one GPU card is required and make sure the versio

Nanjing University of Posts and Telecommunications 39 Dec 12, 2022
Metal-cpp is a low-overhead C++ interface for Metal that helps developers add Metal functionality to graphics apps, games, and game engines that are written in C++.

About metal-cpp is a low overhead and header only C++ interface for Metal that helps developers add Metal functionality to graphics applications that

Бранимир Караџић 164 Dec 31, 2022
vplot is a wrapper for GNU Plot (gnuplot_i)

vplot vplot is a wrapper for GNU Plot (gnuplot_i). The source of gnuplot_i I have downloaded from this link. Files listed on gnuplot_i are taken from

Erdet Nasufi 13 Aug 15, 2022
Dear PyGui 3D Engine (early development) and Graphics API demos.

Marvel This repo is the working location of the eventual Dear PyGui 3D Engine. It also contains several single file examples of creating a triangle wi

Jonathan Hoffstadt 85 Jan 5, 2023
OpenCorr is an open source C++ library for development of 2D, 3D/stereo, and volumetric digital image correlation

OpenCorr OpenCorr is an open source C++ library for development of 2D, 3D/stereo, and volumetric digital image correlation. It aims to provide a devel

Zhenyu Jiang 66 Jan 6, 2023
Modern C++14 library for the development of real-time graphical applications

CI Community Support bs::framework is a C++ library that aims to provide a unified foundation for the development of real-time graphical applications,

null 1.7k Jan 2, 2023
Cocos2d-x is a suite of open-source, cross-platform, game-development tools used by millions of developers all over the world.

Cocos2d-x is a suite of open-source, cross-platform, game-development tools used by millions of developers all over the world.

cocos2d 16.7k Dec 26, 2022
Horde3D is a small 3D rendering and animation engine. It is written in an effort to create an engine being as lightweight and conceptually clean as possible.

Horde3D Horde3D is a 3D rendering engine written in C++ with an effort being as lightweight and conceptually clean as possible. Horde3D requires a ful

Volker Vogelhuber 1.3k Dec 31, 2022
Lightweight and modular C++11 graphics middleware for games and data visualization

Magnum — Lightweight and modular C++11/C++14 graphics middleware for games and data visualization Looking for an open-source library that gives you gr

Vladimír Vondruš 4.3k Dec 30, 2022
This module is a simple, lightweight and flexible way to generate QR codes in Godot

QRCodeTexture Godot Module Summary This module is a simple, lightweight and flexible way to generate QR codes in Godot. It provides a new type of text

Ben Armstrong 18 Oct 20, 2022