Added array example
This commit is contained in:
parent
f03a500625
commit
4e3802cb9d
3 changed files with 167 additions and 0 deletions
53
examples/array_multiplication/src/Main.cpp
Executable file
53
examples/array_multiplication/src/Main.cpp
Executable file
|
|
@ -0,0 +1,53 @@
|
|||
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#include "kompute/Kompute.hpp"
|
||||
|
||||
int main()
|
||||
{
|
||||
#if KOMPUTE_ENABLE_SPDLOG
|
||||
spdlog::set_level(
|
||||
static_cast<spdlog::level::level_enum>(SPDLOG_ACTIVE_LEVEL));
|
||||
#endif
|
||||
|
||||
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<kp::OpAlgoBase<>>(
|
||||
{ tensorInA, tensorInB, tensorOut },
|
||||
std::vector<char>(shader.begin(), shader.end()));
|
||||
|
||||
mgr.evalOpDefault<kp::OpTensorSyncLocal>({tensorOut});
|
||||
|
||||
// prints "Output { 0 4 12 }"
|
||||
std::cout<< "Output: { ";
|
||||
for (const float& elem : tensorOut->data()) {
|
||||
std::cout << elem << " ";
|
||||
}
|
||||
std::cout << "}" << std::endl;
|
||||
}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue