exMatrix.cpp

Add two device matrices. Create two GPU-resident matrices, add them, and verify the result after the language-appropriate completion boundary.

ExSDK source-ownedC++Core

Source

Example

1#include <oa/oa.h>
2
3OA_MAIN("ExampleCoreMatrix") {
4 auto one = oa::FnMatrix::ones({2, 3});
5
6 auto two = oa::FnMatrix::full({2, 3}, 2.0F);
7
8 auto sum = oa::FnMatrix::add(one, two);
9
10 oa::Array<oa::F32, 6> values{};
11 if (not oa::FnMatrix::copyToHost(sum, values.data(), sizeof(values)).isOk()) {
12 return 1;
13 }
14 for (const oa::F32 value : values) {
15 if (oa::abs(value - 3.0F) > 1e-06F) {
16 return 1;
17 }
18 }
19
20 if (not oa::print("Matrix addition verified: every value is 3").isOk()) {
21 return 1;
22 }
23 return 0;
24}
25

Output

Verified Python run. Runtime evidence from the source-owned Python example on an Intel Iris Xe Vulkan device. The batch loader creates only the two shader pipelines required by this graph.

Exmatrix.py · output

$ .venv/bin/python sdk/py/examples/core/matrix.py
10:47:17.554 [INFO ] [RT ] ComputeDevice (0): Intel(R) Iris(R) Xe Graphics (TGL GT2), vulkan 1.4.354, 11 GB
10:47:17.563 [INFO ] [RT ] bindless heap: buffers=262143 imageSlots=16383 samplerSlots=2047, UPDATE_AFTER_BIND
10:47:17.577 [INFO ] [COMP] pipeline cache: loaded 10860399 bytes from $OA_ROOT/var/vk/pipeline.vcache
10:47:17.589 [INFO ] [COMP] Loading 2 shader pipelines on demand (1 thread, warm cache)
10:47:17.590 [INFO ] [COMP] Loaded 2/2 shader pipelines on demand (failed=0, threads=1, 0.96 ms)
Matrix addition verified: every value is 3