#include "gtest/gtest.h" #include "kompute/Kompute.hpp" TEST(TestMultipleAlgoExecutions, SingleSequenceRecord) { kp::Manager mgr; std::shared_ptr tensorA{ new kp::Tensor({ 0, 0, 0 }) }; std::string shader(R"( #version 450 layout (local_size_x = 1) in; layout(set = 0, binding = 0) buffer a { float pa[]; }; void main() { uint index = gl_GlobalInvocationID.x; pa[index] = pa[index] + 1; })"); std::weak_ptr sqWeakPtr = mgr.getOrCreateManagedSequence("newSequence"); if (std::shared_ptr sq = sqWeakPtr.lock()) { sq->begin(); sq->record({ tensorA }); sq->record>( { tensorA }, std::vector(shader.begin(), shader.end())); sq->record>( { tensorA }, std::vector(shader.begin(), shader.end())); sq->record>( { tensorA }, std::vector(shader.begin(), shader.end())); sq->record({ tensorA }); sq->end(); sq->eval(); } sqWeakPtr.reset(); EXPECT_EQ(tensorA->data(), std::vector({ 3, 3, 3 })); } TEST(TestMultipleAlgoExecutions, MultipleCmdBufRecords) { kp::Manager mgr; std::shared_ptr tensorA{ new kp::Tensor({ 0, 0, 0 }) }; std::string shader(R"( #version 450 layout (local_size_x = 1) in; layout(set = 0, binding = 0) buffer a { float pa[]; }; void main() { uint index = gl_GlobalInvocationID.x; pa[index] = pa[index] + 1; })"); std::weak_ptr sqWeakPtr = mgr.getOrCreateManagedSequence("newSequence"); if (std::shared_ptr sq = sqWeakPtr.lock()) { sq->begin(); sq->record({ tensorA }); sq->record>( { tensorA }, std::vector(shader.begin(), shader.end())); sq->end(); sq->eval(); sq->begin(); sq->record>( { tensorA }, std::vector(shader.begin(), shader.end())); sq->end(); sq->eval(); sq->begin(); sq->record>( { tensorA }, std::vector(shader.begin(), shader.end())); sq->end(); sq->eval(); sq->begin(); sq->record({ tensorA }); sq->end(); sq->eval(); } sqWeakPtr.reset(); EXPECT_EQ(tensorA->data(), std::vector({ 3, 3, 3 })); } TEST(TestMultipleAlgoExecutions, MultipleSequences) { kp::Manager mgr; std::shared_ptr tensorA{ new kp::Tensor({ 0, 0, 0 }) }; std::string shader(R"( #version 450 layout (local_size_x = 1) in; layout(set = 0, binding = 0) buffer a { float pa[]; }; void main() { uint index = gl_GlobalInvocationID.x; pa[index] = pa[index] + 1; })"); std::weak_ptr sqWeakPtr = mgr.getOrCreateManagedSequence("newSequence"); if (std::shared_ptr sq = sqWeakPtr.lock()) { sq->begin(); sq->record({ tensorA }); sq->record>( { tensorA }, std::vector(shader.begin(), shader.end())); sq->end(); sq->eval(); } std::weak_ptr sqWeakPtr2 = mgr.getOrCreateManagedSequence("newSequence2"); if (std::shared_ptr sq = sqWeakPtr2.lock()) { sq->begin(); sq->record>( { tensorA }, std::vector(shader.begin(), shader.end())); sq->end(); sq->eval(); } std::weak_ptr sqWeakPtr3 = mgr.getOrCreateManagedSequence("newSequence3"); if (std::shared_ptr sq = sqWeakPtr3.lock()) { sq->begin(); sq->record>( { tensorA }, std::vector(shader.begin(), shader.end())); sq->end(); sq->eval(); } std::weak_ptr sqWeakPtr4 = mgr.getOrCreateManagedSequence("newSequence5"); if (std::shared_ptr sq = sqWeakPtr4.lock()) { sq->begin(); sq->record({ tensorA }); sq->end(); sq->eval(); } EXPECT_EQ(tensorA->data(), std::vector({ 3, 3, 3 })); } TEST(TestMultipleAlgoExecutions, SingleRecordMultipleEval) { kp::Manager mgr; std::shared_ptr tensorA{ new kp::Tensor({ 0, 0, 0 }) }; std::string shader(R"( #version 450 layout (local_size_x = 1) in; layout(set = 0, binding = 0) buffer a { float pa[]; }; void main() { uint index = gl_GlobalInvocationID.x; pa[index] = pa[index] + 1; })"); std::weak_ptr sqWeakPtr = mgr.getOrCreateManagedSequence("newSequence"); if (std::shared_ptr sq = sqWeakPtr.lock()) { sq->begin(); sq->record({ tensorA }); sq->end(); sq->eval(); } std::weak_ptr sqWeakPtr2 = mgr.getOrCreateManagedSequence("newSequence2"); if (std::shared_ptr sq = sqWeakPtr2.lock()) { sq->begin(); sq->record>( { tensorA }, std::vector(shader.begin(), shader.end())); sq->end(); sq->eval(); sq->eval(); sq->eval(); } std::weak_ptr sqWeakPtr3 = mgr.getOrCreateManagedSequence("newSequence3"); if (std::shared_ptr sq = sqWeakPtr2.lock()) { sq->begin(); sq->record({ tensorA }); sq->end(); sq->eval(); sq->eval(); sq->eval(); } EXPECT_EQ(tensorA->data(), std::vector({ 3, 3, 3 })); } TEST(TestMultipleAlgoExecutions, ManagerEvalMultSourceStrOpCreate) { kp::Manager mgr; std::shared_ptr tensorInA{ new kp::Tensor({ 2.0, 4.0, 6.0 }) }; std::shared_ptr tensorInB{ new kp::Tensor({ 0.0, 1.0, 2.0 }) }; std::shared_ptr tensorOut{ new kp::Tensor({ 0.0, 0.0, 0.0 }) }; mgr.evalOpDefault({ tensorInA, tensorInB, tensorOut }); std::string shader(R"( // The version to use #version 450 // The execution structure layout (local_size_x = 1) in; // The buffers are provided via the tensors layout(binding = 0) buffer bufA { float a[]; }; layout(binding = 1) buffer bufB { float b[]; }; layout(binding = 2) buffer bufOut { float o[]; }; void main() { uint index = gl_GlobalInvocationID.x; o[index] = a[index] * b[index]; } )"); mgr.evalOpDefault>( { tensorInA, tensorInB, tensorOut }, std::vector(shader.begin(), shader.end())); mgr.evalOpDefault({ tensorOut }); EXPECT_EQ(tensorOut->data(), std::vector({ 0.0, 4.0, 12.0 })); } TEST(TestMultipleAlgoExecutions, ManagerEvalMultSourceStrMgrCreate) { kp::Manager mgr; auto tensorInA = mgr.buildTensor({ 2.0, 4.0, 6.0 }); auto tensorInB = mgr.buildTensor({ 0.0, 1.0, 2.0 }); auto tensorOut = mgr.buildTensor({ 0.0, 0.0, 0.0 }); std::string shader(R"( // The version to use #version 450 // The execution structure layout (local_size_x = 1) in; // The buffers are provided via the tensors layout(binding = 0) buffer bufA { float a[]; }; layout(binding = 1) buffer bufB { float b[]; }; layout(binding = 2) buffer bufOut { float o[]; }; void main() { uint index = gl_GlobalInvocationID.x; o[index] = a[index] * b[index]; } )"); mgr.evalOpDefault>( { tensorInA, tensorInB, tensorOut }, std::vector(shader.begin(), shader.end())); mgr.evalOpDefault({ tensorOut }); EXPECT_EQ(tensorOut->data(), std::vector({ 0.0, 4.0, 12.0 })); }