Skip to content

Instantly share code, notes, and snippets.

@Overv
Last active May 30, 2020 09:08
Show Gist options
  • Save Overv/95b1f28cc0d3436352e0 to your computer and use it in GitHub Desktop.
Save Overv/95b1f28cc0d3436352e0 to your computer and use it in GitHub Desktop.
GPU backed BUSE block device
#include "buse.h"
#include <CL/cl.hpp>
#include <stdint.h>
#include <vector>
cl::Context context;
cl::Device device;
cl::CommandQueue queue;
cl::Buffer data;
static int vram_read(void* buf, uint32_t len, uint64_t off, void* ud) {
queue.enqueueReadBuffer(data, true, off, len, buf, nullptr, nullptr);
return 0;
}
static int vram_write(const void* buf, uint32_t len, uint64_t off, void* ud) {
queue.enqueueWriteBuffer(data, true, off, len, buf, nullptr, nullptr);
return 0;
}
static void vram_disc(void* ud) {
return;
}
static int vram_flush(void* ud) {
return 0;
}
static int vram_trim(uint64_t off, uint32_t len, void* ud) {
return 0;
}
// BUSE initialisation
static struct buse_operations operations = {
.read = vram_read,
.write = vram_write,
.disc = vram_disc,
.flush = vram_flush,
.trim = vram_trim,
.size = 1024 * 1024 * 1024
};
int main(int argc, const char* argv[]) {
// Find OpenCL capable GPU
std::vector<cl::Platform> platforms;
cl::Platform::get(&platforms);
std::vector<cl::Device> devices;
platforms[0].getDevices(CL_DEVICE_TYPE_GPU, &devices);
device = devices[0];
context = cl::Context(device);
queue = cl::CommandQueue(context, device);
// Create buffer for data
data = cl::Buffer(context, CL_MEM_READ_WRITE, operations.size, nullptr, nullptr);
return buse_main(argv[1], &operations, nullptr);
}
gpu: gpu.cpp
g++ -std=c++11 -O2 -flto -march=native -o gpu gpu.cpp -L. -lbuse -lOpenCL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment