test using the kompute library and possibly vulkan
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
kompute_tests/test2/main.cpp

177 lines
5.2 KiB

#include <chrono>
12 months ago
#include <fstream>
#include <iostream>
#include <iterator>
#include <memory>
#include <numeric>
12 months ago
#include <regex>
#include <sstream>
#include <vector>
#include <kompute/Kompute.hpp>
#include <vulkan/vulkan_handles.hpp>
12 months ago
#include <unistd.h>
12 months ago
#define MSIZE 64
12 months ago
static std::vector<uint32_t> compile_shader(const std::string &source)
12 months ago
{
std::ofstream fileOut("tmp_kp_shader.comp");
fileOut << source;
fileOut.close();
12 months ago
if (system(std::string("glslangValidator -V tmp_kp_shader.comp -o "
"tmp_kp_shader.comp.spv")
.c_str())) {
12 months ago
throw std::runtime_error("Error running glslangValidator command");
12 months ago
}
std::ifstream fileStream("tmp_kp_shader.comp.spv", std::ios::binary);
12 months ago
std::vector<char> buffer;
12 months ago
buffer.insert(
buffer.begin(), std::istreambuf_iterator<char>(fileStream), {}
);
return {
(uint32_t *)buffer.data(), (uint32_t *)(buffer.data() + buffer.size())};
12 months ago
}
static std::string shader_to_string(const char *path)
{
std::ifstream comp_file;
comp_file.open(path);
if (comp_file.is_open() == false) {
return std::string("// bad code");
}
std::ostringstream outstr;
outstr << comp_file.rdbuf();
return outstr.str();
}
12 months ago
template <typename T>
std::string replacewith(const char *needle, T val, std::string str)
12 months ago
{
12 months ago
std::string replace = std::to_string(val);
size_t len = strlen(needle);
12 months ago
12 months ago
for (size_t pos = 0; (pos = str.find(needle)) != std::string::npos;) {
str.replace(pos, len, replace);
}
return str;
12 months ago
}
12 months ago
// compute C = A*B on the GPU
12 months ago
int main()
{
// create the kompute manager
kp::Manager mgr;
// timestampPeriod is the number of nanoseconds required for a timestamp
// query to be incremented by 1.
auto device_proprieties = mgr.getDeviceProperties();
float device_timescale = device_proprieties.limits.timestampPeriod;
12 months ago
// matrices are on the stack, this breaks for large MSIZE (1024)
float matrixA[MSIZE][MSIZE] = {0};
float matrixB[MSIZE][MSIZE] = {0};
float matrixC[MSIZE][MSIZE] = {0};
12 months ago
// fill an identity matrix
12 months ago
for (int y = 0; y < MSIZE; y++) {
12 months ago
matrixA[y][y] = 1.0;
matrixB[y][y] = 2.0;
12 months ago
}
// fill a matrix with data
/*
12 months ago
for (int y = 0; y < MSIZE; y++) {
for (int x = 0; x < MSIZE; x++) {
matrixB[y][x] = x * 0.74 - y * 0.22;
12 months ago
}
}
*/
12 months ago
// create the tensors, tensors are just arrays, in the shader we will have
// to describe how it translates to matrices
kp::Tensor::TensorDataTypes dtype = kp::Tensor::TensorDataTypes::eFloat;
// auto because fuck C++
12 months ago
auto tensorA = mgr.tensor(matrixA, MSIZE * MSIZE, sizeof(float), dtype);
auto tensorB = mgr.tensor(matrixB, MSIZE * MSIZE, sizeof(float), dtype);
auto tensorC = mgr.tensor(matrixC, MSIZE * MSIZE, sizeof(float), dtype);
12 months ago
const std::vector<std::shared_ptr<kp::Tensor>> params = {
12 months ago
tensorA, tensorB, tensorC};
12 months ago
// workgroup, dispatch a 2D array of workgroups (2D matrices)
// TODO: determine the size of the workgroups by doing some calls to vk
const int lcsize_x = 32;
const int lcsize_y = 32;
const int lcsize_z = 1;
const int wgrp_x = std::max(MSIZE / lcsize_x, 1);
const int wgrp_y = std::max(MSIZE / lcsize_y, 1);
12 months ago
// this should call vkCmdDispatch(x, y, z)
kp::Workgroup workgroup({wgrp_x, wgrp_y, 1});
12 months ago
// get the shader code into a string
12 months ago
const char *shader_path = "shader.comp";
12 months ago
std::string shader_str = shader_to_string(shader_path);
12 months ago
12 months ago
// substitute the value for the number of threads (xyz) per workgroup since
// it has to be a compile-time constant
shader_str = replacewith<int>("__lcsize_x__", lcsize_x, shader_str);
shader_str = replacewith<int>("__lcsize_y__", lcsize_y, shader_str);
shader_str = replacewith<int>("__lcsize_z__", lcsize_z, shader_str);
12 months ago
// compile the shader
const std::vector<uint32_t> shader = compile_shader(shader_str);
12 months ago
// prepare the algorithm with shader, parameters, workgroups to dispatch and
// a specialization constant constant to specify the size of each tensor
12 months ago
std::shared_ptr<kp::Algorithm> algo =
mgr.algorithm(params, shader, workgroup, {MSIZE});
// start a timer to measure CPU (host) time
auto start = std::chrono::high_resolution_clock::now();
// evaluate the sequence of events synchronously on queue index 0 and
// attaching a maximum of 10 timestamp
std::shared_ptr<kp::Sequence> sq;
sq = mgr.sequence(0, 10);
sq->rerecord();
sq->record<kp::OpTensorSyncDevice>(params)
12 months ago
->record<kp::OpAlgoDispatch>(algo)
->record<kp::OpTensorSyncLocal>(params)
->eval();
12 months ago
// stop all the timers and get the device (GPU) timestamps
auto end = std::chrono::high_resolution_clock::now();
auto total_time =
std::chrono::duration_cast<std::chrono::microseconds>(end - start)
.count();
std::vector<std::uint64_t> timestamps = sq->getTimestamps();
std::adjacent_difference(
timestamps.begin(), timestamps.end(), timestamps.begin()
);
// print all the timing information
printf("device timescale: %f\n", device_timescale);
printf("cpu time: %ldus\ndevice times: ", total_time);
for (auto i = std::next(timestamps.begin()); i < timestamps.end(); i++) {
float op_us = (float)(*i * device_timescale) / 1000;
printf("%.2fus ", op_us);
}
printf("\n");
12 months ago
// print the resulting matrix
for (int y = 0; y < MSIZE; y++) {
for (int x = 0; x < MSIZE; x++) {
float elem = tensorC->vector<float>().at(y * MSIZE + x);
printf("%.1f ", elem);
}
printf("\n");
12 months ago
}
return 0;
}