Uni20
Asynchronous tensor and runtime library.
Loading...
Searching...
No Matches
backend/lapack/mplapack/common.hpp
Go to the documentation of this file.
1#pragma once
2
10#include <uni20/config.hpp>
11
12#if UNI20_HAS_MPLAPACK_FLOAT128
13// clang-format off
14#include <mplapack_config.h>
15#include <mplapack_binary128.h>
16// clang-format on
17#endif
18
19#include <algorithm>
20#include <vector>
21
22namespace uni20::lapack::mplapack::detail
23{
24
25#if UNI20_HAS_MPLAPACK_FLOAT128
26
27inline std::size_t lapack_int_work_size(blas_int count)
28{
29 return static_cast<std::size_t>(std::max<blas_int>(1, count));
30}
31
32inline std::vector<mplapackint> make_mplapack_int_work(blas_int count)
33{
34 return std::vector<mplapackint>(lapack_int_work_size(count));
35}
36
37inline std::vector<mplapackint> to_mplapack_ints(blas_int const* values, blas_int count)
38{
39 std::vector<mplapackint> result = make_mplapack_int_work(count);
40 for (blas_int i = 0; i < count; ++i)
41 {
42 result[static_cast<std::size_t>(i)] = static_cast<mplapackint>(values[i]);
43 }
44 return result;
45}
46
47inline void copy_from_mplapack_ints(std::vector<mplapackint> const& source, blas_int* target, blas_int count)
48{
49 for (blas_int i = 0; i < count; ++i)
50 {
51 target[i] = static_cast<blas_int>(source[static_cast<std::size_t>(i)]);
52 }
53}
54
55inline void copy_from_mplapack_query_int_work(std::vector<mplapackint> const& source, blas_int* target, blas_int lwork)
56{
57 if (target == nullptr || (lwork != -1 && lwork <= 0))
58 {
59 return;
60 }
61 copy_from_mplapack_ints(source, target, 1);
62}
63
65
66inline blas_int gelsd_iwork_size(blas_int m, blas_int n)
67{
68 blas_int const minmn = std::min(m, n);
69 if (minmn <= 0)
70 {
71 return 1;
72 }
73
74 // LAPACK's bound uses SMLSIZ from ILAENV. Using divisor 2 overestimates the
75 // subdivision depth, avoiding a runtime dependency on ILAENV in this wrapper.
76 std::size_t levels = 0;
77 for (std::size_t size = static_cast<std::size_t>(minmn) / 2; size > 0; size /= 2)
78 {
79 ++levels;
80 }
81 std::size_t const minmn_size = static_cast<std::size_t>(minmn);
82 std::size_t const required = 3 * minmn_size * levels + 11 * minmn_size;
83 return checked_blas_int(std::max<std::size_t>(1, required));
84}
85
86#endif
87
88} // namespace uni20::lapack::mplapack::detail
Safe conversion helpers for the configured signed BLAS integer ABI.
blas_int checked_blas_int(std::size_t value)
Convert a dense dimension to blas_int or throw on overflow.
Definition blas_int.hpp:40