|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +nav-class: dark |
| 4 | +categories: matt |
| 5 | +title: Speed and Safety |
| 6 | +author-id: matt |
| 7 | +author-name: Matt Borland |
| 8 | +--- |
| 9 | + |
| 10 | +In my [last post](https://cppalliance.org/matt/2026/01/15/Matts2025Q4Update.html) I mentioned that [int128](https://github.com/cppalliance/int128) library would be getting CUDA support in the future. |
| 11 | +The good news is that the future is now! |
| 12 | +Nearly all the functions in the library are available on both host and device. |
| 13 | +Any function that has `BOOST_INT128_HOST_DEVICE` in its signature in the [documentation](https://develop.int128.cpp.al/overview.html) is available for usage. |
| 14 | +[An example](https://develop.int128.cpp.al/examples.html#examples_cuda) of how to use the types in the CUDA kernels has been added as well. |
| 15 | +These can be as simple as: |
| 16 | + |
| 17 | +```cpp |
| 18 | +using test_type = boost::int128::uint128_t; |
| 19 | + |
| 20 | +__global__ void cuda_mul(const test_type* in1, const test_type* in2, test_type* out, int num_elements) |
| 21 | +{ |
| 22 | + int i = blockDim.x * blockIdx.x + threadIdx.x; |
| 23 | + |
| 24 | + if (i < num_elements) |
| 25 | + { |
| 26 | + out[i] = in1[i] * in2[i]; |
| 27 | + } |
| 28 | +} |
| 29 | +``` |
| 30 | +
|
| 31 | +Other Boost libraries are or will be beneficiaries of this effort as well. |
| 32 | +First, Boost.Charconv now supports `boost::charconv::from_chars` and `boost::charconv::to_chars` for integers being run on device. |
| 33 | +This can give you up to an order of magnitude improvement in performance. |
| 34 | +These results and benchmarks are available in the [Boost.Charconv documentation](https://www.boost.org/doc/libs/develop/libs/charconv/doc/html/charconv.html). |
| 35 | +Next, in the coming months Boost.Decimal will gain CUDA support as part of this effort. |
| 36 | +We think users will benefit greatly from being able to perform massively parallel parsing, serialization, and calculations on decimal numbers. |
| 37 | +Stay tuned for this likely in Boost 1.92. |
| 38 | +In the meantime, enjoy the initial release of Decimal coming in Boost 1.91! |
| 39 | +
|
| 40 | +On the other side of the performance that we're looking to deliver in coming versions of Boost, we must not forget the importance of safety. |
| 41 | +There exist plenty of [examples of damage and death](https://en.wikipedia.org/wiki/Integer_overflow#Examples) caused by arithmetic errors in computer programs. |
| 42 | +Can we create a library that provides guaranteed safety in arithmetic while minimizing performance losses and integration friction? |
| 43 | +How does one guarantee the behavior of their types? |
| 44 | +In our implementation, [Boost.Safe_Numbers](https://github.com/cppalliance/safe_numbers), we are investigating the usage of the [Why3](https://why3.org) platform for deductive program verification. |
| 45 | +By pursuing these formal methods, safety can have real meaning. |
| 46 | +We will continue to provide additional details as part of the [formal verification page](https://develop.safe-numbers.cpp.al/verification.html) of our documentation. |
| 47 | +Since inevitably the library will cause an increase in the number of errors (which is a good thing), we aim to fail as early as possible, and when we do provide the most helpful error message that we can. |
| 48 | +For example, we have some static arithmetic errors reported in as few as three lines: |
| 49 | +
|
| 50 | +```cpp |
| 51 | +clang-darwin.compile.c++ ../../../bin.v2/libs/safe_numbers/test/compile_fail_basic_usage_constexpr.test/clang-darwin-21/debug/arm_64/cxxstd-20-iso/threading-multi/visibility-hidden/compile_fail_basic_usage_constexpr.o |
| 52 | +../examples/compile_fail_basic_usage_constexpr.cpp:18:22: error: constexpr variable 'z' must be initialized by a constant expression |
| 53 | + 18 | constexpr u8 z {x + y}; |
| 54 | + | ^ ~~~~~~~ |
| 55 | +../../../boost/safe_numbers/detail/unsigned_integer_basis.hpp:397:17: note: subexpression not valid in a constant expression |
| 56 | + 397 | throw std::overflow_error("Overflow detected in u8 addition"); |
| 57 | + | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 58 | +../examples/compile_fail_basic_usage_constexpr.cpp:18:25: note: in call to 'operator+<unsigned char>({255}, {2})' |
| 59 | + 18 | constexpr u8 z {x + y}; |
| 60 | + | ^~~~~ |
| 61 | +1 error generated. |
| 62 | +``` |
| 63 | + |
| 64 | +Our runtime error reporting system fundamentally uses Boost.Throw_Exception so it can report not only the type, operation, file and line, but also up to an entire stack trace when leveraging the optional linking with Boost.Stacktrace. |
| 65 | +Not to forget our discussion of CUDA so quickly, the Safe_Numbers library will have CUDA support. |
| 66 | +One thing that we will continue to refine is synchronizing error reporting on device as one cannot throw an exception on device. |
| 67 | + |
| 68 | +We are always looking for users of all the libraries discussed. |
| 69 | +If you are a current or prospective user, feel free to reach out and let us know what you're using it for, or any issues that you find. |
0 commit comments