nk
1.0.0
C++ Numerical Computing Runtime
Toggle main menu visibility
Loading...
Searching...
No Matches
value_factory.hpp
1
// ops/include/nk/ops/value_factory.hpp
2
//
3
// Value-shape factories shared by the op kernels and the toolboxes: allocate a
4
// Value of a given shape/type. Built only on the Value substrate (value.hpp) —
5
// no engine, no toolbox — so they live at the L0.5 ops layer and every layer
6
// above can use them.
7
//
8
// (Historically in toolboxes/builtin/src/helpers.hpp under namespace nk;
9
// helpers.hpp now re-exports these into nk:: so existing unqualified
10
// callers are unaffected.)
11
12
#pragma once
13
14
#include <nk/value/value.hpp>
15
16
#include <cstddef>
17
#include <memory_resource>
18
19
namespace
nk::ops {
20
21
// 2D/3D shape descriptor for createMatrix (pages == 0 → 2D).
22
struct
DimsArg
23
{
24
std::size_t rows = 1, cols = 1, pages = 0;
25
};
26
27
// Create a zero matrix / 3D array with the given dimensions + element type.
28
inline
Value
createMatrix(
DimsArg
d, ValueType type, std::pmr::memory_resource *mr)
29
{
30
if
(d.pages > 0)
31
return
Value::matrix3d(d.rows, d.cols, d.pages, type, mr);
32
return
Value::matrix(d.rows, d.cols, type, mr);
33
}
34
35
// Allocate a Value with the given Dims, picking the rank-appropriate ctor
36
// (matrix / matrix3d / matrixND).
37
inline
Value createForDims(
const
Dims &d, ValueType type, std::pmr::memory_resource *mr)
38
{
39
const
int
nd = d.ndim();
40
if
(nd >= 4) {
41
std::size_t dims[Dims::kMaxRank];
42
for
(
int
i = 0; i < nd; ++i)
43
dims[i] = d.dim(i);
44
return
Value::matrixND(dims, nd, type, mr);
45
}
46
return
createMatrix({d.rows(), d.cols(), d.is3D() ? d.pages() : 0}, type, mr);
47
}
48
49
// Allocate a Value with the same shape as `src`, optionally of a different
50
// type. Required for any elementwise output (a numel()-into-2D allocation
51
// corrupts the heap when src is 3D+).
52
inline
Value createLike(
const
Value &src, ValueType type, std::pmr::memory_resource *mr)
53
{
54
return
createForDims(src.dims(), type, mr);
55
}
56
57
}
// namespace nk::ops
nk::Value
Core MATLAB-compatible multidimensional array and scalar value type.
Definition
value.hpp:61
nk::ops::DimsArg
Definition
value_factory.hpp:23
src
ops
include
nk
ops
value_factory.hpp
Generated by
1.17.0