main.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #include "kompute/Core.hpp"
  2. #include <string>
  3. #include <vector>
  4. #include <memory>
  5. #include <iostream>
  6. #include <kompute/Kompute.hpp>
  7. #include <spdlog/spdlog.h>
  8. static std::vector<uint32_t> compileSource(const std::string& source) {
  9. std::ofstream file_out("tmp_kp_shader.comp");
  10. file_out << source;
  11. file_out.close();
  12. system(std::string("glslangValidator -V tmp_kp_shader.comp -o tmp_kp_shader.comp.spv").c_str());
  13. std::ifstream fileStream("tmp_kp_shader.comp.spv", std::ios::binary);
  14. std::vector<char> buffer;
  15. buffer.insert(buffer.begin(), std::istreambuf_iterator<char>(fileStream), {});
  16. return {(uint32_t*)buffer.data(), (uint32_t*)(buffer.data() + buffer.size())};
  17. }
  18. void kompute(const std::string &shader) {
  19. kp::Manager mgr;
  20. auto tensor_in_a = mgr.tensor({2., 2., 2.});
  21. auto tensor_in_b = mgr.tensor({1., 2., 3.});
  22. auto tensor_out_a = mgr.tensorT<uint32_t>({0, 0, 0});
  23. auto tensor_out_b = mgr.tensorT<uint32_t>({0, 0, 0});
  24. std::vector<std::shared_ptr<kp::Tensor>> params = {
  25. tensor_in_a, tensor_in_b, tensor_out_a, tensor_out_b };
  26. kp::Workgroup workgroup({3, 1, 1});
  27. std::vector<float> specConsts ({ 2 });
  28. std::vector<float> pushConstsA ({ 2.0 });
  29. std::vector<float> pushConstsB ({ 2.0 });
  30. auto algorith = mgr.algorithm(params,
  31. compileSource(shader),
  32. workgroup,
  33. specConsts,
  34. pushConstsA);
  35. mgr.sequence()
  36. ->record<kp::OpTensorSyncDevice>(params)
  37. ->record<kp::OpAlgoDispatch>(algorith)
  38. ->eval()
  39. ->record<kp::OpAlgoDispatch>(algorith, pushConstsB)
  40. ->eval();
  41. auto sq = mgr.sequence();
  42. sq->evalAsync<kp::OpTensorSyncLocal>(params);
  43. sq->evalAwait();
  44. fmt::print("Result A:\n");
  45. for (const float& elem: tensor_out_a->vector()) {
  46. //fmt::print("{} ", elem);
  47. std::cout << elem << " ";
  48. }
  49. fmt::print("\nResult B:\n");
  50. for (const float& elem: tensor_out_b->vector()) {
  51. //fmt::print("{} ", elem);
  52. std::cout << elem << " ";
  53. }
  54. }
  55. int main(int argc, char **argv) {
  56. std::string shader = (R"(
  57. #version 450
  58. layout (local_size_x = 1) in;
  59. layout(set = 0, binding = 0) buffer buf_in_a { float in_a[]; };
  60. layout(set = 0, binding = 1) buffer buf_in_b { float in_b[]; };
  61. layout(set = 0, binding = 2) buffer buf_out_a { float out_a[]; };
  62. layout(set = 0, binding = 3) buffer buf_out_b { float out_b[]; };
  63. layout(push_constant) uniform PushConstants {
  64. float val;
  65. }push_const;
  66. layout (constant_id = 0) const float const_one = 0;
  67. void main() {
  68. uint index = gl_GlobalInvocationID.x;
  69. out_a[index] += uint( in_a[index] * in_b[index] );
  70. out_b[index] += uint( const_one * push_const.val );
  71. }
  72. )");
  73. kompute(shader);
  74. }