Skip to content

Commit 83bc083

Browse files
committed
Add changes for 061da8c
1 parent 17e1243 commit 83bc083

25 files changed

+1058
-34
lines changed

_sources/guides.rst.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Guides
2+
=============
3+
.. toctree::
4+
:maxdepth: 1
5+
6+
guides/introduction.rst
7+
guides/promotion.rst
8+
guides/prelude.rst
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
Getting started
2+
===============
3+
4+
Kernel Float is a header-only library that makes it easy to work with vector types and low-precision floating-point types, mainly focusing on CUDA kernel code.
5+
6+
Installation
7+
------------
8+
9+
The easiest way to use the library is get the single header file from github:
10+
11+
```bash
12+
wget https://raw.githubusercontent.com/KernelTuner/kernel_float/main/single_include/kernel_float.h
13+
```
14+
15+
Next, include this file into your program.
16+
It is conventient to define a namespace alias `kf` to shorten the full name `kernel_float`.
17+
18+
19+
```C++
20+
#include "kernel_float.h"
21+
namespace kf = kernel_float;
22+
```
23+
24+
25+
Example C++ code
26+
----------------
27+
28+
Kernel Float essentially offers a single data-type `kernel_float::vec<T, N>` that stores `N` elements of type `T`.
29+
This type can be initialized normally using list-initialization (e.g., `{a, b, c}`) and elements can be accessed using the `[]` operator.
30+
Operation overload is available to perform binary operations (such as `+`, `*`, and `&`), where the optimal intrinsic for the available types is selected automatically.
31+
32+
Many mathetical functions (like `log`, `sin`, `cos`) are also available, see the [API reference](../api) for the full list of functions.
33+
In some cases, certain operations might not be natively supported by the platform for the some floating-point type.
34+
In these cases, Kernel Float falls back to performing the operations in 32 bit precision.
35+
36+
The code below shows a very simple example of how to use Kernel Float:
37+
38+
```C++
39+
#include "kernel_float.h"
40+
namespace kf = kernel_float;
41+
42+
int main() {
43+
using Type = float;
44+
const int N = 8;
45+
46+
kf::vec<int, N> i = kf::range<int, N>();
47+
kf::vec<Type, N> x = kf::cast<Type>(i);
48+
kf::vec<Type, N> y = x * kf::sin(x);
49+
Type result = kf::sum(y);
50+
printf("result=%f", double(result));
51+
52+
return EXIT_SUCCESS;
53+
}
54+
```
55+
56+
Notice how easy it would be to change the floating-point type `Type` or the vector length `N` without affecting the rest of the code.

_sources/guides/prelude.md.txt

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
Using `kernel_float::prelude`
2+
===
3+
4+
When working with Kernel Float, you'll find that you need to prefix every function and type with the `kernel_float::...` prefix.
5+
This can be a bit cumbersome.
6+
It's strongly discouraged not to dump the entire `kernel_float` namespace into the global namespace (with `using namespace kernel_float`) since
7+
many symbols in Kernel Float may clash with global symbols, causing conflicts and issues.
8+
9+
To work around this, the library provides a handy `kernel_float::prelude` namespace. This namespace contains a variety of useful type and function aliases that won't conflict with global symbols.
10+
11+
To make use of it, use the following code:
12+
13+
14+
```C++
15+
#include "kernel_float.h"
16+
using namespace kernel_float::prelude;
17+
18+
// You can now use aliases like `kf`, `kvec`, `kint`, etc.
19+
```
20+
21+
The prelude defines many aliases, include the following:
22+
23+
| Prelude name | Full name |
24+
|---|---|
25+
| `kf` | `kernel_float` |
26+
| `kvec<T, N>` | `kernel_float::vec<T, N>` |
27+
| `into_kvec(v)` | `kernel_float::into_vec(v)` |
28+
| `make_kvec(a, b, ...)` | `kernel_float::make_vec(a, b, ...)` |
29+
| `kvec2<T>`, `kvec3<T>`, ... | `kernel_float::vec<T, 2>`, `kernel_float::vec<T, 3>`, ... |
30+
| `kint<N>` | `kernel_float::vec<int, N>` |
31+
| `kint2`, `kint3`, ... | `kernel_float::vec<int, 2>`, `kernel_float::vec<int, 3>`, ... |
32+
| `klong<N>` | `kernel_float::vec<long, N>` |
33+
| `klong2`, `klong3`, ... | `kernel_float::vec<long, 2>`, `kernel_float::vec<long, 3>`, ... |
34+
| `kbfloat16x<N>` | `kernel_float::vec<bfloat16, N>` |
35+
| `kbfloat16x2`, `kbfloat16x3`, ... | `kernel_float::vec<bfloat16, 2>`, `kernel_float::vec<bfloat16, 3>`, ... |
36+
| `khalf<N>` | `kernel_half::vec<half, N>` |
37+
| `khalf2`, `khalf3`, ... | `kernel_half::vec<half, 2>`, `kernel_half::vec<half, 3>`, ... |
38+
| `kfloat<N>` | `kernel_float::vec<float, N>` |
39+
| `kfloat2`, `kfloat3`, ... | `kernel_float::vec<float, 2>`, `kernel_float::vec<float, 3>`, ... |
40+
| `kdouble<N>` | `kernel_float::vec<double, N>` |
41+
| `kdouble2`, `kdouble3`, ... | `kernel_float::vec<double, 2>`, `kernel_float::vec<double, 3>`, ... |
42+
| ... | ... |

_sources/guides/promotion.rst.txt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
Type Promotion
2+
==============
3+
4+
For operations that involve two input arguments (or more), ``kernel_float`` will first convert the inputs into a common type before applying the operation.
5+
For example, when adding ``vec<int, N>`` to a ``vec<float, N>``, both arguments must first be converted into a ``vec<float, N>``.
6+
7+
This procedure is called "type promotion" and is implemented as follows.
8+
First, all arguments are converted into a vector by calling ``into_vec``.
9+
Next, all arguments must have length ``N`` or length ``1`` and vectors of length ``1`` are resized to become length ``N``.
10+
Finally, the vector element types are promoted into a common type.
11+
12+
The rules for element type promotion in ``kernel_float`` are slightly different than in regular C++.
13+
In short, for two element types ``T`` and ``U``, the promotion rules can be summarized as follows:
14+
15+
* If one of the types is ``bool``, the result is the other type.
16+
* If one type is a floating-point type and the other is a signed or unsigned integer, the result is the floating-point type.
17+
* If both types are floating-point types, the result is the largest of the two types. An exception here is combining ``half`` and ``bfloat16``, which results in ``float``.
18+
* If both types are integer types of the same signedness, the result is the largest of the two types.
19+
* Combining a signed integer and unsigned integer type is not allowed.
20+
21+
Overview
22+
--------
23+
24+
The type promotion rules are shown in the table below.
25+
The labels are as follows:
26+
27+
* ``b``: boolean
28+
* ``iN``: signed integer of ``N`` bits (e.g., ``int``, ``long``)
29+
* ``uN``: unsigned integer of ``N`` bits (e.g., ``unsigned int``, ``size_t``)
30+
* ``fN``: floating-point type of ``N`` bits (e.g., ``float``, ``double``)
31+
* ``bf16``: bfloat16 floating-point format.
32+
33+
.. csv-table:: Type Promotion Rules.
34+
:file: promotion_table.csv

_sources/index.rst.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,20 @@
44
:caption: Contents
55

66
Kernel Float <self>
7+
guides
78
api
89
license
910
Github repository <https://github.com/KernelTuner/kernel_float>
1011

1112

12-
.. mdinclude:: ../README.md
13+
.. include:: ../README.md
14+
:parser: myst_parser.sphinx_
1315

1416

1517

1618

1719
Indices and tables
18-
============
20+
==================
1921

2022
* :ref:`genindex`
2123
* :ref:`modindex`

api.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<link rel="index" title="Index" href="genindex.html" />
2121
<link rel="search" title="Search" href="search.html" />
2222
<link rel="next" title="Types" href="api/types.html" />
23-
<link rel="prev" title="Kernel Float" href="index.html" />
23+
<link rel="prev" title="Using kernel_float::prelude" href="guides/prelude.html" />
2424
</head>
2525

2626
<body class="wy-body-for-nav">
@@ -45,6 +45,7 @@
4545
<p class="caption" role="heading"><span class="caption-text">Contents</span></p>
4646
<ul class="current">
4747
<li class="toctree-l1"><a class="reference internal" href="index.html">Kernel Float</a></li>
48+
<li class="toctree-l1"><a class="reference internal" href="guides.html">Guides</a></li>
4849
<li class="toctree-l1 current"><a class="current reference internal" href="#">API Reference</a><ul>
4950
<li class="toctree-l2"><a class="reference internal" href="api/types.html">Types</a></li>
5051
<li class="toctree-l2"><a class="reference internal" href="api/primitives.html">Primitives</a></li>
@@ -220,7 +221,7 @@ <h1>API Reference<a class="headerlink" href="#api-reference" title="Permalink to
220221
</div>
221222
</div>
222223
<footer><div class="rst-footer-buttons" role="navigation" aria-label="Footer">
223-
<a href="index.html" class="btn btn-neutral float-left" title="Kernel Float" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
224+
<a href="guides/prelude.html" class="btn btn-neutral float-left" title="Using kernel_float::prelude" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
224225
<a href="api/types.html" class="btn btn-neutral float-right" title="Types" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
225226
</div>
226227

api/binary_operators.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
<p class="caption" role="heading"><span class="caption-text">Contents</span></p>
4646
<ul class="current">
4747
<li class="toctree-l1"><a class="reference internal" href="../index.html">Kernel Float</a></li>
48+
<li class="toctree-l1"><a class="reference internal" href="../guides.html">Guides</a></li>
4849
<li class="toctree-l1 current"><a class="reference internal" href="../api.html">API Reference</a><ul class="current">
4950
<li class="toctree-l2"><a class="reference internal" href="types.html">Types</a></li>
5051
<li class="toctree-l2"><a class="reference internal" href="primitives.html">Primitives</a></li>

api/conditional.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
<p class="caption" role="heading"><span class="caption-text">Contents</span></p>
4646
<ul class="current">
4747
<li class="toctree-l1"><a class="reference internal" href="../index.html">Kernel Float</a></li>
48+
<li class="toctree-l1"><a class="reference internal" href="../guides.html">Guides</a></li>
4849
<li class="toctree-l1 current"><a class="reference internal" href="../api.html">API Reference</a><ul class="current">
4950
<li class="toctree-l2"><a class="reference internal" href="types.html">Types</a></li>
5051
<li class="toctree-l2"><a class="reference internal" href="primitives.html">Primitives</a></li>

api/generation.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
<p class="caption" role="heading"><span class="caption-text">Contents</span></p>
4646
<ul class="current">
4747
<li class="toctree-l1"><a class="reference internal" href="../index.html">Kernel Float</a></li>
48+
<li class="toctree-l1"><a class="reference internal" href="../guides.html">Guides</a></li>
4849
<li class="toctree-l1 current"><a class="reference internal" href="../api.html">API Reference</a><ul class="current">
4950
<li class="toctree-l2"><a class="reference internal" href="types.html">Types</a></li>
5051
<li class="toctree-l2"><a class="reference internal" href="primitives.html">Primitives</a></li>

api/mathematical.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
<p class="caption" role="heading"><span class="caption-text">Contents</span></p>
4646
<ul class="current">
4747
<li class="toctree-l1"><a class="reference internal" href="../index.html">Kernel Float</a></li>
48+
<li class="toctree-l1"><a class="reference internal" href="../guides.html">Guides</a></li>
4849
<li class="toctree-l1 current"><a class="reference internal" href="../api.html">API Reference</a><ul class="current">
4950
<li class="toctree-l2"><a class="reference internal" href="types.html">Types</a></li>
5051
<li class="toctree-l2"><a class="reference internal" href="primitives.html">Primitives</a></li>

0 commit comments

Comments
 (0)