Skip to content

Commit 83bf703

Browse files
committed
Include more documentation
1 parent f2a34ee commit 83bf703

File tree

11 files changed

+464
-10
lines changed

11 files changed

+464
-10
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ build/
99
docs/_doxygen
1010
cmake-build-debug/
1111
docs/_build
12+
docs/api/*.rst
1213
*.swp

docs/Doxyfile.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2254,7 +2254,7 @@ INCLUDE_FILE_PATTERNS =
22542254
# recursively expanded use the := operator instead of the = operator.
22552255
# This tag requires that the tag ENABLE_PREPROCESSING is set to YES.
22562256

2257-
PREDEFINED = "KERNEL_FLOAT_FP16_AVAILABLE=1,KERNEL_FLOAT_BF16_AVAILABLE=1,KERNEL_FLOAT_FP8_AVAILABLE=1,__CUDA_ARCH__=800"
2257+
PREDEFINED = "KERNEL_FLOAT_FP16_AVAILABLE=1,KERNEL_FLOAT_BF16_AVAILABLE=1,KERNEL_FLOAT_FP8_AVAILABLE=1,__CUDA_ARCH__=800,DOXYGEN_SHOULD_SKIP_THIS=1"
22582258

22592259
# If the MACRO_EXPANSION and EXPAND_ONLY_PREDEF tags are set to YES then this
22602260
# tag can be used to specify a list of macro names that should be expanded. The

docs/api.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ API Reference
33
.. toctree::
44
api/types.rst
55
api/primitives.rst
6-
api/iteration.rst
76
api/unary_operators.rst
87
api/binary_operators.rst
98
api/reductions.rst

docs/build_api.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,19 @@ def build_index_page(groups):
8888
"map",
8989
"reduce",
9090
"zip",
91+
"zip_common",
9192
"cast",
9293
"broadcast",
94+
"resize",
9395
"for_each",
9496
],
9597
"Unary Operators": [
9698
"fill",
99+
"fill_like",
97100
"zeros",
101+
"zeros_like",
98102
"ones",
103+
"ones_like",
99104
"negate",
100105
"bit_not",
101106
"logical_not",

include/kernel_float/binops.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,16 @@ using zip_type = vector_storage<
5252
result_t<F, vector_value_type<L>, vector_value_type<R>>,
5353
common_vector_size<L, R>>;
5454

55+
/**
56+
* Applies ``fun`` to each pair of two elements from ``left`` and ``right`` and returns a new
57+
* vector with the results.
58+
*
59+
* If ``left`` and ``right`` are not the same size, they will first be broadcast into a
60+
* common size using ``resize``.
61+
*
62+
* Note that this function does **not** cast the input vectors to a common element type. See
63+
* ``zip_common`` for that functionality.
64+
*/
5565
template<typename F, typename Left, typename Right, typename Output = zip_type<F, Left, Right>>
5666
KERNEL_FLOAT_INLINE Output zip(F fun, Left&& left, Right&& right) {
5767
static constexpr size_t N = vector_size<Output>;
@@ -66,6 +76,24 @@ using zip_common_type = vector_storage<
6676
result_t<F, common_vector_value_type<L, R>, common_vector_value_type<L, R>>,
6777
common_vector_size<L, R>>;
6878

79+
/**
80+
* Applies ``fun`` to each pair of two elements from ``left`` and ``right`` and returns a new
81+
* vector with the results.
82+
*
83+
* If ``left`` and ``right`` are not the same size, they will first be broadcast into a
84+
* common size using ``resize``.
85+
*
86+
* If ``left`` and ``right`` are not of the same type, they will first be case into a common
87+
* data type. For example, zipping ``float`` and ``double`` first cast vectors to ``double``.
88+
*
89+
* Example
90+
* =======
91+
* ```
92+
* vec<int, 5> x = {1, 2, 3, 4};
93+
* vec<long, 1> = {8};
94+
* vec<long, 5> = zip_common([](auto a, auto b){ return a + b; }, x, y); // [9, 10, 11, 12]
95+
* ```
96+
*/
6997
template<
7098
typename F,
7199
typename Left,

include/kernel_float/interface.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ struct vector: public Storage {
9898
}
9999

100100
/**
101-
* Returns a reference to the ``index``-th item.
101+
* Returns the ``index``-th item.
102102
*/
103103
template<typename I>
104104
KERNEL_FLOAT_INLINE value_type operator[](I index) const noexcept {

include/kernel_float/iterate.h

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ struct range_helper<F, V, index_sequence<Is...>> {
2121
/**
2222
* Generate vector of length ``N`` by applying the given function ``fun`` to
2323
* each index ``0...N-1``.
24+
*
25+
* Example
26+
* =======
27+
* ```
28+
* // returns [0, 2, 4]
29+
* vector<float, 3> vec = range<3>([](auto i) { return float(i * 2); });
30+
* ```
2431
*/
2532
template<size_t N, typename F, typename T = result_t<F, size_t>>
2633
KERNEL_FLOAT_INLINE vector_storage<T, N> range(F fun) {
@@ -29,28 +36,57 @@ KERNEL_FLOAT_INLINE vector_storage<T, N> range(F fun) {
2936

3037
/**
3138
* Generate vector consisting of the numbers ``0...N-1`` of type ``T``.
39+
*
40+
* Example
41+
* =======
42+
* ```
43+
* // Returns [0, 1, 2]
44+
* vector<float, 3> vec = range<float, 3>();
45+
* ```
3246
*/
3347
template<typename T, size_t N>
3448
KERNEL_FLOAT_INLINE vector_storage<T, N> range() {
3549
return range(ops::cast<size_t, T> {});
3650
}
3751

52+
/**
53+
* Generate vector having same size and type as ``V``, but filled with the numbers ``0..N-1``.
54+
*/
55+
template<typename V>
56+
KERNEL_FLOAT_INLINE into_vector_type<V> range_like(V&& vector) {
57+
return range<vector_value_type<T>, vector_size<V>>();
58+
}
59+
3860
/**
3961
* Generate vector of `N` elements of type `T`
4062
*
63+
* Example
64+
* =======
4165
* ```
42-
* vector<float, 3> = fill(1.0);
66+
* // Returns [1.0, 1.0, 1.0]
67+
* vector<float, 3> = fill(1.0f);
4368
* ```
4469
*/
4570
template<size_t N = 1, typename T>
4671
KERNEL_FLOAT_INLINE vector_storage<T, N> fill(T value) {
4772
return {value};
4873
}
4974

75+
/**
76+
* Generate vector having same size and type as ``V``, but filled with the given ``value``.
77+
*/
78+
template<typename V, typename T = vector_value_type<V>>
79+
KERNEL_FLOAT_INLINE into_vector_type<V> fill_like(V&& vector, T value) {
80+
return {value};
81+
}
82+
5083
/**
5184
* Generate vector of ``N`` zeros of type ``T``
5285
*
86+
* Example
87+
* =======
5388
* ```
89+
* // Returns [0.0, 0.0, 0.0]
5490
* vector<float, 3> = zeros();
5591
* ```
5692
*/
@@ -59,10 +95,22 @@ KERNEL_FLOAT_INLINE vector_storage<T, N> zeros() {
5995
return fill<N, T>(T(0));
6096
}
6197

98+
/**
99+
* Generate vector having same size and type as ``V``, but filled with zeros.
100+
*
101+
*/
102+
template<typename V>
103+
KERNEL_FLOAT_INLINE into_vector_type<V> zeros_like(V&& vector) {
104+
return zeros<vector_size<V>, vector_value_type<V>>();
105+
}
106+
62107
/**
63108
* Generate vector of ``N`` ones of type ``T``
64109
*
110+
* Example
111+
* =======
65112
* ```
113+
* // Returns [1.0, 1.0, 1.0]
66114
* vector<float, 3> = ones();
67115
* ```
68116
*/
@@ -71,6 +119,15 @@ KERNEL_FLOAT_INLINE vector_storage<T, N> ones() {
71119
return fill<N, T>(T(1));
72120
}
73121

122+
/**
123+
* Generate vector having same size and type as ``V``, but filled with ones.
124+
*
125+
*/
126+
template<typename V>
127+
KERNEL_FLOAT_INLINE into_vector_type<V> ones_like(V&& vector) {
128+
return ones<vector_size<V>, vector_value_type<V>>();
129+
}
130+
74131
namespace detail {
75132
template<typename F, typename V, typename Indices = make_index_sequence<vector_size<V>>>
76133
struct iterate_helper;
@@ -93,6 +150,14 @@ struct iterate_helper<F, V, index_sequence<I, Rest...>> {
93150

94151
/**
95152
* Apply the function ``fun`` for each element from ``input``.
153+
*
154+
* Example
155+
* =======
156+
* ```
157+
* for_each(range<3>(), [&](auto i) {
158+
* printf("element: %d\n", i);
159+
* });
160+
* ```
96161
*/
97162
template<typename V, typename F>
98163
KERNEL_FLOAT_INLINE void for_each(V&& input, F fun) {

include/kernel_float/reduce.h

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,43 +48,116 @@ struct reduce_helper<F, vector_compound<T, N>> {
4848
};
4949
} // namespace detail
5050

51+
/**
52+
* Reduce the elements of the given vector ``input`` into a single value using
53+
* the function ``fun``. This function should be a binary function that takes
54+
* two elements and returns one element. The order in which the elements
55+
* are reduced is not specified and depends on the reduction function and
56+
* the vector type.
57+
*
58+
* Example
59+
* =======
60+
* ```
61+
* vec<int, 3> x = {5, 2, 1};
62+
* int y = reduce(x, [](int a, int b) { return a + b; }); // returns 8
63+
* ```
64+
*/
5165
template<typename F, typename V>
5266
KERNEL_FLOAT_INLINE vector_value_type<V> reduce(F fun, V&& input) {
5367
return detail::reduce_helper<F, into_vector_type<V>>::call(
5468
fun,
5569
into_vector(std::forward<V>(input)));
5670
}
5771

72+
/**
73+
* Find the minimum element in the given vector ``input``.
74+
*
75+
* Example
76+
* =======
77+
* ```
78+
* vec<int, 3> x = {5, 0, 2, 1, 0};
79+
* int y = sum(x); // Returns 8
80+
* ```
81+
*/
5882
template<typename V, typename T = vector_value_type<V>>
5983
KERNEL_FLOAT_INLINE T min(V&& input) {
6084
return reduce(ops::min<T> {}, std::forward<V>(input));
6185
}
6286

87+
/**
88+
* Find the maximum element in the given vector ``input``.
89+
*
90+
* Example
91+
* =======
92+
* ```
93+
* vec<int, 3> x = {5, 0, 2, 1, 0};
94+
* int y = sum(x); // Returns 8
95+
* ```
96+
*/
6397
template<typename V, typename T = vector_value_type<V>>
6498
KERNEL_FLOAT_INLINE T max(V&& input) {
6599
return reduce(ops::max<T> {}, std::forward<V>(input));
66100
}
67101

102+
/**
103+
* Sum the items in the given vector ``input``.
104+
*
105+
* Example
106+
* =======
107+
* ```
108+
* vec<int, 3> x = {5, 0, 2, 1, 0};
109+
* int y = sum(x); // Returns 8
110+
* ```
111+
*/
68112
template<typename V, typename T = vector_value_type<V>>
69113
KERNEL_FLOAT_INLINE T sum(V&& input) {
70114
return reduce(ops::add<T> {}, std::forward<V>(input));
71115
}
72116

117+
/**
118+
* Multiply the items in the given vector ``input``.
119+
*
120+
* Example
121+
* =======
122+
* ```
123+
* vec<int, 5> x = {5, 0, 2, 1, 0};
124+
* int y = sum(x); // Returns 5+0+2+1+0 = 8
125+
* ```
126+
*/
73127
template<typename V, typename T = vector_value_type<V>>
74128
KERNEL_FLOAT_INLINE T product(V&& input) {
75129
return reduce(ops::multiply<T> {}, std::forward<V>(input));
76130
}
77131

132+
/**
133+
* Check if all elements in the given vector ``input`` are non-zero. An element ``v`` is considered
134+
* non-zero if ``bool(v)`` returns ``true``.
135+
*/
78136
template<typename V>
79137
KERNEL_FLOAT_INLINE bool all(V&& input) {
80138
return reduce(ops::bit_and<bool> {}, cast<bool>(input));
81139
}
82140

141+
/**
142+
* Check if any element in the given vector ``input`` is non-zero. An element ``v`` is considered
143+
* non-zero if ``bool(v)`` returns ``true``.
144+
*/
83145
template<typename V>
84146
KERNEL_FLOAT_INLINE bool any(V&& input) {
85147
return reduce(ops::bit_or<bool> {}, cast<bool>(input));
86148
}
87149

150+
/**
151+
* Count the number of non-zero items in the given vector ``input``. An element ``v`` is considered
152+
* non-zero if ``bool(v)`` returns true.
153+
*
154+
* Example
155+
* =======
156+
* ```
157+
* vec<int, 3> x = {5, 0, 2, 1, 0};
158+
* int y = count(x); // Returns 3
159+
* ```
160+
*/
88161
template<typename V>
89162
KERNEL_FLOAT_INLINE int count(V&& input) {
90163
return sum(cast<int>(cast<bool>(input)));

0 commit comments

Comments
 (0)